Treasury & Bank Transfer Management System — سامانه مدیریت خزانهداری و حوالههای بانکی
dev/v7-production-failclosed-20261021
ci/public-windows-bridge-probe-20261017
ci/windows-dispatch-bootstrap-20261016
ci/windows-dual-app-lab-20261016
dev/v7-production-failclosed-20261021
feature/reconciliation-guard-20261013
main
phase-1-foundation
phase-1-implementation
phase-2-treasury-core
phase-3-banking-reconciliation
phase-4-operations-enterprise-ux
phase-5-hardening-release
phase-6-production-platform
phase-7-production-infrastructure-bank-golive
phase-7-sql-readonly-ux-20261015
transfer-exe-to-drive-20261006
| 1 | import {existsSync,mkdirSync,readFileSync,renameSync,rmSync,writeFileSync} from 'node:fs'; |
| 2 | import {dirname} from 'node:path'; |
| 3 | import type {LocalStoreData} from '../application/types'; |
| 4 | |
| 5 | const empty=():LocalStoreData=>({ |
| 6 | version:1, |
| 7 | meta:{organizationName:'TreasuryFlow'}, |
| 8 | accounts:[], |
| 9 | beneficiaries:[], |
| 10 | payments:[], |
| 11 | audit:[] |
| 12 | }); |
| 13 | |
| 14 | export class LocalStore{ |
| 15 | constructor(private readonly file:string){} |
| 16 | |
| 17 | snapshot():LocalStoreData{ |
| 18 | if(!existsSync(this.file)) return empty(); |
| 19 | const parsed=JSON.parse(readFileSync(this.file,'utf8')) as Partial<LocalStoreData>; |
| 20 | if(parsed.version!==1) throw new Error('unsupported local store version'); |
| 21 | return parsed as LocalStoreData; |
| 22 | } |
| 23 | |
| 24 | update(mutator:(data:LocalStoreData)=>LocalStoreData){ |
| 25 | const before=this.snapshot(); |
| 26 | let next:LocalStoreData; |
| 27 | try{ |
| 28 | next=mutator(structuredClone(before)); |
| 29 | }catch(error){ |
| 30 | rmSync(this.file+'.tmp',{force:true}); |
| 31 | throw error; |
| 32 | } |
| 33 | if(next.version!==1) throw new Error('unsupported local store version'); |
| 34 | mkdirSync(dirname(this.file),{recursive:true}); |
| 35 | const tmp=this.file+'.tmp'; |
| 36 | writeFileSync(tmp,JSON.stringify(next,null,2),'utf8'); |
| 37 | renameSync(tmp,this.file); |
| 38 | return structuredClone(next); |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | export function defaultLocalStorePath(){ |
| 43 | const base=process.env.LOCALAPPDATA||process.env.APPDATA||process.cwd(); |
| 44 | return `${base}\\TreasuryFlow\\data\\treasuryflow-local.json`; |
| 45 | } |