Skip to content

armgit / dub

Public Imported from GitHub
0

The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.

Handle folder deletion on Stripe subscription changes

Kiran Ke6f3eba2cdcb
diff --git a/apps/web/app/api/stripe/webhook/customer-subscription-deleted.ts b/apps/web/app/api/stripe/webhook/customer-subscription-deleted.ts
index ba87ac6507..020346e412 100644
--- a/apps/web/app/api/stripe/webhook/customer-subscription-deleted.ts
+++ b/apps/web/app/api/stripe/webhook/customer-subscription-deleted.ts
@@ -177,6 +177,13 @@ export async function customerSubscriptionDeleted(event: Stripe.Event) {
webhookEnabled: false,
},
}),
+
+ // Remove all folders for the workspace
+ prisma.folder.deleteMany({
+ where: {
+ projectId: workspace.id,
+ },
+ }),
]);
// Update the webhooks cache
diff --git a/apps/web/app/api/stripe/webhook/customer-subscription-updated.ts b/apps/web/app/api/stripe/webhook/customer-subscription-updated.ts
index 9e02fd5d1e..7a2ff4460f 100644
--- a/apps/web/app/api/stripe/webhook/customer-subscription-updated.ts
+++ b/apps/web/app/api/stripe/webhook/customer-subscription-updated.ts
@@ -61,6 +61,7 @@ export async function customerSubscriptionUpdated(event: Stripe.Event) {
const newPlan = plan.name.toLowerCase();
const shouldDisableWebhooks = newPlan === "free" || newPlan === "pro";
+ const shouldDeleteFolders = newPlan === "free";
// If a workspace upgrades/downgrades their subscription, update their usage limit in the database.
if (workspace.plan !== newPlan) {
@@ -131,6 +132,19 @@ export async function customerSubscriptionUpdated(event: Stripe.Event) {
await webhookCache.mset(webhooks);
}
+
+ // Delete the folders if the new plan is free
+ // For downgrade from Business → Pro, it should be fine since we're accounting that to make sure all folders get write access.
+ if (shouldDeleteFolders) {
+ await prisma.folder.deleteMany({
+ where: {
+ projectId: workspace.id,
+ },
+ });
+
+ // TODO
+ // call recordLink to update the Links in TB
+ }
} else if (workspace.paymentFailedAt) {
await prisma.project.update({
where: {