|
| 1 | +import { prisma } from "@dub/prisma"; |
| 2 | +import "dotenv-flow/config"; |
| 3 | +import * as fs from "fs"; |
| 4 | +import * as Papa from "papaparse"; |
| 5 | +import { tb } from "../../lib/tinybird/client"; |
| 6 | +import z from "../../lib/zod"; |
| 7 | + |
| 8 | +const getFramerLeadEvents = tb.buildPipe({ |
| 9 | + pipe: "get_framer_lead_events", |
| 10 | + parameters: z.object({ |
| 11 | + linkId: z.string(), |
| 12 | + }), |
| 13 | + data: z.any(), |
| 14 | +}); |
| 15 | + |
| 16 | +const framerCombinedData: { |
| 17 | + via: string; |
| 18 | + externalId: string; |
| 19 | + eventName: string; |
| 20 | + creationDate: string; |
| 21 | +}[] = []; |
| 22 | +const externalIdEventNameSet = new Set<string>(); |
| 23 | + |
| 24 | +async function processFramerData(linkToBackfill: { |
| 25 | + via: string; |
| 26 | + linkId: string; |
| 27 | +}) { |
| 28 | + return new Promise<void>((resolve) => { |
| 29 | + Papa.parse(fs.createReadStream("framer_combined.csv", "utf-8"), { |
| 30 | + header: true, |
| 31 | + skipEmptyLines: true, |
| 32 | + step: (result: { |
| 33 | + data: { |
| 34 | + via: string; |
| 35 | + externalId: string; |
| 36 | + eventName: string; |
| 37 | + creationDate: string; |
| 38 | + }; |
| 39 | + }) => { |
| 40 | + if (linkToBackfill.via === result.data.via) { |
| 41 | + // check if externalId:eventName pair already exists in framerCombinedData |
| 42 | + if ( |
| 43 | + externalIdEventNameSet.has( |
| 44 | + `${result.data.externalId}:${result.data.eventName}`, |
| 45 | + ) |
| 46 | + ) { |
| 47 | + return; |
| 48 | + } |
| 49 | + externalIdEventNameSet.add( |
| 50 | + `${result.data.externalId}:${result.data.eventName}`, |
| 51 | + ); |
| 52 | + framerCombinedData.push({ |
| 53 | + via: result.data.via, |
| 54 | + externalId: result.data.externalId, |
| 55 | + eventName: result.data.eventName, |
| 56 | + creationDate: result.data.creationDate, |
| 57 | + }); |
| 58 | + } |
| 59 | + }, |
| 60 | + complete: async () => { |
| 61 | + console.log( |
| 62 | + `Found ${framerCombinedData.length} framerCombinedData for ${linkToBackfill.via} (${linkToBackfill.linkId})`, |
| 63 | + ); |
| 64 | + |
| 65 | + const { data: leadEvents } = await getFramerLeadEvents({ |
| 66 | + linkId: linkToBackfill.linkId, |
| 67 | + }); |
| 68 | + |
| 69 | + const customerIdsSet = new Set( |
| 70 | + leadEvents.map((event) => event.customer_id), |
| 71 | + ); |
| 72 | + |
| 73 | + const customers = await prisma.customer.findMany({ |
| 74 | + where: { |
| 75 | + id: { |
| 76 | + in: Array.from(customerIdsSet), |
| 77 | + }, |
| 78 | + }, |
| 79 | + select: { |
| 80 | + id: true, |
| 81 | + externalId: true, |
| 82 | + }, |
| 83 | + }); |
| 84 | + |
| 85 | + const processedLeadEvents = leadEvents.map((event) => { |
| 86 | + const customer = customers.find( |
| 87 | + (customer) => customer.id === event.customer_id, |
| 88 | + )!; |
| 89 | + return { |
| 90 | + linkId: event.link_id, |
| 91 | + externalId: customer.externalId!, |
| 92 | + eventName: event.event_name, |
| 93 | + creationDate: event.timestamp, |
| 94 | + }; |
| 95 | + }); |
| 96 | + |
| 97 | + console.log( |
| 98 | + `Found ${processedLeadEvents.length} processedLeadEvents for ${linkToBackfill.via} (${linkToBackfill.linkId})`, |
| 99 | + ); |
| 100 | + |
| 101 | + // find events in framerCombinedData that don't exist in processedLeadEvents |
| 102 | + const eventsToBackfill = framerCombinedData.filter( |
| 103 | + (event) => |
| 104 | + !processedLeadEvents.find( |
| 105 | + (ple) => |
| 106 | + ple.externalId.toLowerCase() === |
| 107 | + event.externalId.toLowerCase() && |
| 108 | + ple.eventName.toLowerCase() === event.eventName.toLowerCase(), |
| 109 | + ), |
| 110 | + ); |
| 111 | + |
| 112 | + console.log( |
| 113 | + `Found ${eventsToBackfill.length} events to backfill for ${linkToBackfill.via} (${linkToBackfill.linkId})`, |
| 114 | + ); |
| 115 | + // append to framer_remaining_events_final.csv |
| 116 | + fs.appendFileSync( |
| 117 | + "framer_remaining_events_final.csv", |
| 118 | + Papa.unparse(eventsToBackfill), |
| 119 | + ); |
| 120 | + resolve(); |
| 121 | + }, |
| 122 | + }); |
| 123 | + }); |
| 124 | +} |
| 125 | + |
| 126 | +async function main() { |
| 127 | + const linksWithSales = JSON.parse( |
| 128 | + fs.readFileSync("framer_links_with_sales_to_backfill.json", "utf-8"), |
| 129 | + ); |
| 130 | + |
| 131 | + if (linksWithSales.length === 0) { |
| 132 | + console.log("No more links to process!"); |
| 133 | + return; |
| 134 | + } |
| 135 | + |
| 136 | + // Get the first link to process |
| 137 | + const linkToProcess = linksWithSales[0]; |
| 138 | + |
| 139 | + if (linkToProcess.linkId) { |
| 140 | + console.log( |
| 141 | + `Processing link: ${linkToProcess.via} (${linkToProcess.linkId})`, |
| 142 | + ); |
| 143 | + |
| 144 | + await processFramerData(linkToProcess); |
| 145 | + } else { |
| 146 | + console.log("No linkId found for linkToProcess"); |
| 147 | + } |
| 148 | + |
| 149 | + // Remove the processed link from the array |
| 150 | + linksWithSales.shift(); |
| 151 | + |
| 152 | + // Write the updated array back to the file |
| 153 | + fs.writeFileSync( |
| 154 | + "framer_links_with_sales_to_backfill.json", |
| 155 | + JSON.stringify(linksWithSales, null, 2), |
| 156 | + ); |
| 157 | + |
| 158 | + console.log(`Remaining links to process: ${linksWithSales.length}`); |
| 159 | +} |
| 160 | + |
| 161 | +main(); |
0 commit comments