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.

customerId -> externalId

Kiran K4af573520d81
diff --git a/apps/web/app/api/track/lead/route.ts b/apps/web/app/api/track/lead/route.ts
index c3fe257090..11893cc313 100644
--- a/apps/web/app/api/track/lead/route.ts
+++ b/apps/web/app/api/track/lead/route.ts
@@ -24,22 +24,32 @@ export const POST = withWorkspaceEdge(
const {
clickId,
eventName,
- customerId: externalId,
+ externalId,
+ customerId, // deprecated
customerName,
customerEmail,
customerAvatar,
metadata,
} = trackLeadRequestSchema.parse(await parseRequestBody(req));
+ const customerExternalId = externalId || customerId;
+
+ if (!customerExternalId) {
+ throw new DubApiError({
+ code: "bad_request",
+ message: "externalId is required",
+ });
+ }
+
// deduplicate lead events – only record 1 event per hour
const { success } = await ratelimit(1, "1 h").limit(
- `recordLead:${externalId}:${eventName.toLowerCase().replace(" ", "-")}`,
+ `recordLead:${customerExternalId}:${eventName.toLowerCase().replace(" ", "-")}`,
);
if (!success) {
throw new DubApiError({
code: "rate_limit_exceeded",
- message: `Rate limit exceeded for customer ${externalId}: ${eventName}`,
+ message: `Rate limit exceeded for customer ${customerExternalId}: ${eventName}`,
});
}
@@ -67,7 +77,7 @@ export const POST = withWorkspaceEdge(
where: {
projectId_externalId: {
projectId: workspace.id,
- externalId,
+ externalId: customerExternalId,
},
},
create: {
@@ -75,7 +85,7 @@ export const POST = withWorkspaceEdge(
name: finalCustomerName,
email: customerEmail,
avatar: customerAvatar,
- externalId,
+ externalId: customerExternalId,
projectId: workspace.id,
projectConnectId: workspace.stripeConnectId,
},
@@ -143,7 +153,7 @@ export const POST = withWorkspaceEdge(
id: clickId,
},
customer: {
- id: externalId,
+ id: customerExternalId,
name: finalCustomerName,
email: customerEmail,
avatar: customerAvatar,
@@ -154,7 +164,7 @@ export const POST = withWorkspaceEdge(
...lead,
// for backwards compatibility – will remove soon
clickId,
- customerId: externalId,
+ customerId: customerExternalId,
customerName: finalCustomerName,
customerEmail: customerEmail,
customerAvatar: customerAvatar,
diff --git a/apps/web/app/api/track/sale/route.ts b/apps/web/app/api/track/sale/route.ts
index 307560cecf..0cfa295a63 100644
--- a/apps/web/app/api/track/sale/route.ts
+++ b/apps/web/app/api/track/sale/route.ts
@@ -20,7 +20,8 @@ export const runtime = "edge";
export const POST = withWorkspaceEdge(
async ({ req, workspace }) => {
const {
- customerId: externalId,
+ externalId,
+ customerId, // deprecated
paymentProcessor,
invoiceId,
amount,
@@ -29,12 +30,21 @@ export const POST = withWorkspaceEdge(
eventName,
} = trackSaleRequestSchema.parse(await parseRequestBody(req));
+ const customerExternalId = externalId || customerId;
+
+ if (!customerExternalId) {
+ throw new DubApiError({
+ code: "bad_request",
+ message: "externalId is required",
+ });
+ }
+
// Find customer
const customer = await prismaEdge.customer.findUnique({
where: {
projectId_externalId: {
projectId: workspace.id,
- externalId,
+ externalId: customerExternalId,
},
},
});
@@ -42,7 +52,7 @@ export const POST = withWorkspaceEdge(
if (!customer) {
throw new DubApiError({
code: "not_found",
- message: `Customer not found for customerId: ${externalId}`,
+ message: `Customer not found for externalId: ${customerExternalId}`,
});
}
@@ -52,7 +62,7 @@ export const POST = withWorkspaceEdge(
if (!leadEvent || leadEvent.data.length === 0) {
throw new DubApiError({
code: "not_found",
- message: `Lead event not found for customerId: ${externalId}`,
+ message: `Lead event not found for externalId: ${customerExternalId}`,
});
}
@@ -148,7 +158,7 @@ export const POST = withWorkspaceEdge(
return NextResponse.json({
...sale,
// for backwards compatibility – will remove soon
- customerId: externalId,
+ customerId: customerExternalId,
amount,
currency,
invoiceId,
diff --git a/apps/web/lib/webhook/transform.ts b/apps/web/lib/webhook/transform.ts
index 4cc9efc51c..a8e4c94b8b 100644
--- a/apps/web/lib/webhook/transform.ts
+++ b/apps/web/lib/webhook/transform.ts
@@ -84,7 +84,9 @@ export const transformSaleEventData = (data: any) => {
externalId: sale.customerExternalId,
name: sale.customerName,
email: sale.customerEmail,
- avatar: sale.customerAvatar,
+ avatar:
+ sale.customerAvatar ||
+ `https://api.dicebear.com/9.x/micah/svg?seed=${sale.customerId}`,
createdAt: sale.customerCreatedAt,
},
sale: {
diff --git a/apps/web/lib/zod/schemas/leads.ts b/apps/web/lib/zod/schemas/leads.ts
index 3a280c3690..8b78b6cf12 100644
--- a/apps/web/lib/zod/schemas/leads.ts
+++ b/apps/web/lib/zod/schemas/leads.ts
@@ -5,7 +5,6 @@ import { commonDeprecatedEventFields } from "./deprecated";
import { linkEventSchema, LinkSchema } from "./links";
export const trackLeadRequestSchema = z.object({
- // Required
clickId: z
.string({ required_error: "clickId is required" })
.trim()
@@ -20,16 +19,24 @@ export const trackLeadRequestSchema = z.object({
.max(50)
.describe("The name of the event to track.")
.openapi({ example: "Sign up" }),
- customerId: z
- .string({ required_error: "customerId is required" })
+ externalId: z
+ .string()
.trim()
- .min(1, "customerId is required")
.max(100)
+ .default("") // Remove this after migrating users from customerId to externalId
.describe(
"This is the unique identifier for the customer in the client's app. This is used to track the customer's journey.",
),
-
- // Optional
+ customerId: z
+ .string()
+ .trim()
+ .max(100)
+ .nullish()
+ .default(null)
+ .describe(
+ "This is the unique identifier for the customer in the client's app. This is used to track the customer's journey.",
+ )
+ .openapi({ deprecated: true }),
customerName: z
.string()
.max(100)
diff --git a/apps/web/lib/zod/schemas/sales.ts b/apps/web/lib/zod/schemas/sales.ts
index 27ccacbaca..00f58a5d4d 100644
--- a/apps/web/lib/zod/schemas/sales.ts
+++ b/apps/web/lib/zod/schemas/sales.ts
@@ -5,15 +5,24 @@ import { commonDeprecatedEventFields } from "./deprecated";
import { linkEventSchema, LinkSchema } from "./links";
export const trackSaleRequestSchema = z.object({
- // Required
- customerId: z
- .string({ required_error: "customerId is required" })
+ externalId: z
+ .string()
.trim()
- .min(1, "customerId is required")
.max(100)
+ .default("") // Remove this after migrating users from customerId to externalId
.describe(
"This is the unique identifier for the customer in the client's app. This is used to track the customer's journey.",
),
+ customerId: z
+ .string()
+ .trim()
+ .max(100)
+ .nullish()
+ .default(null)
+ .describe(
+ "This is the unique identifier for the customer in the client's app. This is used to track the customer's journey.",
+ )
+ .openapi({ deprecated: true }),
amount: z
.number({ required_error: "amount is required" })
.int()
@@ -22,8 +31,6 @@ export const trackSaleRequestSchema = z.object({
paymentProcessor: z
.enum(["stripe", "shopify", "paddle"])
.describe("The payment processor via which the sale was made."),
-
- // Optional
eventName: z
.string()
.max(50)