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

1 import sql from 'mssql';
2 import type {ExternalSqlServerConfig} from './sql-server-adapter';
3
4 type Driver={connect:(config:any)=>Promise<any>};
5 export class SqlServerAccountingAdapter{
6 constructor(private config:ExternalSqlServerConfig,private driver:Driver=sql as unknown as Driver,private approvedWriteContract?:string){}
7 private driverConfig(){const c:any={server:this.config.server,database:this.config.database,options:{encrypt:this.config.encrypt??false,trustServerCertificate:this.config.trustServerCertificate??true}};if(this.config.auth==='sql'){c.user=this.config.user;c.password=this.config.password}else{c.options.trustedConnection=true}return c}
8 private async withPool<T>(fn:(pool:any)=>Promise<T>){const pool=await this.driver.connect(this.driverConfig());try{return await fn(pool)}finally{await pool.close()}}
9 async testConnection(){return this.withPool(async pool=>{const r=await pool.request().query('SELECT 1 AS ok');return {ok:Number(r.recordset?.[0]?.ok)===1}})}
10 async inspectSchema(){return this.withPool(async pool=>(await pool.request().query(`SELECT TABLE_SCHEMA AS schema_name,TABLE_NAME AS table_name,COLUMN_NAME AS column_name,DATA_TYPE AS data_type FROM INFORMATION_SCHEMA.COLUMNS ORDER BY TABLE_SCHEMA,TABLE_NAME,ORDINAL_POSITION`)).recordset)}
11 async executeApprovedWrite(_operation:string,_params:Record<string,unknown>){if(!this.approvedWriteContract)throw new Error('approved write contract required');throw new Error('write contract implementation not installed')}
12 }