The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.
TypeScript clickhousemysqlnextjsopen-sourceplanetscaleprismatailwindcsstinybirdtypescriptupstashvercel
Fix customer.created webhook race condition
Steven Teyffb1cb3bad6f
diff --git a/apps/web/app/api/stripe/integration/webhook/customer-created.ts b/apps/web/app/api/stripe/integration/webhook/customer-created.tsindex 91c77b381c..59c635bd6b 100644--- a/apps/web/app/api/stripe/integration/webhook/customer-created.ts+++ b/apps/web/app/api/stripe/integration/webhook/customer-created.ts@@ -3,6 +3,7 @@ import { getClickEvent, recordLead } from "@/lib/tinybird";import { sendWorkspaceWebhook } from "@/lib/webhook/publish";import { transformLeadEventData } from "@/lib/webhook/transform";import { prisma } from "@dub/prisma";+import { Customer } from "@dub/prisma/client";import { nanoid } from "@dub/utils";import { waitUntil } from "@vercel/functions";import type Stripe from "stripe";@@ -50,27 +51,40 @@ export async function customerCreated(event: Stripe.Event) {},});+ let customer: Customer;+if (customerFound) {- return `Customer with external ID ${externalId} already exists, skipping...`;+ // if customer exists, update it+ customer = await prisma.customer.update({+ where: {+ id: customerFound.id,+ },+ data: {+ name: stripeCustomer.name,+ email: stripeCustomer.email,+ stripeCustomerId: stripeCustomer.id,+ projectConnectId: stripeAccountId,+ },+ });+ } else {+ // else create a new customer+ customer = await prisma.customer.create({+ data: {+ id: createId({ prefix: "cus_" }),+ name: stripeCustomer.name,+ email: stripeCustomer.email,+ stripeCustomerId: stripeCustomer.id,+ projectConnectId: stripeAccountId,+ externalId,+ projectId: link.projectId,+ linkId,+ clickId,+ clickedAt: new Date(clickData.timestamp + "Z"),+ country: clickData.country,+ },+ });}- // Create customer- const customer = await prisma.customer.create({- data: {- id: createId({ prefix: "cus_" }),- name: stripeCustomer.name,- email: stripeCustomer.email,- stripeCustomerId: stripeCustomer.id,- projectConnectId: stripeAccountId,- externalId,- projectId: link.projectId,- linkId,- clickId,- clickedAt: new Date(clickData.timestamp + "Z"),- country: clickData.country,- },- });-const leadData = {...clickData,event_id: nanoid(16),