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

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}))}}