web/website/node/public-staff-roster.mjs

11 lines
3.1 KiB
JavaScript

import {readPublicOrganisation} from './organisation-public-roster.mjs';
// Public roster only includes explicitly published staff. No private staff directory leakage.
import {STAFF_SUBTEAMS} from './staff-bureau-subteams.mjs';
import {STAFF_DEPARTMENTS} from './staff-bureau-policy.mjs';
import {accessFor} from './permissions.mjs';
import {staffClosureStats,statsForAccount,departmentAwardEarned} from './staff-closure-stats.mjs';
import {badgesFor} from './community-badges.mjs';
import {AWARD_LABELS,AWARD_FILES} from './recognition.mjs';
export const PUBLIC_ROSTER_SCHEMA=`CREATE TABLE IF NOT EXISTS staff_roster_publications (account_id CHAR(36) PRIMARY KEY, visible TINYINT(1) NOT NULL DEFAULT 0, subteam_key VARCHAR(40) NULL, updated_at BIGINT NOT NULL DEFAULT 0) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4`;
export async function initializePublicRoster(db){await db.prepare(PUBLIC_ROSTER_SCHEMA).run();}
export function createPublicStaffRoster(db){return async request=>{if(request.method!=='GET')return Response.json({message:'Read-only roster'},{status:405});if(db.organisationAccessEnabled){try{return Response.json(await readPublicOrganisation(db),{headers:{'Cache-Control':'public,max-age=30'}});}catch{return Response.json({message:'Roster temporarily unavailable'}, {status:503,headers:{'Cache-Control':'no-store'}});}}const groups=Object.entries(STAFF_DEPARTMENTS).map(([key,label])=>({key,label,subteams:STAFF_SUBTEAMS[key],staff:[]}));try{const {results=[]}=await db.prepare(`SELECT a.id,a.username,a.display_name,p.department_key,p.staff_level,r.subteam_key FROM staff_roster_publications r JOIN staff_positions p ON p.account_id=r.account_id JOIN site_accounts a ON a.id=p.account_id WHERE r.visible=1 AND p.status='active' AND p.staff_level NOT IN ('player','trainee') ORDER BY p.department_key,p.staff_level DESC,a.username LIMIT 250`).all();const closureStats=await staffClosureStats(db);for(const item of results){const group=groups.find(g=>g.key===item.department_key);if(!group)continue;const subteam=group.subteams.find(t=>t.key===item.subteam_key);const access=await accessFor(db,{id:item.id,username:item.username});const ranks=Array.isArray(access.ranks)?access.ranks:[];const crysknife=ranks.includes('crysknife');const stats=statsForAccount(closureStats,item.id);const seals=[];const earned=await badgesFor(db,item.id);for(const key of earned){const file=AWARD_FILES[key];if(typeof file==='string'&&/^[a-z0-9_-]+\.png$/.test(file))seals.push({key,label:AWARD_LABELS[key]||key,file});}for(const key of Object.keys(stats?.departments||{})){if(departmentAwardEarned(stats,key)&&/^(support|ban-appeals|forum-support|legal|sales|network-staff|dev)$/.test(key))seals.push({key:'closure-'+key,label:key+' Ticket Closure Award',file:'award-'+key+'-150.png'});}group.staff.push({username:item.username,displayName:item.display_name,rank:item.staff_level,department:group.key,subteam:subteam?.name||null,subteamKey:subteam?.key||null,crysknife,seals});}}catch{return Response.json({message:'Roster temporarily unavailable'},{status:503,headers:{'Cache-Control':'no-store'}})}return Response.json({groups,mode:'published',publishedOnly:true},{headers:{'Cache-Control':'public,max-age=60'}})}}