MC/website/ops/run-economy-reset.mjs
2026-11-27 23:43:05 +00:00

17 lines
1.4 KiB
JavaScript

import {pathToFileURL} from 'node:url';
import {openDatabase} from '../node/mysql.mjs';
import {openPostbankDatabase} from '../node/postbank-db.mjs';
import {openOrganisationKeyService} from '../node/organisation-key-service.mjs';
import {createEconomyRuntime,ECONOMY_EPOCH} from '../node/economy-runtime.mjs';
// Explicit server-only entry point for the owner's already authorised reset.
// No web route, startup hook, secret output or automatic payroll execution.
export async function runAuthorisedEconomyReset(settings){
const central=await openDatabase(settings);let bank;
try{bank=await openPostbankDatabase(settings);if(!bank)throw Error('The central bank is unavailable');const keys=await openOrganisationKeyService(central,settings);return await createEconomyRuntime(central,bank,keys,{allowReset:true,epoch:ECONOMY_EPOCH}).reset();}
finally{await Promise.allSettled([central.close(),bank?.close()]);}
}
if(process.argv[1]&&import.meta.url===pathToFileURL(process.argv[1]).href){
if(process.argv.length!==3||process.argv[2]!=='--execute-authorised-reset='+ECONOMY_EPOCH)throw Error('The exact authorised economic epoch is required');
try{const result=await runAuthorisedEconomyReset(process.env);const {beforeBalances,...summary}=result;console.log(JSON.stringify(summary));}
catch(error){console.error('Economic reset not confirmed:',error.code||error.message);process.exitCode=1;}
}