web/website/node/jarvis-management.mjs

33 lines
3.9 KiB
JavaScript

import {identity} from './auth.mjs';
import {can} from './permissions.mjs';
import {KEY_TYPES} from './organisation-key-policy.mjs';
import {currentCodexEdition} from './organisation-key-service.mjs';
const reply=(body,status=200)=>Response.json(body,{status,headers:{'Cache-Control':'private, no-store','X-Content-Type-Options':'nosniff'}});
const SECTIONS={
states:{store:'central',sql:'SELECT e.key_id AS id,e.action,e.target_uuid AS target_id,e.source,e.scope,e.occurred_at,e.payload_json,k.revoked_at FROM jarvis_entity_state_events e JOIN organisation_keys k ON k.id=e.key_id ORDER BY e.occurred_at DESC,e.key_id DESC'},
keys:{store:'central',sql:'SELECT k.id,k.key_type AS type,a.username AS recipient,k.created_at,k.expires_at,k.used_at,k.revoked_at FROM organisation_keys k LEFT JOIN site_accounts a ON a.id=k.account_id ORDER BY k.created_at DESC,k.id DESC'},
history:{store:'central',sql:'SELECT e.id,e.key_id AS instrument,e.event_type AS action,a.username AS actor,e.target_id,e.created_at FROM organisation_key_events e LEFT JOIN site_accounts a ON a.id=e.account_id ORDER BY e.created_at DESC,e.id DESC'},
listings:{store:'bank',sql:'SELECT id,title,definition_id AS type,organisation_name AS owner,state,created_at FROM jarvis_listings ORDER BY created_at DESC,id DESC'},
delivery:{store:'bank',sql:'SELECT p.id,l.title AS listing,p.channel,p.state,p.attempts,p.next_attempt,p.created_at FROM jarvis_promotion_jobs p JOIN jarvis_listings l ON l.id=p.listing_id ORDER BY p.created_at DESC,p.id DESC'},
deeds:{store:'central',sql:'SELECT t.id,t.office AS type,t.faction,a.username AS holder,t.starts_at,t.expires_at,t.outcome AS state FROM political_office_terms t LEFT JOIN site_accounts a ON a.id=t.user_id ORDER BY t.starts_at DESC,t.id DESC'}
};
// Explicit projections intentionally omit codes, sealed material, signing keys,
// private applications, payment accounts and full audit payloads.
export function createJarvisManagement(central,bank,settings={},deps={}){
const authenticate=deps.authenticate||(r=>identity(r,central)),authorize=deps.authorize||(u=>can(central,u,'cp.items.manage'));
return async request=>{
if(request.method!=='GET')return reply({message:'Read-only J.A.R.V.I.S. register.'},405);
const user=await authenticate(request);if(!user)return reply({message:'Sign in first.'},401);
if(!await authorize(user))return reply({message:'J.A.R.V.I.S. management access required.'},403);
const url=new URL(request.url),section=url.searchParams.get('section')||'keys',page=Number(url.searchParams.get('page')||1);
if([...url.searchParams.keys()].some(k=>!['section','page'].includes(k))||url.searchParams.getAll('section').length>1||url.searchParams.getAll('page').length>1||!Number.isInteger(page)||page<1||page>10000)return reply({message:'Choose a valid register page.'},400);
if(section==='seasons')return reply({section,edition:await (deps.edition||(()=>currentCodexEdition(central,settings)))()});
const definition=SECTIONS[section];if(!definition)return reply({message:'Register not found.'},404);
const db=definition.store==='central'?central:bank;if(!db)return reply({message:'Register temporarily unavailable.'},503);
try{
const args=[26,(page-1)*25],sql=definition.sql+' LIMIT ? OFFSET ?',rows=definition.store==='central'?(await db.prepare(sql).bind(...args).all()).results:await db.query(sql,args);
const records=rows.slice(0,25).map(row=>section==='keys'?{...row,family:KEY_TYPES[row.type]?.family||'unclassified',operation:KEY_TYPES[row.type]?.operation||'restricted',playerRedeemable:KEY_TYPES[row.type]?.playerRedeemable===true}:section==='states'?(({payload_json,...metadata})=>({...metadata,details:JSON.parse(payload_json)}))(row):row);
return reply({section,page,hasMore:rows.length>25,records});
}catch(error){if(/doesn't exist|no such table|unknown column/i.test(error.message||''))return reply({message:'This register is not installed yet.'},503);throw error;}
};
}