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.

Trigger aggregate due commissions in finally when a later pass fails.

Rafib7a5afb8edb1
diff --git a/apps/web/lib/api/fraud/release-all-hold-commissions.ts b/apps/web/lib/api/fraud/release-all-hold-commissions.ts
index 8f81d6ccf0..d077b7731e 100644
--- a/apps/web/lib/api/fraud/release-all-hold-commissions.ts
+++ b/apps/web/lib/api/fraud/release-all-hold-commissions.ts
@@ -22,115 +22,121 @@ export async function releaseAllHoldCommissions({
let totalReleased = 0;
- while (true) {
- const commissionsToRelease = await prisma.commission.findMany({
- where: {
- programId,
- status: CommissionStatus.hold,
- },
- select: {
- id: true,
- amount: true,
- earnings: true,
- status: true,
- partnerId: true,
- },
- take: PRISMA_UPDATEMANY_LIMIT,
- });
+ try {
+ while (true) {
+ const commissionsToRelease = await prisma.commission.findMany({
+ where: {
+ programId,
+ status: CommissionStatus.hold,
+ },
+ select: {
+ id: true,
+ amount: true,
+ earnings: true,
+ status: true,
+ partnerId: true,
+ },
+ take: PRISMA_UPDATEMANY_LIMIT,
+ });
- if (commissionsToRelease.length === 0) {
- console.log(`No hold commissions to release for program ${programId}`);
- break;
- }
+ if (commissionsToRelease.length === 0) {
+ console.log(`No hold commissions to release for program ${programId}`);
+ break;
+ }
- // Update the commissions to pending
- const { count: updatedCount } = await prisma.commission.updateMany({
- where: {
- id: {
- in: commissionsToRelease.map((c) => c.id),
+ // Update the commissions to pending
+ const { count: updatedCount } = await prisma.commission.updateMany({
+ where: {
+ id: {
+ in: commissionsToRelease.map((c) => c.id),
+ },
+ status: CommissionStatus.hold,
+ },
+ data: {
+ status: CommissionStatus.pending,
},
- status: CommissionStatus.hold,
- },
- data: {
- status: CommissionStatus.pending,
- },
- });
+ });
- if (updatedCount === 0) {
- console.log(`No hold commissions to release for program ${programId}`);
- break;
- }
+ if (updatedCount === 0) {
+ console.log(`No hold commissions to release for program ${programId}`);
+ break;
+ }
- totalReleased += updatedCount;
+ totalReleased += updatedCount;
- const partnerIds = Array.from(
- new Set(commissionsToRelease.map((c) => c.partnerId)),
- );
+ const partnerIds = Array.from(
+ new Set(commissionsToRelease.map((c) => c.partnerId)),
+ );
- // Get the released commissions
- const releasedCommissions =
- updatedCount < commissionsToRelease.length
- ? await prisma.commission
- .findMany({
- where: {
- id: {
- in: commissionsToRelease.map((c) => c.id),
+ // Get the released commissions
+ const releasedCommissions =
+ updatedCount < commissionsToRelease.length
+ ? await prisma.commission
+ .findMany({
+ where: {
+ id: {
+ in: commissionsToRelease.map((c) => c.id),
+ },
+ status: CommissionStatus.pending,
+ },
+ select: {
+ id: true,
+ amount: true,
+ earnings: true,
+ status: true,
},
- status: CommissionStatus.pending,
- },
- select: {
- id: true,
- amount: true,
- earnings: true,
- status: true,
- },
- })
- .then((commissions) =>
- commissions.map((c) => ({
- ...c,
- // need to make sure the releasedCommissions have the old "hold" status for the status update log
- status: CommissionStatus.hold,
- })),
- )
- : commissionsToRelease;
+ })
+ .then((commissions) =>
+ commissions.map((c) => ({
+ ...c,
+ // need to make sure the releasedCommissions have the old "hold" status for the status update log
+ status: CommissionStatus.hold,
+ })),
+ )
+ : commissionsToRelease;
- const results = await Promise.allSettled([
- trackCommissionStatusUpdate({
- workspaceId: program.workspaceId,
- programId,
- commissions: releasedCommissions,
- newStatus: CommissionStatus.pending,
- }),
- ...partnerIds.map((partnerId) =>
- syncTotalCommissions({
- partnerId,
+ const results = await Promise.allSettled([
+ trackCommissionStatusUpdate({
+ workspaceId: program.workspaceId,
programId,
+ commissions: releasedCommissions,
+ newStatus: CommissionStatus.pending,
}),
- ),
- ]);
+ ...partnerIds.map((partnerId) =>
+ syncTotalCommissions({
+ partnerId,
+ programId,
+ }),
+ ),
+ ]);
- console.log(
- `Summary of releaseAllHoldCommissions: ${JSON.stringify(
- [
- "trackCommissionStatusUpdate",
- ...partnerIds.map((partnerId) => `syncTotalCommissions:${partnerId}`),
- ].map((step, index) => ({
- step,
- result: results[index],
- })),
- )}`,
- );
- }
-
- if (totalReleased > 0) {
- try {
- await triggerAggregateDueCommissionsCronJob(programId);
- } catch (error) {
- console.error(
- `Failed to trigger aggregate due commissions for program ${programId}`,
- error,
+ console.log(
+ `Summary of releaseAllHoldCommissions: ${JSON.stringify(
+ [
+ "trackCommissionStatusUpdate",
+ ...partnerIds.map(
+ (partnerId) => `syncTotalCommissions:${partnerId}`,
+ ),
+ ].map((step, index) => ({
+ step,
+ result: results[index],
+ })),
+ )}`,
);
}
+ } finally {
+ // Runs even when a later pass throws, so the rows that earlier
+ // passes released still get aggregated
+ if (totalReleased > 0) {
+ try {
+ await triggerAggregateDueCommissionsCronJob(programId);
+ } catch (error) {
+ console.error(
+ `Failed to trigger aggregate due commissions for program ${programId}`,
+ error,
+ );
+ }
+ }
}
return totalReleased;
diff --git a/apps/web/lib/api/fraud/release-hold-commissions.ts b/apps/web/lib/api/fraud/release-hold-commissions.ts
index c5bec694e6..a2216a2f21 100644
--- a/apps/web/lib/api/fraud/release-hold-commissions.ts
+++ b/apps/web/lib/api/fraud/release-hold-commissions.ts
@@ -120,125 +120,129 @@ export async function releaseHoldCommissions({
let totalReleased = 0;
- while (true) {
- const commissionsToRelease = await prisma.commission.findMany({
- where: releaseWhere,
- select: {
- id: true,
- amount: true,
- earnings: true,
- status: true,
- },
- take: PRISMA_UPDATEMANY_LIMIT,
- });
+ try {
+ while (true) {
+ const commissionsToRelease = await prisma.commission.findMany({
+ where: releaseWhere,
+ select: {
+ id: true,
+ amount: true,
+ earnings: true,
+ status: true,
+ },
+ take: PRISMA_UPDATEMANY_LIMIT,
+ });
- if (commissionsToRelease.length === 0) {
- console.log(
- `No hold commissions to release for partner ${partnerId} in program ${programId}`,
- );
- break;
- }
+ if (commissionsToRelease.length === 0) {
+ console.log(
+ `No hold commissions to release for partner ${partnerId} in program ${programId}`,
+ );
+ break;
+ }
- // Update the commissions to pending
- const { count: updatedCount } = await prisma.commission.updateMany({
- where: {
- id: {
- in: commissionsToRelease.map((c) => c.id),
+ // Update the commissions to pending
+ const { count: updatedCount } = await prisma.commission.updateMany({
+ where: {
+ id: {
+ in: commissionsToRelease.map((c) => c.id),
+ },
+ status: CommissionStatus.hold,
},
- status: CommissionStatus.hold,
- },
- data: {
- status: CommissionStatus.pending,
- },
- });
+ data: {
+ status: CommissionStatus.pending,
+ },
+ });
- if (updatedCount > 0) {
- console.log(`Released ${updatedCount} hold commissions`);
- totalReleased += updatedCount;
+ if (updatedCount > 0) {
+ console.log(`Released ${updatedCount} hold commissions`);
+ totalReleased += updatedCount;
- // Get the released commissions
- const releasedCommissions =
- updatedCount < commissionsToRelease.length
- ? await prisma.commission
- .findMany({
- where: {
- id: {
- in: commissionsToRelease.map((c) => c.id),
+ // Get the released commissions
+ const releasedCommissions =
+ updatedCount < commissionsToRelease.length
+ ? await prisma.commission
+ .findMany({
+ where: {
+ id: {
+ in: commissionsToRelease.map((c) => c.id),
+ },
+ status: CommissionStatus.pending,
},
- status: CommissionStatus.pending,
- },
- select: {
- id: true,
- amount: true,
- earnings: true,
- status: true,
- },
- })
- .then((commissions) =>
- commissions.map((c) => ({
- ...c,
- // need to make sure the releasedCommissions have the old "hold" status for the status update log
- status: CommissionStatus.hold,
- })),
- )
- : commissionsToRelease;
+ select: {
+ id: true,
+ amount: true,
+ earnings: true,
+ status: true,
+ },
+ })
+ .then((commissions) =>
+ commissions.map((c) => ({
+ ...c,
+ // need to make sure the releasedCommissions have the old "hold" status for the status update log
+ status: CommissionStatus.hold,
+ })),
+ )
+ : commissionsToRelease;
- const releasedEarnings = releasedCommissions.reduce(
- (sum, commission) => sum + commission.earnings,
- 0,
- );
+ const releasedEarnings = releasedCommissions.reduce(
+ (sum, commission) => sum + commission.earnings,
+ 0,
+ );
- const results = await Promise.allSettled([
- trackCommissionStatusUpdate({
- workspaceId: program.workspaceId,
- programId,
- commissions: releasedCommissions,
- newStatus: CommissionStatus.pending,
- }),
- syncTotalCommissions({
- partnerId,
- programId,
- }),
- // should always be > 0, but just in case
- releasedEarnings > 0 &&
- executeWorkflows({
- event: "commissionRecorded",
- identity: {
- workspaceId: program.workspaceId,
- programId,
- partnerId,
- },
- metrics: {
- current: {
- commissions: releasedEarnings,
- },
- },
+ const results = await Promise.allSettled([
+ trackCommissionStatusUpdate({
+ workspaceId: program.workspaceId,
+ programId,
+ commissions: releasedCommissions,
+ newStatus: CommissionStatus.pending,
}),
- ]);
+ syncTotalCommissions({
+ partnerId,
+ programId,
+ }),
+ // should always be > 0, but just in case
+ releasedEarnings > 0 &&
+ executeWorkflows({
+ event: "commissionRecorded",
+ identity: {
+ workspaceId: program.workspaceId,
+ programId,
+ partnerId,
+ },
+ metrics: {
+ current: {
+ commissions: releasedEarnings,
+ },
+ },
+ }),
+ ]);
- console.log(
- `Summary of releaseHoldCommissions: ${JSON.stringify(
- [
- "trackCommissionStatusUpdate",
- "syncTotalCommissions",
- "executeWorkflows",
- ].map((step, index) => ({
- step,
- result: results[index],
- })),
- )}`,
- );
+ console.log(
+ `Summary of releaseHoldCommissions: ${JSON.stringify(
+ [
+ "trackCommissionStatusUpdate",
+ "syncTotalCommissions",
+ "executeWorkflows",
+ ].map((step, index) => ({
+ step,
+ result: results[index],
+ })),
+ )}`,
+ );
+ }
}
- }
-
- if (totalReleased > 0) {
- try {
- await triggerAggregateDueCommissionsCronJob(programId);
- } catch (error) {
- console.error(
- `Failed to trigger aggregate due commissions for program ${programId}`,
- error,
- );
+ } finally {
+ // Runs even when a later pass throws, so the rows that earlier
+ // passes released still get aggregated
+ if (totalReleased > 0) {
+ try {
+ await triggerAggregateDueCommissionsCronJob(programId);
+ } catch (error) {
+ console.error(
+ `Failed to trigger aggregate due commissions for program ${programId}`,
+ error,
+ );
+ }
}
}