Treasury & Bank Transfer Management System — سامانه مدیریت خزانهداری و حوالههای بانکی
Persist atomic dispatch reservation before bank call and fail closed to manual review on uncertain outcome
davoodmehraban89 committed
Oct 23, 2026 at 11:08 UTC
9957a3e471550e2324c4f8b7182740c93f876c7f
1 file changed
+39
-10
src/platform/application/treasury-service.ts
+39
-10
@@ -197,20 +197,49 @@ export class TreasuryService{
197
}
198
199
async dispatchPayment(id:string,adapter:BankAdapter){
200
- const payment=this.getPayment(id);
201
- if(payment.state!=='QUEUED'||payment.approvedVersion!==payment.version) throw new Error('payment not eligible for dispatch');
200
+ // Reserve once in durable storage BEFORE any external bank request.
201
+ // The QUEUED -> DISPATCHING transition is atomic across SQLite writers.
202
+ // A crash at any point leaves DISPATCHING/UNKNOWN for manual reconciliation;
203
+ // neither state is eligible for automatic re-dispatch.
204
+ const reserved=this.store.update(d=>{
205
+ const row=d.payments.find(x=>x.tenantId===this.actor.tenantId&&x.id===id);
206
+ if(!row||row.state!=='QUEUED'||row.approvedVersion!==row.version){
207
+ throw new Error('payment not eligible for dispatch');
208
+ }
209
+ row.state=transitionPayment(row.state,'BEGIN_DISPATCH');
210
+ this.appendAuditTo(d,'PAYMENT_DISPATCH_RESERVED',id);
211
+ return d;
212
+ }).payments.find(x=>x.tenantId===this.actor.tenantId&&x.id===id)!;
213
const registry=new BankDispatchRegistry();
203
- const dispatch=registry.begin({tenantId:payment.tenantId,paymentId:payment.id,version:payment.version});
204
- const result=await adapter.submit({tenantId:payment.tenantId,paymentId:payment.id,version:payment.version,idempotencyKey:dispatch.idempotencyKey});
205
- registry.record(dispatch.idempotencyKey,result.outcome);
214
+ const dispatch=registry.begin({tenantId:reserved.tenantId,paymentId:reserved.id,version:reserved.version});
215
+ let result:Awaited<ReturnType<BankAdapter['submit']>>;
216
+ try{
217
+ result=await adapter.submit({tenantId:reserved.tenantId,paymentId:reserved.id,
218
+ version:reserved.version,idempotencyKey:dispatch.idempotencyKey});
219
+ registry.record(dispatch.idempotencyKey,result.outcome);
220
+ }catch{
221
+ // The bank might have accepted the request before the transport failed.
222
+ // Conservatively mark UNKNOWN rather than rolling back to QUEUED.
223
+ this.store.update(d=>{
224
+ const row=d.payments.find(x=>x.tenantId===this.actor.tenantId&&x.id===id);
225
+ if(!row)throw new Error('payment not found');
226
+ if(row.state==='DISPATCHING'&&row.version===reserved.version){
227
+ row.state=transitionPayment(row.state,'UNKNOWN_RESULT');
228
+ this.appendAuditTo(d,'PAYMENT_BANK_UNKNOWN',id);
229
+ }
230
+ return d;
231
+ });
232
+ throw new Error('bank dispatch outcome unknown: manual review required');
233
+ }
234
return this.store.update(d=>{
235
const row=d.payments.find(x=>x.tenantId===this.actor.tenantId&&x.id===id);
208
- if(!row) throw new Error('payment not found');
209
- row.state=transitionPayment(row.state,'DISPATCH');
236
+ if(!row||row.state!=='DISPATCHING'||row.version!==reserved.version){
237
+ throw new Error('stale dispatch reservation: manual review required');
238
+ }
239
+ row.state=transitionPayment(row.state,result.outcome==='REJECTED'?'FAIL'
240
+ :result.outcome==='UNKNOWN'?'UNKNOWN_RESULT':'DISPATCH');
241
row.bankReference=result.bankReference;
211
- if(result.outcome==='REJECTED') row.state=transitionPayment(row.state,'FAIL');
212
- else if(result.outcome==='UNKNOWN') row.state=transitionPayment(row.state,'UNKNOWN_RESULT');
213
- d.audit.push({tenantId:this.actor.tenantId,seq:d.audit.length+1,actor:this.actor.userId,action:'PAYMENT_BANK_'+result.outcome,entity:id,at:Date.now()});
242
+ this.appendAuditTo(d,'PAYMENT_BANK_'+result.outcome,id);
243
return d;
244
}).payments.find(x=>x.tenantId===this.actor.tenantId&&x.id===id)!;
245
}