forked from unimatrix/web
12 lines
1.8 KiB
JavaScript
12 lines
1.8 KiB
JavaScript
import {randomUUID} from 'node:crypto';
|
|
export const REGISTRY_ACTOR='00000000-0000-5000-a000-000000000001';
|
|
// Read identity/status only from the central clan registry. Never read old/game funds.
|
|
export async function reconcilePostbankClans(central,bank){
|
|
if(!central||!bank)return {paused:0,restored:0};
|
|
const entities=await bank.query("SELECT id,external_key,active FROM pb_entities WHERE kind='clan' ORDER BY id");if(!entities.length)return {paused:0,restored:0};
|
|
const current=new Map();
|
|
// Obtain the complete source result before any mutation. A failed registry read
|
|
// must never be interpreted as an empty/deleted clan registry.
|
|
for(let i=0;i<entities.length;i+=200){const ids=entities.slice(i,i+200).map(e=>e.external_key),result=await central.prepare('SELECT id,active FROM site_clans WHERE id IN ('+ids.map(()=>'?').join(',')+')').bind(...ids).all();if(!Array.isArray(result.results))throw Error('Clan registry snapshot unavailable.');for(const row of result.results)current.set(String(row.id),Number(row.active)===1);}
|
|
return bank.transaction(async tx=>{let paused=0,restored=0;for(const entity of entities){const row=await tx.first("SELECT id,active FROM pb_entities WHERE id=? AND kind='clan' FOR UPDATE",[entity.id]);if(!row)continue;const active=current.get(String(entity.external_key))===true?1:0;if(Number(row.active)===active)continue;await tx.query('UPDATE pb_entities SET active=? WHERE id=?',[active,entity.id]);await tx.query('INSERT INTO pb_audit(id,actor_id,action,entity_id,details,created_at) VALUES(?,?,?,?,?,?)',[randomUUID(),REGISTRY_ACTOR,active?'entity_restored':'entity_paused',entity.id,JSON.stringify({reason:active?'Clan is active in the central registry.':'Clan is dissolved or missing from the central registry.','source':'central_clan_registry'}),Date.now()]);if(active)restored++;else paused++;}return {paused,restored};});
|
|
}
|

Staff