web/website/scripts/rollout-directory-numbers.mjs

11 lines
1.3 KiB
JavaScript

import {fileURLToPath} from 'node:url';import {resolve} from 'node:path';
import {openDatabase} from '../node/mysql.mjs';import {initializeOrganisation} from '../node/organisation-model.mjs';import {canRunScheduler} from '../node/scheduler-authority.mjs';
import {DIRECTORY_OWNER_ID,ensurePersonnelNumber} from '../node/personnel-numbers.mjs';
if(process.argv[1]&&fileURLToPath(import.meta.url)===resolve(process.argv[1])){
const db=await openDatabase(process.env);try{
const actorId=process.argv.find(a=>a.startsWith('--actor='))?.slice(8);if(!await canRunScheduler(db,{id:actorId}))throw Error('Current central control is required.');
const {results:people}=await db.prepare("SELECT a.id FROM site_accounts a LEFT JOIN site_account_policies p ON p.user_id=a.id WHERE a.password_hash NOT LIKE '!%' AND a.role<>'official_account' AND COALESCE(p.game_link_exempt,0)=0 ORDER BY CASE WHEN a.id=? THEN 0 ELSE 1 END,a.created_at,a.username,a.id").bind(DIRECTORY_OWNER_ID).all();
if(!process.argv.includes('--apply'))console.log(JSON.stringify({plan:true,people:people.length,ownerNumber:'DR-0001'}));
else{await initializeOrganisation(db);for(const person of people)await db.transaction(tx=>ensurePersonnelNumber(tx,person.id,{actorId}));console.log(JSON.stringify({applied:true,people:people.length,ownerNumber:'DR-0001'}));}
}finally{await db.close();}
}