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.

limit reconcilePayoutAmounts chunk size to 10 and add a for loop

Steven Teyf89487d9a007
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 574d4d0b66..83c1ae8982 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
@@ -2,7 +2,6 @@ import { reconcilePayoutAmounts } from "@/lib/api/commissions/reconcile-payout-a
import { withCron } from "@/lib/cron/with-cron";
import { prisma } from "@/lib/prisma";
import { withRedisLock } from "@/lib/upstash/redis-lock";
-import { chunk } from "@dub/utils";
import { PayoutStatus } from "@prisma/client";
import { subMinutes } from "date-fns";
import { logAndRespond } from "../../utils";
@@ -99,14 +98,10 @@ export const GET = withCron(async () => {
if (mismatches.length > 0) {
console.table(mismatches);
- const payoutIdChunks = chunk(
+ await reconcilePayoutAmounts(
mismatches.map((mismatch) => mismatch.id),
- 50,
);
- for (const payoutIdChunk of payoutIdChunks) {
- await reconcilePayoutAmounts(payoutIdChunk);
- fixed += payoutIdChunk.length;
- }
+ fixed += mismatches.length;
}
if (payouts.length < BATCH_SIZE) {
diff --git a/apps/web/lib/api/commissions/reconcile-payout-amounts.ts b/apps/web/lib/api/commissions/reconcile-payout-amounts.ts
index 60a18836ae..010cbc4b82 100644
--- a/apps/web/lib/api/commissions/reconcile-payout-amounts.ts
+++ b/apps/web/lib/api/commissions/reconcile-payout-amounts.ts
@@ -1,5 +1,6 @@
import { MUTABLE_PAYOUT_STATUSES } from "@/lib/constants/payouts";
import { prisma } from "@/lib/prisma";
+import { chunk } from "@dub/utils";
export async function reconcilePayoutAmounts(payoutIds: string[]) {
const uniquePayoutIds = [...new Set(payoutIds)];
@@ -8,75 +9,78 @@ export async function reconcilePayoutAmounts(payoutIds: string[]) {
return;
}
- await prisma.$transaction(async (tx) => {
- const aggregates = await tx.commission.groupBy({
- by: ["payoutId"],
- where: {
- payoutId: {
- in: uniquePayoutIds,
+ const payoutIdChunks = chunk(uniquePayoutIds, 10);
+ for (const payoutIdChunk of payoutIdChunks) {
+ await prisma.$transaction(async (tx) => {
+ const aggregates = await tx.commission.groupBy({
+ by: ["payoutId"],
+ where: {
+ payoutId: {
+ in: payoutIdChunk,
+ },
},
- },
- _sum: {
- earnings: true,
- },
- });
+ _sum: {
+ earnings: true,
+ },
+ });
- const sumByPayoutId = new Map(
- aggregates.map((a) => [a.payoutId!, a._sum.earnings ?? 0]),
- );
+ const sumByPayoutId = new Map(
+ aggregates.map((a) => [a.payoutId!, a._sum.earnings ?? 0]),
+ );
- const toDelete: string[] = [];
- const toUpdate: { id: string; amount: number }[] = [];
+ const toDelete: string[] = [];
+ const toUpdate: { id: string; amount: number }[] = [];
- for (const id of uniquePayoutIds) {
- const newPayoutAmount = sumByPayoutId.get(id) ?? 0;
+ for (const id of payoutIdChunk) {
+ const newPayoutAmount = sumByPayoutId.get(id) ?? 0;
- if (newPayoutAmount === 0) {
- toDelete.push(id);
- } else {
- toUpdate.push({ id, amount: newPayoutAmount });
+ if (newPayoutAmount === 0) {
+ toDelete.push(id);
+ } else {
+ toUpdate.push({ id, amount: newPayoutAmount });
+ }
}
- }
-
- if (toDelete.length > 0) {
- await tx.payout.deleteMany({
- where: {
- id: {
- in: toDelete,
- },
- status: {
- in: MUTABLE_PAYOUT_STATUSES,
- },
- },
- });
- }
- await Promise.all(
- toUpdate.map(({ id, amount }) =>
- tx.payout.update({
+ if (toDelete.length > 0) {
+ await tx.payout.deleteMany({
where: {
- id,
+ id: {
+ in: toDelete,
+ },
status: {
in: MUTABLE_PAYOUT_STATUSES,
},
},
- data: {
- amount,
- },
- }),
- ),
- );
+ });
+ }
- for (const id of toDelete) {
- console.log(
- `[reconcilePayoutAmount] Deleted payout ${id} because it has no commissions.`,
+ await Promise.all(
+ toUpdate.map(({ id, amount }) =>
+ tx.payout.update({
+ where: {
+ id,
+ status: {
+ in: MUTABLE_PAYOUT_STATUSES,
+ },
+ },
+ data: {
+ amount,
+ },
+ }),
+ ),
);
- }
- for (const { id, amount } of toUpdate) {
- console.log(
- `[reconcilePayoutAmount] Updated payout amount for payout ${id} to ${amount}`,
- );
- }
- });
+ for (const id of toDelete) {
+ console.log(
+ `[reconcilePayoutAmount] Deleted payout ${id} because it has no commissions.`,
+ );
+ }
+
+ for (const { id, amount } of toUpdate) {
+ console.log(
+ `[reconcilePayoutAmount] Updated payout amount for payout ${id} to ${amount}`,
+ );
+ }
+ });
+ }
}