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

feat: add deterministic reconciliation engine

Davood Mehraban committed Oct 5, 2026 at 16:38 UTC 7929e468473a116688b38f3bf57d69633e74e0cc
2 files changed +4
src/domain/reconciliation.test.ts new
+3
@@ -0,0 +1,3 @@
1 +import {describe,expect,it} from 'vitest';import {reconcile} from './reconciliation';
2 +const tx={tenantId:'t1',accountId:'a1',sourceId:'s1',amountIrr:-100,date:'2026-10-05',reference:'REF1'};
3 +describe('reconciliation',()=>{it('exactly matches tenant account reference and amount',()=>expect(reconcile(tx,[{tenantId:'t1',accountId:'a1',paymentId:'p1',amountIrr:100,reference:'REF1'}]).status).toBe('MATCHED'));it('does not guess ambiguous candidates',()=>expect(reconcile(tx,[{tenantId:'t1',accountId:'a1',paymentId:'p1',amountIrr:100,reference:'REF1'},{tenantId:'t1',accountId:'a1',paymentId:'p2',amountIrr:100,reference:'REF1'}]).status).toBe('AMBIGUOUS'));it('never cross-matches tenant',()=>expect(reconcile(tx,[{tenantId:'t2',accountId:'a1',paymentId:'p1',amountIrr:100,reference:'REF1'}]).status).toBe('UNMATCHED'))});
src/domain/reconciliation.ts new
+1
@@ -0,0 +1 @@
1 +type Tx={tenantId:string;accountId:string;sourceId:string;amountIrr:number;date:string;reference:string};type Payment={tenantId:string;accountId:string;paymentId:string;amountIrr:number;reference:string};export type Reconciliation={status:'MATCHED';paymentId:string}|{status:'AMBIGUOUS';candidateIds:string[]}|{status:'UNMATCHED'};export function reconcile(tx:Tx,payments:Payment[]):Reconciliation{const candidates=payments.filter(p=>p.tenantId===tx.tenantId&&p.accountId===tx.accountId&&p.reference.trim().toUpperCase()===tx.reference.trim().toUpperCase()&&p.amountIrr===Math.abs(tx.amountIrr));if(candidates.length===1)return{status:'MATCHED',paymentId:candidates[0].paymentId};if(candidates.length>1)return{status:'AMBIGUOUS',candidateIds:candidates.map(x=>x.paymentId)};return{status:'UNMATCHED'}}