web/tools/jarvis-dev-clan-reset.mjs

39 lines
3.3 KiB
JavaScript

// CLI-only domain handler. Uses the existing archive semantics: inactive clan,
// removed roster and closed requests; financial/history rows are not erased.
import {createHash,randomUUID} from 'node:crypto';
const digest=value=>createHash('sha256').update(JSON.stringify(value)).digest('hex');
export async function clanResetManifest(tx,ownerUsername){
const owner=await tx.first('SELECT id,username FROM site_accounts WHERE username=? FOR UPDATE',[ownerUsername]);
if(!owner)throw Error('Preserved owner not found');
const clans=await tx.query('SELECT id,name,owner_id,active,version FROM site_clans ORDER BY id FOR UPDATE');
const institutions=await tx.query('SELECT clan_id,institution_key FROM site_clan_institutions ORDER BY clan_id FOR UPDATE');
const members=await tx.query('SELECT user_id,clan_id,clan_role FROM site_clan_members ORDER BY user_id FOR UPDATE');
const protectedIds=new Set(institutions.map(i=>i.clan_id));
const active=clans.filter(c=>Number(c.active)===1),keep=active.filter(c=>protectedIds.has(c.id)||c.owner_id===owner.id);
if(!keep.some(c=>c.owner_id===owner.id))throw Error('Preserved owner clan is missing');
return {action:'clan-reset',state:'planned',ownerId:owner.id,
retainClanIds:keep.map(c=>c.id),deleteClanIds:active.filter(c=>!keep.some(k=>k.id===c.id)).map(c=>c.id),
inventoryHash:digest({clans,institutions,members}),preserveOwnerMembers:true,
scope:'clans-only-existing-archive-flow',companiesChanged:false,balancesChanged:false};
}
export async function executeClanReset(tx,{record,expectedManifest,batchId,now=Date.now()}){
if(record.manifestHash==null||JSON.stringify(record.manifest)!==JSON.stringify(expectedManifest)){
// Key manifests are canonicalised; compare the actual scope and inventory,
// independent of JSON property order.
const actual=record.manifest;
if(!actual||Object.keys(actual).sort().join()!==Object.keys(expectedManifest).sort().join()||Object.keys(expectedManifest).some(k=>JSON.stringify(actual[k])!==JSON.stringify(expectedManifest[k])))throw Error('Reset manifest mismatch');
}
const prior=await tx.first("SELECT id FROM account_audit WHERE action='dev-clan-reset-complete' AND details LIKE ? FOR UPDATE",['%'+batchId+'%']);
if(prior)return {replayed:true,archived:0};
let archived=0;
for(const id of expectedManifest.deleteClanIds){
const changed=await tx.query('UPDATE site_clans SET active=0,version=version+1,updated_at=? WHERE id=? AND active=1',[now,id]);
if(changed.affectedRows!==1)throw Error('Clan inventory changed');
await tx.query('DELETE FROM site_clan_members WHERE clan_id=?',[id]);
await tx.query("UPDATE site_clan_requests SET status='closed',updated_at=? WHERE clan_id=? AND status='pending'",[now,id]);
await tx.query('INSERT INTO account_audit(id,actor_id,action,details,created_at) VALUES(?,?,?,?,?)',[randomUUID(),expectedManifest.ownerId,'dev-clan-reset-step',JSON.stringify({batchId,keyId:record.id,executor:record.actorId,clanId:id,action:'archive'}),now]);
archived++;
}
await tx.query('INSERT INTO account_audit(id,actor_id,action,details,created_at) VALUES(?,?,?,?,?)',[randomUUID(),expectedManifest.ownerId,'dev-clan-reset-complete',JSON.stringify({batchId,keyId:record.id,manifestHash:record.manifestHash,archived,retained:expectedManifest.retainClanIds}),now]);
return {replayed:false,archived,retained:expectedManifest.retainClanIds};
}