import {fileURLToPath} from 'node:url'; import {realpathSync} from 'node:fs'; import {applyUnicorpRoyalMailbox,reconcileRoyalCompanyMail} from '../node/company-royal-mail.mjs'; export async function runUnicorpRoyalMailbox({db,keys,broker,apply=false}){const result=await applyUnicorpRoyalMailbox(db,keys,{apply});if(apply&&result.companyId){await reconcileRoyalCompanyMail(db,broker);return{...result,mailboxState:(await db.prepare('SELECT state FROM site_company_royal_mail WHERE company_id=?').bind(result.companyId).first()).state};}return result;} if(process.argv[1]&&fileURLToPath(import.meta.url)===realpathSync(process.argv[1])){ if(process.argv.slice(2).some(a=>a!=='--apply'))throw Error('Usage: provision-unicorp-royal-mail.mjs [--apply]'); const {openDatabase}=await import('../node/mysql.mjs'),{openOrganisationKeyService}=await import('../node/organisation-key-service.mjs'),{internalMailBroker}=await import('../node/internal-mail.mjs'); const db=await openDatabase(process.env);try{console.log(JSON.stringify(await runUnicorpRoyalMailbox({db,keys:await openOrganisationKeyService(db,process.env),broker:internalMailBroker,apply:process.argv.includes('--apply')})));}finally{await db.close();} }