The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.
TypeScript clickhousemysqlnextjsopen-sourceplanetscaleprismatailwindcsstinybirdtypescriptupstashvercel
Replace fingerpring with hash on FraudEvent table
Kiran K9e85d5907a14
diff --git a/apps/web/lib/api/fraud/create-fraud-events.ts b/apps/web/lib/api/fraud/create-fraud-events.tsindex 2ade4a89d8..255c1a39b6 100644--- a/apps/web/lib/api/fraud/create-fraud-events.ts+++ b/apps/web/lib/api/fraud/create-fraud-events.ts@@ -2,7 +2,7 @@ import { CreateFraudEventInput } from "@/lib/types";import { prisma } from "@dub/prisma";import { Prisma } from "@dub/prisma/client";import { createId } from "../create-id";-import { createFraudEventFingerprint, createFraudEventGroupKey } from "./utils";+import { createFraudEventHash, createFraudEventGroupKey } from "./utils";export async function createFraudEvents(fraudEvents: CreateFraudEventInput[]) {if (fraudEvents.length === 0) {@@ -11,12 +11,12 @@ export async function createFraudEvents(fraudEvents: CreateFraudEventInput[]) {const eventsWithHash = fraudEvents.map((e) => ({...e,- fingerprint: createFraudEventFingerprint(e),+ hash: createFraudEventHash(e),}));- // Deduplicate by fingerprint in-memory first+ // Deduplicate by hash in-memory firstconst uniqueEvents = Array.from(- new Map(eventsWithHash.map((e) => [e.fingerprint, e])).values(),+ new Map(eventsWithHash.map((e) => [e.hash, e])).values(),);// Find existing groups to avoid creating duplicates and maintain group continuity@@ -76,11 +76,11 @@ export async function createFraudEvents(fraudEvents: CreateFraudEventInput[]) {);// Fetch existing events to prevent duplicate fraud event records- // A fraud event with the same fingerprint in a pending group is considered a duplicate+ // A fraud event with the same hash in a pending group is considered a duplicateconst existingEvents = await prisma.fraudEvent.findMany({where: {- fingerprint: {- in: uniqueEvents.map((e) => e.fingerprint),+ hash: {+ in: uniqueEvents.map((e) => e.hash),},fraudEventGroup: {status: "pending",@@ -89,15 +89,15 @@ export async function createFraudEvents(fraudEvents: CreateFraudEventInput[]) {select: {id: true,fraudEventGroupId: true,- fingerprint: true,+ hash: true,},});- // Deduplicate events by fingerprint+ // Deduplicate events by hashconst newEvents = uniqueEvents.filter((e) =>!existingEvents.some(- (existingEvent) => existingEvent.fingerprint === e.fingerprint,+ (existingEvent) => existingEvent.hash === e.hash,),);@@ -109,7 +109,7 @@ export async function createFraudEvents(fraudEvents: CreateFraudEventInput[]) {linkId: e.linkId,customerId: e.customerId,eventId: e.eventId,- fingerprint: e.fingerprint,+ hash: e.hash,metadata: e.metadata ?? undefined,// DEPRECATED FIELDS: TODO – remove after migrationdiff --git a/apps/web/lib/api/fraud/utils.ts b/apps/web/lib/api/fraud/utils.tsindex 9146d7de90..b51fcabb20 100644--- a/apps/web/lib/api/fraud/utils.ts+++ b/apps/web/lib/api/fraud/utils.ts@@ -8,7 +8,7 @@ interface CreateGroupKeyInput {groupingKey: string;}-interface CreateFingerprintInput+interface CreateHashInputextends Pick<CreateFraudEventInput,"type" | "programId" | "partnerId" | "customerId" | "metadata"@@ -60,9 +60,9 @@ export function createFraudEventGroupKey(input: CreateGroupKeyInput): string {return createHashKey(parts.join("|"));}-// Creates a unique fingerprint for a fraud event to enable deduplication.-export function createFraudEventFingerprint(- fraudEvent: CreateFingerprintInput,+// Creates a unique hash for a fraud event to enable deduplication.+export function createFraudEventHash(+ fraudEvent: CreateHashInput,) {const identityFields = getIdentityFieldsForFraudEvent(fraudEvent);@@ -83,7 +83,7 @@ export function createFraudEventFingerprint(return createHashKey(raw);}-// Determines which identity fields should be used for fraud event fingerprinting based on the fraud rule type.+// Determines which identity fields should be used for fraud event hashing based on the fraud rule type.// Different fraud rules use different combinations of fields to uniquely identify fraud events.function getIdentityFieldsForFraudEvent({type,diff --git a/apps/web/scripts/migrations/backfill-fraud-event-fingerprints.ts b/apps/web/scripts/migrations/backfill-fraud-event-hashes.tssimilarity index 67%rename from apps/web/scripts/migrations/backfill-fraud-event-fingerprints.tsrename to apps/web/scripts/migrations/backfill-fraud-event-hashes.tsindex 059096d3d4..acbbb2929e 100644--- a/apps/web/scripts/migrations/backfill-fraud-event-fingerprints.ts+++ b/apps/web/scripts/migrations/backfill-fraud-event-hashes.ts@@ -1,17 +1,17 @@-import { createFraudEventFingerprint } from "@/lib/api/fraud/utils";+import { createFraudEventHash } from "@/lib/api/fraud/utils";import { prisma } from "@dub/prisma";import { chunk } from "@dub/utils";import "dotenv-flow/config";async function main() {- // Fetch all fraud events without a fingerprint+ // Fetch all fraud events without a hashconst fraudEvents = await prisma.fraudEvent.findMany({where: {- fingerprint: null,+ hash: null,},});- console.log(`Found ${fraudEvents.length} fraud events without fingerprints.`);+ console.log(`Found ${fraudEvents.length} fraud events without hashes.`);const chunks = chunk(fraudEvents, 100);@@ -22,12 +22,12 @@ async function main() {await prisma.fraudEvent.update({where: { id: event.id },data: {- fingerprint: createFraudEventFingerprint(event),+ hash: createFraudEventHash(event),},});} catch (error) {console.error(- `Failed to backfill fingerprint for event ${event.id}:`,+ `Failed to backfill hash for event ${event.id}:`,error instanceof Error ? error.message : String(error),);}@@ -35,7 +35,8 @@ async function main() {);}- console.log("Fingerprint backfill completed.");+ console.log("Hash backfill completed.");}main();+diff --git a/apps/web/scripts/migrations/migrate-duplicate-payout-fraud-events.ts b/apps/web/scripts/migrations/migrate-duplicate-payout-fraud-events.tsindex f253f59a0d..6771b7630a 100644--- a/apps/web/scripts/migrations/migrate-duplicate-payout-fraud-events.ts+++ b/apps/web/scripts/migrations/migrate-duplicate-payout-fraud-events.ts@@ -96,7 +96,7 @@ async function main() {linkId: event.linkId,customerId: event.customerId,eventId: event.eventId,- fingerprint: event.fingerprint,+ hash: event.hash,metadata: event.metadata ?? undefined,createdAt: event.createdAt,updatedAt: event.updatedAt,diff --git a/packages/prisma/schema/fraud.prisma b/packages/prisma/schema/fraud.prismaindex 21410acde8..97cfccc75a 100644--- a/packages/prisma/schema/fraud.prisma+++ b/packages/prisma/schema/fraud.prisma@@ -59,7 +59,7 @@ model FraudEvent {linkId String?customerId String?eventId String?- fingerprint String? // TODO: Make it required after migration+ hash String? // TODO: Make it required after migrationmetadata Json? @db.JsoncreatedAt DateTime @default(now())updatedAt DateTime @updatedAt@@ -87,5 +87,5 @@ model FraudEvent {@@index(userId)@@index(commissionId)@@index(groupKey)- @@index(fingerprint)+ @@index(hash)}