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.

Backfill the URLs in metadata for paid traffic

Kiran K52ceae53f34a
diff --git a/apps/web/scripts/migrations/backfill-fraud-event-metadata.ts b/apps/web/scripts/migrations/backfill-fraud-event-metadata.ts
new file mode 100644
index 0000000000..f14d9de36e
--- /dev/null
+++ b/apps/web/scripts/migrations/backfill-fraud-event-metadata.ts
@@ -0,0 +1,92 @@
+import { getLeadEvents } from "@/lib/tinybird/get-lead-events";
+import { prisma } from "@dub/prisma";
+import { chunk } from "@dub/utils";
+import "dotenv-flow/config";
+
+async function main() {
+ const fraudEvents = await prisma.fraudEvent.findMany({
+ where: {
+ type: "paidTrafficDetected",
+ customerId: {
+ not: null,
+ },
+ },
+ select: {
+ id: true,
+ customerId: true,
+ metadata: true,
+ },
+ });
+
+ console.log(`Found ${fraudEvents.length} paidTrafficDetected fraud events.`);
+
+ const fraudEventsToUpdate = fraudEvents.filter((event) => {
+ const metadata = event.metadata as { url: string | null };
+ return !metadata.url;
+ });
+
+ console.log(
+ `Found ${fraudEventsToUpdate.length} paidTrafficDetected fraud events without metadata.url`,
+ );
+
+ // Group events by customerId
+ const eventsByCustomerId = fraudEventsToUpdate
+ .filter((event) => event.customerId !== null)
+ .reduce(
+ (acc, event) => {
+ acc[event.customerId!] = acc[event.customerId!] || [];
+ acc[event.customerId!].push(event);
+ return acc;
+ },
+ {} as Record<string, typeof fraudEventsToUpdate>,
+ );
+
+ const customerIds = Object.keys(eventsByCustomerId);
+ const batches = chunk(customerIds, 100);
+
+ // Find the URL for each customerId
+ const customerIdToUrl = new Map<string, string>();
+
+ for (const batch of batches) {
+ const { data: events } = await getLeadEvents({
+ customerIds: batch,
+ });
+
+ events.forEach((event) => {
+ customerIdToUrl.set(event.customer_id, event.url);
+ });
+ }
+
+ // Update the fraud events with the new metadata
+ const eventsBatch = chunk(fraudEventsToUpdate, 100);
+
+ for (const batch of eventsBatch) {
+ await Promise.all(
+ batch.map((event) => {
+ const url = customerIdToUrl.get(event.customerId!);
+
+ if (!url) {
+ console.log(
+ `Skipping fraud event ${event.id}: No URL found for customerId ${event.customerId}`,
+ );
+
+ return Promise.resolve(null);
+ }
+
+ return prisma.fraudEvent.update({
+ where: {
+ id: event.id,
+ },
+ data: {
+ metadata: {
+ ...(event.metadata as any),
+ url,
+ },
+ },
+ });
+ }),
+ );
+ }
+}
+
+main();