#4575·dub

Runtime `PrismaClientValidationError` in Payout Reconciliation Cron Job

Author: codeCraft-RitikCreated Sep 28, 2026Updated Sep 28, 2026

️ Metadata

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:

typescript
// 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:

  1. id
  2. paypalTransferId
  3. webhookEventId

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

  1. In the database, create a pending Payout record with amount = 500.
  2. Insert/adjust a Commission record belonging to that payout with earnings = 400 so that SUM(earnings) !== payout.amount.
  3. Trigger GET /api/cron/payouts/reconcile-amounts.
  4. Observe the execution failure when Prisma attempts tx.payout.update(...) with status: { in: ... }.

️ Proposed Solution & Patch

Replace tx.payout.update with tx.payout.updateMany inside the batch transaction:

diff
--- 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,
             },
           }),
         ),
       );