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

55 lines
6.5 KiB
JavaScript

import {ORGANISATION,assignmentLabel,validStaffRole} from './organisation-model.mjs';
import {EXECUTIVE_TITLES} from './organisation-appointments.mjs';
import {OFFICIAL_ACCOUNT} from './official-account.mjs';
import {organisationPersonalSeal,organisationFunctionSeals} from './organisation-seals.mjs';
import {badgesFor} from './community-badges.mjs';
import {AWARD_LABELS,AWARD_FILES} from './recognition.mjs';
import {staffClosureStats,statsForAccount,departmentAwardEarned} from './staff-closure-stats.mjs';
const weights={owner_ceo:100,executive:90,department_head:80,branch_lead:70,officer:50};
export function projectPublicOrganisation(rows,orderRows=[],officialRows=[]){
const people=new Map();
for(const row of rows){
if(!validStaffRole(row.scope_type,row.scope_key,row.role_key)||row.game_link_exempt)continue;
let person=people.get(row.username);
if(!person){
const display=row.display_name||row.username;
const bedrock=typeof row.source_name==='string'&&row.source_name.startsWith('.')&&row.source_uuid===row.minecraft_uuid;
person={username:row.username,displayName:bedrock?display.replace(/^bedrock_/i,'').replace(/^\./,''):display,bio:row.bio||'',profile:'/people/'+encodeURIComponent(row.username),avatar:'https://cp.dunerealms.com/api/avatars/face/roster/'+encodeURIComponent(row.username),functions:[],seals:[],priority:0};people.set(row.username,person);
}
const f=assignmentLabel(row);
if(!person.functions.some(x=>x.scopeType===f.scopeType&&x.scopeKey===f.scopeKey&&x.role===f.role))person.functions.push({scopeType:f.scopeType,scopeKey:f.scopeKey,scopeLabel:f.scopeLabel,role:f.role,roleLabel:f.roleLabel});
person.priority=Math.max(person.priority,weights[row.role_key]||0);
if(row.scope_type==='organisation')person.title=EXECUTIVE_TITLES[row.username]||f.roleLabel;
}
const staff=[...people.values()].sort((a,b)=>b.priority-a.priority||a.displayName.localeCompare(b.displayName));
for(const p of staff){p.title||=p.functions.slice().sort((a,b)=>(weights[b.role]||0)-(weights[a.role]||0))[0].roleLabel;const personal=organisationPersonalSeal(p.username,p.functions.map(f=>'org_'+f.role));p.personalSeal=personal?{file:personal.split('/').pop(),label:p.displayName+' · Personal seal'}:null;p.functionSeals=organisationFunctionSeals(p.functions);delete p.priority;}
const holders=(type,key,role)=>staff.filter(p=>p.functions.some(f=>f.scopeType===type&&f.scopeKey===key&&f.role===role)).map(p=>({username:p.username,displayName:p.displayName,title:p.title}));
const organisation=ORGANISATION.map(d=>({key:d.key,label:d.label,heads:holders('department',d.key,'department_head'),branches:d.branches.map(b=>({...b,leads:holders('branch',b.key,'branch_lead'),members:holders('branch',b.key,'officer')}))}));
const order=[...new Map(orderRows.map(r=>[r.username,{username:r.username,displayName:r.display_name||r.username,citation:r.citation||'',profile:r.slug?'/memorial/'+encodeURIComponent(r.slug):'/people/'+encodeURIComponent(r.username)}])).values()];
const officialAccounts=officialRows.filter(r=>r.id===OFFICIAL_ACCOUNT.id&&r.username===OFFICIAL_ACCOUNT.username&&r.role==='official_account').map(r=>({username:r.username,displayName:'DuneRealms',title:'Official DuneRealms Account',bio:r.bio||'Official DuneRealms announcements and correspondence. Verify this profile through the staff directory.',profile:'/people/'+r.username,avatar:'https://cp.dunerealms.com/api/avatars/face/roster/'+r.username,official:true,functions:[],seals:[]}));
return {model:'organisation-v2',publishedOnly:true,executives:staff.filter(p=>p.functions.some(f=>f.scopeType==='organisation')).map(p=>({username:p.username,displayName:p.displayName,title:p.title})),organisation,staff,order,officialAccounts};
}
// A manual staff appointment is public by default unless explicitly hidden.
// Imported accounts still require publication; explicit visible=0 always wins.
export async function readPublicStaffAssignments(reader,{now=Date.now()}={}){
return reader.query("SELECT a.username,a.display_name,f.scope_type,f.scope_key,f.role_key,p.bio,g.minecraft_uuid,s.source_uuid,s.source_name,pol.game_link_exempt FROM org_assignments f JOIN site_accounts a ON a.id=f.user_id LEFT JOIN staff_roster_publications r ON r.account_id=a.id LEFT JOIN site_profiles p ON p.user_id=a.id LEFT JOIN site_game_links g ON g.user_id=a.id LEFT JOIN staff_source_members s ON s.user_id=a.id LEFT JOIN site_account_policies pol ON pol.user_id=a.id WHERE (r.visible=1 OR (r.account_id IS NULL AND f.source_kind='manual')) AND a.role NOT IN ('system_account','official_account') AND a.password_hash NOT LIKE '!%' AND f.status='active' AND f.starts_at<=? AND (f.ends_at IS NULL OR f.ends_at>?) ORDER BY a.username,f.scope_type,f.scope_key LIMIT 2000",[now,now]);
}
export async function readPublicOrganisation(db){
return db.readSnapshot(async reader=>{
const rows=await readPublicStaffAssignments(reader);
const order=await reader.query("SELECT a.username,a.display_name,h.citation,m.slug FROM site_accounts a LEFT JOIN site_honours h ON h.user_id=a.id AND h.rank_key='crysknife' LEFT JOIN site_memorials m ON m.user_id=a.id WHERE (a.password_hash NOT LIKE '!%' OR a.password_hash='!memorial') AND a.role<>'official_account' AND (a.role='crysknife' OR EXISTS (SELECT 1 FROM cp_account_ranks r WHERE r.user_id=a.id AND r.rank_key='crysknife')) ORDER BY a.username LIMIT 250");
const official=await reader.query("SELECT a.id,a.username,a.role,p.bio FROM site_accounts a JOIN site_game_links l ON l.user_id=a.id LEFT JOIN site_profiles p ON p.user_id=a.id WHERE a.id=? AND a.username=? AND a.role='official_account' AND a.password_hash NOT LIKE '!%' AND l.minecraft_uuid=?",[OFFICIAL_ACCOUNT.id,OFFICIAL_ACCOUNT.username,OFFICIAL_ACCOUNT.minecraftUuid]);
const result=projectPublicOrganisation(rows,order,official);
// Public recognition is separate from appointments and never includes private audit data.
const stats=await staffClosureStats(reader);
for(const person of result.staff){
const account=await reader.query('SELECT id FROM site_accounts WHERE username=?',[person.username]);
if(!account[0])continue;
const id=account[0].id;
for(const key of await badgesFor(reader,id)){const file=AWARD_FILES[key]||key+'-150.png';if(/^[a-z0-9_-]+\.png$/.test(file))person.seals.push({key,label:AWARD_LABELS[key]||key.replaceAll('-',' '),file});}
const closed=statsForAccount(stats,id);
for(const key of ['support','ban-appeals','forum-support','legal','sales','network-staff','dev'])if(departmentAwardEarned(closed,key))person.seals.push({key:'closure-'+key,label:key+' · Service award',file:'award-'+key+'-150.png'});
}
return result;
});
}