The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.
TypeScript clickhousemysqlnextjsopen-sourceplanetscaleprismatailwindcsstinybirdtypescriptupstashvercel
store additional columns
Kiran Kfe74973a5e44
diff --git a/apps/web/app/api/stripe/connect/webhook/customer-created.ts b/apps/web/app/api/stripe/connect/webhook/customer-created.tsindex 7dff362c6a..afbc2e72d7 100644--- a/apps/web/app/api/stripe/connect/webhook/customer-created.ts+++ b/apps/web/app/api/stripe/connect/webhook/customer-created.ts@@ -26,8 +26,10 @@ export async function customerCreated(event: Stripe.Event) {return `Click event with ID ${clickId} not found, skipping...`;}+ const clickData = clickEventSchemaTB.parse(clickEvent.data[0]);+// Find link- const linkId = clickEvent.data[0].link_id;+ const linkId = clickData.link_id;const link = await prisma.link.findUnique({where: {id: linkId,@@ -60,6 +62,11 @@ export async function customerCreated(event: Stripe.Event) {stripeCustomerId: stripeCustomer.id,projectConnectId: stripeAccountId,externalId,+ linkId: clickData.link_id,+ clickId: clickData.click_id,+ clickedAt: new Date(clickData.timestamp).toISOString(),+ leadCreatedAt: new Date(), // TODO: Sync with lead timestamp+ country: clickData.country,project: {connect: {stripeConnectId: stripeAccountId,@@ -68,10 +75,6 @@ export async function customerCreated(event: Stripe.Event) {},});- const clickData = clickEventSchemaTB- .omit({ timestamp: true })- .parse(clickEvent.data[0]);-const leadData = {...clickData,event_id: nanoid(16),@@ -81,7 +84,9 @@ export async function customerCreated(event: Stripe.Event) {const [_lead, _link, workspace] = await Promise.all([// Record lead- recordLead(leadData),+ recordLead({+ ...leadData,+ }),// update link leads countprisma.link.update({diff --git a/apps/web/app/api/track/customer/route.ts b/apps/web/app/api/track/customer/route.tsindex 7a3bf094b7..b970678592 100644--- a/apps/web/app/api/track/customer/route.ts+++ b/apps/web/app/api/track/customer/route.ts@@ -10,7 +10,7 @@ import { NextResponse } from "next/server";export const runtime = "edge";-// POST /api/track/customer – Track a customer object+// POST /api/track/customer – Track a customer object (Deprecated)export const POST = withWorkspaceEdge(async ({ req, workspace }) => {const {diff --git a/apps/web/app/api/track/lead/route.ts b/apps/web/app/api/track/lead/route.tsindex 72c7686ac0..c10c87d083 100644--- a/apps/web/app/api/track/lead/route.ts+++ b/apps/web/app/api/track/lead/route.ts@@ -68,9 +68,7 @@ export const POST = withWorkspaceEdge(waitUntil((async () => {- const clickData = clickEventSchemaTB- .omit({ timestamp: true })- .parse(clickEvent.data[0]);+ const clickData = clickEventSchemaTB.parse(clickEvent.data[0]);// Find customer or create if not existsconst customer = await prismaEdge.customer.upsert({@@ -88,6 +86,11 @@ export const POST = withWorkspaceEdge(externalId: customerExternalId,projectId: workspace.id,projectConnectId: workspace.stripeConnectId,+ clickedAt: new Date(clickData.timestamp).toISOString(),+ leadCreatedAt: new Date(), // TODO: Sync with lead timestamp+ clickId: clickData.click_id,+ linkId: clickData.link_id,+ country: clickData.country,},update: {name: finalCustomerName,@@ -96,6 +99,9 @@ export const POST = withWorkspaceEdge(},});+ // TODO:+ // I think some of below requests should only be made if the customer is new (not on update)?+const [_lead, link, _project] = await Promise.all([recordLead({...clickData,diff --git a/apps/web/scripts/backfill-customer-columns.ts b/apps/web/scripts/backfill-customer-columns.tsnew file mode 100644index 0000000000..f70ae70508--- /dev/null+++ b/apps/web/scripts/backfill-customer-columns.ts@@ -0,0 +1,66 @@+import { getLeadEvent } from "@/lib/tinybird";+import { prisma } from "@dub/prisma";+import "dotenv-flow/config";++async function main() {+ const customers = await prisma.customer.findMany({+ where: {+ linkId: null,+ },+ select: {+ id: true,+ },+ take: 100,+ });++ if (customers.length === 0) {+ console.log("No customers left to update.");+ return;+ }++ const results = await Promise.all(+ customers.map(async (customer) =>+ getLeadEvent({ customerId: customer.id }),+ ),+ );++ const leadEvents = results.map((event) =>+ event.data.length > 0 ? event.data[0] : undefined,+ );++ const updatedCustomers = await Promise.all(+ leadEvents.map(async (event) => {+ if (!event) {+ return;+ }++ return await prisma.customer.update({+ where: { id: event.customer_id },+ data: {+ linkId: event.link_id,+ clickId: event.click_id,+ country: event.country,+ // TODO:+ // Update other columns+ },+ select: {+ id: true,+ linkId: true,+ clickId: true,+ country: true,+ },+ });+ }),+ );++ const remaining = await prisma.customer.count({+ where: {+ linkId: null,+ },+ });++ console.table(updatedCustomers);+ console.log(`${remaining} remaining`);+}++main();diff --git a/packages/prisma/schema/customer.prisma b/packages/prisma/schema/customer.prismaindex 6e849886cd..202f6e191e 100644--- a/packages/prisma/schema/customer.prisma+++ b/packages/prisma/schema/customer.prisma@@ -1,15 +1,20 @@model Customer {- id String @id @default(cuid())+ id String @id @default(cuid())name String?email String?- avatar String? @db.LongText+ avatar String? @db.LongTextexternalId String?- project Project @relation(fields: [projectId], references: [id], onDelete: Cascade)+ linkId String?+ clickId String?+ clickedAt DateTime?+ leadCreatedAt DateTime?+ country String?+ project Project @relation(fields: [projectId], references: [id], onDelete: Cascade)projectId StringprojectConnectId String?- stripeCustomerId String? @unique- createdAt DateTime @default(now())- updatedAt DateTime @updatedAt+ stripeCustomerId String? @unique+ createdAt DateTime @default(now())+ updatedAt DateTime @updatedAtsales Sale[]