import {randomUUID} from 'node:crypto'; import {entityId} from './postbank-authority.mjs'; import {bankNumber,clientCandidate} from './postbank-catalogue.mjs'; import {REGISTRY_ACTOR} from './postbank-registry.mjs'; // Internal payroll operation. The caller supplies a freshly verified staff // recipient and runs this inside the payroll transaction, never a public route. export async function ensurePayrollCurrentAccount(tx,recipient,{now=Date.now(),makeId=randomUUID}={}){ if(!recipient?.id||!recipient.username)throw Error('A verified payroll recipient is required.'); const id=entityId('person',recipient.id); await tx.query("INSERT INTO pb_entities(id,kind,external_key,display_name,active,created_at) VALUES(?,'person',?,?,1,?) ON DUPLICATE KEY UPDATE id=id",[id,recipient.id,recipient.username,now]); const entity=await tx.first('SELECT id,kind,active FROM pb_entities WHERE id=? FOR UPDATE',[id]); if(!entity||entity.kind!=='person'||Number(entity.active)!==1)throw Error('Personal banking identity needs review.'); let client=await tx.first('SELECT client_number FROM pb_clients WHERE entity_id=? FOR UPDATE',[id]); if(client){ const accounts=await tx.query('SELECT a.id,a.type_code,a.state,a.system_key,p.state package_state FROM pb_accounts a LEFT JOIN pb_packages p ON p.id=a.package_id WHERE a.client_number=? ORDER BY a.id FOR UPDATE',[client.client_number]); const suitable=accounts.find(a=>!a.system_key&&a.state==='active'&&a.package_state==='active'&&['102','108'].includes(a.type_code)); if(suitable)return {accountId:String(suitable.id),created:false,entityId:id}; // Do not evade an account restriction by opening another salary account. if(accounts.some(a=>['102','108'].includes(a.type_code)))throw Error('Existing personal current account needs review.'); }else{ for(let attempt=0;attempt<32;attempt++){ const number=clientCandidate('person',recipient.id,attempt); try{await tx.query('INSERT INTO pb_clients(client_number,entity_id,created_at) VALUES(?,?,?)',[number,id,now]);client={client_number:number};break;} catch(error){if(error.code!=='ER_DUP_ENTRY')throw error;} } if(!client)throw Error('Personal client number could not be allocated.'); } const packageId=makeId(); await tx.query("INSERT INTO pb_packages(id,client_number,package_key,monthly_minor,terms_version,accepted_by,accepted_at,state) VALUES(?,?,'pb_start',0,'payroll-provisioned-basic-v1',?,?,'active')",[packageId,client.client_number,REGISTRY_ACTOR,now]); const result=await tx.query("INSERT INTO pb_accounts(client_number,package_id,type_code,display_name,balance_minor,reserved_minor,state,created_at) VALUES(?,?,'108',?,0,0,'active',?)",[client.client_number,packageId,recipient.username+' ยท Current',now]); const accountId=String(result.insertId); await tx.query('UPDATE pb_accounts SET account_number=? WHERE id=?',[bankNumber('108',Number(accountId)),accountId]); await tx.query('INSERT INTO pb_activations(entity_id,package_id) VALUES(?,?) ON DUPLICATE KEY UPDATE entity_id=entity_id',[id,packageId]); await tx.query("INSERT INTO pb_audit(id,actor_id,action,entity_id,details,created_at) VALUES(?,?,'payroll_account_provisioned',?,?,?)",[makeId(),REGISTRY_ACTOR,id,JSON.stringify({recipientId:recipient.id,accountId,packageKey:'pb_start',typeCode:'108',openingBalanceMinor:0}),now]); return {accountId,created:true,entityId:id}; }