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.

add holdProcessedCommissions

Steven Tey745fb50fa9b6
diff --git a/apps/web/lib/api/fraud/detect-duplicate-identity-fraud.ts b/apps/web/lib/api/fraud/detect-duplicate-identity-fraud.ts
index b5c9740ab5..bdbdaa9eba 100644
--- a/apps/web/lib/api/fraud/detect-duplicate-identity-fraud.ts
+++ b/apps/web/lib/api/fraud/detect-duplicate-identity-fraud.ts
@@ -10,6 +10,7 @@ import { FraudRuleType, ProgramEnrollment } from "@prisma/client";
import { createFraudEvents } from "./create-fraud-events";
import { isFraudRuleEnabled } from "./get-merged-fraud-rules";
import { holdPendingCommissions } from "./hold-pending-commissions";
+import { holdProcessedCommissions } from "./hold-processed-commissions";
// Check for duplicate identities: if multiple partners share the same identity,
// create fraud events for all their active program enrollments to flag potential fraud
@@ -133,5 +134,8 @@ export async function detectDuplicateIdentityFraud({
const { affectedGroups } = await createFraudEvents(fraudEvents);
- await holdPendingCommissions(affectedGroups);
+ await Promise.allSettled([
+ holdPendingCommissions(affectedGroups),
+ holdProcessedCommissions(affectedGroups),
+ ]);
}
diff --git a/apps/web/lib/api/fraud/detect-duplicate-payout-method-fraud.ts b/apps/web/lib/api/fraud/detect-duplicate-payout-method-fraud.ts
index 193757e65e..6767983b4b 100644
--- a/apps/web/lib/api/fraud/detect-duplicate-payout-method-fraud.ts
+++ b/apps/web/lib/api/fraud/detect-duplicate-payout-method-fraud.ts
@@ -5,6 +5,7 @@ import { FraudRuleType, ProgramEnrollment } from "@prisma/client";
import { createFraudEvents } from "./create-fraud-events";
import { isFraudRuleEnabled } from "./get-merged-fraud-rules";
import { holdPendingCommissions } from "./hold-pending-commissions";
+import { holdProcessedCommissions } from "./hold-processed-commissions";
type DetectDuplicatePayoutMethodFraudOptions =
| { payoutMethodHash: string; cryptoWalletAddress?: never }
@@ -109,5 +110,8 @@ export async function detectDuplicatePayoutMethodFraud({
const { affectedGroups } = await createFraudEvents(fraudEvents);
- await holdPendingCommissions(affectedGroups);
+ await Promise.allSettled([
+ holdPendingCommissions(affectedGroups),
+ holdProcessedCommissions(affectedGroups),
+ ]);
}
diff --git a/apps/web/lib/api/fraud/hold-pending-commissions.ts b/apps/web/lib/api/fraud/hold-pending-commissions.ts
index 6c953c12bf..7276ea2428 100644
--- a/apps/web/lib/api/fraud/hold-pending-commissions.ts
+++ b/apps/web/lib/api/fraud/hold-pending-commissions.ts
@@ -10,6 +10,7 @@ export async function holdPendingCommissions(
programEnrollments: Pick<ProgramEnrollment, "programId" | "partnerId">[],
) {
if (programEnrollments.length === 0) {
+ console.log("No program enrollments to hold pending commissions for");
return;
}
@@ -36,105 +37,112 @@ export async function holdPendingCommissions(
};
for (const chunk of chunks) {
- const pendingCommissions = await prisma.commission.findMany({
- where: {
- OR: chunk.map(({ programId, partnerId }) => ({
- programId,
- partnerId,
- })),
- ...holdEligibleWhere,
- },
- select: {
- id: true,
- amount: true,
- earnings: true,
- status: true,
- programId: true,
- partnerId: true,
- program: {
- select: {
- id: true,
- workspaceId: true,
- },
+ while (true) {
+ const pendingCommissions = await prisma.commission.findMany({
+ where: {
+ OR: chunk.map(({ programId, partnerId }) => ({
+ programId,
+ partnerId,
+ })),
+ ...holdEligibleWhere,
},
- },
- });
-
- if (pendingCommissions.length === 0) {
- continue;
- }
-
- // Update the status of the commissions to hold
- const { count: updatedCount } = await prisma.commission.updateMany({
- where: {
- id: {
- in: pendingCommissions.map((c) => c.id),
+ select: {
+ id: true,
+ amount: true,
+ earnings: true,
+ status: true,
+ programId: true,
+ partnerId: true,
+ program: {
+ select: {
+ id: true,
+ workspaceId: true,
+ },
+ },
},
- ...holdEligibleWhere,
- },
- data: {
- status: CommissionStatus.hold,
- },
- });
-
- if (updatedCount === 0) {
- continue;
- }
-
- // updateMany re-checks eligibility, so a commission can change between findMany
- // and updateMany (e.g. payout processing). Only log/sync rows we actually held.
- let heldCommissions = pendingCommissions;
+ take: 250,
+ });
- if (updatedCount < pendingCommissions.length) {
- const pendingCommissionIds = pendingCommissions.map((c) => c.id);
+ if (pendingCommissions.length === 0) {
+ console.log("No more pending commissions to hold, breaking...");
+ break;
+ }
- const heldCommissionIds = await prisma.commission.findMany({
+ // Update the status of the commissions to hold
+ const { count: updatedCount } = await prisma.commission.updateMany({
where: {
id: {
- in: pendingCommissionIds,
+ in: pendingCommissions.map((c) => c.id),
},
- status: CommissionStatus.hold,
+ ...holdEligibleWhere,
},
- select: {
- id: true,
+ data: {
+ status: CommissionStatus.hold,
},
});
- const heldIds = new Set(heldCommissionIds.map(({ id }) => id));
+ if (updatedCount === 0) {
+ console.log("No commissions updated to hold, breaking...");
+ break;
+ }
- heldCommissions = pendingCommissions.filter((c) => heldIds.has(c.id));
- }
+ console.log(`Updated ${updatedCount} commissions to hold`);
- // Activity logs are scoped to a workspace + program, so batch by program.
- const commissionsByProgram = groupBy(heldCommissions, (c) => c.programId);
+ // updateMany re-checks eligibility, so a commission's status can change between findMany
+ // and updateMany (i.e. if they're aggregated to a payout). Only log/sync rows we actually held.
+ let heldCommissions = pendingCommissions;
- for (const commissions of Object.values(commissionsByProgram)) {
- const { id: programId, workspaceId } = commissions[0].program;
+ if (updatedCount < pendingCommissions.length) {
+ const pendingCommissionIds = pendingCommissions.map((c) => c.id);
- await trackCommissionStatusUpdate({
- workspaceId,
- programId,
- commissions,
- newStatus: CommissionStatus.hold,
- });
- }
+ const heldCommissionIds = await prisma.commission.findMany({
+ where: {
+ id: {
+ in: pendingCommissionIds,
+ },
+ status: CommissionStatus.hold,
+ },
+ select: {
+ id: true,
+ },
+ });
+
+ const heldIds = new Set(heldCommissionIds.map(({ id }) => id));
- const affectedPairs = [
- ...new Map(
- heldCommissions.map((c) => [
- `${c.programId}:${c.partnerId}`,
- { programId: c.programId, partnerId: c.partnerId },
- ]),
- ).values(),
- ];
-
- await Promise.all(
- affectedPairs.map(({ programId, partnerId }) =>
- syncTotalCommissions({
+ heldCommissions = pendingCommissions.filter((c) => heldIds.has(c.id));
+ }
+
+ // Activity logs are scoped to a workspace + program, so batch by program.
+ const commissionsByProgram = groupBy(heldCommissions, (c) => c.programId);
+
+ for (const commissions of Object.values(commissionsByProgram)) {
+ const { id: programId, workspaceId } = commissions[0].program;
+
+ await trackCommissionStatusUpdate({
+ workspaceId,
programId,
- partnerId,
- }),
- ),
- );
+ commissions,
+ newStatus: CommissionStatus.hold,
+ });
+ }
+
+ const affectedPairs = [
+ ...new Map(
+ heldCommissions.map((c) => [
+ `${c.programId}:${c.partnerId}`,
+ { programId: c.programId, partnerId: c.partnerId },
+ ]),
+ ).values(),
+ ];
+
+ await Promise.all(
+ affectedPairs.map(({ programId, partnerId }) =>
+ syncTotalCommissions({
+ programId,
+ partnerId,
+ }),
+ ),
+ );
+ }
}
}
diff --git a/apps/web/lib/api/fraud/hold-processed-commissions.ts b/apps/web/lib/api/fraud/hold-processed-commissions.ts
new file mode 100644
index 0000000000..6545153a48
--- /dev/null
+++ b/apps/web/lib/api/fraud/hold-processed-commissions.ts
@@ -0,0 +1,169 @@
+import { trackCommissionStatusUpdate } from "@/lib/api/commissions/track-commission-update-activity-log";
+import { retallyPayoutsAmount } from "@/lib/payouts/retally-payouts-amount";
+import { prisma } from "@/lib/prisma";
+import { chunk, groupBy } from "@dub/utils";
+import {
+ CommissionStatus,
+ PayoutStatus,
+ Prisma,
+ ProgramEnrollment,
+} from "@prisma/client";
+import { syncTotalCommissions } from "../partners/sync-total-commissions";
+
+// Bulk-hold processed commissions for partner-program pairs flagged by partner-level fraud
+// (duplicate identity, duplicate payout method, cross-program ban).
+export async function holdProcessedCommissions(
+ programEnrollments: Pick<ProgramEnrollment, "programId" | "partnerId">[],
+) {
+ if (programEnrollments.length === 0) {
+ console.log("No program enrollments to hold processed commissions for");
+ return;
+ }
+
+ const uniquePairs = [
+ ...new Map(
+ programEnrollments.map((e) => [`${e.programId}:${e.partnerId}`, e]),
+ ).values(),
+ ];
+
+ const chunks = chunk(uniquePairs, 50);
+
+ const holdEligibleWhere: Prisma.CommissionWhereInput = {
+ status: CommissionStatus.processed,
+ payout: {
+ status: PayoutStatus.pending,
+ },
+ earnings: {
+ gt: 0,
+ },
+ program: {
+ workspace: {
+ plan: {
+ in: ["enterprise", "advanced"],
+ },
+ },
+ },
+ };
+
+ const payoutIdsToRetallySet = new Set<string>();
+
+ for (const chunk of chunks) {
+ while (true) {
+ const processedCommissions = await prisma.commission.findMany({
+ where: {
+ OR: chunk.map(({ programId, partnerId }) => ({
+ programId,
+ partnerId,
+ })),
+ ...holdEligibleWhere,
+ },
+ select: {
+ id: true,
+ amount: true,
+ earnings: true,
+ status: true,
+ programId: true,
+ partnerId: true,
+ payoutId: true,
+ program: {
+ select: {
+ id: true,
+ workspaceId: true,
+ },
+ },
+ },
+ take: 250,
+ });
+
+ if (processedCommissions.length === 0) {
+ console.log("No more processed commissions to hold, breaking...");
+ break;
+ }
+
+ // add payout IDs to retally for later
+ for (const { payoutId } of processedCommissions) {
+ if (payoutId) payoutIdsToRetallySet.add(payoutId);
+ }
+
+ // Update the status of the commissions to hold
+ const { count: updatedCount } = await prisma.commission.updateMany({
+ where: {
+ id: {
+ in: processedCommissions.map((c) => c.id),
+ },
+ ...holdEligibleWhere,
+ },
+ data: {
+ status: CommissionStatus.hold,
+ payoutId: null,
+ },
+ });
+
+ if (updatedCount === 0) {
+ console.log("No commissions updated to hold, breaking...");
+ break;
+ }
+
+ console.log(`Updated ${updatedCount} commissions to hold`);
+
+ // updateMany re-checks eligibility, so a commission's status can change between findMany
+ // and updateMany (i.e. if they're aggregated to a payout). Only log/sync rows we actually held.
+ let heldCommissions = processedCommissions;
+
+ if (updatedCount < processedCommissions.length) {
+ const processedCommissionIds = processedCommissions.map((c) => c.id);
+
+ const heldCommissionIds = await prisma.commission.findMany({
+ where: {
+ id: {
+ in: processedCommissionIds,
+ },
+ status: CommissionStatus.hold,
+ },
+ select: {
+ id: true,
+ },
+ });
+
+ const heldIds = new Set(heldCommissionIds.map(({ id }) => id));
+
+ heldCommissions = processedCommissions.filter((c) => heldIds.has(c.id));
+ }
+
+ // Activity logs are scoped to a workspace + program, so batch by program.
+ const commissionsByProgram = groupBy(heldCommissions, (c) => c.programId);
+
+ for (const commissions of Object.values(commissionsByProgram)) {
+ const { id: programId, workspaceId } = commissions[0].program;
+
+ await trackCommissionStatusUpdate({
+ workspaceId,
+ programId,
+ commissions,
+ newStatus: CommissionStatus.hold,
+ });
+ }
+
+ const affectedPairs = [
+ ...new Map(
+ heldCommissions.map((c) => [
+ `${c.programId}:${c.partnerId}`,
+ { programId: c.programId, partnerId: c.partnerId },
+ ]),
+ ).values(),
+ ];
+
+ await Promise.allSettled(
+ affectedPairs.map(({ programId, partnerId }) =>
+ syncTotalCommissions({
+ programId,
+ partnerId,
+ }),
+ ),
+ );
+ }
+ }
+
+ // need to retally payouts to ensure the payout amount is correct
+ await retallyPayoutsAmount(Array.from(payoutIdsToRetallySet));
+}
diff --git a/apps/web/lib/api/fraud/report-cross-program-ban-to-network.ts b/apps/web/lib/api/fraud/report-cross-program-ban-to-network.ts
index 1300c579b7..8df50666a7 100644
--- a/apps/web/lib/api/fraud/report-cross-program-ban-to-network.ts
+++ b/apps/web/lib/api/fraud/report-cross-program-ban-to-network.ts
@@ -4,6 +4,7 @@ import { prisma } from "@/lib/prisma";
import { INACTIVE_ENROLLMENT_STATUSES } from "@/lib/zod/schemas/partners";
import { FraudRuleType, PartnerBannedReason } from "@prisma/client";
import { holdPendingCommissions } from "./hold-pending-commissions";
+import { holdProcessedCommissions } from "./hold-processed-commissions";
// Creates partnerCrossProgramBan fraud events in other programs where the partner is enrolled.
// Used when a program bans a partner so that other programs can be alerted about cross-program
@@ -70,5 +71,8 @@ export async function reportCrossProgramBanToNetwork({
})),
);
- await holdPendingCommissions(affectedGroups);
+ await Promise.allSettled([
+ holdPendingCommissions(affectedGroups),
+ holdProcessedCommissions(affectedGroups),
+ ]);
}
diff --git a/apps/web/lib/payouts/retally-payouts-amount.ts b/apps/web/lib/payouts/retally-payouts-amount.ts
new file mode 100644
index 0000000000..88bd6819b9
--- /dev/null
+++ b/apps/web/lib/payouts/retally-payouts-amount.ts
@@ -0,0 +1,27 @@
+import { prisma } from "@/lib/prisma";
+
+export const retallyPayoutsAmount = async (payoutIdsToRetally: string[]) => {
+ for (const payoutId of payoutIdsToRetally) {
+ const commissionsSum = await prisma.commission.aggregate({
+ where: {
+ payoutId,
+ },
+ _sum: {
+ earnings: true,
+ },
+ });
+ const payoutAmount = commissionsSum._sum?.earnings ?? 0;
+ if (payoutAmount > 0) {
+ await prisma.payout.update({
+ where: { id: payoutId },
+ data: { amount: payoutAmount },
+ });
+ console.log(`Updated payout ${payoutId} with amount ${payoutAmount}`);
+ } else {
+ await prisma.payout.delete({
+ where: { id: payoutId },
+ });
+ console.log(`Deleted payout ${payoutId} because it has no earnings`);
+ }
+ }
+};