// Explicit operator action: the normal staff overwrite key, never a Dev key. import {openDatabase} from '../node/mysql.mjs'; import {openOrganisationKeyService} from '../node/organisation-key-service.mjs'; import {openBoardKeyRuntime} from '../node/board-key-runtime.mjs'; if(process.argv.length!==3||process.argv[2]!=='--apply')throw Error('Use --apply with one approved board-control request on standard input'); let raw='';for await(const chunk of process.stdin){raw+=chunk;if(Buffer.byteLength(raw)>4096)throw Error('Board control request exceeds its bound');} const request=JSON.parse(raw);if(!request||Object.keys(request).some(k=>!['actorUsername','input'].includes(k))||typeof request.actorUsername!=='string')throw Error('Named current staff actor required'); const db=await openDatabase(process.env);db.companyDeedsEnabled=true;db.passiveOrdersEnabled=true; try{ const actor=await db.prepare("SELECT id,username,role FROM site_accounts WHERE username=? AND password_hash NOT LIKE '!%'").bind(request.actorUsername).first();if(!actor)throw Error('Current staff actor unavailable'); const keys=await openOrganisationKeyService(db,process.env),runtime=await openBoardKeyRuntime(db,keys,process.env);if(!runtime)throw Error('Board key publication is unavailable'); const result=await runtime.delegations.issue(actor,request.input);await runtime.boards.refresh(request.input.kind,request.input.id); console.log(JSON.stringify({...result,publication:'queued',ticketId:request.input.ticketId})); }finally{await db.close();}