web/website/node/jarvis-organisation-context.mjs
2026-11-27 23:43:05 +00:00

26 lines
3.6 KiB
JavaScript

import {clanRecruitmentQuote} from './jarvis-recruitment-policy.mjs';
import {readCompanyDeed,companyDeedView} from './company-deeds.mjs';
import {PUBLIC_OFFICES} from './public-government.mjs';
import {entityId} from './postbank-authority.mjs';
import {JarvisError} from './jarvis-listings.mjs';
// A verified workspace is not itself a charter, a recruitment quota or an
// appointment. Keep these sources separate until their domain writers exist.
export function createJarvisOrganisationContext(db,{clock=Date.now}={}){
return async(user,{kind,id})=>{
if(kind==='company'){
const company=await db.prepare("SELECT id,slug,name,founded_by,ceo_account_id,status FROM corrino_companies WHERE id=? AND status='active'").bind(id).first();
if(db.companyDeedsEnabled){let view;try{view=companyDeedView(await readCompanyDeed(db,id),user.id);}catch(error){if(error.status)throw new JarvisError(error.message,error.status);throw error;}if(company?.status!=='active')throw new JarvisError('An active company is required.',403);return {...view,worldName:'Company register',canView:true,canPublish:view.canManage,canReview:view.canManage,payerEntityId:entityId('company',id),recruitment:clanRecruitmentQuote({charterKind:view.charterKind,memberCount:view.memberCount}),membershipSource:'company-deed',publishingReason:'Publish recruitment against the current company deed and available membership places.',applicationReason:'Applications are reviewed through the shared company membership service.',links:{settings:'/company-cp?company='+encodeURIComponent(id)+'#company/settings'}};}
if(!company||![company.founded_by,company.ceo_account_id].includes(user.id))throw new JarvisError('Company management authority required.',403);
return {name:company.name,canView:true,canPublish:false,canReview:false,charterKind:null,payerEntityId:entityId('company',company.id),recruitment:null,membershipSource:'company-register',publishingReason:'Company recruitment is not available yet. You can manage your company identity and welcome message in Settings.',applicationReason:'Company membership applications are not connected yet.',links:{settings:'/company-cp?company='+encodeURIComponent(id)+'#company/settings',profile:'https://www.dunerealms.com/companies/'+encodeURIComponent(company.slug)}};
}
if(kind==='politics'){
const office=PUBLIC_OFFICES.find(o=>o.key===id);if(!office)throw new JarvisError('Political House not found.',404);
const now=clock(),kinds=office.kind==='bene'?['bene','house']:office.kind==='guild'?['guild','court']:[office.kind];
const {results=[]}=await db.prepare("SELECT id,user_id,kind,starts_at,expires_at FROM political_office_terms WHERE faction=? AND office=? AND outcome='winner' AND starts_at<=? AND expires_at>?").bind(office.faction,office.office,now,now).all(),terms=results.filter(t=>kinds.includes(t.kind));
if(terms.length!==1||terms[0].user_id!==user.id)throw new JarvisError('A current unambiguous office deed is required.',403);
return {name:office.scope,canView:true,canPublish:false,canReview:false,charterKind:'royal',payerEntityId:null,recruitment:null,membershipSource:'active-office-deed',officeDeed:{id:terms[0].id,startsAt:Number(terms[0].starts_at),expiresAt:Number(terms[0].expires_at)},publishingReason:'Political House membership follows current office deeds. Recruitment advertisements cannot appoint officeholders.',applicationReason:'Candidacy and election decisions use the existing election service.',links:{settings:'/politics-cp?office='+encodeURIComponent(id)+'#household/settings',elections:'/politics-cp?office='+encodeURIComponent(id)+'#household/campaigns'}};
}
throw new JarvisError('Choose an organisation.',400);
};
}