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 route.ts

Steven Tey42587e627e17
diff --git a/apps/web/app/api/cron/framer/backfill-leads/route.ts b/apps/web/app/api/cron/framer/backfill-leads/route.ts
index 020254431c..ee394e9438 100644
--- a/apps/web/app/api/cron/framer/backfill-leads/route.ts
+++ b/apps/web/app/api/cron/framer/backfill-leads/route.ts
@@ -36,77 +36,78 @@ export const POST = withWorkspace(async ({ req, workspace }) => {
},
});
- if (customerFound) {
- return NextResponse.json({
- message: "Customer already exists",
+ // if customer doesn't exist:
+ // - record a dummy click
+ // - create a customer
+ // - record a lead
+ // - increment link leads
+ if (!customerFound) {
+ const link = await prisma.link.findUniqueOrThrow({
+ where: {
+ domain_key: { domain: "framer.link", key: via },
+ },
});
- }
- const link = await prisma.link.findUniqueOrThrow({
- where: {
- domain_key: { domain: "framer.link", key: via },
- },
- });
-
- const dummyRequest = new Request(link.url, {
- headers: new Headers({
- "user-agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7)",
- "x-forwarded-for": "127.0.0.1",
- "x-vercel-ip-country": "US",
- "x-vercel-ip-country-region": "CA",
- "x-vercel-ip-continent": "NA",
- }),
- });
+ const dummyRequest = new Request(link.url, {
+ headers: new Headers({
+ "user-agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7)",
+ "x-forwarded-for": "127.0.0.1",
+ "x-vercel-ip-country": "US",
+ "x-vercel-ip-country-region": "CA",
+ "x-vercel-ip-continent": "NA",
+ }),
+ });
- const clickData = await recordClick({
- req: dummyRequest,
- linkId: link.id,
- clickId: nanoid(16),
- url: link.url,
- domain: link.domain,
- key: link.key,
- workspaceId: workspace.id,
- skipRatelimit: true,
- timestamp: new Date(creationDate).toISOString(),
- });
+ const clickData = await recordClick({
+ req: dummyRequest,
+ linkId: link.id,
+ clickId: nanoid(16),
+ url: link.url,
+ domain: link.domain,
+ key: link.key,
+ workspaceId: workspace.id,
+ skipRatelimit: true,
+ timestamp: new Date(creationDate).toISOString(),
+ });
- const clickEvent = clickEventSchemaTB.parse({
- ...clickData,
- bot: 0,
- qr: 0,
- });
+ const clickEvent = clickEventSchemaTB.parse({
+ ...clickData,
+ bot: 0,
+ qr: 0,
+ });
- const randomCustomerId = createId({ prefix: "cus_" });
+ const randomCustomerId = createId({ prefix: "cus_" });
- await Promise.all([
- prisma.customer.create({
- data: {
- id: randomCustomerId,
- name: generateRandomName(),
- externalId,
- projectId: workspace.id,
- projectConnectId: workspace.stripeConnectId,
- clickId: clickEvent.click_id,
- linkId: link.id,
- country: clickEvent.country,
- clickedAt: new Date(creationDate),
- createdAt: new Date(creationDate),
- },
- }),
+ await Promise.all([
+ prisma.customer.create({
+ data: {
+ id: randomCustomerId,
+ name: generateRandomName(),
+ externalId,
+ projectId: workspace.id,
+ projectConnectId: workspace.stripeConnectId,
+ clickId: clickEvent.click_id,
+ linkId: link.id,
+ country: clickEvent.country,
+ clickedAt: new Date(creationDate),
+ createdAt: new Date(creationDate),
+ },
+ }),
- recordLeadWithTimestamp({
- ...clickEvent,
- event_id: nanoid(16),
- event_name: eventName,
- customer_id: randomCustomerId,
- timestamp: new Date(creationDate).toISOString(),
- }),
+ recordLeadWithTimestamp({
+ ...clickEvent,
+ event_id: nanoid(16),
+ event_name: eventName,
+ customer_id: randomCustomerId,
+ timestamp: new Date(creationDate).toISOString(),
+ }),
- prisma.link.update({
- where: { id: link.id },
- data: { leads: { increment: 1 } },
- }),
- ]);
+ prisma.link.update({
+ where: { id: link.id },
+ data: { leads: { increment: 1 } },
+ }),
+ ]);
+ }
return NextResponse.json({
success: true,