The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.
TypeScript clickhousemysqlnextjsopen-sourceplanetscaleprismatailwindcsstinybirdtypescriptupstashvercel
transferIfNotExistElseDelete
Steven Teye1bcfcc95698
diff --git a/apps/web/app/(ee)/api/workflows/merge-partner-accounts/route.ts b/apps/web/app/(ee)/api/workflows/merge-partner-accounts/route.tsindex 674af8b3ac..a67d8cd004 100644--- a/apps/web/app/(ee)/api/workflows/merge-partner-accounts/route.ts+++ b/apps/web/app/(ee)/api/workflows/merge-partner-accounts/route.ts@@ -347,6 +347,7 @@ async function loadMergePlan({};}+// High-volume resources: move in batches of PRISMA_UPDATEMANY_LIMITasync function transferRowsInBatches(updateBatch: () => Promise<number>,{@@ -364,6 +365,31 @@ async function transferRowsInBatches(}}+// Rows unique on (programId, partnerId): move the source row when the target+// has none, otherwise delete it so rewriting enrollment.partnerId cannot collide.+async function transferIfNotExistElseDelete({+ findTarget,+ transferSource,+ deleteSource,+ resourceName,+}: {+ findTarget: () => Promise<unknown>;+ transferSource: () => Promise<number>;+ deleteSource: () => Promise<number>;+ resourceName: string;+}) {+ if (await findTarget()) {+ const count = await deleteSource();+ console.log(+ `Deleted ${count} source ${resourceName} (target already exists)`,+ );+ return;+ }++ const count = await transferSource();+ console.log(`Transferred ${count} ${resourceName}`);+}+async function transferPartnerProgramData({sourcePartnerId,targetPartnerId,@@ -422,6 +448,33 @@ async function transferPartnerProgramData({prisma.notificationEmail.updateMany(payload),prisma.message.updateMany(payload),prisma.partnerComment.updateMany(payload),+ // Unique on (programId, partnerId) — transfer or delete, never rewrite over+ transferIfNotExistElseDelete({+ findTarget: () =>+ prisma.programApplicationEvent.findUnique({+ where: {+ programId_partnerId: { programId, partnerId: targetPartnerId },+ },+ }),+ transferSource: async () =>+ (await prisma.programApplicationEvent.updateMany(payload)).count,+ deleteSource: async () =>+ (await prisma.programApplicationEvent.deleteMany({ where })).count,+ resourceName: "application event",+ }),+ transferIfNotExistElseDelete({+ findTarget: () =>+ prisma.discoveredPartner.findUnique({+ where: {+ programId_partnerId: { programId, partnerId: targetPartnerId },+ },+ }),+ transferSource: async () =>+ (await prisma.discoveredPartner.updateMany(payload)).count,+ deleteSource: async () =>+ (await prisma.discoveredPartner.deleteMany({ where })).count,+ resourceName: "discovered partner",+ }),]);// After payouts are moved onto the target partner, fold any duplicate