diff --git a/src/payment/payment.service.ts b/src/payment/payment.service.ts index dd5c4d9..a09c465 100644 --- a/src/payment/payment.service.ts +++ b/src/payment/payment.service.ts @@ -124,7 +124,7 @@ export class PaymentService { // ─── Remittance (custodial clearing) ───────────────────────── async findRemittances(tenantId: string) { - return this.prisma.remittance.findMany({ + const remittances = await this.prisma.remittance.findMany({ where: { tenantId }, include: { collector: { select: { id: true, firstName: true, lastName: true } }, @@ -133,6 +133,29 @@ export class PaymentService { }, orderBy: { submittedAt: 'desc' }, }); + + // Collect all payment IDs across remittances + const paymentIds = remittances.flatMap((r) => r.payments.map((p) => p.paymentId)); + if (paymentIds.length === 0) return remittances; + + // Fetch full payment details in one query + const payments = await this.prisma.payment.findMany({ + where: { id: { in: paymentIds } }, + include: { + client: { select: { id: true, firstName: true, lastName: true, accountNumber: true } }, + invoice: { select: { id: true, number: true } }, + }, + }); + const paymentMap = new Map(payments.map((p) => [p.id, p])); + + // Attach full payment details to each remittance's join records + return remittances.map((r) => ({ + ...r, + payments: r.payments.map((rp) => ({ + ...rp, + payment: paymentMap.get(rp.paymentId) ?? null, + })), + })); } async getUnremittedPayments(tenantId: string, collectorId: string) {