The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.
TypeScript clickhousemysqlnextjsopen-sourceplanetscaleprismatailwindcsstinybirdtypescriptupstashvercel
return the created lead and customer
Kiran Kb38b25ec7807
diff --git a/apps/web/app/api/track/customer/route.ts b/apps/web/app/api/track/customer/route.tsindex 723521cbac..dda83dbd52 100644--- a/apps/web/app/api/track/customer/route.ts+++ b/apps/web/app/api/track/customer/route.ts@@ -1,7 +1,12 @@import { parseRequestBody } from "@/lib/api/utils";import { withWorkspaceEdge } from "@/lib/auth/workspace-edge";+import { prismaEdge } from "@/lib/prisma/edge";import { recordCustomer } from "@/lib/tinybird";-import { trackCustomerRequestSchema } from "@/lib/zod/schemas/customers";+import {+ trackCustomerRequestSchema,+ trackCustomerResponsetSchema,+} from "@/lib/zod/schemas/customers";+import { nanoid } from "@dub/utils";import { NextResponse } from "next/server";export const runtime = "edge";@@ -9,19 +14,50 @@ export const runtime = "edge";// POST /api/track/customer – Track a customer objectexport const POST = withWorkspaceEdge(async ({ req, workspace }) => {- const { customerId, customerName, customerEmail, customerAvatar } =+ const { customerName, customerEmail, customerAvatar, customerId } =trackCustomerRequestSchema.parse(await parseRequestBody(req));+ // Create or update customer+ const customer = await prismaEdge.customer.upsert({+ where: {+ projectId_externalId: {+ projectId: workspace.id,+ externalId: customerId,+ },+ },+ create: {+ id: nanoid(16),+ name: customerName, // TODO: Generate random name if not provided+ email: customerEmail,+ avatar: customerAvatar,+ externalId: customerId,+ projectId: workspace.id,+ projectConnectId: "", // TODO: This can be null+ },+ update: {+ name: customerName,+ email: customerEmail,+ avatar: customerAvatar,+ },+ });+// Record customerawait recordCustomer({- customer_id: customerId,+ workspace_id: workspace.id,+ customer_id: customer.id,name: customerName,email: customerEmail,avatar: customerAvatar,- workspace_id: workspace.id,});- return NextResponse.json({ success: true });+ const response = trackCustomerResponsetSchema.parse({+ customerId,+ customerName,+ customerEmail,+ customerAvatar,+ });++ return NextResponse.json(response);},{ betaFeature: true },);diff --git a/apps/web/app/api/track/lead/route.ts b/apps/web/app/api/track/lead/route.tsindex b6cba05e2c..5da756c16a 100644--- a/apps/web/app/api/track/lead/route.ts+++ b/apps/web/app/api/track/lead/route.ts@@ -78,11 +78,11 @@ export const POST = withWorkspaceEdge(await Promise.all([recordCustomer({+ workspace_id: workspace.id,customer_id: customer.id,name: customerName,email: customerEmail,avatar: customerAvatar,- workspace_id: workspace.id,}),recordLead({@@ -94,7 +94,7 @@ export const POST = withWorkspaceEdge(}),]);- const response = {+ const response = trackLeadResponseSchema.parse({clickId,eventName,customerName,@@ -102,9 +102,9 @@ export const POST = withWorkspaceEdge(customerAvatar,customerId: externalId,metadata: metadata ? JSON.parse(metadata) : null,- };+ });- return NextResponse.json(trackLeadResponseSchema.parse(response));+ return NextResponse.json(response);},{ betaFeature: true },);diff --git a/apps/web/lib/zod/schemas/customers.ts b/apps/web/lib/zod/schemas/customers.tsindex 3bbd86c8f4..1339e3279e 100644--- a/apps/web/lib/zod/schemas/customers.ts+++ b/apps/web/lib/zod/schemas/customers.ts@@ -2,16 +2,23 @@ import z from "@/lib/zod";export const trackCustomerRequestSchema = z.object({customerId: z.string({ required_error: "customerId is required" }),- customerName: z.string().nullish(),- customerEmail: z.string().email().nullish(),- customerAvatar: z.string().url().nullish(),+ customerName: z.string().nullish().default(null),+ customerEmail: z.string().email().nullish().default(null),+ customerAvatar: z.string().url().nullish().default(null),+});++export const trackCustomerResponsetSchema = z.object({+ customerId: z.string(),+ customerName: z.string().nullable(),+ customerEmail: z.string().nullable(),+ customerAvatar: z.string().nullable(),});export const customersMetadataSchema = z.object({- customer_id: z.string(),- name: z.string().nullish().default(""),- email: z.string().nullish().default(""),- avatar: z.string().nullish().default(""),workspace_id: z.string(),+ customer_id: z.string(),+ name: z.string().nullable().default(""),+ email: z.string().nullable().default(""),+ avatar: z.string().nullable().default(""),deleted: z.number().default(0),});diff --git a/apps/web/tests/tracks/create-lead.test.ts b/apps/web/tests/tracks/create-lead.test.tsnew file mode 100644index 0000000000..8d08311ab5--- /dev/null+++ b/apps/web/tests/tracks/create-lead.test.ts@@ -0,0 +1,65 @@+import { trackLeadResponseSchema } from "@/lib/zod/schemas/leads";+import { Tag } from "@prisma/client";+import { expect, test } from "vitest";+import { IntegrationHarness } from "../utils/integration";++const customer = {+ customerId: "david",+ customerName: "David",+ customerEmail: "[email protected]",+ customerAvatar: "https://example.com/david.jpeg",+ metadata: { plan: "enterprise" },+};++test.skip("POST /track/lead", async (ctx) => {+ const h = new IntegrationHarness(ctx);+ const { workspace, http } = await h.init();+ const { workspaceId } = workspace;+ const clickId = "uxdcuMfBa5Nqnbwk";++ // With required params+ let response = await http.post<Tag>({+ path: "/track/lead",+ query: { workspaceId },+ body: {+ clickId,+ eventName: "Signup",+ customerId: customer.customerId,+ },+ });++ let parsed = trackLeadResponseSchema.safeParse(response.data);++ expect(parsed.success).toBe(true);+ expect(response.status).toEqual(201);+ expect(response.data).toStrictEqual({+ clickId,+ eventName: "Signup",+ customerId: customer.customerId,+ customerName: null,+ customerEmail: null,+ customerAvatar: null,+ metadata: null,+ });++ // With all params+ response = await http.post<Tag>({+ path: "/track/lead",+ query: { workspaceId },+ body: {+ clickId,+ eventName: "Signup",+ ...customer,+ },+ });++ parsed = trackLeadResponseSchema.safeParse(response.data);++ expect(parsed.success).toBe(true);+ expect(response.status).toEqual(201);+ expect(response.data).toStrictEqual({+ clickId,+ eventName: "Signup",+ ...customer,+ });+});