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.

Cleanup script for duplicate payout method fraud events.

Kiran Kc85c7b4e0cd5
diff --git a/apps/web/scripts/migrations/backfill-fraud-event-hashes.ts b/apps/web/scripts/migrations/backfill-fraud-event-hashes.ts
index acbbb2929e..e40abed7c7 100644
--- a/apps/web/scripts/migrations/backfill-fraud-event-hashes.ts
+++ b/apps/web/scripts/migrations/backfill-fraud-event-hashes.ts
@@ -3,8 +3,12 @@ import { prisma } from "@dub/prisma";
import { chunk } from "@dub/utils";
import "dotenv-flow/config";
+/**
+ * Backfill hashes for existing fraud events.
+ * Generates and updates hash values for fraud events that are missing them.
+ * Hashes are used for deduplication to prevent creating duplicate fraud events.
+ */
async function main() {
- // Fetch all fraud events without a hash
const fraudEvents = await prisma.fraudEvent.findMany({
where: {
hash: null,
@@ -17,21 +21,16 @@ async function main() {
for (const batch of chunks) {
await Promise.all(
- batch.map(async (event) => {
- try {
- await prisma.fraudEvent.update({
- where: { id: event.id },
- data: {
- hash: createFraudEventHash(event),
- },
- });
- } catch (error) {
- console.error(
- `Failed to backfill hash for event ${event.id}:`,
- error instanceof Error ? error.message : String(error),
- );
- }
- }),
+ batch.map((event) =>
+ prisma.fraudEvent.update({
+ where: {
+ id: event.id,
+ },
+ data: {
+ hash: createFraudEventHash(event),
+ },
+ }),
+ ),
);
}
@@ -39,4 +38,3 @@ async function main() {
}
main();
-
diff --git a/apps/web/scripts/migrations/cleanup-fraud-events.ts b/apps/web/scripts/migrations/cleanup-fraud-events.ts
index 9d212c7c7f..5354f96a2d 100644
--- a/apps/web/scripts/migrations/cleanup-fraud-events.ts
+++ b/apps/web/scripts/migrations/cleanup-fraud-events.ts
@@ -4,10 +4,13 @@ import "dotenv-flow/config";
/**
* Cleanup script for duplicate payout method fraud events.
- * Removes fraud events of type "partnerDuplicatePayoutMethod" where there's only
- * one partner in the group.
+ * Removes fraud events of type "partnerDuplicatePayoutMethod" where:
+ * 1. There's only one partner in the group, OR
+ * 2. All events in the group have the same partnerId (no actual cross-partner duplicate)
*/
-async function main() {
+
+// Case 1: Remove fraud events where there's only one event in the group
+async function cleanupSingleEventGroups() {
const fraudGroups = await prisma.fraudEvent.groupBy({
by: ["groupKey"],
where: {
@@ -21,12 +24,15 @@ async function main() {
);
console.log(
- `Found ${fraudGroupWithOnePartner.length} fraud groups with one partner.`,
+ `Found ${fraudGroupWithOnePartner.length} fraud groups with one event.`,
);
+ if (fraudGroupWithOnePartner.length === 0) {
+ return 0;
+ }
+
const chunks = chunk(fraudGroupWithOnePartner, 100);
- // delete one by one
for (const batch of chunks) {
await prisma.fraudEvent.deleteMany({
where: {
@@ -35,9 +41,83 @@ async function main() {
},
},
});
+ }
+}
+
+// Case 2: Remove fraud events where all events in the group have the same partnerId
+async function cleanupSamePartnerGroups() {
+ const fraudGroups = await prisma.fraudEvent.groupBy({
+ by: ["groupKey"],
+ where: {
+ type: "partnerDuplicatePayoutMethod",
+ },
+ _count: true,
+ });
+
+ const groupsWithMultipleEvents = fraudGroups.filter(
+ (group) => group._count >= 2,
+ );
+
+ console.log(
+ `Checking ${groupsWithMultipleEvents.length} groups with multiple events...`,
+ );
+
+ const fraudGroupsWithSamePartner: {
+ groupKey: string;
+ partnerId: string;
+ count: number;
+ }[] = [];
+
+ for (const group of groupsWithMultipleEvents) {
+ const events = await prisma.fraudEvent.findMany({
+ where: {
+ groupKey: group.groupKey,
+ type: "partnerDuplicatePayoutMethod",
+ },
+ select: {
+ partnerId: true,
+ },
+ });
+
+ // Check if all partnerIds are the same
+ const uniquePartnerIds = new Set(events.map((e) => e.partnerId));
+
+ if (uniquePartnerIds.size === 1) {
+ fraudGroupsWithSamePartner.push({
+ groupKey: group.groupKey,
+ partnerId: events[0].partnerId,
+ count: events.length,
+ });
+ }
+ }
+
+ console.log(
+ `Found ${fraudGroupsWithSamePartner.length} fraud groups where all events have the same partnerId.`,
+ );
- console.log(`Deleted ${batch.length} fraud events.`);
+ if (fraudGroupsWithSamePartner.length === 0) {
+ return 0;
}
+
+ console.table(fraudGroupsWithSamePartner);
+
+ const chunks = chunk(fraudGroupsWithSamePartner, 100);
+
+ for (const batch of chunks) {
+ await prisma.fraudEvent.deleteMany({
+ where: {
+ groupKey: {
+ in: batch.map(({ groupKey }) => groupKey),
+ },
+ },
+ });
+ }
+}
+
+async function main() {
+ // Run one at a time
+ await cleanupSingleEventGroups();
+ // await cleanupSamePartnerGroups();
}
main();