max user limits + check plan active on webhook + flow tweaks

This commit is contained in:
Michael Dausmann
2023-03-25 16:03:28 +11:00
parent 45fb639fcf
commit f2fa72b2a1
12 changed files with 86 additions and 28 deletions

View File

@@ -30,7 +30,6 @@ export default defineEventHandler(async (event) => {
line_items: [
{
price: price_id,
// For metered billing, do not pass quantity
quantity: 1,
},
],

View File

@@ -28,14 +28,22 @@ export default defineEventHandler(async (event) => {
console.log(`****** Web Hook Recieved (${stripeEvent.type}) ******`);
let subscription = stripeEvent.data.object as Stripe.Subscription;
const userService = new UserAccountService();
let current_period_ends: Date = new Date(subscription.current_period_end * 1000);
current_period_ends.setDate(current_period_ends.getDate() + config.subscriptionGraceDays);
console.log(`updating stripe sub details subscription.current_period_end:${subscription.current_period_end}, subscription.id:${subscription.id}`);
userService.updateStripeSubscriptionDetailsForAccount(subscription.customer.toString(), subscription.id, current_period_ends)
if(subscription.status == 'active'){
const sub_item = subscription.items.data.find(item => item?.object && item?.object == 'subscription_item')
const stripe_product_id = sub_item?.plan.product?.toString(); // TODO - is the product ever a product object and in that case should I check for deleted?
if(!stripe_product_id){
throw createError({ statusCode: 400, statusMessage: `Error validating Webhook Event` });
}
const userService = new UserAccountService();
let current_period_ends: Date = new Date(subscription.current_period_end * 1000);
current_period_ends.setDate(current_period_ends.getDate() + config.subscriptionGraceDays);
console.log(`updating stripe sub details subscription.current_period_end:${subscription.current_period_end}, subscription.id:${subscription.id}, stripe_product_id:${stripe_product_id}`);
userService.updateStripeSubscriptionDetailsForAccount(subscription.customer.toString(), subscription.id, current_period_ends, stripe_product_id);
}
}
return `handled ${stripeEvent.type}.`;
});

View File

@@ -30,7 +30,7 @@ export async function createContext(event: H3Event){
if(!user || !dbUser) {
throw new TRPCError({
code: 'INTERNAL_SERVER_ERROR',
message: `Unable to fetch user data, please try again later. Missing ->[user:${(!user)},dbUser:${(!dbUser)}]`,
message: `Unable to fetch user data, please try again later. Missing ->[supabase:${(!supabase)},user:${(!user)},dbUser:${(!dbUser)}]`,
});
}