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 cron to reconcile mismatched pending payout amounts.

Kiran K74a33984a1a0
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
new file mode 100644
index 0000000000..e835ee7af6
--- /dev/null
+++ b/apps/web/app/(ee)/api/cron/payouts/reconcile-amounts/route.ts
@@ -0,0 +1,98 @@
+import { reconcilePayoutAmounts } from "@/lib/api/commissions/reconcile-payout-amounts";
+import { withCron } from "@/lib/cron/with-cron";
+import { prisma } from "@/lib/prisma";
+import { withRedisLock } from "@/lib/upstash/redis-lock";
+import { PayoutStatus } from "@prisma/client";
+import { subMinutes } from "date-fns";
+import { logAndRespond } from "../../utils";
+
+export const dynamic = "force-dynamic";
+
+const BATCH_SIZE = 500;
+const LOOKBACK_MINUTES = 2;
+
+// Finds recently updated pending payouts whose amount does not match
+// SUM(commission.earnings) and reconciles them (update amount or delete empty payouts).
+// Runs every minute.
+// GET /api/cron/payouts/reconcile-amounts
+export const GET = withCron(async () => {
+ const response = await withRedisLock({
+ key: "lock:payouts:reconcile-amounts",
+ ttlSeconds: 60,
+ fn: async () => {
+ const payouts = await prisma.payout.findMany({
+ where: {
+ status: PayoutStatus.pending,
+ updatedAt: {
+ gte: subMinutes(new Date(), LOOKBACK_MINUTES),
+ },
+ },
+ select: {
+ id: true,
+ amount: true,
+ },
+ take: BATCH_SIZE,
+ orderBy: {
+ updatedAt: "desc",
+ },
+ });
+
+ if (payouts.length === 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),
+ },
+ },
+ _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;
+ }[] = [];
+
+ 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));
+ }
+
+ return logAndRespond(
+ `Finished reconciling payout amounts. Scanned ${payouts.length} recently updated payout(s), fixed ${mismatches.length} mismatch(es).`,
+ );
+ },
+ });
+
+ if (!response) {
+ return logAndRespond(
+ "[reconcile-amounts] Another run is in progress. Skipping...",
+ );
+ }
+
+ return response;
+});
diff --git a/apps/web/lib/upstash/redis-lock.ts b/apps/web/lib/upstash/redis-lock.ts
new file mode 100644
index 0000000000..62bf3f8865
--- /dev/null
+++ b/apps/web/lib/upstash/redis-lock.ts
@@ -0,0 +1,31 @@
+import { redis } from "./redis";
+
+/**
+ * Runs `fn` while holding a Redis NX lock. Returns `null` if the lock
+ * could not be acquired (another holder is active). Always releases the
+ * lock in `finally` when acquired.
+ */
+export async function withRedisLock<T>({
+ key,
+ ttlSeconds,
+ fn,
+}: {
+ key: string;
+ ttlSeconds: number;
+ fn: () => Promise<T>;
+}): Promise<T | null> {
+ const acquired = await redis.set(key, "1", {
+ nx: true,
+ ex: ttlSeconds,
+ });
+
+ if (!acquired) {
+ return null;
+ }
+
+ try {
+ return await fn();
+ } finally {
+ await redis.del(key);
+ }
+}
diff --git a/apps/web/prisma/schema/payout.prisma b/apps/web/prisma/schema/payout.prisma
index a680629c50..051695b960 100644
--- a/apps/web/prisma/schema/payout.prisma
+++ b/apps/web/prisma/schema/payout.prisma
@@ -52,6 +52,7 @@ model Payout {
@@index(partnerId)
@@index(invoiceId)
@@index(status)
+ @@index([status, updatedAt])
@@index(method)
@@index(userId)
@@index(stripePayoutId)
diff --git a/apps/web/vercel.json b/apps/web/vercel.json
index 8c45a4a896..2627027c59 100644
--- a/apps/web/vercel.json
+++ b/apps/web/vercel.json
@@ -52,6 +52,10 @@
"path": "/api/cron/payouts/aggregate-due-commissions",
"schedule": "0 * * * *"
},
+ {
+ "path": "/api/cron/payouts/reconcile-amounts",
+ "schedule": "* * * * *"
+ },
{
"path": "/api/cron/trigger-withdrawal",
"schedule": "0 1,13 * * *"