diff --git a/dashboard/server/api/pay/webhook.post.ts b/dashboard/server/api/pay/webhook.post.ts index afbba75..f8416f8 100644 --- a/dashboard/server/api/pay/webhook.post.ts +++ b/dashboard/server/api/pay/webhook.post.ts @@ -35,19 +35,21 @@ async function addSubscriptionToProject(project_id: string, plan: PREMIUM_DATA, async function onPaymentFailed(event: Event.InvoicePaymentFailedEvent) { //TODO: Send emails - + if (event.data.object.attempt_count > 1) { const customer_id = event.data.object.customer as string; const project = await ProjectModel.findOne({ customer_id }); if (!project) return { error: 'CUSTOMER NOT EXIST' } const allSubscriptions = await StripeService.getAllSubscriptions(customer_id); + if (!allSubscriptions) return; for (const subscription of allSubscriptions.data) { await StripeService.deleteSubscription(subscription.id); } const freeSub = await StripeService.createFreeSubscription(customer_id); + if (!freeSub) return; await addSubscriptionToProject( project._id.toString(), @@ -75,7 +77,8 @@ async function onPaymentSuccess(event: Event.InvoicePaidEvent) { const subscription_id = event.data.object.subscription as string; const allSubscriptions = await StripeService.getAllSubscriptions(customer_id); - + if (!allSubscriptions) return; + const currentSubscription = allSubscriptions.data.find(e => e.id === subscription_id); if (!currentSubscription) return { error: 'SUBSCRIPTION NOT EXIST' } diff --git a/dashboard/server/api/project/create.post.ts b/dashboard/server/api/project/create.post.ts index 75ab197..8fd7868 100644 --- a/dashboard/server/api/project/create.post.ts +++ b/dashboard/server/api/project/create.post.ts @@ -1,5 +1,6 @@ import { ProjectModel, TProject } from "@schema/ProjectSchema"; import { ProjectCountModel } from "@schema/ProjectsCounts"; +import { ProjectLimitModel } from "@schema/ProjectsLimits"; import { UserSettingsModel } from "@schema/UserSettings"; import StripeService from '~/server/services/StripeService'; @@ -41,6 +42,16 @@ export default defineEventHandler(async event => { visits: 0 }); + await ProjectLimitModel.updateOne({ project_id: project._id }, { + events: 0, + visits: 0, + ai_messages: 0, + limit: 10_000_000, + ai_limit: 1_000_000, + billing_start_at: Date.now(), + billing_expire_at: new Date(3000, 1, 1) + }, { upsert: true }) + return project.toJSON() as TProject; } else {