The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.
TypeScript clickhousemysqlnextjsopen-sourceplanetscaleprismatailwindcsstinybirdtypescriptupstashvercel
Enhance fraud event creation by adding deduplication logic for existing events and refining group identification based on programId/partnerId/type combinations.
Kiran K82b7922efb4a
diff --git a/apps/web/lib/api/fraud/create-fraud-events.ts b/apps/web/lib/api/fraud/create-fraud-events.tsindex b78374d6ab..c595a9e07b 100644--- a/apps/web/lib/api/fraud/create-fraud-events.ts+++ b/apps/web/lib/api/fraud/create-fraud-events.ts@@ -28,11 +28,39 @@ export async function createFraudEvents(fraudEvents: CreateFraudEventInput[]) {new Map(eventsWithHash.map((e) => [e.hash, e])).values(),);+ // Fetch existing events to prevent duplicate fraud event records+ // A fraud event with the same hash in a pending group is considered a duplicate+ const existingEvents = await prisma.fraudEvent.findMany({+ where: {+ hash: {+ in: uniqueEvents.map((e) => e.hash),+ },+ fraudEventGroup: {+ status: "pending",+ },+ },+ select: {+ id: true,+ fraudEventGroupId: true,+ hash: true,+ },+ });++ // Deduplicate events by hash+ const newEvents = uniqueEvents.filter(+ (e) =>+ !existingEvents.some((existingEvent) => existingEvent.hash === e.hash),+ );++ if (newEvents.length === 0) {+ return;+ }+// Find existing groups to avoid creating duplicates and maintain group continuity// Events with the same programId/partnerId/type should be grouped togetherconst existingGroups = await prisma.fraudEventGroup.findMany({where: {- OR: uniqueEvents.map((e) => ({+ OR: newEvents.map((e) => ({programId: e.programId,partnerId: e.partnerId,type: e.type,@@ -47,8 +75,9 @@ export async function createFraudEvents(fraudEvents: CreateFraudEventInput[]) {},});- // Identify events that need new groups created because they represent- const groupsToCreate = uniqueEvents.filter(+ // Identify events that need new groups created for programId/partnerId/type combinations+ // that don't have existing pending groups+ const groupsToCreate = newEvents.filter((e) =>!existingGroups.some((g) =>@@ -84,30 +113,6 @@ export async function createFraudEvents(fraudEvents: CreateFraudEventInput[]) {finalGroups.map((g) => [createGroupCompositeKey(g), g.id]),);- // Fetch existing events to prevent duplicate fraud event records- // A fraud event with the same hash in a pending group is considered a duplicate- const existingEvents = await prisma.fraudEvent.findMany({- where: {- hash: {- in: uniqueEvents.map((e) => e.hash),- },- fraudEventGroup: {- status: "pending",- },- },- select: {- id: true,- fraudEventGroupId: true,- hash: true,- },- });-- // Deduplicate events by hash- const newEvents = uniqueEvents.filter(- (e) =>- !existingEvents.some((existingEvent) => existingEvent.hash === e.hash),- );-const newEventsWithGroup: Prisma.FraudEventCreateManyInput[] = newEvents.map((e) => ({id: createId({ prefix: "fre_" }),@@ -130,10 +135,6 @@ export async function createFraudEvents(fraudEvents: CreateFraudEventInput[]) {}),);- if (newEventsWithGroup.length === 0) {- return;- }-const createdEvents = await prisma.fraudEvent.createMany({data: newEventsWithGroup,});