import { PrismaClient } from '@prisma/client'; const prisma = new PrismaClient(); async function backfill() { const schedules = await prisma.pOPaymentSchedule.findMany({ include: { installments: { orderBy: { sortOrder: 'desc' } } }, }); console.log(`Found ${schedules.length} payment schedules`); let totalPatched = 0; for (const s of schedules) { let toMark: number[] = []; if (s.paymentType === 'POST_DELIVERY') { toMark = s.installments.map(i => i.id); } else if (s.paymentType === 'INSTALLMENT') { if (s.installments.length > 0) { toMark = [s.installments[0].id]; // [0] car orderBy desc → dernier sortOrder } } if (toMark.length > 0) { const r = await prisma.pOInstallment.updateMany({ where: { id: { in: toMark } }, data: { isPostDelivery: true }, }); console.log(` Schedule ${s.id} (PO ${s.purchaseOrderId}, type=${s.paymentType}): ${r.count} installments → isPostDelivery=true`); totalPatched += r.count; } } console.log(`\nBackfill terminé : ${totalPatched} installments marqués isPostDelivery=true`); console.log(`postpayMode reste à default 'SIMPLE' pour tous les schedules existants (rétrocompat).`); } backfill().catch(e => { console.error(e); process.exit(1); }).finally(() => prisma.$disconnect());