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.

Keep reconciling payout amounts until all recently updated payouts are scanned.

Kiran Kee9d5796f9bb
diff --git a/apps/web/app/(ee)/api/cron/payouts/reconcile-amounts/route.ts b/apps/web/app/(ee)/api/cron/payouts/reconcile-amounts/route.ts
index 858e2749fb..7919b45ee5 100644
--- a/apps/web/app/(ee)/api/cron/payouts/reconcile-amounts/route.ts
+++ b/apps/web/app/(ee)/api/cron/payouts/reconcile-amounts/route.ts
@@ -22,70 +22,98 @@ export const GET = withCron(async () => {
key: "lock:payouts:reconcile-amounts",
ttlSeconds: LOCK_TTL_SECONDS,
fn: async () => {
- const payouts = await prisma.payout.findMany({
- where: {
- updatedAt: {
- gte: subMinutes(new Date(), LOOKBACK_MINUTES),
+ const updatedAfter = subMinutes(new Date(), LOOKBACK_MINUTES);
+ let startingAfter: string | undefined;
+ let scanned = 0;
+ let fixed = 0;
+
+ while (true) {
+ const payouts = await prisma.payout.findMany({
+ where: {
+ updatedAt: {
+ gte: updatedAfter,
+ },
+ status: PayoutStatus.pending,
},
- status: PayoutStatus.pending,
- },
- select: {
- id: true,
- amount: true,
- },
- take: BATCH_SIZE,
- orderBy: {
- updatedAt: "desc",
- },
- });
-
- if (payouts.length === 0) {
- return logAndRespond("No recently updated pending payouts found.");
- }
+ select: {
+ id: true,
+ amount: true,
+ },
+ take: BATCH_SIZE,
+ orderBy: {
+ id: "asc",
+ },
+ ...(startingAfter && {
+ skip: 1,
+ cursor: {
+ id: startingAfter,
+ },
+ }),
+ });
+
+ if (payouts.length === 0) {
+ if (scanned === 0) {
+ return logAndRespond("No recently updated pending payouts found.");
+ }
- const aggregates = await prisma.commission.groupBy({
- by: ["payoutId"],
- where: {
- payoutId: {
- in: payouts.map((payout) => payout.id),
+ break;
+ }
+
+ scanned += payouts.length;
+
+ const aggregates = await prisma.commission.groupBy({
+ by: ["payoutId"],
+ where: {
+ payoutId: {
+ in: payouts.map((payout) => payout.id),
+ },
},
- },
- _sum: {
- earnings: true,
- },
- });
-
- const commissionSumByPayoutId = new Map(
- aggregates.map((a) => [a.payoutId!, a._sum.earnings ?? 0]),
- );
+ _sum: {
+ earnings: true,
+ },
+ });
+
+ const commissionSumByPayoutId = new Map(
+ aggregates.map((a) => [a.payoutId!, a._sum.earnings ?? 0]),
+ );
+
+ const mismatches: {
+ id: string;
+ payoutAmount: number;
+ commissionSum: number;
+ diff: number;
+ }[] = [];
- const mismatches: {
- id: string;
- payoutAmount: number;
- commissionSum: number;
- diff: number;
- }[] = [];
-
- for (const payout of payouts) {
- const commissionSum = commissionSumByPayoutId.get(payout.id) ?? 0;
-
- if (payout.amount !== commissionSum) {
- mismatches.push({
- id: payout.id,
- payoutAmount: payout.amount,
- commissionSum,
- diff: payout.amount - commissionSum,
- });
+ for (const payout of payouts) {
+ const commissionSum = commissionSumByPayoutId.get(payout.id) ?? 0;
+
+ if (payout.amount !== commissionSum) {
+ mismatches.push({
+ id: payout.id,
+ payoutAmount: payout.amount,
+ commissionSum,
+ diff: payout.amount - commissionSum,
+ });
+ }
+ }
+
+ if (mismatches.length > 0) {
+ console.table(mismatches);
+ await reconcilePayoutAmounts(
+ mismatches.map((mismatch) => mismatch.id),
+ );
+ fixed += mismatches.length;
+ }
+
+ if (payouts.length < BATCH_SIZE) {
+ break;
}
- }
- if (mismatches.length > 0) {
- console.table(mismatches);
- await reconcilePayoutAmounts(mismatches.map((mismatch) => mismatch.id));
+ startingAfter = payouts[payouts.length - 1].id;
}
return logAndRespond(
- `Finished reconciling payout amounts. Scanned ${payouts.length} recently updated payout(s), fixed ${mismatches.length} mismatch(es).`,
+ `Finished reconciling payout amounts. Scanned ${scanned} recently updated payout(s), fixed ${fixed} mismatch(es).`,
);
},
});