forked from unimatrix/web
32 lines
3.8 KiB
JavaScript
32 lines
3.8 KiB
JavaScript
import {ORGANISATION,assignmentLabel,validStaffRole} from './organisation-model.mjs';
|
|
import {EXECUTIVE_TITLES} from './organisation-appointments.mjs';
|
|
const weights={owner_ceo:100,executive:90,department_head:80,branch_lead:70,officer:50};
|
|
export function projectPublicOrganisation(rows,orderRows=[]){
|
|
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/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;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()];
|
|
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};
|
|
}
|
|
export async function readPublicOrganisation(db){
|
|
return db.readSnapshot(async reader=>{
|
|
const rows=await 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 staff_roster_publications r JOIN site_accounts a ON a.id=r.account_id JOIN org_assignments f ON f.user_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 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",[Date.now(),Date.now()]);
|
|
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");
|
|
return projectPublicOrganisation(rows,order);
|
|
});
|
|
}
|

Staff