From c8c6e0ebc3afef8e772891a2aa19f99d975efada Mon Sep 17 00:00:00 2001 From: kevin-asprec Date: Sun, 3 May 2026 11:00:03 +0800 Subject: [PATCH] fix: include full payment details in remittance history findRemittances used `payments: true` which returned raw RemittancePayment join records without actual payment data. Now fetches full Payment objects via manual join and maps them to the remittance records so the mobile app can display client names, amounts, and invoice numbers. --- src/payment/payment.service.ts | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) 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) {