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.

Add Project.stripeConnectMode and backfill script

Kiran Kf5ab6d82123c
diff --git a/apps/web/app/(ee)/api/stripe/integration/route.ts b/apps/web/app/(ee)/api/stripe/integration/route.ts
index 34aeb34da9..0eccc85ad2 100644
--- a/apps/web/app/(ee)/api/stripe/integration/route.ts
+++ b/apps/web/app/(ee)/api/stripe/integration/route.ts
@@ -15,16 +15,17 @@ const CORS_HEADERS = new Headers({
"Access-Control-Allow-Headers": "Content-Type, Authorization",
});
+const updateStripeIntegrationSchema = z.object({
+ stripeAccountId: z.string().nullable(), // null on disconnect
+ stripeMode: stripeIntegrationSettingsSchema.shape.stripeMode,
+});
+
// PATCH /api/stripe/integration - update a workspace with a stripe connect account id
export const PATCH = withWorkspace(
async ({ req, workspace, token }) => {
const body = await parseRequestBody(req);
- const { stripeAccountId, stripeMode } = z
- .object({
- stripeAccountId: z.string().nullable(),
- stripeMode: stripeIntegrationSettingsSchema.shape.stripeMode,
- })
- .parse(body);
+ const { stripeAccountId, stripeMode } =
+ updateStripeIntegrationSchema.parse(body);
if (!token?.installationId) {
throw new DubApiError({
@@ -63,6 +64,8 @@ export const PATCH = withWorkspace(
},
data: {
stripeConnectId: stripeAccountId,
+ // stripeMode always defaults to "live", so clear it explicitly on disconnect
+ stripeConnectMode: stripeAccountId ? stripeMode : null,
},
select: {
stripeConnectId: true,
diff --git a/apps/web/app/(ee)/api/stripe/integration/webhook/account-application-deauthorized.ts b/apps/web/app/(ee)/api/stripe/integration/webhook/account-application-deauthorized.ts
index 42e06af4d5..a98c034da9 100644
--- a/apps/web/app/(ee)/api/stripe/integration/webhook/account-application-deauthorized.ts
+++ b/apps/web/app/(ee)/api/stripe/integration/webhook/account-application-deauthorized.ts
@@ -17,24 +17,24 @@ export async function accountApplicationDeauthorized({
};
}
- await prisma.project.update({
- where: {
- stripeConnectId: stripeAccountId,
- },
- data: {
- stripeConnectId: null,
- },
- select: {
- id: true,
- },
- });
+ await prisma.$transaction([
+ prisma.project.update({
+ where: {
+ id: workspace.id,
+ },
+ data: {
+ stripeConnectId: null,
+ stripeConnectMode: null,
+ },
+ }),
- await prisma.installedIntegration.deleteMany({
- where: {
- projectId: workspace.id,
- integrationId: STRIPE_INTEGRATION_ID,
- },
- });
+ prisma.installedIntegration.deleteMany({
+ where: {
+ projectId: workspace.id,
+ integrationId: STRIPE_INTEGRATION_ID,
+ },
+ }),
+ ]);
return {
response: `Stripe Connect account ${stripeAccountId} deauthorized for workspace ${workspace.id}`,
diff --git a/apps/web/prisma/schema/workspace.prisma b/apps/web/prisma/schema/workspace.prisma
index a25a651c8c..7f09959cc9 100644
--- a/apps/web/prisma/schema/workspace.prisma
+++ b/apps/web/prisma/schema/workspace.prisma
@@ -11,6 +11,12 @@ enum WorkspaceEnvironment {
sandbox
}
+enum StripeConnectMode {
+ live
+ test
+ sandbox
+}
+
enum DubProduct {
program
links
@@ -39,8 +45,9 @@ model Project {
environment WorkspaceEnvironment @default(production)
stagingWorkspaceId String? @unique
- stripeConnectId String? @unique // for Stripe Integration
- shopifyStoreId String? @unique // for Shopify Integration
+ stripeConnectId String? @unique // for Stripe Integration
+ stripeConnectMode StripeConnectMode? // Stripe mode the account is connected in
+ shopifyStoreId String? @unique // for Shopify Integration
totalLinks Int @default(0) // Total number of links in the workspace
totalClicks Int @default(0) // Total number of clicks in the workspace
diff --git a/apps/web/scripts/stripe/backfill-stripe-connect-mode.ts b/apps/web/scripts/stripe/backfill-stripe-connect-mode.ts
new file mode 100644
index 0000000000..2b490c641c
--- /dev/null
+++ b/apps/web/scripts/stripe/backfill-stripe-connect-mode.ts
@@ -0,0 +1,101 @@
+import { stripeIntegrationSettingsSchema } from "@/lib/integrations/stripe/schema";
+import { prisma } from "@/lib/prisma";
+import { STRIPE_INTEGRATION_ID } from "@dub/utils";
+import "dotenv-flow/config";
+
+const DRY_RUN = true;
+const BATCH_SIZE = 100;
+
+// Backfill Project.stripeConnectMode from the Stripe installation settings
+// Run after adding the nullable column and before swapping the stripeConnectId unique index
+async function main() {
+ let totalUpdated = 0;
+ let cursor: string | undefined;
+
+ while (true) {
+ const workspaces = await prisma.project.findMany({
+ where: {
+ stripeConnectId: {
+ not: null,
+ },
+ stripeConnectMode: null,
+ ...(cursor && {
+ id: {
+ gt: cursor,
+ },
+ }),
+ },
+ select: {
+ id: true,
+ stripeConnectId: true,
+ installedIntegrations: {
+ where: {
+ integrationId: STRIPE_INTEGRATION_ID,
+ },
+ select: {
+ settings: true,
+ },
+ orderBy: {
+ createdAt: "asc",
+ },
+ take: 1,
+ },
+ },
+ orderBy: {
+ id: "asc",
+ },
+ take: BATCH_SIZE,
+ });
+
+ if (workspaces.length === 0) {
+ break;
+ }
+
+ cursor = workspaces[workspaces.length - 1].id;
+
+ const updates = workspaces.map((workspace) => {
+ const parsed = stripeIntegrationSettingsSchema.safeParse(
+ workspace.installedIntegrations[0]?.settings ?? {},
+ );
+
+ return {
+ id: workspace.id,
+ stripeConnectId: workspace.stripeConnectId,
+ stripeConnectMode: parsed.success ? parsed.data.stripeMode : "live",
+ };
+ });
+
+ if (DRY_RUN) {
+ console.table(updates);
+ } else {
+ await Promise.all(
+ updates.map(async ({ id, stripeConnectId, stripeConnectMode }) => {
+ await prisma.project.update({
+ where: {
+ id,
+ },
+ data: {
+ stripeConnectMode,
+ },
+ });
+
+ console.log(
+ `Set stripeConnectMode=${stripeConnectMode} for workspace ${id} (${stripeConnectId})`,
+ );
+ }),
+ );
+ }
+
+ totalUpdated += updates.length;
+
+ console.log(
+ `${DRY_RUN ? "Would backfill" : "Backfilled"} ${updates.length} workspaces up to ${cursor}`,
+ );
+ }
+
+ console.log(
+ `Done. ${DRY_RUN ? "Would backfill" : "Backfilled"} ${totalUpdated} workspaces.`,
+ );
+}
+
+main();