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.

Refactor click data handling and timestamp processing across integrations

Kiran K1f6248f819f6
diff --git a/apps/web/app/api/stripe/integration/webhook/customer-created.ts b/apps/web/app/api/stripe/integration/webhook/customer-created.ts
index a0fd50cbf0..3fc4afd6f7 100644
--- a/apps/web/app/api/stripe/integration/webhook/customer-created.ts
+++ b/apps/web/app/api/stripe/integration/webhook/customer-created.ts
@@ -127,7 +127,7 @@ export async function customerCreated(event: Stripe.Event) {
trigger: "lead.created",
workspace,
data: transformLeadEventData({
- ...leadData,
+ ...clickData,
link: linkUpdated,
customerId: customer.id,
customerExternalId: customer.externalId,
diff --git a/apps/web/app/api/stripe/integration/webhook/invoice-paid.ts b/apps/web/app/api/stripe/integration/webhook/invoice-paid.ts
index f693e2ad6e..e60156099e 100644
--- a/apps/web/app/api/stripe/integration/webhook/invoice-paid.ts
+++ b/apps/web/app/api/stripe/integration/webhook/invoice-paid.ts
@@ -178,6 +178,7 @@ export async function invoicePaid(event: Stripe.Event) {
data: transformSaleEventData({
...saleData,
link: linkUpdated,
+ clickedAt: customer.clickedAt || customer.createdAt,
customerId: customer.id,
customerExternalId: customer.externalId,
customerName: customer.name,
diff --git a/apps/web/app/api/track/lead/route.ts b/apps/web/app/api/track/lead/route.ts
index 4ad1e95916..bbf9d48f77 100644
--- a/apps/web/app/api/track/lead/route.ts
+++ b/apps/web/app/api/track/lead/route.ts
@@ -121,21 +121,19 @@ export const POST = withWorkspaceEdge(
}),
]);
- const lead = transformLeadEventData({
- ...clickData,
- link,
- eventName,
- customerId: customer.id,
- customerExternalId: customer.externalId,
- customerName: customer.name,
- customerEmail: customer.email,
- customerAvatar: customer.avatar,
- customerCreatedAt: customer.createdAt,
- });
-
await sendWorkspaceWebhookOnEdge({
trigger: "lead.created",
- data: lead,
+ data: transformLeadEventData({
+ ...clickData,
+ link,
+ eventName,
+ customerId: customer.id,
+ customerExternalId: customer.externalId,
+ customerName: customer.name,
+ customerEmail: customer.email,
+ customerAvatar: customer.avatar,
+ customerCreatedAt: customer.createdAt,
+ }),
workspace,
});
})(),
diff --git a/apps/web/app/api/track/sale/route.ts b/apps/web/app/api/track/sale/route.ts
index d637cc0219..2b160f009b 100644
--- a/apps/web/app/api/track/sale/route.ts
+++ b/apps/web/app/api/track/sale/route.ts
@@ -188,6 +188,7 @@ export const POST = withWorkspaceEdge(
invoiceId,
amount,
currency,
+ clickedAt: customer.clickedAt || customer.createdAt,
customerId: customer.id,
customerExternalId: customer.externalId,
customerName: customer.name,
diff --git a/apps/web/lib/integrations/shopify/create-lead.ts b/apps/web/lib/integrations/shopify/create-lead.ts
index bb91946f07..11c932b7cc 100644
--- a/apps/web/lib/integrations/shopify/create-lead.ts
+++ b/apps/web/lib/integrations/shopify/create-lead.ts
@@ -87,7 +87,7 @@ export async function createShopifyLead({
trigger: "lead.created",
workspace,
data: transformLeadEventData({
- ...leadData,
+ ...clickData,
link,
customerId: customer.id,
customerExternalId: customer.externalId,
diff --git a/apps/web/lib/integrations/shopify/create-sale.ts b/apps/web/lib/integrations/shopify/create-sale.ts
index 42f78e0670..0b50f1dae0 100644
--- a/apps/web/lib/integrations/shopify/create-sale.ts
+++ b/apps/web/lib/integrations/shopify/create-sale.ts
@@ -97,7 +97,7 @@ export async function createShopifySale({
},
}),
- prisma.customer.findUnique({
+ prisma.customer.findUniqueOrThrow({
where: {
id: customerId,
},
@@ -113,16 +113,13 @@ export async function createShopifySale({
data: transformSaleEventData({
...saleData,
link,
- ...(customer
- ? {
- customerId: customer.id,
- customerExternalId: customer.externalId,
- customerName: customer.name,
- customerEmail: customer.email,
- customerAvatar: customer.avatar,
- customerCreatedAt: customer.createdAt,
- }
- : {}),
+ clickedAt: customer.clickedAt || customer.createdAt,
+ customerId: customer.id,
+ customerExternalId: customer.externalId,
+ customerName: customer.name,
+ customerEmail: customer.email,
+ customerAvatar: customer.avatar,
+ customerCreatedAt: customer.createdAt,
}),
}),
);
diff --git a/apps/web/lib/webhook/transform.ts b/apps/web/lib/webhook/transform.ts
index 913052da4b..c80401c4d4 100644
--- a/apps/web/lib/webhook/transform.ts
+++ b/apps/web/lib/webhook/transform.ts
@@ -66,6 +66,7 @@ export const transformLeadEventData = (data: any) => {
click: {
...lead,
id: lead.clickId,
+ timestamp: new Date(lead.timestamp as string),
},
// transformLink -> add shortLink, qrCode, workspaceId, etc.
link: transformLink(lead.link as ExpandedLink),
diff --git a/apps/web/lib/zod/schemas/clicks.ts b/apps/web/lib/zod/schemas/clicks.ts
index f31cc1de28..b3736a8340 100644
--- a/apps/web/lib/zod/schemas/clicks.ts
+++ b/apps/web/lib/zod/schemas/clicks.ts
@@ -54,7 +54,7 @@ export const clickEventSchemaTBEndpoint = z.object({
export const clickEventSchema = z.object({
id: z.string(),
- timestamp: z.string(),
+ timestamp: z.date(),
url: z.string(),
country: z.string(),
city: z.string(),