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

feat: add immutable bank statement ingestion

Davood Mehraban committed Oct 5, 2026 at 16:37 UTC f2a10afa8375149df6a9380b3a87140b7ff4bed1
2 files changed +3
src/domain/statement.test.ts new
+2
@@ -0,0 +1,2 @@
1 +import {describe,expect,it} from 'vitest';import {ingestStatement} from './statement';
2 +describe('statement ingestion',()=>{it('normalizes rows and rejects duplicate source ids',()=>{const r=ingestStatement('t1','a1',[{sourceId:'x',amountIrr:100,date:'2026-10-05',reference:' ab '},{sourceId:'x',amountIrr:100,date:'2026-10-05',reference:'ab'}]);expect(r.ok).toBe(false)});it('rejects malformed batch atomically',()=>{const r=ingestStatement('t1','a1',[{sourceId:'x',amountIrr:1.2,date:'bad',reference:'x'}]);expect(r.ok).toBe(false)})});
src/domain/statement.ts new
+1
@@ -0,0 +1 @@
1 +export type StatementInput={sourceId:string;amountIrr:number;date:string;reference:string};export type BankTransaction=Readonly<StatementInput&{tenantId:string;accountId:string;reference:string}>;export function ingestStatement(tenantId:string,accountId:string,rows:StatementInput[]){const errors:string[]=[];const seen=new Set<string>();const txns:BankTransaction[]=[];for(const [i,r] of rows.entries()){if(!r.sourceId||seen.has(r.sourceId))errors.push(`row ${i+1}: duplicate/missing source id`);seen.add(r.sourceId);if(!Number.isSafeInteger(r.amountIrr))errors.push(`row ${i+1}: invalid amount`);if(!/^\d{4}-\d{2}-\d{2}$/.test(r.date)||Number.isNaN(Date.parse(r.date+'T00:00:00Z')))errors.push(`row ${i+1}: invalid date`);txns.push(Object.freeze({...r,tenantId,accountId,reference:r.reference.trim().toUpperCase()}))}return errors.length?{ok:false as const,errors}:{ok:true as const,transactions:Object.freeze(txns)}}