import type { PrismaClient } from '@generated/prisma/client'; import prisma from '@/lib/prisma'; /** * Marks overdue rows. A payment whose due date (plus a day of grace) passed and * is still PENDING becomes OVERDUE; it stays billable, so the professor can * renew its link from Cobros. */ export const OVERDUE_GRACE_MS = 24 * 60 * 60 * 1000; export type MarkOverduePaymentsResult = { marked: number; }; type MarkOverduePaymentsDeps = { db?: PrismaClient; now?: Date; }; export class MarkOverduePayments { constructor(private readonly deps: MarkOverduePaymentsDeps = {}) {} async execute(): Promise { const db = this.deps.db ?? prisma; const now = this.deps.now ?? new Date(); const horizon = new Date(now.getTime() - OVERDUE_GRACE_MS); const updated = await db.payment.updateMany({ where: { status: 'PENDING', dueDate: { lt: horizon } }, data: { status: 'OVERDUE' }, }); return { marked: updated.count }; } }