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.

update track lead response

Kiran K6b96278f87c8
diff --git a/apps/web/app/api/track/lead/route.ts b/apps/web/app/api/track/lead/route.ts
index 9d49bdaa25..b6cba05e2c 100644
--- a/apps/web/app/api/track/lead/route.ts
+++ b/apps/web/app/api/track/lead/route.ts
@@ -4,7 +4,10 @@ import { withWorkspaceEdge } from "@/lib/auth/workspace-edge";
import { prismaEdge } from "@/lib/prisma/edge";
import { getClickEvent, recordCustomer, recordLead } from "@/lib/tinybird";
import { clickEventSchemaTB } from "@/lib/zod/schemas/clicks";
-import { trackLeadRequestSchema } from "@/lib/zod/schemas/leads";
+import {
+ trackLeadRequestSchema,
+ trackLeadResponseSchema,
+} from "@/lib/zod/schemas/leads";
import { nanoid } from "@dub/utils";
import { Customer } from "@prisma/client";
import { NextResponse } from "next/server";
@@ -66,29 +69,42 @@ export const POST = withWorkspaceEdge(
});
}
+ if (!customer) {
+ throw new DubApiError({
+ code: "bad_request",
+ message: `Failed to create customer with customerId: ${externalId}`,
+ });
+ }
+
await Promise.all([
+ recordCustomer({
+ customer_id: customer.id,
+ name: customerName,
+ email: customerEmail,
+ avatar: customerAvatar,
+ workspace_id: workspace.id,
+ }),
+
recordLead({
...clickData,
event_id: nanoid(16),
event_name: eventName,
- customer_id: customer?.id,
+ customer_id: customer.id,
metadata,
}),
-
- ...(customer
- ? [
- recordCustomer({
- customer_id: customer?.id,
- name: customerName,
- email: customerEmail,
- avatar: customerAvatar,
- workspace_id: workspace.id,
- }),
- ]
- : []),
]);
- return NextResponse.json({ success: true });
+ const response = {
+ clickId,
+ eventName,
+ customerName,
+ customerEmail,
+ customerAvatar,
+ customerId: externalId,
+ metadata: metadata ? JSON.parse(metadata) : null,
+ };
+
+ return NextResponse.json(trackLeadResponseSchema.parse(response));
},
{ betaFeature: true },
);
diff --git a/apps/web/lib/openapi/track/lead.ts b/apps/web/lib/openapi/track/lead.ts
index 598e269d56..f5a1a58a97 100644
--- a/apps/web/lib/openapi/track/lead.ts
+++ b/apps/web/lib/openapi/track/lead.ts
@@ -1,6 +1,8 @@
import { openApiErrorResponses } from "@/lib/openapi/responses";
-import z from "@/lib/zod";
-import { trackLeadRequestSchema } from "@/lib/zod/schemas/leads";
+import {
+ trackLeadRequestSchema,
+ trackLeadResponseSchema,
+} from "@/lib/zod/schemas/leads";
import { ZodOpenApiOperationObject } from "zod-openapi";
export const trackLead: ZodOpenApiOperationObject = {
@@ -20,9 +22,7 @@ export const trackLead: ZodOpenApiOperationObject = {
description: "A lead was tracked.",
content: {
"application/json": {
- schema: z.object({
- success: z.literal(true),
- }),
+ schema: trackLeadResponseSchema,
},
},
},
diff --git a/apps/web/lib/zod/schemas/leads.ts b/apps/web/lib/zod/schemas/leads.ts
index ff033b9c6f..1fb7dc6d49 100644
--- a/apps/web/lib/zod/schemas/leads.ts
+++ b/apps/web/lib/zod/schemas/leads.ts
@@ -7,10 +7,6 @@ export const trackLeadRequestSchema = z.object({
.string({ required_error: "eventName is required" })
.min(1)
.max(50),
- metadata: z
- .record(z.unknown())
- .nullish()
- .transform((val) => (val ? JSON.stringify(val) : "")),
customerId: z
.string()
.max(100)
@@ -21,6 +17,7 @@ export const trackLeadRequestSchema = z.object({
.string()
.max(100)
.nullish()
+ .default(null)
.describe(
"This is the name of the customer in the client's app. This is used to track the customer's journey.",
),
@@ -29,6 +26,7 @@ export const trackLeadRequestSchema = z.object({
.email()
.max(100)
.nullish()
+ .default(null)
.describe(
"This is the email of the customer in the client's app. This is used to track the customer's journey.",
),
@@ -36,9 +34,24 @@ export const trackLeadRequestSchema = z.object({
.string()
.max(100)
.nullish()
+ .default(null)
.describe(
"This is the avatar of the customer in the client's app. This is used to track the customer's journey.",
),
+ metadata: z
+ .record(z.unknown())
+ .nullish()
+ .transform((val) => (val ? JSON.stringify(val) : "")),
+});
+
+export const trackLeadResponseSchema = z.object({
+ clickId: z.string(),
+ eventName: z.string(),
+ customerId: z.string(),
+ customerName: z.string().nullable(),
+ customerEmail: z.string().nullable(),
+ customerAvatar: z.string().nullable(),
+ metadata: z.record(z.unknown()).nullable(),
});
export const leadEventSchemaTB = clickEventSchemaTB