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.

Refactor fraud event handling: remove deprecated fields and update schema for required properties

Kiran K889366cabf65
diff --git a/apps/web/lib/api/fraud/create-fraud-events.ts b/apps/web/lib/api/fraud/create-fraud-events.ts
index c595a9e07b..6ef3e0517b 100644
--- a/apps/web/lib/api/fraud/create-fraud-events.ts
+++ b/apps/web/lib/api/fraud/create-fraud-events.ts
@@ -4,7 +4,6 @@ import { Prisma } from "@dub/prisma/client";
import { prettyPrint } from "@dub/utils";
import { createId } from "../create-id";
import {
- createFraudEventGroupKey,
createFraudEventHash,
createGroupCompositeKey,
getPartnerIdForFraudEvent,
@@ -117,21 +116,13 @@ export async function createFraudEvents(fraudEvents: CreateFraudEventInput[]) {
(e) => ({
id: createId({ prefix: "fre_" }),
programId: e.programId,
- fraudEventGroupId: finalGroupLookup.get(createGroupCompositeKey(e)),
+ fraudEventGroupId: finalGroupLookup.get(createGroupCompositeKey(e))!,
partnerId: getPartnerIdForFraudEvent(e),
linkId: e.linkId,
customerId: e.customerId,
eventId: e.eventId,
hash: e.hash,
metadata: e.metadata ? sanitizeFraudEventMetadata(e.metadata) : undefined,
-
- // DEPRECATED FIELDS: TODO – remove after migration
- type: e.type,
- groupKey: createFraudEventGroupKey({
- programId: e.programId,
- type: e.type,
- groupingKey: e.partnerId,
- }),
}),
);
diff --git a/apps/web/scripts/remove-duplicate-fraud-events.ts b/apps/web/scripts/remove-duplicate-fraud-events.ts
deleted file mode 100644
index fad58f1262..0000000000
--- a/apps/web/scripts/remove-duplicate-fraud-events.ts
+++ /dev/null
@@ -1,97 +0,0 @@
-import { prisma } from "@dub/prisma";
-import "dotenv-flow/config";
-
-async function main() {
- // Find all pending fraud events
- const pendingEvents = await prisma.fraudEvent.findMany({
- where: {
- status: "pending",
- customerId: {
- not: null,
- },
- commissionId: null,
- },
- orderBy: {
- id: "asc",
- },
- });
-
- console.log(`Found ${pendingEvents.length} pending fraud events.`);
-
- if (pendingEvents.length === 0) {
- console.log("No pending events to process.");
- return;
- }
-
- // Group events by the deduplication key: programId + partnerId + customerId + type
- const eventGroups = new Map<string, Array<{ id: string; createdAt: Date }>>();
-
- for (const event of pendingEvents) {
- const key = [
- event.programId,
- event.partnerId,
- event.customerId,
- event.type,
- ].join("|");
-
- if (!eventGroups.has(key)) {
- eventGroups.set(key, []);
- }
-
- eventGroups.get(key)!.push({
- id: event.id,
- createdAt: event.createdAt,
- });
- }
-
- // Find groups with duplicates (more than 1 event)
- const duplicateGroups = Array.from(eventGroups.entries()).filter(
- ([, events]) => events.length > 1,
- );
-
- console.log(`Found ${duplicateGroups.length} duplicate groups.`);
-
- if (duplicateGroups.length === 0) {
- console.log("No duplicates found.");
- return;
- }
-
- // Collect IDs to delete (keep the oldest event in each group)
- const idsToDelete: string[] = [];
- let totalDuplicates = 0;
-
- for (const [, events] of duplicateGroups) {
- // Sort by createdAt to ensure we keep the oldest
- const sortedEvents = [...events].sort(
- (a, b) => a.createdAt.getTime() - b.createdAt.getTime(),
- );
-
- // Keep the first (oldest) event, delete the rest
- const toDelete = sortedEvents.slice(1);
- idsToDelete.push(...toDelete.map((e) => e.id));
- totalDuplicates += toDelete.length;
-
- console.log(
- `Group has ${events.length} events, keeping oldest (${sortedEvents[0].id}), deleting ${toDelete.length} duplicates.`,
- );
- }
-
- console.log(`Total ${totalDuplicates} duplicates to delete.`);
-
- if (idsToDelete.length === 0) {
- console.log("No events to delete.");
- return;
- }
-
- const deletedEvents = await prisma.fraudEvent.deleteMany({
- where: {
- id: {
- in: idsToDelete,
- },
- },
- });
-
- console.log(`Deleted ${deletedEvents.count} duplicate fraud events.`);
-}
-
-main();
diff --git a/packages/prisma/schema/fraud.prisma b/packages/prisma/schema/fraud.prisma
index 0e66bad63a..95d83d9631 100644
--- a/packages/prisma/schema/fraud.prisma
+++ b/packages/prisma/schema/fraud.prisma
@@ -54,26 +54,17 @@ model FraudEventGroup {
model FraudEvent {
id String @id @default(cuid())
- fraudEventGroupId String? // TODO: Make it required after migration
+ fraudEventGroupId String
programId String
partnerId String
linkId String?
customerId String?
eventId String?
- hash String? // TODO: Make it required after migration
+ hash String
metadata Json? @db.Json
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
- // DEPRECATED FIELDS: TODO – remove after migration
- commissionId String?
- type FraudRuleType
- groupKey String
- userId String?
- resolutionReason String? @db.Text
- resolvedAt DateTime?
- status FraudEventStatus @default(pending)
-
fraudEventGroup FraudEventGroup? @relation(fields: [fraudEventGroupId], references: [id], onDelete: Cascade)
customer Customer? @relation(fields: [customerId], references: [id])
link Link? @relation(fields: [linkId], references: [id])
@@ -84,8 +75,5 @@ model FraudEvent {
@@index(customerId)
@@index(linkId)
@@index(eventId)
- @@index(userId)
- @@index(commissionId)
- @@index(groupKey)
@@index(hash)
}