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.

Skip Stripe test events for workspaces with a staging workspace

Kiran Kf8780a5a9b88
diff --git a/apps/web/app/(ee)/api/stripe/integration/webhook/route.ts b/apps/web/app/(ee)/api/stripe/integration/webhook/route.ts
index 110a3ea8bf..dd69d708e6 100644
--- a/apps/web/app/(ee)/api/stripe/integration/webhook/route.ts
+++ b/apps/web/app/(ee)/api/stripe/integration/webhook/route.ts
@@ -1,5 +1,6 @@
import { captureWebhookLog } from "@/lib/api-logs/capture-webhook-log";
import { withAxiom } from "@/lib/axiom/server";
+import { prisma } from "@/lib/prisma";
import { isStripeRateLimitError, stripe } from "@/lib/stripe";
import { StripeMode } from "@/lib/types";
import { waitUntil } from "@vercel/functions";
@@ -14,7 +15,6 @@ import { customerSubscriptionDeleted } from "./customer-subscription-deleted";
import { invoicePaid } from "./invoice-paid";
import { promotionCodeUpdated } from "./promotion-code-updated";
import { WebhookHandlerResponse } from "./types";
-import { resolveWebhookWorkspace } from "./utils/resolve-webhook-workspace";
import { syncCustomer } from "./utils/sync-customer";
export const dynamic = "force-dynamic";
@@ -105,9 +105,17 @@ export const POST = withAxiom(async (req: Request) => {
}
// Find the workspace
- const workspace = await resolveWebhookWorkspace({
- stripeAccountId: event.account,
- mode,
+ const workspace = await prisma.project.findUnique({
+ where: {
+ stripeConnectId: event.account,
+ },
+ select: {
+ id: true,
+ stripeConnectId: true,
+ defaultProgramId: true,
+ webhookEnabled: true,
+ stagingWorkspaceId: true,
+ },
});
if (!workspace) {
@@ -117,6 +125,13 @@ export const POST = withAxiom(async (req: Request) => {
});
}
+ if ((mode === "test" || mode === "sandbox") && workspace.stagingWorkspaceId) {
+ return logAndRespond({
+ eventType: event.type,
+ response: `Skipping ${mode} event for workspace ${workspace.id} because it has a staging workspace.`,
+ });
+ }
+
console.log("Workspace found", workspace);
let result: WebhookHandlerResponse = {
diff --git a/apps/web/app/(ee)/api/stripe/integration/webhook/utils/resolve-webhook-workspace.ts b/apps/web/app/(ee)/api/stripe/integration/webhook/utils/resolve-webhook-workspace.ts
deleted file mode 100644
index 495644fa14..0000000000
--- a/apps/web/app/(ee)/api/stripe/integration/webhook/utils/resolve-webhook-workspace.ts
+++ /dev/null
@@ -1,59 +0,0 @@
-import { prisma } from "@/lib/prisma";
-import { StripeMode } from "@/lib/types";
-
-export async function resolveWebhookWorkspace({
- stripeAccountId,
- mode,
-}: {
- stripeAccountId?: string | null;
- mode: StripeMode;
-}) {
- if (!stripeAccountId) {
- return null;
- }
-
- const workspace = await prisma.project.findUnique({
- where: {
- stripeConnectId: stripeAccountId,
- },
- select: {
- id: true,
- stripeConnectId: true,
- defaultProgramId: true,
- webhookEnabled: true,
- stagingWorkspaceId: true,
- },
- });
-
- if (!workspace) {
- return null;
- }
-
- // Test/sandbox events still arrive with the live workspace's Connect account.
- // If that workspace has a staging workspace that hasn't connected Stripe
- // itself, route the event there so test data doesn't land on live. Overlay
- // stripeConnectId so handlers can still call Stripe with the connected account.
- if (["test", "sandbox"].includes(mode) && workspace.stagingWorkspaceId) {
- const stagingWorkspace = await prisma.project.findUnique({
- where: {
- id: workspace.stagingWorkspaceId,
- },
- select: {
- id: true,
- stripeConnectId: true,
- defaultProgramId: true,
- webhookEnabled: true,
- stagingWorkspaceId: true,
- },
- });
-
- if (stagingWorkspace && !stagingWorkspace.stripeConnectId) {
- return {
- ...stagingWorkspace,
- stripeConnectId: stripeAccountId,
- };
- }
- }
-
- return workspace;
-}