forked from unimatrix/web
53 lines
6.6 KiB
JavaScript
53 lines
6.6 KiB
JavaScript
import {readFileSync,realpathSync} from 'node:fs';
|
|
import {pathToFileURL} from 'node:url';
|
|
import {createHash} from 'node:crypto';
|
|
import {buildAlphaResetReview} from '../node/alpha-reset-review.mjs';
|
|
import {OFFICIAL_ACCOUNT} from '../node/official-account.mjs';
|
|
|
|
// SELECT-only inventory. Never calls a schema initializer or domain executor.
|
|
export async function collectAlphaResetInventory(site,bank){
|
|
const read=async(db,sql)=>{if(!/^SELECT /i.test(sql))throw Error('Read-only inventory required');return db.query(sql);};
|
|
const central={
|
|
clans:'SELECT id,name,owner_id,active,version FROM site_clans ORDER BY id',
|
|
companies:'SELECT c.id,c.name,c.founded_by,c.status,d.owner_account_id,d.charter_kind,d.version FROM corrino_companies c LEFT JOIN site_company_deeds d ON d.company_id=c.id ORDER BY c.id',
|
|
relations:'SELECT child_kind,child_id,parent_kind,parent_id,key_id FROM jarvis_entity_relations ORDER BY child_kind,child_id',
|
|
clanInstitutions:'SELECT clan_id,institution_key FROM site_clan_institutions ORDER BY clan_id',
|
|
companyInstitutions:'SELECT company_id,institution_key FROM site_company_institutions ORDER BY company_id',
|
|
claims:'SELECT organisation_kind,organisation_id,revision,snapshot_json FROM site_claim_registry ORDER BY organisation_kind,organisation_id',
|
|
foundingKeys:"SELECT id,account_id,key_type,scope_id,used_at,revoked_at FROM organisation_keys WHERE key_type IN ('clan-founding','company-founding') ORDER BY id",
|
|
questRewards:'SELECT key_id,account_id,season_id,state FROM jarvis_quest_reward_jobs ORDER BY key_id',
|
|
polRewards:'SELECT key_id,account_id,season_id,state FROM jarvis_pol_quest_reward_jobs ORDER BY key_id',
|
|
projectJobs:'SELECT id,project_id,kind,state FROM jarvis_project_jobs ORDER BY id',
|
|
kitListings:'SELECT id,blueprint_id,blueprint_version,active,published FROM site_kit_listings ORDER BY id'
|
|
};
|
|
const financial={
|
|
accounts:'SELECT a.id,a.account_number,a.system_key,a.balance_minor,a.reserved_minor,a.state,e.kind,e.external_key FROM pb_accounts a LEFT JOIN pb_clients c ON c.client_number=a.client_number LEFT JOIN pb_entities e ON e.id=c.entity_id ORDER BY a.id',
|
|
orders:"SELECT id,source_id,target_id,amount_minor,mode,state FROM pb_orders WHERE state NOT IN ('posted','failed','cancelled','rejected') ORDER BY id",
|
|
auctionHolds:'SELECT id,auction_id,source_id,amount_minor,state FROM pb_auction_holds ORDER BY id',
|
|
auctions:'SELECT id,office_key,state,highest_minor,hold_id,winner_id FROM jarvis_office_auctions ORDER BY id',
|
|
deedPurchases:'SELECT id,listing_id,key_id,parent_kind,parent_id,source_account,recipient_account,price_minor,state FROM jarvis_deed_purchases ORDER BY id',
|
|
loans:"SELECT id,account_id,state,outstanding_minor,collateral_minor,treasury_hold_minor,borrower_hold_minor FROM pb_credit_loans WHERE state NOT IN ('closed','repaid','cancelled','rejected','expired') ORDER BY id",
|
|
creditActions:"SELECT id,loan_id,kind,amount_minor,state FROM pb_credit_actions WHERE state NOT IN ('posted','completed','failed','cancelled','rejected') ORDER BY id",
|
|
creditInstalments:"SELECT loan_id,sequence_no,state,principal_minor,interest_minor,paid_principal,paid_interest FROM pb_credit_instalments WHERE state<>'paid' ORDER BY loan_id,sequence_no",
|
|
listings:'SELECT id,definition_id,organisation_kind,organisation_id,revision,state FROM jarvis_listings ORDER BY id'
|
|
};
|
|
const inventory={};for(const [field,sql]of Object.entries(central))inventory[field]=await read(site,sql);for(const [field,sql]of Object.entries(financial))inventory[field]=await read(bank,sql);
|
|
for(const row of inventory.claims){const snapshot=JSON.parse(row.snapshot_json);row.snapshotHash=createHash('sha256').update(row.snapshot_json).digest('hex');row.homeBlock=snapshot.homeBlock?{id:snapshot.homeBlock.id||null,worldUuid:snapshot.homeBlock.worldUuid||null}:null;delete row.snapshot_json;}
|
|
const fallbackClans=await read(site,'SELECT clan_id,parent_clan_id FROM site_clan_deeds WHERE parent_clan_id IS NOT NULL ORDER BY clan_id'),fallbackCompanies=await read(site,'SELECT company_id,parent_company_id FROM site_company_relations WHERE parent_company_id IS NOT NULL ORDER BY company_id');
|
|
for(const [kind,rows]of [['clan',fallbackClans],['company',fallbackCompanies]])for(const r of rows)if(!inventory.relations.some(x=>x.child_kind===kind&&x.child_id===r[kind+'_id']))inventory.relations.push({child_kind:kind,child_id:r[kind+'_id'],parent_kind:kind,parent_id:r['parent_'+kind+'_id'],key_id:null});
|
|
inventory.payrollItems=await read(bank,"SELECT period,actor_id,account_id,order_id,amount_minor,state FROM pb_payroll_items WHERE state NOT IN ('posted','paid','cancelled','failed') ORDER BY period,actor_id");
|
|
inventory.additionalPayrollItems=await read(bank,"SELECT period,actor_id,account_id,order_id,amount_minor,state FROM pb_payroll_additional_items WHERE state NOT IN ('posted','paid','cancelled','failed') ORDER BY period,actor_id");
|
|
inventory.standingInstructions=await read(bank,'SELECT instruction_key,source_key,target_key,amount_minor,enabled,version FROM pb_standing_instructions ORDER BY instruction_key');
|
|
inventory.financialPolicyVersions=await read(bank,'SELECT policy_key,version,updated_at FROM pb_financial_policies ORDER BY policy_key');
|
|
inventory.ledgerSummary=await read(bank,'SELECT COUNT(*) entries,COALESCE(SUM(amount_minor),0) signed_total_minor FROM pb_entries');
|
|
return inventory;
|
|
}
|
|
if(process.argv[1]&&import.meta.url===pathToFileURL(realpathSync(process.argv[1])).href){
|
|
if(process.argv.length!==2)throw Error('This command accepts no execution or mutation options');
|
|
process.loadEnvFile('/home/dunerealms/.config/dunerealms.env');const {default:mysql}=await import('mysql2/promise'),e=process.env;
|
|
const root=realpathSync('/home/dunerealms/apps/dunerealms/current'),config=e.POSTBANK_DB_CONFIG?JSON.parse(readFileSync(e.POSTBANK_DB_CONFIG)):null;if(!config)throw Error('Explicit current bank configuration required');
|
|
const site=await mysql.createConnection({host:e.DB_HOST||'127.0.0.1',port:Number(e.DB_PORT||3306),user:e.DB_USER,password:e.DB_PASSWORD,database:e.DB_NAME,supportBigNumbers:true,bigNumberStrings:true}),bank=await mysql.createConnection({...config,supportBigNumbers:true,bigNumberStrings:true});
|
|
try{for(const db of [site,bank]){await db.query('SET TRANSACTION ISOLATION LEVEL REPEATABLE READ');await db.query('START TRANSACTION WITH CONSISTENT SNAPSHOT, READ ONLY');}
|
|
const reader=db=>({query:async sql=>(await db.query(sql))[0]});const inventory=await collectAlphaResetInventory(reader(site),reader(bank));console.log(JSON.stringify(buildAlphaResetReview(inventory,{ownerId:OFFICIAL_ACCOUNT.ownerId,release:root.split('/').at(-1),observedAt:Date.now()})));
|
|
}finally{await site.rollback();await bank.rollback();await site.end();await bank.end();}
|
|
}
|

Staff