import type { PrismaClient } from '@generated/prisma/client'; import type { ProblemDetails, Result, StudentFeeStatus, StudentFeesDto } from '@gruperly/shared'; import { err, ok } from '@gruperly/shared'; import prisma from '@/lib/prisma'; import { startOfDayInTimeZone } from '@/modules/home/lib/scheduler'; import { findLinkedAttendee, studentAttendeeNotFoundProblem, teacherTimeZone } from '../../lib'; type GetStudentFeesDeps = { db?: Pick; now?: Date; }; const REPORTS_SHOWN = 10; const REPORT_STATUS = { REPORTED: 'reported', CONFIRMED: 'confirmed', REJECTED: 'rejected' } as const; /* Unpaid fees of the attendee and their latest reports. A fee inside a report the teacher hasn't reviewed shows as "reported" (the Payment row itself is not touched until the teacher confirms). */ export class GetStudentFees { constructor(private readonly deps: GetStudentFeesDeps = {}) {} async execute(accountId: string, attendeeId: string): Promise> { const db = this.deps.db ?? prisma; const now = this.deps.now ?? new Date(); const attendee = await findLinkedAttendee(db, accountId, attendeeId); if (!attendee) return err(studentAttendeeNotFoundProblem()); const startOfToday = startOfDayInTimeZone(now, teacherTimeZone(attendee.group.owner.country)); const [payments, reports] = await Promise.all([ db.payment.findMany({ where: { attendeeId, status: { in: ['PENDING', 'OVERDUE'] } }, orderBy: { dueDate: 'asc' }, select: { id: true, description: true, amount: true, currency: true, dueDate: true, status: true, checkoutUrl: true, periodKey: true, checkout: { select: { status: true, amount: true, payments: { where: { status: { in: ['PENDING', 'OVERDUE'] } }, orderBy: { createdAt: 'asc' }, select: { group: { select: { name: true } }, attendee: { select: { person: { select: { fullName: true } } } }, }, }, }, }, reportedItems: { where: { reportedPayment: { status: 'REPORTED' } }, select: { paymentId: true } }, }, }), db.reportedPayment.findMany({ where: { attendeeId }, orderBy: { createdAt: 'desc' }, take: REPORTS_SHOWN, select: { id: true, totalAmount: true, currency: true, status: true, createdAt: true, reviewedAt: true, note: true, rejectionReason: true, items: { select: { payment: { select: { id: true, description: true, dueDate: true } } } }, }, }), ]); const { transferAlias, transferHolder } = attendee.group.owner; return ok({ attendeeId, fees: payments.map((payment) => { let status: StudentFeeStatus = 'pending'; if (payment.reportedItems.length > 0) status = 'reported'; else if (payment.status === 'OVERDUE' || payment.dueDate < startOfToday) status = 'overdue'; return { id: payment.id, description: payment.description, amount: payment.amount.toNumber(), currency: payment.currency, dueDate: payment.dueDate.toISOString(), status, checkoutUrl: payment.checkoutUrl, periodKey: payment.periodKey, checkout: payment.checkout?.status === 'OPEN' && payment.checkout.payments.length > 1 ? { total: payment.checkout.amount.toNumber(), items: payment.checkout.payments.map((line) => ({ groupName: line.group.name, studentName: line.attendee.person.fullName, })), } : null, }; }), transfer: transferAlias ? { alias: transferAlias, holder: transferHolder } : null, reports: reports.map((report) => ({ id: report.id, totalAmount: report.totalAmount.toNumber(), currency: report.currency, status: REPORT_STATUS[report.status], createdAt: report.createdAt.toISOString(), reviewedAt: report.reviewedAt?.toISOString() ?? null, note: report.note, rejectionReason: report.rejectionReason, fees: report.items.map(({ payment }) => ({ id: payment.id, description: payment.description, dueDate: payment.dueDate.toISOString(), })), })), }); } }