forked from unimatrix/web
Connect isolated Company and political office contexts to J.A.R.V.I.S.
This commit is contained in:
parent
48cb3d839f
commit
9b72de835d
3 changed files with 83 additions and 5 deletions
|
|
@ -1,3 +1,4 @@
|
|||
import {createJarvisOrganisationContext} from './jarvis-organisation-context.mjs';
|
||||
import {identity} from './auth.mjs';import {accessFor} from './permissions.mjs';
|
||||
import {clanDeedAuthority} from './clan-deeds.mjs';import {CLAN_HOMEWORLDS} from '../control-panel/clans.mjs';
|
||||
import {clanRecruitmentQuote} from './jarvis-recruitment-policy.mjs';
|
||||
|
|
@ -8,7 +9,8 @@ const uuid=v=>typeof v==='string'&&/^[a-f0-9]{8}(?:-[a-f0-9]{4}){3}-[a-f0-9]{12}
|
|||
export function createJarvisServices(central,bank,settings={},deps={}){
|
||||
const authenticate=deps.authenticate||((r)=>identity(r,central)),access=deps.access||((u)=>accessFor(central,u)),origin=settings.CP_ORIGIN||'https://cp.dunerealms.com',clock=deps.clock||Date.now,edition=deps.edition||(()=>currentCodexEdition(central,settings));
|
||||
const bankAuthority=deps.bankAuthority||createPostbankAuthority(central,{bank}),payments=bank?createJarvisPayments(bank,bankAuthority,{clock}):null;
|
||||
const authority=deps.authority||(async(user,{kind,id})=>{if(kind!=='clan'||!uuid(id))throw new JarvisError('This organisation recruitment adapter is not available.',409);
|
||||
const organisationContext=createJarvisOrganisationContext(central,{clock});
|
||||
const authority=deps.authority||(async(user,{kind,id})=>{if(kind!=='clan')return organisationContext(user,{kind,id});if(!uuid(id))throw new JarvisError('Choose a clan.');
|
||||
const rights=await clanDeedAuthority(central,user.id,id),clan=rights.deed?.clan;if(!clan)return {canPublish:false};
|
||||
const member=await central.prepare('SELECT clan_role FROM site_clan_members WHERE clan_id=? AND user_id=?').bind(id,user.id).first(),count=await central.prepare('SELECT COUNT(*) total FROM site_clan_members WHERE clan_id=?').bind(id).first(),charterKind=rights.deed.kind;
|
||||
return {name:clan.name,worldName:CLAN_HOMEWORLDS.find(w=>w.key===clan.home_world)?.name||'',charterKind,payerEntityId:entityId('clan',id),canPublish:rights.canManage&&!clan.institution_key,canReview:!clan.institution_key&&(rights.canManage||['owner','officer'].includes(member?.clan_role)),recruitment:clanRecruitmentQuote({charterKind,memberCount:Number(count.total),protectedInstitution:!!clan.institution_key})};
|
||||
|
|
@ -33,7 +35,7 @@ export function createJarvisServices(central,bank,settings={},deps={}){
|
|||
if(request.method==='GET'&&path==='/api/jarvis/context'){
|
||||
if([...url.searchParams.keys()].some(k=>!['kind','id'].includes(k))||url.searchParams.getAll('kind').length>1||url.searchParams.getAll('id').length>1)throw new JarvisError('Choose one organisation.');
|
||||
const kind=url.searchParams.get('kind')||'clan';let id=url.searchParams.get('id');if(!id&&kind==='clan')id=(await central.prepare('SELECT clan_id FROM site_clan_members WHERE user_id=?').bind(user.id).first())?.clan_id;
|
||||
await closeFilledJarvisListings(central,bank,{clock});const data=await listings.context(user,{kind,id});const clan=await central.prepare('SELECT version FROM site_clans WHERE id=? AND active=1').bind(id).first();
|
||||
await closeFilledJarvisListings(central,bank,{clock});const data=await listings.context(user,{kind,id});if(kind!=='clan')return reply({...data,applications:[],accounts:[]});const clan=await central.prepare('SELECT version FROM site_clans WHERE id=? AND active=1').bind(id).first();
|
||||
const {results:applications}=await central.prepare('SELECT a.username,j.listing_id,j.message,r.status,j.created_at FROM site_jarvis_clan_applications j JOIN site_clan_requests r ON r.clan_id=j.clan_id AND r.user_id=j.user_id JOIN site_accounts a ON a.id=j.user_id WHERE j.clan_id=? ORDER BY j.created_at DESC LIMIT 100').bind(id).all();
|
||||
const accounts=data.canPublish?(await new PostbankPayments(bank,bankAuthority,{clock}).accounts(user)).filter(a=>a.entityId===data.payerEntityId&&a.canDebit):[];return reply({...data,clanVersion:Number(clan?.version),applications,accounts});
|
||||
}
|
||||
|
|
|
|||
|
|
@ -14,7 +14,7 @@ export function publicJarvisListing(row){return {id:row.id,organisationKind:row.
|
|||
export function createJarvisListings(store,{authority,reserveFee,edition,clock=Date.now,promotionChannels=[]}){
|
||||
if(![authority,reserveFee,edition].every(f=>typeof f==='function'))throw TypeError('J.A.R.V.I.S. adapters required');
|
||||
const validScope=(kind,id)=>['clan','company','politics'].includes(kind)&&typeof id==='string'&&/^[a-z0-9_-]{1,36}$/i.test(id);
|
||||
const scope=async(actor,kind,id,review=false)=>{if(!actor?.id||!validScope(kind,id))throw new JarvisError('Choose an organisation.');const current=await authority(actor,{kind,id});if(!(current?.canPublish||review&¤t?.canReview))throw new JarvisError('Current organisation deed authority is required.',403);return current;};
|
||||
const scope=async(actor,kind,id,review=false)=>{if(!actor?.id||!validScope(kind,id))throw new JarvisError('Choose an organisation.');const current=await authority(actor,{kind,id});if(!(current?.canPublish||review&&(current?.canReview||current?.canView)))throw new JarvisError('Current organisation deed authority is required.',403);return current;};
|
||||
return Object.freeze({
|
||||
async context(actor,{kind,id}){const current=await scope(actor,kind,id,true),rows=await store.query("SELECT * FROM jarvis_listings WHERE organisation_kind=? AND organisation_id=? ORDER BY (state='open') DESC,created_at DESC LIMIT 100",[kind,id]),credits=await store.first('SELECT promotion_credits FROM jarvis_organisation_state WHERE organisation_kind=? AND organisation_id=?',[kind,id]);const jobs=await store.query('SELECT p.listing_id,p.channel,p.state FROM jarvis_promotion_jobs p JOIN jarvis_listings l ON l.id=p.listing_id WHERE l.organisation_kind=? AND l.organisation_id=? ORDER BY p.created_at DESC LIMIT 100',[kind,id]);return {...current,kind,id,promotions:jobs.map(p=>({listingId:p.listing_id,channel:p.channel,state:p.state})),credits:current.charterKind==='basic'?Number(credits?.promotion_credits??5):null,promotionChannels,listings:rows.map(row=>({...publicJarvisListing(row),feeMinor:Number(row.fee_minor)}))};},
|
||||
async browse(){const rows=await store.query("SELECT * FROM jarvis_listings WHERE state='open' ORDER BY created_at DESC,id LIMIT 500");return rows.map(publicJarvisListing);},
|
||||
|
|
|
|||
23
website/node/jarvis-organisation-context.mjs
Normal file
23
website/node/jarvis-organisation-context.mjs
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
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(!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:'protected',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);
|
||||
};
|
||||
}
|
||||
Loading…
Reference in a new issue

Staff