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

feat: add atomic payment intake validation

Davood Mehraban committed Oct 5, 2026 at 16:30 UTC d509ec79febf11a3764a044b8b230328a2309276
2 files changed +4
src/domain/core/intake.test.ts new
+2
@@ -0,0 +1,2 @@
1 +import {describe,expect,it} from 'vitest'; import {parsePaymentCsv} from './intake';
2 +describe('payment intake',()=>{it('parses valid tenant batch atomically',()=>{const r=parsePaymentCsv('t1','beneficiaryId,amountIrr,paymentDate,clientReference\nb1,1000,2026-10-05,ref-1');expect(r.ok).toBe(true);if(r.ok)expect(r.payments[0].clientReference).toBe('REF-1')});it('rejects entire malformed batch',()=>{const r=parsePaymentCsv('t1','beneficiaryId,amountIrr,paymentDate,clientReference\nb1,1.5,2026-10-05,x\nb2,100,broken,y');expect(r.ok).toBe(false);if(!r.ok)expect(r.errors.length).toBe(2)})});
src/domain/core/intake.ts new
+2
@@ -0,0 +1,2 @@
1 +export type IntakePayment={tenantId:string;beneficiaryId:string;amountIrr:number;paymentDate:string;clientReference:string}; export type IntakeResult={ok:true;payments:IntakePayment[]}|{ok:false;errors:string[]};
2 +export function parsePaymentCsv(tenantId:string,csv:string):IntakeResult{const lines=csv.trim().split(/\r?\n/);const errors:string[]=[];const payments:IntakePayment[]=[];if(lines[0]?.trim()!=='beneficiaryId,amountIrr,paymentDate,clientReference')return{ok:false,errors:['invalid header']};for(let i=1;i<lines.length;i++){const [beneficiaryId,amountText,paymentDate,ref]=lines[i].split(',').map(x=>x.trim());const amount=Number(amountText);const rowErrors:string[]=[];if(!beneficiaryId)rowErrors.push(`row ${i}: beneficiary required`);if(!Number.isSafeInteger(amount)||amount<0)rowErrors.push(`row ${i}: invalid IRR amount`);if(!/^\d{4}-\d{2}-\d{2}$/.test(paymentDate??'')||Number.isNaN(Date.parse(`${paymentDate}T00:00:00Z`)))rowErrors.push(`row ${i}: invalid date`);if(!ref)rowErrors.push(`row ${i}: reference required`);if(rowErrors.length)errors.push(...rowErrors);else payments.push({tenantId,beneficiaryId,amountIrr:amount,paymentDate,clientReference:ref.toUpperCase()});}return errors.length?{ok:false,errors}:{ok:true,payments};}