The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.
TypeScript clickhousemysqlnextjsopen-sourceplanetscaleprismatailwindcsstinybirdtypescriptupstashvercel
fix the backfill scripts
Kiran K826748a3fc07
diff --git a/apps/web/app/api/stripe/integration/webhook/customer-created.ts b/apps/web/app/api/stripe/integration/webhook/customer-created.tsindex afbc2e72d7..c77a80f0d3 100644--- a/apps/web/app/api/stripe/integration/webhook/customer-created.ts+++ b/apps/web/app/api/stripe/integration/webhook/customer-created.ts@@ -62,10 +62,9 @@ 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+ linkId,+ clickId,+ clickedAt: new Date(clickData.timestamp),country: clickData.country,project: {connect: {diff --git a/apps/web/app/api/track/lead/route.ts b/apps/web/app/api/track/lead/route.tsindex f94e5a3279..08f82766fb 100644--- a/apps/web/app/api/track/lead/route.ts+++ b/apps/web/app/api/track/lead/route.ts@@ -95,11 +95,10 @@ 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 timestampclickId: clickData.click_id,linkId: clickData.link_id,country: clickData.country,+ clickedAt: new Date(clickData.timestamp),},});diff --git a/apps/web/scripts/backfill-customer-columns.ts b/apps/web/scripts/backfill-customer-columns.tsdeleted file mode 100644index f70ae70508..0000000000--- a/apps/web/scripts/backfill-customer-columns.ts+++ /dev/null@@ -1,66 +0,0 @@-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/apps/web/scripts/backfill-customers.ts b/apps/web/scripts/backfill-customers.tsnew file mode 100644index 0000000000..7687eac27d--- /dev/null+++ b/apps/web/scripts/backfill-customers.ts@@ -0,0 +1,101 @@+import "dotenv-flow/config";+import { getClickEvent, getLeadEvent } from "@/lib/tinybird";+import { prisma } from "@dub/prisma";+++// Backfill new customer columns such as linkId, clickId, country++// linkId String?+// clickId String?+// clickedAt DateTime?+// leadCreatedAt DateTime?+// country String?++async function main() {+ const customers = await prisma.customer.findMany({+ where: {+ // linkId: null,+ },+ select: {+ id: true,+ },+ take: 1,+ });++ if (customers.length === 0) {+ console.log("No customers left to update.");+ return;+ }++ // Find leads+ const leadResponse = await Promise.all(+ customers.map((customer) => getLeadEvent({ customerId: customer.id })),+ );++ const leadEvents = leadResponse.map((event) =>+ event.data.length > 0 ? event.data[0] : undefined,+ );++ // Find clicks+ // We're fetching clicks, because leads doesn't have information about when click happened+ const clickResponse = await Promise.all(+ leadEvents.map((event) => getClickEvent({ clickId: event?.click_id! })),+ );++ const clickEvents = clickResponse.map((event) =>+ event.data.length > 0 ? event.data[0] : undefined,+ );++ // Update customers+ const result = await Promise.all(+ customers.map(async (customer) => {+ const leadEvent = leadEvents.find(+ (lead) => lead?.customer_id === customer.id,+ );++ const clickEvent = clickEvents.find(+ (click) => click?.click_id === leadEvent?.click_id,+ );++ console.log(leadEvent, clickEvent);++ if (!leadEvent || !clickEvent) {+ return;+ }++ return prisma.customer.update({+ where: { id: customer.id },+ data: {+ linkId: leadEvent.link_id,+ clickId: leadEvent.click_id,+ country: leadEvent.country,+ clickedAt: clickEvent.timestamp,+ leadCreatedAt: null,+ },+ select: {+ id: true,+ linkId: true,+ clickId: true,+ country: true,+ clickedAt: true,+ leadCreatedAt: true,+ },+ });+ }),+ );++ const remaining = await prisma.customer.count({+ where: {+ linkId: null,+ },+ });++ console.table(result);+ console.log(`${remaining} remaining`);+}++main();+++// '2024-12-17 13:48:52.533'+// new Date('2024-12-17 13:48:52.533')\ No newline at end of file