ensure pessimistic-lock writes run in a transaction for invoice processing

This commit is contained in:
Marshal
2026-07-08 10:13:22 +00:00
parent 44023efb12
commit 3349d256b8
2 changed files with 92 additions and 4 deletions

View File

@@ -825,6 +825,13 @@ export class BillingService {
type?: string,
manager?: EntityManager,
): Promise<Invoice | null> {
// Lookup can use the default manager (no lock). But the pessimistic-lock write
// inside `transition` NEEDS an open transaction: pass the caller's `manager`
// through untouched (undefined when there is no caller txn) so `runTransition`
// opens its own. Passing `this.dataSource.manager` here made `runTransition`
// treat it as an already-open transaction and skip wrapping — the lock then
// threw `An open transaction is required for pessimistic lock`, aborting the
// whole settle pass (the "reservations settle/reserve one at a time" symptom).
const mg = manager ?? this.dataSource.manager;
const invoice = await mg.findOne(Invoice, {
where: {
@@ -842,7 +849,7 @@ export class BillingService {
Freight.InvoiceStatus.Expired,
"expired",
{},
mg,
manager,
);
}