import {openDatabase} from '../node/mysql.mjs'; import {openPostbankDatabase} from '../node/postbank-db.mjs'; import {canRunScheduler} from '../node/scheduler-authority.mjs'; import {queueRatesPublication} from '../node/postbank-financial-policy.mjs'; import {PostbankRatePublications} from '../node/postbank-rate-publications.mjs'; const central=await openDatabase(process.env),bank=await openPostbankDatabase(process.env); try{ const actorId=process.argv.find(a=>a.startsWith('--actor='))?.slice(8);if(!bank||!await canRunScheduler(central,{id:actorId}))throw Error('Current deployment or Big Four control required.'); if(!process.argv.includes('--apply'))console.log(JSON.stringify({plan:true,package:'Pro',monthlyMinor:1500,payrollRun:false})); else{ const id=await bank.transaction(async tx=>{ await tx.query("INSERT INTO pb_financial_policies(policy_key,policy_json,version,updated_by,updated_at) VALUES('publication_guard','{}',0,?,?) ON DUPLICATE KEY UPDATE policy_key=VALUES(policy_key)",[actorId,Date.now()]); await tx.first("SELECT policy_key FROM pb_financial_policies WHERE policy_key='publication_guard' FOR UPDATE"); const latest=await tx.first('SELECT id,data_json FROM pb_rate_publications ORDER BY created_at DESC,id DESC LIMIT 1 FOR UPDATE'); if(latest&&JSON.parse(latest.data_json).packages.some(p=>p.key==='pb_pro'&&p.unlimitedInstant===true))return latest.id; return queueRatesPublication(tx,actorId); }); const publisher=new PostbankRatePublications(bank,{python:process.env.POSTBANK_PYTHON,logo:process.env.POSTBANK_STATEMENT_LOGO}); for(let i=0;i<20;i++){if((await bank.first('SELECT state FROM pb_rate_publications WHERE id=?',[id]))?.state==='ready')break;const result=await publisher.process();if(result.failed||!result.generated)throw Error('Tariff generation not confirmed; inspect the publication worker before retrying.');} const ready=(await bank.first('SELECT state FROM pb_rate_publications WHERE id=?',[id]))?.state==='ready';if(!ready)throw Error('Pro tariff PDF not ready.');console.log(JSON.stringify({published:true,publication:id,payrollRun:false})); } }finally{await bank?.close();await central.close();}