From 63937db32b56f0d5efa0451ddc2c09be5d72817d Mon Sep 17 00:00:00 2001 From: Nathnael Date: Thu, 2 Jul 2026 09:22:25 +0000 Subject: [PATCH] fix: update the updateStatus iss --- .../src/modules/billing/billing.service.ts | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/apps/edr-freight-api/src/modules/billing/billing.service.ts b/apps/edr-freight-api/src/modules/billing/billing.service.ts index 0eabcbde7..2fd7ce72f 100644 --- a/apps/edr-freight-api/src/modules/billing/billing.service.ts +++ b/apps/edr-freight-api/src/modules/billing/billing.service.ts @@ -722,6 +722,12 @@ export class BillingService { await mg.update(Invoice, { id: invoice.id }, { dueAt }); } + /** + * Force an invoice to `status`, including issuing a still-DRAFT invoice + * (stamping `issuedAt`) — unlike the other transitions here, this is a + * blunt admin/workflow override, not a settlement. No-op when the invoice + * is missing or already terminal (paid/cancelled/refunded/expired). + */ async updateStatus( invoiceId: string, status: Freight.InvoiceStatus, @@ -729,11 +735,14 @@ export class BillingService { ): Promise { const mg = manager ?? this.dataSource.manager; const invoice = await mg.findOne(Invoice, { - where: { id: invoiceId, status: In(OPEN_STATUSES) }, - order: { issuedAt: "DESC" }, + where: { id: invoiceId, status: In([Freight.InvoiceStatus.Draft, ...OPEN_STATUSES]) }, }); if (!invoice) return; - await mg.update(Invoice, { id: invoice.id }, { status }); + await mg.update( + Invoice, + { id: invoice.id }, + { status, issuedAt: invoice.issuedAt ?? new Date() }, + ); } // ── Payment initiation & settlement (the gateway boundary) ───────────────────