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.

Revert "Update cleanup-fraud-events.ts"

Kiran K35c4c98a2f6f
diff --git a/apps/web/scripts/migrations/cleanup-fraud-events.ts b/apps/web/scripts/migrations/cleanup-fraud-events.ts
index 5354f96a2d..fb6905a2ce 100644
--- a/apps/web/scripts/migrations/cleanup-fraud-events.ts
+++ b/apps/web/scripts/migrations/cleanup-fraud-events.ts
@@ -1,123 +1,55 @@
import { prisma } from "@dub/prisma";
-import { chunk } from "@dub/utils";
import "dotenv-flow/config";
-/**
- * Cleanup script for duplicate payout method fraud events.
- * 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)
- */
-
// 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"],
+ const fraudGroups = await prisma.fraudEventGroup.findMany({
where: {
type: "partnerDuplicatePayoutMethod",
+ eventCount: 1,
+ },
+ select: {
+ id: true,
+ eventCount: true,
+ createdAt: true,
},
- _count: true,
});
- const fraudGroupWithOnePartner = fraudGroups.filter(
- (group) => group._count === 1,
- );
-
console.log(
- `Found ${fraudGroupWithOnePartner.length} fraud groups with one event.`,
+ `Found ${fraudGroups.length} fraud groups with one event for partnerDuplicatePayoutMethod.`,
);
- if (fraudGroupWithOnePartner.length === 0) {
- return 0;
- }
+ console.table(fraudGroups);
- const chunks = chunk(fraudGroupWithOnePartner, 100);
-
- for (const batch of chunks) {
- await prisma.fraudEvent.deleteMany({
- where: {
- groupKey: {
- in: batch.map(({ groupKey }) => groupKey),
- },
- },
- });
+ if (fraudGroups.length === 0) {
+ return;
}
-}
-// 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,
- );
+ const groupIds = fraudGroups.map(({ id }) => id);
- 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({
+ await prisma.$transaction(async (tx) => {
+ // Delete the group
+ await tx.fraudEventGroup.deleteMany({
where: {
- groupKey: group.groupKey,
- type: "partnerDuplicatePayoutMethod",
- },
- select: {
- partnerId: true,
+ id: {
+ in: groupIds,
+ },
},
});
- // 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.`,
- );
-
- if (fraudGroupsWithSamePartner.length === 0) {
- return 0;
- }
-
- console.table(fraudGroupsWithSamePartner);
-
- const chunks = chunk(fraudGroupsWithSamePartner, 100);
-
- for (const batch of chunks) {
- await prisma.fraudEvent.deleteMany({
+ // Delete the raw events
+ await tx.fraudEvent.deleteMany({
where: {
- groupKey: {
- in: batch.map(({ groupKey }) => groupKey),
+ fraudEventGroupId: {
+ in: groupIds,
},
},
});
- }
+ });
}
async function main() {
- // Run one at a time
await cleanupSingleEventGroups();
- // await cleanupSamePartnerGroups();
}
main();