import {isClandestineInstitution} from './clandestine-identity.mjs'; import {createHash} from 'node:crypto'; import {PUBLIC_OFFICES} from './public-government.mjs'; import {politicalOfficeId} from './political-office-charters.mjs'; const digest=value=>createHash('sha256').update(JSON.stringify(value)).digest('hex'); const bounded=(rows,max)=>{if(rows.length>max)throw Error('Board source inventory exceeds the reviewed batch bound');return rows;}; const group=(rows,key)=>{const result=new Map();for(const row of rows){const id=row[key];if(!result.has(id))result.set(id,[]);result.get(id).push(row);}return result;}; // One bounded background observation, not a request-time query cascade. Full // deed resolution is deferred to refresh() only for changed organisations. export async function boardSourceObservations(db,{clock=Date.now}={}){ const now=clock(); const [clans,clanMembers,companies,companyMembers,terms,appointments,charters,orders,orderMembers,overrides,known]=await Promise.all([ db.query("SELECT c.id,c.name,c.owner_id,c.active,c.version,c.home_world,a.role owner_role,(a.password_hash NOT LIKE '!%') owner_active,d.version deed_version,ch.key_id,ch.charter_kind FROM site_clans c JOIN site_accounts a ON a.id=c.owner_id LEFT JOIN site_clan_deeds d ON d.clan_id=c.id LEFT JOIN site_clan_charters ch ON ch.clan_id=c.id ORDER BY c.id LIMIT 501"), db.query('SELECT m.clan_id,m.user_id,m.clan_role,m.joined_at,m.minecraft_uuid,l.minecraft_uuid linked_uuid FROM site_clan_members m LEFT JOIN site_game_links l ON l.user_id=m.user_id ORDER BY m.clan_id,m.user_id LIMIT 5001'), db.query('SELECT c.id,c.name,c.status,c.ceo_account_id,d.owner_account_id,d.version,d.original_key_id FROM corrino_companies c LEFT JOIN site_company_deeds d ON d.company_id=c.id ORDER BY c.id LIMIT 501'), db.query('SELECT company_id,user_id,joined_at FROM site_company_members ORDER BY company_id,user_id LIMIT 5001'), db.query("SELECT id,kind,faction,office,user_id,player_uuid,starts_at,expires_at FROM political_office_terms WHERE outcome='winner' AND starts_at<=? AND expires_at>? ORDER BY id LIMIT 501",[now,now]), db.query('SELECT id,office_key,source_term_id,user_id,revoked_at,starts_at,expires_at FROM political_appointment_deeds WHERE revoked_at IS NULL AND starts_at<=? AND expires_at>? ORDER BY id LIMIT 5001',[now,now]), db.query('SELECT c.office_key,c.organisation_id,c.staff_key_id,k.revoked_at,k.used_at FROM political_office_charters c JOIN organisation_keys k ON k.id=c.staff_key_id ORDER BY c.office_key LIMIT 101'), db.query('SELECT o.id,o.name,o.legacy_company_id,o.leader_account_id,o.parent_office,o.staff_key_id,o.version,k.revoked_at,k.used_at FROM political_passive_orders o JOIN organisation_keys k ON k.id=o.staff_key_id ORDER BY o.id LIMIT 101'), db.query('SELECT order_id,user_id,joined_at FROM political_passive_order_members ORDER BY order_id,user_id LIMIT 5001'), db.query("SELECT d.organisation_kind,d.organisation_id,d.key_id,d.member_id,d.member_binding,d.source_owner_id,d.source_id,d.expires_at,a.role member_role,(a.password_hash NOT LIKE '!%') member_active FROM jarvis_board_delegations d JOIN organisation_keys k ON k.id=d.key_id JOIN site_accounts a ON a.id=d.member_id WHERE d.expires_at>? AND k.expires_at=d.expires_at AND k.revoked_at IS NULL AND k.used_at IS NULL ORDER BY d.organisation_kind,d.organisation_id LIMIT 501",[now]), db.query('SELECT organisation_kind,organisation_id,watch_fingerprint FROM jarvis_board_states ORDER BY organisation_kind,organisation_id LIMIT 1001') ]); for(const rows of [clans,companies,terms,overrides])bounded(rows,500);for(const rows of [clanMembers,companyMembers,appointments,orderMembers])bounded(rows,5000);bounded(charters,100);bounded(orders,100);bounded(known,1000); const saved=new Map(known.map(r=>[r.organisation_kind+':'+r.organisation_id,r.watch_fingerprint])),clanRoster=group(clanMembers,'clan_id'),companyRoster=group(companyMembers,'company_id'),orderRoster=group(orderMembers,'order_id'),delegations=new Map(overrides.map(r=>[r.organisation_kind+':'+r.organisation_id,r])),result=[]; const add=(kind,id,parts)=>{const identity=kind+':'+id,fingerprint=digest([...parts,delegations.get(identity)||null]);if(saved.get(identity)!==fingerprint)result.push({kind,id,watchFingerprint:fingerprint});}; for(const row of clans)if(!isClandestineInstitution('clan',row.id)&&(Number(row.active)===1||saved.has('clan:'+row.id)))add('clan',row.id,[row,clanRoster.get(row.id)||[]]); for(const row of companies)if(!isClandestineInstitution('company',row.id)&&(['active','converted-order'].includes(row.status)||saved.has('company:'+row.id))){const order=orders.find(o=>o.legacy_company_id===row.id);add('company',row.id,[row,companyRoster.get(row.id)||[],order||null,order?orderRoster.get(order.id)||[]:[],order?terms:[]]);} for(const office of PUBLIC_OFFICES){const id=politicalOfficeId(office.key),active=terms.filter(t=>t.kind===office.kind&&t.faction===office.faction&&t.office===office.office);if(!active.length&&!saved.has('politics:'+id)&&!(office.kind==='naib_chair'&&clans.some(c=>Number(c.active)===1&&c.home_world==='arrakduin')))continue;add('politics',id,[charters.find(c=>c.office_key===office.key)||null,active,appointments.filter(a=>a.office_key===office.key),office.kind==='naib_chair'?clans:[],office.kind==='naib_chair'?clanMembers:[]]);} return result; }