import {clanElectionAuthority,clanDeedOperators} from './clan-deeds.mjs'; export async function lockClanVotingDeed(db,connection,clanId){ const query=async(sql,args=[])=>(await connection.execute(sql,args))[0]; if(db.companyDeedsEnabled){const [guard]=await query('SELECT id FROM jarvis_entity_relation_guard WHERE id=1 FOR UPDATE');if(!guard)throw Error('Deed relation guard unavailable');} const seen=new Set();let current={kind:'clan',id:clanId}; for(let depth=0;depth<32;depth++){ const key=current.kind+':'+current.id;if(seen.has(key))throw Error('Deed hierarchy cycle');seen.add(key); if(current.kind==='politics'){ // Term authority can change independently of the child clan's owner row. await query('SELECT id FROM political_office_terms WHERE outcome=? FOR UPDATE',['winner']);return; } if(current.kind==='clan'){ await query('SELECT id FROM site_clans WHERE id=? FOR UPDATE',[current.id]); if(!db.clanDeedsEnabled)return; await query('SELECT clan_id FROM site_clan_members WHERE clan_id=? FOR UPDATE',[current.id]); await query('SELECT clan_id FROM site_clan_deeds WHERE clan_id=? FOR UPDATE',[current.id]); }else{ await query('SELECT id FROM corrino_companies WHERE id=? FOR UPDATE',[current.id]); await query('SELECT company_id FROM site_company_deeds WHERE company_id=? FOR UPDATE',[current.id]); await query('SELECT company_id FROM site_company_members WHERE company_id=? FOR UPDATE',[current.id]); } let parent=db.companyDeedsEnabled?(await query('SELECT * FROM jarvis_entity_relations WHERE child_kind=? AND child_id=? FOR UPDATE',[current.kind,current.id]))[0]:null; if(!parent){ const [legacy]=await query(current.kind==='clan'?'SELECT parent_clan_id parent_id FROM site_clan_deeds WHERE clan_id=? FOR UPDATE':'SELECT parent_company_id parent_id FROM site_company_relations WHERE company_id=? FOR UPDATE',[current.id]); if(legacy?.parent_id)parent={parent_kind:current.kind,parent_id:legacy.parent_id}; } if(!parent)return;current={kind:parent.parent_kind,id:parent.parent_id}; } throw Error('Deed hierarchy exceeds supported depth'); } // Only organisation ballots use delegated deed authority. Personal ballots keep // their authenticated player identity and their existing personal allowance. export async function clanDelegateAuthority(db,userId,clanId){ if(db.clanDeedsEnabled)return (await clanElectionAuthority(db,userId,clanId)).canManageClanElections; return !!await db.prepare("SELECT c.id FROM site_clans c JOIN site_clan_members m ON m.clan_id=c.id AND m.user_id=c.owner_id AND m.clan_role='owner' JOIN site_game_links l ON l.user_id=m.user_id AND l.minecraft_uuid=m.minecraft_uuid WHERE c.id=? AND c.owner_id=? AND c.active=1").bind(clanId,userId).first(); } export async function clanDelegateChoices(db,userId){ const own=await db.prepare("SELECT c.id,c.name FROM site_clans c JOIN site_clan_members m ON m.clan_id=c.id JOIN site_game_links l ON l.user_id=m.user_id AND l.minecraft_uuid=m.minecraft_uuid WHERE m.user_id=? AND c.active=1").bind(userId).first(); const operators=await clanDeedOperators(db),ids=new Set(own?[own.id]:[]); for(const [id,users]of operators)if(users.includes(userId))ids.add(id); const choices=[]; for(const id of ids){ if(!await clanDelegateAuthority(db,userId,id))continue; const clan=id===own?.id?own:await db.prepare('SELECT id,name FROM site_clans WHERE id=? AND active=1').bind(id).first(); if(clan)choices.push({id:clan.id,name:clan.name}); } return choices.sort((a,b)=>a.name.localeCompare(b.name)); }