forked from unimatrix/web
Attach the protected Crysknife Order to the Emperor through a scoped JARVIS parent instrument
This commit is contained in:
parent
7fddf78351
commit
95c875d570
2 changed files with 53 additions and 5 deletions
23
website/node/crysknife-parent.mjs
Normal file
23
website/node/crysknife-parent.mjs
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
import {STAFF_OFFICE_ACCOUNT_ID} from './organisation-keys.mjs';
|
||||
import {entityParent,setEntityParent} from './entity-relations.mjs';
|
||||
import {CRYSKNIFE_COMPANY_ID} from './crysknife-company.mjs';
|
||||
const REQUEST_ID='bd5cc574-f474-4df4-bc66-c659e5c49f32';
|
||||
const SCOPE='protected:order-crysknife:emperor:v1';
|
||||
// Fixed institutional relocation approved for this Order; not a general transfer endpoint.
|
||||
export async function attachCrysknifeToEmperor(db,keys,{clock=Date.now}={}){
|
||||
const verify=async tx=>{
|
||||
const row=await tx.first("SELECT d.owner_account_id FROM site_company_institutions i JOIN site_company_deeds d ON d.company_id=i.company_id JOIN site_memorials m ON m.user_id=d.owner_account_id AND m.slug='patrick-stewart' JOIN corrino_companies c ON c.id=i.company_id AND c.status='active' WHERE i.company_id=? AND i.institution_key='order-crysknife'",[CRYSKNIFE_COMPANY_ID]);
|
||||
if(!row)throw Error('Protected Order deed requires review');
|
||||
const parent=await entityParent(tx,'company',CRYSKNIFE_COMPANY_ID);
|
||||
if(parent&&(parent.parent_kind!=='politics'||parent.parent_id!=='emperor'))throw Error('Protected Order parent changed; review required');
|
||||
};
|
||||
const issued=await keys.issue({accountId:STAFF_OFFICE_ACCOUNT_ID,keyType:'staff-parent-link',scopeId:SCOPE,authorize:verify});
|
||||
const key=(await keys.list(STAFF_OFFICE_ACCOUNT_ID)).find(k=>k.id===issued.id);
|
||||
if(key?.status==='used')return {state:'linked',replayed:true};
|
||||
if(key?.status!=='available')throw Error('Protected Order parent instrument unavailable');
|
||||
return keys.consume({accountId:STAFF_OFFICE_ACCOUNT_ID,keyType:'staff-parent-link',code:key.code,targetId:CRYSKNIFE_COMPANY_ID,requestId:REQUEST_ID,apply:async(tx,{keyId,scopeId})=>{
|
||||
if(scopeId!==SCOPE)throw Error('Protected Order instrument scope mismatch');
|
||||
await verify(tx);
|
||||
await setEntityParent(tx,{childKind:'company',childId:CRYSKNIFE_COMPANY_ID,parentKind:'politics',parentId:'emperor',origin:'staff',keyId,requestId:REQUEST_ID,actorId:STAFF_OFFICE_ACCOUNT_ID,now:clock()});
|
||||
}});
|
||||
}
|
||||
|
|
@ -5,7 +5,7 @@ function rootOf(rows,id){const map=new Map(rows.map(r=>[r.id,r])),seen=new Set()
|
|||
// Common ownership is an alliance projection, never a bank mandate or rank.
|
||||
export function projectOwnershipNetwork({clans,companies,owner,offices=[],layout=null}){
|
||||
const person='person:'+owner.username,nodes=[{id:person,kind:'person',name:owner.display_name||owner.username,url:'https://www.dunerealms.com/people/'+encodeURIComponent(owner.username),image:'https://cp.dunerealms.com/api/avatars/face/roster/'+encodeURIComponent(owner.username)}],edges=[];
|
||||
for(const [kind,rows]of [['clan',clans],['company',companies]])for(const row of rows){const root=rootOf(rows,row.id);if(root.owner_id!==owner.id)continue;const id=kind+':'+row.id;nodes.push({id,kind,name:row.name,url:'https://www.dunerealms.com/'+(kind==='clan'?'clans/':'companies/')+encodeURIComponent(kind==='clan'?row.tag:row.slug),image:'https://cp.dunerealms.com/api/'+(kind==='clan'?'clan-emblems/':'company-emblems/')+encodeURIComponent(row.id)});edges.push({from:row.parent_id?kind+':'+row.parent_id:person,to:id,kind:row.parent_id?'subsidiary':'ownership'});}
|
||||
for(const [kind,rows]of [['clan',clans],['company',companies]])for(const row of rows){const root=rootOf(rows,row.id);const office= root.parent_office&&offices.find(o=>o.key===root.parent_office&&o.holder?.username.toLowerCase()===owner.username.toLowerCase());if(root.owner_id!==owner.id&&!office)continue;const id=kind+':'+row.id;nodes.push({id,kind,name:row.name,url:'https://www.dunerealms.com/'+(kind==='clan'?'clans/':'companies/')+encodeURIComponent(kind==='clan'?row.tag:row.slug),image:'https://cp.dunerealms.com/api/'+(kind==='clan'?'clan-emblems/':'company-emblems/')+encodeURIComponent(row.id)});edges.push({from:row.parent_id?kind+':'+row.parent_id:office?'office:'+office.key:person,to:id,kind:row.parent_id?'subsidiary':'ownership'});}
|
||||
for(const office of offices.filter(o=>o.holder?.username.toLowerCase()===owner.username.toLowerCase())){const id='office:'+office.key;nodes.push({id,kind:'office',name:office.name,url:'https://www.dunerealms.com/political-houses/'+office.key,image:'https://www.dunerealms.com/assets/seals/'+office.art,expiresAt:office.term.expiresAt});edges.push({from:person,to:id,kind:'office-term'});}
|
||||
const known=new Set(nodes.map(n=>n.id)),positions={};for(const [key,value]of Object.entries(layout||{}))if(known.has(key)&&Number.isInteger(value.column)&&value.column>=0&&value.column<=2&&Number.isInteger(value.row)&&value.row>=0&&value.row<=100)positions[key]=value;
|
||||
return {owner:owner.username,nodes,edges,positions};
|
||||
|
|
@ -13,10 +13,11 @@ export function projectOwnershipNetwork({clans,companies,owner,offices=[],layout
|
|||
export async function readOrganisationNetwork(central,{kind,id,ownerId=null,clock=Date.now}){
|
||||
if(kind&&!validKind(kind))throw Error('Choose an ownership network');
|
||||
const clans=(await central.prepare('SELECT c.id,c.name,c.tag,c.owner_id,d.parent_clan_id AS parent_id FROM site_clans c LEFT JOIN site_clan_deeds d ON d.clan_id=c.id WHERE c.active=1 ORDER BY c.id LIMIT 1001').all()).results;
|
||||
const companies=(await central.prepare("SELECT c.id,c.name,c.slug,d.owner_account_id AS owner_id,r.parent_company_id AS parent_id FROM corrino_companies c JOIN site_company_deeds d ON d.company_id=c.id LEFT JOIN site_company_relations r ON r.company_id=c.id WHERE c.status='active' ORDER BY c.id LIMIT 1001").all()).results;
|
||||
const companies=(await central.prepare("SELECT c.id,c.name,c.slug,d.owner_account_id AS owner_id,r.parent_company_id AS parent_id,e.parent_id AS parent_office FROM corrino_companies c JOIN site_company_deeds d ON d.company_id=c.id LEFT JOIN site_company_relations r ON r.company_id=c.id LEFT JOIN jarvis_entity_relations e ON e.child_kind='company' AND e.child_id=c.id AND e.parent_kind='politics' WHERE c.status='active' ORDER BY c.id LIMIT 1001").all()).results;
|
||||
if(clans.length>1000||companies.length>1000)throw Error('Ownership network needs paginated reconciliation');
|
||||
const selected=kind?rootOf(kind==='clan'?clans:companies,id):null,owner=await central.prepare("SELECT id,username,display_name FROM site_accounts WHERE id=? AND password_hash NOT LIKE '!%'").bind(ownerId||selected?.owner_id||'').first();if(!owner)throw Error('Ownership network unavailable');
|
||||
const layout=await central.prepare('SELECT revision,positions_json FROM site_network_layouts WHERE owner_id=?').bind(owner.id).first(),network=projectOwnershipNetwork({clans,companies,owner,offices:await readGovernmentOffices(central,clock()),layout:layout?JSON.parse(layout.positions_json):null});
|
||||
const offices=await readGovernmentOffices(central,clock());
|
||||
const selected=kind?rootOf(kind==='clan'?clans:companies,id):null,officeHolder=selected?.parent_office&&offices.find(o=>o.key===selected.parent_office)?.holder,officeOwner=officeHolder?await central.prepare('SELECT id FROM site_accounts WHERE LOWER(username)=LOWER(?)').bind(officeHolder.username).first():null,owner=await central.prepare("SELECT id,username,display_name FROM site_accounts WHERE id=? AND password_hash NOT LIKE '!%'").bind(ownerId||officeOwner?.id||selected?.owner_id||'').first();if(!owner)throw Error('Ownership network unavailable');
|
||||
const layout=await central.prepare('SELECT revision,positions_json FROM site_network_layouts WHERE owner_id=?').bind(owner.id).first(),network=projectOwnershipNetwork({clans,companies,owner,offices,layout:layout?JSON.parse(layout.positions_json):null});
|
||||
return {network:{...network,revision:Number(layout?.revision||0)},ownerId:owner.id};
|
||||
}
|
||||
export function createNetworkLayoutAPI(central,{clock=Date.now,authenticate=r=>identity(r,central),origins=['https://cp.dunerealms.com','https://www.dunerealms.com']}={}){
|
||||
|
|
|
|||
Loading…
Reference in a new issue

Staff