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.

Sync workspace plan to staging on billing changes

Kiran Kce9ade230bb7
diff --git a/apps/web/app/(ee)/api/stripe/webhook/checkout-session-completed.ts b/apps/web/app/(ee)/api/stripe/webhook/checkout-session-completed.ts
index a75793faab..52a370c688 100644
--- a/apps/web/app/(ee)/api/stripe/webhook/checkout-session-completed.ts
+++ b/apps/web/app/(ee)/api/stripe/webhook/checkout-session-completed.ts
@@ -6,6 +6,7 @@ import { tokenCache } from "@/lib/auth/token-cache";
import { wouldGainPartnerAccess } from "@/lib/plans/has-partner-access";
import { prisma } from "@/lib/prisma";
import { queueCreateStagingWorkspace } from "@/lib/sandbox/create-staging-workspace";
+import { syncWorkspacePlanToStaging } from "@/lib/sandbox/sync-workspace";
import { stripe } from "@/lib/stripe";
import { redis } from "@/lib/upstash";
import { sendBatchEmail } from "@dub/email";
@@ -140,7 +141,11 @@ export async function checkoutSessionCompleted(
await Promise.allSettled([
completeOnboarding({ users, workspaceId, subscription }),
+
queueCreateStagingWorkspace(updatedWorkspace),
+
+ syncWorkspacePlanToStaging(updatedWorkspace),
+
// if workspace had a program from before and is upgrading to an eligible plan, reactivate it
updatedWorkspace.defaultProgramId &&
wouldGainPartnerAccess({
diff --git a/apps/web/app/(ee)/api/stripe/webhook/customer-subscription-deleted.ts b/apps/web/app/(ee)/api/stripe/webhook/customer-subscription-deleted.ts
index 1961a27ee9..e345b0211b 100644
--- a/apps/web/app/(ee)/api/stripe/webhook/customer-subscription-deleted.ts
+++ b/apps/web/app/(ee)/api/stripe/webhook/customer-subscription-deleted.ts
@@ -8,6 +8,7 @@ import { deactivateProgram } from "@/lib/api/programs/deactivate-program";
import { tokenCache } from "@/lib/auth/token-cache";
import { wouldLoseAdvancedFeatures } from "@/lib/plans/would-lose-advanced-features";
import { prisma } from "@/lib/prisma";
+import { syncWorkspacePlanToStaging } from "@/lib/sandbox/sync-workspace";
import { stripe } from "@/lib/stripe";
import { recordLink } from "@/lib/tinybird";
import { sendEmail } from "@dub/email";
@@ -102,7 +103,7 @@ export async function customerSubscriptionDeleted(
const workspaceLinks = workspace.links;
const workspaceUsers = workspace.users.map(({ user }) => user);
- await prisma.project.update({
+ const updatedWorkspace = await prisma.project.update({
where: {
stripeId,
},
@@ -204,6 +205,8 @@ export async function customerSubscriptionDeleted(
tokenCache.expireMany({
hashedKeys: workspace.restrictedTokens.map(({ hashedKey }) => hashedKey),
}),
+
+ syncWorkspacePlanToStaging(updatedWorkspace),
]);
// Reset cancellation feedback dedupe so a future resubscribe + cancel can send again
diff --git a/apps/web/app/(ee)/api/stripe/webhook/utils/update-workspace-plan.ts b/apps/web/app/(ee)/api/stripe/webhook/utils/update-workspace-plan.ts
index 04757a13b4..50135c2fbe 100644
--- a/apps/web/app/(ee)/api/stripe/webhook/utils/update-workspace-plan.ts
+++ b/apps/web/app/(ee)/api/stripe/webhook/utils/update-workspace-plan.ts
@@ -13,6 +13,7 @@ import {
import { wouldLoseAdvancedFeatures } from "@/lib/plans/would-lose-advanced-features";
import { prisma } from "@/lib/prisma";
import { queueCreateStagingWorkspace } from "@/lib/sandbox/create-staging-workspace";
+import { syncWorkspacePlanToStaging } from "@/lib/sandbox/sync-workspace";
import {
getSubscriptionCancellationFields,
getSubscriptionTrialEndsAt,
@@ -101,7 +102,7 @@ export async function updateWorkspacePlan({
(workspace.partnersLimit < newPlan.limits.partners &&
NEW_BUSINESS_PRICE_IDS.includes(priceId))
) {
- const [updatedWorkspace] = await Promise.allSettled([
+ const [updatedWorkspace] = await Promise.all([
prisma.project.update({
where: {
id: workspace.id,
@@ -152,12 +153,13 @@ export async function updateWorkspacePlan({
},
}),
- // expire tokens cache
+ // Expire tokens cache
tokenCache.expireMany({
hashedKeys: workspace.restrictedTokens.map(
({ hashedKey }) => hashedKey,
),
}),
+
...(workspace.defaultProgramId
? [
prisma.program.update({
@@ -171,6 +173,7 @@ export async function updateWorkspacePlan({
]
: []),
]);
+
console.log(
`Updated workspace ${workspace.id} plan / limits / subscription details.`,
);
@@ -232,15 +235,12 @@ export async function updateWorkspacePlan({
console.log(`Reactivated program for workspace ${workspace.id}.`);
}
- await queueCreateStagingWorkspace({
- ...workspace,
- plan: newPlanName,
- });
+ await Promise.all([
+ queueCreateStagingWorkspace(updatedWorkspace),
+ syncWorkspacePlanToStaging(updatedWorkspace),
+ ]);
- const workspaceOwners =
- updatedWorkspace.status === "fulfilled"
- ? updatedWorkspace.value.users.map((user) => user.user)
- : [];
+ const workspaceOwners = updatedWorkspace.users.map((user) => user.user);
if (
workspace.defaultProgramId &&
diff --git a/apps/web/app/api/user/route.ts b/apps/web/app/api/user/route.ts
index 37a694e8df..5355436c36 100644
--- a/apps/web/app/api/user/route.ts
+++ b/apps/web/app/api/user/route.ts
@@ -101,7 +101,8 @@ export const PATCH = withSession(async ({ req, session }) => {
}
assertProductionWorkspace(workspaceUser.project, {
- message: `You can only set your default workspace to a production workspace.`,
+ message:
+ "You can only set your default workspace to a production workspace.",
});
}
diff --git a/apps/web/lib/sandbox/sync-workspace.ts b/apps/web/lib/sandbox/sync-workspace.ts
index 119692e24a..8f27f4baa9 100644
--- a/apps/web/lib/sandbox/sync-workspace.ts
+++ b/apps/web/lib/sandbox/sync-workspace.ts
@@ -116,3 +116,29 @@ export async function syncWorkspaceSettingsToStaging(
console.error("[syncWorkspaceSettingsToStaging]", error);
}
}
+
+export async function syncWorkspacePlanToStaging(
+ workspace: Pick<
+ Project,
+ "stagingWorkspaceId" | "plan" | "planTier" | "planPeriod"
+ >,
+) {
+ if (!workspace.stagingWorkspaceId) {
+ return;
+ }
+
+ try {
+ await prisma.project.update({
+ where: {
+ id: workspace.stagingWorkspaceId,
+ },
+ data: {
+ plan: workspace.plan,
+ planTier: workspace.planTier,
+ planPeriod: workspace.planPeriod,
+ },
+ });
+ } catch (error) {
+ console.error("[syncWorkspacePlanToStaging]", error);
+ }
+}