web/website/node/member-holdings.mjs
2026-11-27 23:43:05 +00:00

35 lines
6.1 KiB
JavaScript

import {readCharacterPreferences} from './character-preferences.mjs';
import {readPoliticalIdentity} from './political-identity.mjs';
import {clanHierarchyRoot} from './clan-deeds.mjs';
import {clanTitles} from '../control-panel/public/clan-titles.mjs';
import {dynasticAddress} from '../control-panel/public/dynastic-address.mjs';
const missing=e=>/no such table|doesn't exist|unknown column|no such column/i.test(e.message||'');
// A public ownership projection, not a bank/mandate or permission source.
export async function memberHoldings(db,target){
const result={state:'available',clansState:'available',companiesState:'available',clans:[],companies:[],appointments:[],deeds:[],subhouses:[],address:null};let clans;
try{clans=(await db.prepare('SELECT c.id,c.name,c.tag,c.home_world,c.owner_id,i.institution_key,d.parent_clan_id,d.heir_account_id,d.heir_joined_at,d.consort_account_id,d.consort_joined_at,ch.charter_kind FROM site_clans c LEFT JOIN site_clan_deeds d ON d.clan_id=c.id LEFT JOIN site_clan_charters ch ON ch.clan_id=c.id LEFT JOIN site_clan_institutions i ON i.clan_id=c.id WHERE c.active=1 ORDER BY c.name_key LIMIT 501').all()).results;if(clans.length>500)throw Error('Public clan register exceeds its current view.');}catch(error){if(!missing(error))throw error;clans=[];result.state='partial';result.clansState='unavailable';}
const current=await db.prepare('SELECT m.clan_id,m.joined_at FROM site_clan_members m JOIN site_game_links l ON l.user_id=m.user_id AND l.minecraft_uuid=m.minecraft_uuid WHERE m.user_id=?').bind(target.id).first();
const politicalIdentity=await readPoliticalIdentity(db,target),characterGender=(await readCharacterPreferences(db,target.id)).ownerDefault;
for(const office of politicalIdentity.poldeeds.records||[])result.deeds.push({id:office.key,name:office.scope,kind:'royal',entityKind:'politics',url:'https://www.dunerealms.com/political-houses/'+encodeURIComponent(office.key),emblem:office.seal,basis:'Active office deed'});
const rank=[],owned=new Set();
for(const c of clans){const kind=c.charter_kind||'basic',record={id:c.id,name:c.name,tag:c.tag,kind,entityKind:'clan',url:'https://www.dunerealms.com/clans/'+encodeURIComponent(c.tag),emblem:'https://cp.dunerealms.com/api/clan-emblems/'+encodeURIComponent(c.id)};let role=null;
if(c.owner_id===target.id){result.clans.push(record);result.deeds.push(record);owned.add(c.id);role='ruler';}
else if(c.heir_account_id===target.id&&['original','royal'].includes(kind)&&current&&Number(current.joined_at)===Number(c.heir_joined_at)&&clanHierarchyRoot(clans,current.clan_id)===clanHierarchyRoot(clans,c.id))role='heir';
else if(c.consort_account_id===target.id&&current&&Number(current.joined_at)===Number(c.consort_joined_at))role='consort';
if(role){const address=dynasticAddress({username:target.username,characterName:target.display_name,politicalIdentity,characterGender}, {name:c.name,institution:c.institution_key},clanTitles({home_world:c.home_world,charterKind:kind,institution:c.institution_key}),role);result.appointments.push({...record,role,address});rank.push({address,score:({royal:30,original:20,basic:10})[kind]+({ruler:3,heir:2,consort:1})[role]});}
}
for(const c of clans)if(c.parent_clan_id&&owned.has(c.parent_clan_id))result.subhouses.push({id:c.id,name:c.name,tag:c.tag,parentClanId:c.parent_clan_id,url:'https://www.dunerealms.com/clans/'+encodeURIComponent(c.tag),emblem:'https://cp.dunerealms.com/api/clan-emblems/'+encodeURIComponent(c.id)});
result.address=rank.sort((a,b)=>b.score-a.score)[0]?.address||null;
try{
const {results}=await db.prepare("SELECT c.id,c.name,c.slug,c.founded_by,(SELECT COALESCE(SUM(p.quantity),0) FROM corrino_share_positions p JOIN corrino_share_classes s ON s.id=p.share_class_id WHERE s.company_id=c.id AND p.account_id=?) owned_shares,(SELECT COALESCE(SUM(s.issued_shares),0) FROM corrino_share_classes s WHERE s.company_id=c.id) issued_shares FROM corrino_companies c WHERE c.status='active' AND (c.founded_by=? OR EXISTS(SELECT 1 FROM corrino_share_positions p JOIN corrino_share_classes s ON s.id=p.share_class_id WHERE s.company_id=c.id AND p.account_id=? AND p.quantity>0)) ORDER BY c.name LIMIT 101").bind(target.id,target.id,target.id).all();
if(results.length>100)throw Error('Public company register exceeds its current view.');
result.companies=results.filter(c=>BigInt(c.owned_shares)>0n||c.founded_by===target.id&&BigInt(c.issued_shares)===0n).map(c=>({id:c.id,name:c.name,entityKind:'company',emblem:'https://cp.dunerealms.com/api/company-emblems/'+encodeURIComponent(c.id),url:'https://www.dunerealms.com/companies/'+encodeURIComponent(c.slug),basis:BigInt(c.owned_shares)>0n?'Shareholder':'Registered founder'}));
}catch(error){if(!missing(error))throw error;result.state='partial';result.companiesState='unavailable';}
if(db.companyDeedsEnabled){
const {results:companies}=await db.prepare("SELECT c.id,c.name,c.slug,d.charter_kind FROM corrino_companies c JOIN site_company_deeds d ON d.company_id=c.id WHERE c.status='active' AND d.owner_account_id=? ORDER BY c.name").bind(target.id).all();
const {results:children}=await db.prepare("SELECT c.id,c.name,c.slug,r.parent_company_id FROM site_company_relations r JOIN corrino_companies c ON c.id=r.company_id JOIN site_company_deeds d ON d.company_id=r.parent_company_id WHERE d.owner_account_id=? AND c.status='active' ORDER BY c.name").bind(target.id).all();
for(const c of children)result.subhouses.push({id:c.id,name:c.name,entityKind:'company',parentCompanyId:c.parent_company_id,url:'https://www.dunerealms.com/companies/'+encodeURIComponent(c.slug),emblem:'https://cp.dunerealms.com/api/company-emblems/'+encodeURIComponent(c.id)});
for(const c of companies){const record={id:c.id,name:c.name,kind:c.charter_kind,entityKind:'company',url:'https://www.dunerealms.com/companies/'+encodeURIComponent(c.slug),emblem:'https://cp.dunerealms.com/api/company-emblems/'+encodeURIComponent(c.id),basis:'Registered deed owner'};result.deeds.push(record);const existing=result.companies.findIndex(r=>r.id===c.id);if(existing<0)result.companies.push(record);else result.companies[existing]={...result.companies[existing],...record};}
}
return result;
}