The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.
TypeScript clickhousemysqlnextjsopen-sourceplanetscaleprismatailwindcsstinybirdtypescriptupstashvercel
Refactor `/track/sale` endpoint to return 304 status for non-existent customers and update response schemas to allow nullable customer and sale objects.
Kiran K96cf8eedd609
diff --git a/apps/web/app/api/track/sale/route.ts b/apps/web/app/api/track/sale/route.tsindex 205293bc17..d69f58c804 100644--- a/apps/web/app/api/track/sale/route.ts+++ b/apps/web/app/api/track/sale/route.ts@@ -52,12 +52,16 @@ export const POST = withWorkspaceEdge(});if (!customer) {- // instead of throwing an error, maybe we should return a 304 status code- // and say that the customer was not found, so sale was not tracked?- throw new DubApiError({- code: "not_found",- message: `Customer not found for externalId: ${customerExternalId}`,- });+ return NextResponse.json(+ {+ eventName,+ customer: null,+ sale: null,+ },+ {+ status: 304,+ },+ );}// Find leaddiff --git a/apps/web/lib/zod/schemas/sales.ts b/apps/web/lib/zod/schemas/sales.tsindex 80498b8299..969d4fbd54 100644--- a/apps/web/lib/zod/schemas/sales.ts+++ b/apps/web/lib/zod/schemas/sales.ts@@ -59,20 +59,24 @@ export const trackSaleRequestSchema = z.object({export const trackSaleResponseSchema = z.object({eventName: z.string(),- customer: z.object({- id: z.string(),- name: z.string().nullable(),- email: z.string().nullable(),- avatar: z.string().nullable(),- externalId: z.string().nullable(),- }),- sale: z.object({- amount: z.number(),- currency: z.string(),- paymentProcessor: z.string(),- invoiceId: z.string().nullable(),- metadata: z.record(z.unknown()).nullable(),- }),+ customer: z+ .object({+ id: z.string(),+ name: z.string().nullable(),+ email: z.string().nullable(),+ avatar: z.string().nullable(),+ externalId: z.string().nullable(),+ })+ .nullable(),+ sale: z+ .object({+ amount: z.number(),+ currency: z.string(),+ paymentProcessor: z.string(),+ invoiceId: z.string().nullable(),+ metadata: z.record(z.unknown()).nullable(),+ })+ .nullable(),});export const saleEventSchemaTB = clickEventSchemaTBdiff --git a/apps/web/tests/tracks/track-sale.test.ts b/apps/web/tests/tracks/track-sale.test.tsindex b312f54407..218f3470f1 100644--- a/apps/web/tests/tracks/track-sale.test.ts+++ b/apps/web/tests/tracks/track-sale.test.ts@@ -50,4 +50,20 @@ test("POST /track/sale", async () => {metadata: null,invoiceId: sale.invoiceId,});++ // An externalId that does not exist should return a 304 status code+ const response2 = await http.post<TrackSaleResponse>({+ path: "/track/sale",+ body: {+ ...sale,+ externalId: "external-id-that-does-not-exist",+ },+ });++ expect(response2.status).toEqual(304);+ expect(response2.data).toStrictEqual({+ eventName: "Subscription",+ customer: null,+ sale: null,+ });});