// Server operator only. Uses the ordinary JARVIS identity adapter; no browser session. import {fileURLToPath} from 'node:url'; import {realpathSync} from 'node:fs'; import {openDatabase} from '../node/mysql.mjs'; import {openOrganisationKeyService} from '../node/organisation-key-service.mjs'; import {createSystemCharacters} from '../node/system-characters.mjs'; import {SYSTEM_CHARACTERS} from '../node/system-character-catalogue.mjs'; export async function currentSystemCharacterOperator(actor,db){ const rows=await db.query("SELECT p.principal_kind FROM org_control_principals p JOIN site_accounts a ON a.id=p.user_id WHERE p.user_id=? AND a.password_hash NOT LIKE '!%'",[actor?.id||'']); return rows[0]?.principal_kind==='owner'; } export async function provisionCharacters(db,keys,{actorId,apply=false}={}){ const actor={id:actorId};if(!await currentSystemCharacterOperator(actor,db))throw Error('Current owner authority required'); const service=createSystemCharacters(db,keys,{authorizeOperator:currentSystemCharacterOperator}),results=[]; for(const character of SYSTEM_CHARACTERS){ results.push(apply?await service.provision(actor,character.key):{character:character.key,existing:!!await service.inspect(character.key)}); } return {applied:apply,count:results.length,accounts:results}; } if(process.argv[1]&&fileURLToPath(import.meta.url)===realpathSync(process.argv[1])){ if(process.getuid?.()!==0)throw Error('Server administrator required'); const db=await openDatabase(process.env); try{const keys=await openOrganisationKeyService(db,process.env);console.log(JSON.stringify(await provisionCharacters(db,keys,{actorId:process.argv.find(a=>a.startsWith('--actor='))?.slice(8),apply:process.argv.includes('--apply')})));}finally{await db.close();} }