Runtime `PrismaClientValidationError` in Payout Reconciliation Cron Job
️ Metadata
- Affected File:
apps/web/lib/api/commissions/reconcile-payout-amounts.ts - Invoked From:
apps/web/app/(ee)/api/cron/payouts/reconcile-amounts/route.ts:101-104 - Severity:
Critical (P0 - Runtime Exception) - Category: Database Query Validation / Payouts Integrity
Overview & Problem Statement
The automated reconciliation cron job (GET /api/cron/payouts/reconcile-amounts) scans for pending payouts whose current amount does not match the sum of its associated commissions (SUM(commission.earnings)). When a mismatch is identified, it invokes reconcilePayoutAmounts(payoutIds).
Inside reconcilePayoutAmounts, the update is executed as follows:
// apps/web/lib/api/commissions/reconcile-payout-amounts.ts:58-71
await Promise.all(
toUpdate.map(({ id, amount }) =>
tx.payout.update({
where: {
id,
status: {
in: MUTABLE_PAYOUT_STATUSES,
},
},
data: {
amount,
},
}),
),
);Root Cause Analysis
In Prisma Client, the update() method accepts a strict WhereUniqueInput type (PayoutWhereUniqueInput). In apps/web/prisma/schema/payout.prisma, the Payout model definition only defines @unique constraints on:
idpaypalTransferIdwebhookEventId
There is no compound unique constraint on @@unique([id, status]).
When non-unique fields (such as status: { in: MUTABLE_PAYOUT_STATUSES }) are passed to tx.payout.update({ where: ... }), Prisma rejects the query at runtime with:
PrismaClientValidationError:
Argument 'status' for where must not be defined. Did you mean ...?As a result:
- Every time a payout amount discrepancy occurs in production, the transaction throws an unhandled rejection and aborts immediately.
- The mismatched payout amounts are never repaired.
- The cron job fails and logs an error continuously every minute.
Notice that in apps/web/lib/actions/partners/retry-failed-paypal-payouts.ts:83-90, the codebase correctly utilized tx.payout.updateMany for conditional status updates on non-unique fields.
Steps to Reproduce
- In the database, create a pending
Payoutrecord withamount = 500. - Insert/adjust a
Commissionrecord belonging to that payout withearnings = 400so thatSUM(earnings) !== payout.amount. - Trigger
GET /api/cron/payouts/reconcile-amounts. - Observe the execution failure when Prisma attempts
tx.payout.update(...)withstatus: { in: ... }.
️ Proposed Solution & Patch
Replace tx.payout.update with tx.payout.updateMany inside the batch transaction:
--- a/apps/web/lib/api/commissions/reconcile-payout-amounts.ts
+++ b/apps/web/lib/api/commissions/reconcile-payout-amounts.ts
@@ -56,8 +56,8 @@ export async function reconcilePayoutAmounts(payoutIds: string[]) {
}
await Promise.all(
toUpdate.map(({ id, amount }) =>
- tx.payout.update({
+ tx.payout.updateMany({
where: {
id,
status: {
in: MUTABLE_PAYOUT_STATUSES,
},
},
data: {
amount,
},
}),
),
);Source: dubinc/dub