-
Notifications
You must be signed in to change notification settings - Fork 3.3k
Expand file tree
/
Copy pathtrack-lead.ts
More file actions
387 lines (356 loc) · 11.9 KB
/
Copy pathtrack-lead.ts
File metadata and controls
387 lines (356 loc) · 11.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
import { createId } from "@/lib/api/create-id";
import { DubApiError } from "@/lib/api/errors";
import { includeTags } from "@/lib/api/links/include-tags";
import { generateRandomName } from "@/lib/names";
import { queuePartnerCommissionCreation } from "@/lib/partners/queue-partner-commission-creation";
import { sendPartnerPostback } from "@/lib/postback/send-partner-postback";
import { prisma } from "@/lib/prisma";
import { isStored, storage } from "@/lib/storage";
import { getClickEvent, recordLead } from "@/lib/tinybird";
import { CustomerSource, WorkspaceProps } from "@/lib/types";
import { redis } from "@/lib/upstash";
import { sendWorkspaceWebhook } from "@/lib/webhook/publish";
import { transformLeadEventData } from "@/lib/webhook/transform";
import {
trackLeadRequestSchema,
trackLeadResponseSchema,
} from "@/lib/zod/schemas/leads";
import { nanoid, R2_URL } from "@dub/utils";
import { Link } from "@prisma/client";
import { waitUntil } from "@vercel/functions";
import * as z from "zod/v4";
import { syncPartnerLinksStats } from "../partners/sync-partner-links-stats";
import { executeWorkflows } from "../workflows/execute-workflows";
type TrackLeadParams = z.input<typeof trackLeadRequestSchema> & {
workspace: Pick<WorkspaceProps, "id" | "stripeConnectId" | "webhookEnabled">;
source?: CustomerSource; // default is "tracked"
};
export const trackLead = async ({
clickId,
eventName,
customerExternalId,
customerName,
customerEmail,
customerAvatar,
mode,
eventQuantity,
metadata,
workspace,
source = "tracked",
}: TrackLeadParams) => {
// try to find the customer to use if it exists
let customer = await prisma.customer.findUnique({
where: {
projectId_externalId: {
projectId: workspace.id,
externalId: customerExternalId,
},
},
});
let link: Link | null = null;
// if clickId is an empty string, use the existing customer's clickId if it exists
// otherwise, throw an error (this is for mode="deferred" lead tracking)
if (!clickId) {
if (!customer || !customer.clickId) {
throw new DubApiError({
code: "bad_request",
message:
"The `clickId` property was not provided in the request, and no existing customer with the provided `customerExternalId` was found.",
});
}
clickId = customer.clickId;
}
const stringifiedEventName = eventName.toLowerCase().replaceAll(" ", "-");
const finalCustomerId = createId({ prefix: "cus_" });
const finalCustomerName =
customerName || customerEmail || generateRandomName();
const finalCustomerAvatar =
customerAvatar && !isStored(customerAvatar)
? `${R2_URL}/customers/${finalCustomerId}/avatar_${nanoid(7)}`
: customerAvatar;
let isDuplicateEvent = false;
// if not deferred mode, we need to deduplicate lead events – only record 1 unique event for the same customer and event name
// TODO: Maybe we can replace this to rely only on MySQL directly since we're checking the customer above?
if (mode !== "deferred") {
const res = await redis.set(
`trackLead:${workspace.id}:${customerExternalId}:${stringifiedEventName}`,
{
timestamp: Date.now(),
clickId,
eventName,
customerExternalId,
customerName,
customerEmail,
customerAvatar,
},
{
ex: 60 * 60 * 24 * 7, // cache for 1 week
nx: true,
},
);
// if res = null it means the key was already set
isDuplicateEvent = res === null ? true : false;
}
// if it's not a duplicate event
// (e.g. mode === 'deferred' or it's regular mode but the first time processing this event)
// we can proceed with the lead tracking process
if (!isDuplicateEvent) {
// First, we need to find the click event
const clickData = await getClickEvent({ clickId });
// if there is no click data, throw an error
if (!clickData) {
throw new DubApiError({
code: "not_found",
message: `Click event not found for clickId: ${clickId}`,
});
}
// get the referral link from the from the clickData
link = await prisma.link.findUnique({
where: {
id: clickData.link_id,
},
});
if (!link) {
throw new DubApiError({
code: "not_found",
message: `Link not found for clickId: ${clickId}`,
});
}
if (link.projectId !== workspace.id) {
throw new DubApiError({
code: "not_found",
message: `Link ${link.id} for clickId ${clickId} does not belong to the workspace`,
});
}
if (link.disabledAt) {
throw new DubApiError({
code: "not_found",
message: `Link ${link.id} for clickId ${clickId} is disabled, lead not tracked`,
});
}
const leadEventId = nanoid(16);
// Create a function to prepare the lead event payload
const createLeadEventPayload = (customerId: string) => {
const basePayload = {
...clickData,
workspace_id: clickData.workspace_id || workspace.id, // in case for some reason the click event doesn't have workspace_id
event_id: leadEventId,
event_name: eventName,
customer_id: customerId,
metadata: metadata ? JSON.stringify(metadata) : "",
};
return eventQuantity
? Array(eventQuantity)
.fill(null)
.map(() => ({
...basePayload,
event_id: nanoid(16),
}))
: basePayload;
};
// if the customer doesn't exist in our MySQL DB yet, upsert it
// (here we're doing upsert and not create in case of race conditions)
if (!customer) {
customer = await prisma.customer.upsert({
where: {
projectId_externalId: {
projectId: workspace.id,
externalId: customerExternalId,
},
},
create: {
id: finalCustomerId,
name: finalCustomerName,
email: customerEmail,
avatar: finalCustomerAvatar,
externalId: customerExternalId,
projectId: workspace.id,
projectConnectId: workspace.stripeConnectId,
clickId: clickData.click_id,
linkId: link.id,
programId: link.programId,
partnerId: link.partnerId,
country: clickData.country,
clickedAt: new Date(clickData.timestamp + "Z"),
},
update: {},
});
}
// if wait mode, record the lead event synchronously
if (mode === "wait") {
const leadEventPayload = createLeadEventPayload(customer.id);
const cacheLeadEventPayload = Array.isArray(leadEventPayload)
? leadEventPayload[0]
: leadEventPayload;
await Promise.all([
// Cache the latest lead event for 5 minutes because the ingested event is not available immediately on Tinybird
// we're setting two keys because we want to support the use case where the customer has multiple lead events
redis.set(`leadCache:${customer.id}`, cacheLeadEventPayload, {
ex: 60 * 5,
}),
redis.set(
`leadCache:${customer.id}:${stringifiedEventName}`,
cacheLeadEventPayload,
{
ex: 60 * 5,
},
),
]);
}
waitUntil(
(async () => {
// for deferred mode, we defer the lead event creation to a subsequent request
if (mode !== "deferred") {
await recordLead(createLeadEventPayload(customer.id));
}
if (
customerAvatar &&
!isStored(customerAvatar) &&
finalCustomerAvatar
) {
// persist customer avatar to R2
await storage
.upload({
key: finalCustomerAvatar.replace(`${R2_URL}/`, ""),
body: customerAvatar,
opts: {
width: 128,
height: 128,
},
})
.catch(async (error) => {
console.error("Error persisting customer avatar to R2", error);
// if the avatar fails to upload to R2, set the avatar to null in the database
if (customer) {
await prisma.customer.update({
where: { id: customer.id },
data: { avatar: null },
});
}
});
}
// if not deferred mode, process the following right away:
// - update link, workspace, and customer stats
// - for partner links, create partner commission and execute workflows
// - send lead.created webhook
if (mode !== "deferred") {
const [updatedLink, _project] = await Promise.all([
// update link leads count
prisma.link.update({
where: {
id: link.id,
},
data: {
leads: {
increment: eventQuantity ?? 1,
},
lastLeadAt: new Date(),
},
include: includeTags,
}),
// update workspace events usage
prisma.project.update({
where: {
id: workspace.id,
},
data: {
usage: {
increment: eventQuantity ?? 1,
},
},
}),
]);
link = updatedLink; // update the link variable to the latest version
let result: Awaited<
ReturnType<typeof queuePartnerCommissionCreation>
> | null = null;
if (link.programId && link.partnerId && customer) {
result = await queuePartnerCommissionCreation({
event: "lead",
programId: link.programId,
partnerId: link.partnerId,
linkId: link.id,
eventId: leadEventId,
customerId: customer.id,
quantity: eventQuantity ?? 1,
context: {
customer: {
country: customer.country,
source,
},
lead: {
...(metadata != null && { metadata }),
},
},
clickEvent: {
url: clickData.url,
referer: clickData.referer,
},
});
await Promise.allSettled([
executeWorkflows({
trigger: "partnerMetricsUpdated",
reason: "lead",
identity: {
workspaceId: workspace.id,
programId: link.programId,
partnerId: link.partnerId,
},
metrics: {
current: {
leads: 1,
},
},
}),
syncPartnerLinksStats({
partnerId: link.partnerId,
programId: link.programId,
eventType: "lead",
}),
]);
}
await Promise.allSettled([
sendWorkspaceWebhook({
trigger: "lead.created",
data: transformLeadEventData({
...clickData,
eventName,
link,
customer,
partner: result?.webhookPartner,
metadata,
}),
workspace,
}),
...(link.partnerId
? [
sendPartnerPostback({
partnerId: link.partnerId,
event: "lead.created",
data: {
...clickData,
eventName,
link,
customer,
},
}),
]
: []),
]);
}
})(),
);
}
return trackLeadResponseSchema.parse({
click: {
id: clickId,
},
link,
customer: customer ?? {
name: finalCustomerName,
email: customerEmail || null,
avatar: finalCustomerAvatar || null,
externalId: customerExternalId,
},
});
};