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.
This commit is contained in:
kevin-asprec
2026-05-03 11:00:03 +08:00
parent bef320f32e
commit a5ed2cc666

View File

@@ -124,7 +124,7 @@ export class PaymentService {
// ─── Remittance (custodial clearing) ───────────────────────── // ─── Remittance (custodial clearing) ─────────────────────────
async findRemittances(tenantId: string) { async findRemittances(tenantId: string) {
return this.prisma.remittance.findMany({ const remittances = await this.prisma.remittance.findMany({
where: { tenantId }, where: { tenantId },
include: { include: {
collector: { select: { id: true, firstName: true, lastName: true } }, collector: { select: { id: true, firstName: true, lastName: true } },
@@ -133,6 +133,29 @@ export class PaymentService {
}, },
orderBy: { submittedAt: 'desc' }, 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) { async getUnremittedPayments(tenantId: string, collectorId: string) {