mirror of
https://github.com/PretendoNetwork/website.git
synced 2026-08-23 00:57:21 -05:00
feat: add unsubscribe to API methods
This commit is contained in:
33
server/api/account/unsubscribe.post.ts
Normal file
33
server/api/account/unsubscribe.post.ts
Normal file
@@ -0,0 +1,33 @@
|
||||
import { usePapr } from '~~/server/utils/papr';
|
||||
|
||||
export default defineEventHandler(async (event): Promise<void> => {
|
||||
const auth = enforceLoggedIn(event);
|
||||
const papr = await usePapr(event);
|
||||
const stripe = useStripe(event);
|
||||
if (!stripe || !papr) {
|
||||
throw createApiError('INTEGRATION_DISABLED');
|
||||
}
|
||||
|
||||
if (!auth.stripeSubscriptionId) {
|
||||
return; // No subscription, do nothing
|
||||
}
|
||||
|
||||
await stripe.subscriptions.cancel(auth.stripeSubscriptionId);
|
||||
|
||||
let newAccessLevel = 0;
|
||||
if (auth.accessLevel >= 2) {
|
||||
newAccessLevel = auth.accessLevel; // Staff shouldn't be downgraded on unsubscribe
|
||||
}
|
||||
|
||||
await papr.Pnid.updateOne({ pid: auth.pid }, {
|
||||
$unset: {
|
||||
'connections.stripe.subscription_id': 1,
|
||||
'connections.stripe.price_id': 1,
|
||||
'connections.stripe.tier_name': 1
|
||||
},
|
||||
$set: {
|
||||
'connections.stripe.tier_level': 0,
|
||||
'access_level': newAccessLevel
|
||||
}
|
||||
});
|
||||
});
|
||||
@@ -12,6 +12,13 @@ export default defineEventHandler(async (event): Promise<GetApiAuthMe> => {
|
||||
accessLevel: data.accessLevel,
|
||||
serverAccessLevel: data.serverAccessLevel as any,
|
||||
discordId: data.connections?.discord?.id ?? null,
|
||||
stripeTier: data.connections?.stripe?.subscriptionId
|
||||
? {
|
||||
subscriptionId: data.connections.stripe.subscriptionId,
|
||||
priceId: data.connections.stripe.priceId ?? '',
|
||||
tierName: data.connections.stripe.tierName ?? ''
|
||||
}
|
||||
: null,
|
||||
mii: data.mii
|
||||
? {
|
||||
imageUrl: `${config.public.cdnBaseUrl}/mii/${data.pid}/normal_face.png`,
|
||||
|
||||
@@ -18,7 +18,8 @@ export default defineEventHandler(async (event) => {
|
||||
username: userData.username,
|
||||
token: token,
|
||||
accessLevel: userData.accessLevel,
|
||||
email: userData.emailAddress
|
||||
email: userData.emailAddress,
|
||||
stripeSubscriptionId: userData.connections?.stripe?.subscriptionId ?? null
|
||||
});
|
||||
} catch (err) {
|
||||
console.error('Failed to request user data: ', err);
|
||||
|
||||
@@ -6,6 +6,7 @@ export type AuthContext = {
|
||||
token: string;
|
||||
email: string;
|
||||
accessLevel: number;
|
||||
stripeSubscriptionId: string | null;
|
||||
};
|
||||
|
||||
export function setAuthContext(event: H3Event, context: AuthContext | null): void {
|
||||
|
||||
@@ -6,6 +6,11 @@ export type GetApiAuthMe = {
|
||||
accessLevel: number;
|
||||
serverAccessLevel: 'dev' | 'test' | 'prod';
|
||||
discordId: string | null;
|
||||
stripeTier: {
|
||||
subscriptionId: string;
|
||||
priceId: string;
|
||||
tierName: string;
|
||||
} | null;
|
||||
mii: {
|
||||
imageUrl: string;
|
||||
name: string;
|
||||
|
||||
Reference in New Issue
Block a user