Treasury & Bank Transfer Management System — سامانه مدیریت خزانه‌داری و حواله‌های بانکی

feat: add maker checker treasury workflow

Davood Mehraban committed Oct 5, 2026 at 16:31 UTC 70a3f4b002b215f6eb16b404e57cddd983541987
2 files changed +5
src/domain/core/workflow.test.ts new
+2
@@ -0,0 +1,2 @@
1 +import {describe,expect,it} from 'vitest'; import {TreasuryWorkflow} from './workflow';
2 +describe('treasury workflow',()=>{const policy={transactionLimitIrr:5000,dailyLimitIrr:10000,whitelist:new Set(['t1:b1'])};it('runs maker-checker simulated settlement',()=>{const w=new TreasuryWorkflow(policy);const p=w.create({id:'p1',tenantId:'t1',makerId:'maker',beneficiaryId:'b1',amountIrr:1000,paymentDate:'2026-10-05',clientReference:'R1'});expect(p.state).toBe('DRAFT');expect(w.submit('p1','maker').state).toBe('PENDING_APPROVAL');expect(()=>w.approve('p1','maker')).toThrow();expect(w.approve('p1','checker').state).toBe('APPROVED');expect(w.queue('p1').state).toBe('QUEUED');expect(w.simulateSettlement('p1').state).toBe('SETTLED')});it('material edit invalidates approval and increments version',()=>{const w=new TreasuryWorkflow(policy);w.create({id:'p2',tenantId:'t1',makerId:'m',beneficiaryId:'b1',amountIrr:100,paymentDate:'2026-10-05',clientReference:'R2'});w.submit('p2','m');w.approve('p2','c');const edited=w.editAmount('p2','m',200);expect(edited.version).toBe(2);expect(edited.state).toBe('DRAFT');expect(edited.approvedVersion).toBeUndefined()});it('exposes approval and settlement queues',()=>{const w=new TreasuryWorkflow(policy);w.create({id:'p3',tenantId:'t1',makerId:'m',beneficiaryId:'b1',amountIrr:100,paymentDate:'2026-10-05',clientReference:'R3'});w.submit('p3','m');expect(w.queueView('PENDING_APPROVAL')).toHaveLength(1)})});
src/domain/core/workflow.ts new
+3
@@ -0,0 +1,3 @@
1 +import {evaluatePaymentPolicy} from './policy'; import type {PaymentState} from '../payments/payment-state';
2 +type P={id:string;tenantId:string;makerId:string;beneficiaryId:string;amountIrr:number;paymentDate:string;clientReference:string;version:number;state:PaymentState;approvedVersion?:number}; type Create=Omit<P,'version'|'state'|'approvedVersion'>; type Config={transactionLimitIrr:number;dailyLimitIrr:number;whitelist:Set<string>};
3 +export class TreasuryWorkflow{private ps=new Map<string,P>();constructor(private config:Config){}create(x:Create){if(this.ps.has(x.id))throw new Error('duplicate payment');const p:P={...x,version:1,state:'DRAFT'};this.ps.set(p.id,p);return{...p}}private get(id:string){const p=this.ps.get(id);if(!p)throw new Error('not found');return p}submit(id:string,actor:string){const p=this.get(id);if(actor!==p.makerId||p.state!=='DRAFT')throw new Error('forbidden');const today=[...this.ps.values()].filter(x=>x.tenantId===p.tenantId&&x.paymentDate===p.paymentDate&&x.state==='SETTLED').reduce((s,x)=>s+x.amountIrr,0);const r=evaluatePaymentPolicy({...this.config,tenantId:p.tenantId,beneficiaryId:p.beneficiaryId,amountIrr:p.amountIrr,todayTotalIrr:today});if(!r.allowed)throw new Error(r.reason);p.state='PENDING_APPROVAL';return{...p}}approve(id:string,actor:string){const p=this.get(id);if(p.state!=='PENDING_APPROVAL'||actor===p.makerId)throw new Error('forbidden');p.state='APPROVED';p.approvedVersion=p.version;return{...p}}queue(id:string){const p=this.get(id);if(p.state!=='APPROVED'||p.approvedVersion!==p.version)throw new Error('approval invalid');p.state='QUEUED';return{...p}}simulateSettlement(id:string){const p=this.get(id);if(p.state!=='QUEUED')throw new Error('not queued');p.state='SETTLED';return{...p}}editAmount(id:string,actor:string,amountIrr:number){const p=this.get(id);if(actor!==p.makerId||!Number.isSafeInteger(amountIrr)||amountIrr<0)throw new Error('forbidden');p.amountIrr=amountIrr;p.version++;p.state='DRAFT';delete p.approvedVersion;return{...p}}queueView(state:PaymentState){return[...this.ps.values()].filter(p=>p.state===state).map(p=>({...p}))}}