import {createHash} from 'node:crypto'; import {PostbankPayments} from './postbank-payments.mjs'; import {creditID} from './postbank-credit-ledger.mjs'; const taxTreasury='sys_tax_treasury'; const systemActor=creditID('rihla:internal-fee-actor'); export function createRihlaFeePoster({backingSystemKey,clock=Date.now}){ if(typeof backingSystemKey!=='string'||!/^sys_rihla_[a-z0-9_]{1,24}$/.test(backingSystemKey)) throw Error('A dedicated Rihla backing system account is required.'); const payments=new PostbankPayments(null,null,{clock}); return async (tx,{transferId,taxMinor})=>{ const rows=await tx.query('SELECT * FROM pb_accounts WHERE system_key IN (?,?) ORDER BY id FOR UPDATE', [backingSystemKey,taxTreasury]); const source=rows.find(row=>row.system_key===backingSystemKey); const target=rows.find(row=>row.system_key===taxTreasury); if(!source||!target||source.state!=='active'||target.state!=='active'|| source.client_number!==null||source.package_id!==null||source.type_code!=='999') throw Error('Rihla backing and tax treasury must be active.'); if(taxMinor<=0n||BigInt(source.balance_minor)-BigInt(source.reserved_minor)