Treasury & Bank Transfer Management System — سامانه مدیریت خزانهداری و حوالههای بانکی
Prevent stale maker-checker and bank-state snapshots from overwriting concurrent financial transitions
davoodmehraban89 committed
Oct 23, 2026 at 11:03 UTC
83db80f79271dc46fd7ac2fd93ee5b1430e5f6eb
1 file changed
+21
-6
src/platform/application/treasury-service.ts
+21
-6
@@ -148,7 +148,7 @@ export class TreasuryService{
148
whitelist:new Set(beneficiary?.whitelisted?[`${payment.tenantId}:${payment.beneficiaryId}`]:[])
149
});
150
if(!policy.allowed) throw new Error(policy.reason);
151
- return this.changeState(id,transitionPayment(payment.state,'SUBMIT'),'PAYMENT_SUBMITTED');
151
+ return this.changeState(id,transitionPayment(payment.state,'SUBMIT'),'PAYMENT_SUBMITTED',payment);
152
}
153
154
approvePayment(id:string){
@@ -158,6 +158,12 @@ export class TreasuryService{
158
return this.store.update(d=>{
159
const row=d.payments.find(x=>x.tenantId===this.actor.tenantId&&x.id===id);
160
if(!row) throw new Error('payment not found');
161
+ if(row.state!=='PENDING_APPROVAL'||row.version!==payment.version){
162
+ throw new Error('payment not pending approval');
163
+ }
164
+ if(!canApprove({actorId:this.actor.userId,makerId:row.makerId},row.version)){
165
+ throw new Error('maker cannot approve own payment');
166
+ }
167
row.state=transitionPayment(row.state,'APPROVE');
168
row.approvedVersion=row.version;
169
d.audit.push({tenantId:this.actor.tenantId,seq:d.audit.length+1,actor:this.actor.userId,action:'PAYMENT_APPROVED',entity:id,at:Date.now()});
@@ -165,12 +171,21 @@ export class TreasuryService{
171
}).payments.find(x=>x.tenantId===this.actor.tenantId&&x.id===id)!;
172
}
173
168
- private changeState(id:string,state:LocalPayment['state'],action:string){
174
+ private changeState(id:string,state:LocalPayment['state'],action:string,expected:LocalPayment){
175
return this.store.update(d=>{
176
const row=d.payments.find(x=>x.tenantId===this.actor.tenantId&&x.id===id);
177
if(!row) throw new Error('payment not found');
178
+ // A state transition from a preflight snapshot must not overwrite a
179
+ // concurrent approval, bank outcome or revised payment version.
180
+ if(row.state!==expected.state||row.version!==expected.version){
181
+ throw new Error('stale payment state');
182
+ }
183
+ if(action==='PAYMENT_SUBMITTED'&&row.makerId!==this.actor.userId)throw new Error('forbidden');
184
+ if(action==='PAYMENT_QUEUED'&&row.approvedVersion!==row.version){
185
+ throw new Error('payment not eligible for queue');
186
+ }
187
row.state=state;
173
- d.audit.push({tenantId:this.actor.tenantId,seq:d.audit.length+1,actor:this.actor.userId,action,entity:id,at:Date.now()});
188
+ this.appendAuditTo(d,action,id);
189
return d;
190
}).payments.find(x=>x.tenantId===this.actor.tenantId&&x.id===id)!;
191
}
@@ -178,7 +193,7 @@ export class TreasuryService{
193
queuePayment(id:string){
194
const payment=this.getPayment(id);
195
if(payment.state!=='APPROVED'||payment.approvedVersion!==payment.version) throw new Error('payment not eligible for queue');
181
- return this.changeState(id,transitionPayment(payment.state,'QUEUE'),'PAYMENT_QUEUED');
196
+ return this.changeState(id,transitionPayment(payment.state,'QUEUE'),'PAYMENT_QUEUED',payment);
197
}
198
199
async dispatchPayment(id:string,adapter:BankAdapter){
@@ -208,8 +223,8 @@ export class TreasuryService{
223
this.appendAudit('PAYMENT_BANK_'+outcome,id);
224
return this.getPayment(id);
225
}
211
- if(outcome==='REJECTED') return this.changeState(id,transitionPayment(payment.state,'FAIL'),'PAYMENT_BANK_REJECTED');
212
- return this.changeState(id,transitionPayment(payment.state,'UNKNOWN_RESULT'),'PAYMENT_BANK_UNKNOWN');
226
+ if(outcome==='REJECTED') return this.changeState(id,transitionPayment(payment.state,'FAIL'),'PAYMENT_BANK_REJECTED',payment);
227
+ return this.changeState(id,transitionPayment(payment.state,'UNKNOWN_RESULT'),'PAYMENT_BANK_UNKNOWN',payment);
228
}
229
230
getDashboard(){return dashboard(this.actor.tenantId,this.listPayments())}