forked from unimatrix/web
89 lines
5.4 KiB
JavaScript
89 lines
5.4 KiB
JavaScript
import {NATIVE_SYNC_SCHEMA} from './native-sync-schema.mjs';
|
|
// Central assignment vocabulary. This catalogue never grants permissions by itself.
|
|
export const ORGANISATION = [
|
|
{key:'community_operations',label:'Community Operations',branches:[
|
|
{key:'customer_support',label:'Customer Support'},
|
|
{key:'sales_accounts',label:'Sales & Account Services'},
|
|
{key:'content_creative',label:'Content & Creative'},
|
|
{key:'news_media',label:'News & Media'}]},
|
|
{key:'dunerealms_operations',label:'DuneRealms Operations',branches:[
|
|
{key:'trust_safety',label:'Trust & Safety'},
|
|
{key:'it_operations',label:'IT Operations'},
|
|
{key:'engineering',label:'Engineering'},
|
|
{key:'people_operations',label:'People Operations'},
|
|
{key:'legal_compliance',label:'Legal & Compliance'}]}
|
|
];
|
|
export const STAFF_SCOPE_TYPES = ['organisation','department','branch'];
|
|
export const ASSIGNMENT_ROLES = {
|
|
organisation:[{key:'owner_ceo',label:'Owner & CEO'},{key:'executive',label:'Executive'}],
|
|
department:[{key:'department_head',label:'Department Head'}],
|
|
branch:[{key:'branch_lead',label:'Branch Lead'},{key:'officer',label:'Officer'}],
|
|
clan:[{key:'leader',label:'Clan Leader'},{key:'officer',label:'Clan Officer'},{key:'member',label:'Clan Member'}],
|
|
house:[{key:'member',label:'House Member'},{key:'leader',label:'House Leader'},{key:'mentat',label:'Mentat'}],
|
|
office:[{key:'emperor',label:'Emperor'},{key:'water_rights',label:'Water Rights'},{key:'spice_rights',label:'Spice Rights'}],
|
|
company:[{key:'owner',label:'Company Owner'},{key:'director',label:'Company Director'},{key:'delegate',label:'Company Delegate'}],
|
|
order:[{key:'member',label:'Order Member'}],
|
|
service:[{key:'officeholder',label:'Service Officeholder'}]
|
|
};
|
|
export function assignmentLabel(row){
|
|
const scopeType=row.scope_type??row.scopeType,scopeKey=row.scope_key??row.scopeKey,roleKey=row.role_key??row.roleKey;
|
|
const scopes=ORGANISATION.flatMap(d=>[d,...d.branches]);
|
|
return {scopeType,scopeKey,scopeLabel:scopeType==='organisation'?'DuneRealms':scopes.find(s=>s.key===scopeKey)?.label||scopeKey,
|
|
role:roleKey,roleLabel:ASSIGNMENT_ROLES[scopeType]?.find(r=>r.key===roleKey)?.label||roleKey,
|
|
source:row.source_kind??row.source,startsAt:row.starts_at??row.startsAt,endsAt:row.ends_at??row.endsAt,
|
|
id:row.id,version:Number(row.version||1)};
|
|
}
|
|
export const organisationSchema = [
|
|
`CREATE TABLE IF NOT EXISTS org_control_principals (
|
|
user_id CHAR(36) PRIMARY KEY, principal_kind VARCHAR(24) NOT NULL,
|
|
created_by CHAR(36) NOT NULL, created_at BIGINT NOT NULL,
|
|
FOREIGN KEY(user_id) REFERENCES site_accounts(id)
|
|
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4`,
|
|
`CREATE TABLE IF NOT EXISTS org_scope_versions (
|
|
scope_type VARCHAR(24) NOT NULL, scope_key VARCHAR(80) NOT NULL,
|
|
version INT NOT NULL DEFAULT 1, PRIMARY KEY(scope_type,scope_key)
|
|
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4`,
|
|
`CREATE TABLE IF NOT EXISTS org_role_policies (
|
|
scope_type VARCHAR(24) NOT NULL, scope_key VARCHAR(80) NOT NULL,
|
|
role_key VARCHAR(40) NOT NULL, permissions_json TEXT NOT NULL,
|
|
version INT NOT NULL DEFAULT 1, updated_by CHAR(36) NOT NULL, updated_at BIGINT NOT NULL,
|
|
PRIMARY KEY(scope_type,scope_key,role_key)
|
|
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4`,
|
|
`CREATE TABLE IF NOT EXISTS org_assignment_events (
|
|
id CHAR(36) PRIMARY KEY, user_id CHAR(36) NULL, assignment_id CHAR(36) NULL,
|
|
actor_id CHAR(36) NOT NULL, action VARCHAR(24) NOT NULL,
|
|
scope_type VARCHAR(24) NOT NULL, scope_key VARCHAR(80) NOT NULL,
|
|
reason VARCHAR(500) NOT NULL, before_json TEXT NULL, after_json TEXT NOT NULL,
|
|
created_at BIGINT NOT NULL,
|
|
INDEX org_events_target(user_id,created_at), INDEX org_events_scope(scope_type,scope_key,created_at)
|
|
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4`,
|
|
`CREATE TABLE IF NOT EXISTS org_assignments (
|
|
id CHAR(36) PRIMARY KEY, user_id CHAR(36) NOT NULL,
|
|
scope_type VARCHAR(24) NOT NULL, scope_key VARCHAR(80) NOT NULL, role_key VARCHAR(40) NOT NULL,
|
|
source_kind VARCHAR(24) NOT NULL, source_ref VARCHAR(160) NULL,
|
|
status VARCHAR(16) NOT NULL DEFAULT 'active', starts_at BIGINT NOT NULL, ends_at BIGINT NULL,
|
|
created_by CHAR(36) NOT NULL, created_at BIGINT NOT NULL, version INT NOT NULL DEFAULT 1,
|
|
INDEX org_user_status(user_id,status), INDEX org_scope_role(scope_type,scope_key,role_key,status),
|
|
FOREIGN KEY(user_id) REFERENCES site_accounts(id)
|
|
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4`,
|
|
`CREATE TABLE IF NOT EXISTS staff_source_members (
|
|
user_id CHAR(36) NOT NULL, source_key VARCHAR(40) NOT NULL, source_name VARCHAR(80) NOT NULL,
|
|
source_uuid CHAR(36) NOT NULL, source_ranks_json TEXT NOT NULL, source_ref VARCHAR(255) NOT NULL,
|
|
imported_at BIGINT NOT NULL, imported_by CHAR(36) NOT NULL,
|
|
PRIMARY KEY(user_id,source_key), UNIQUE KEY staff_source_uuid(source_key,source_uuid),
|
|
FOREIGN KEY(user_id) REFERENCES site_accounts(id)
|
|
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4`
|
|
];
|
|
export async function initializeOrganisation(db){
|
|
for(const sql of [...organisationSchema,NATIVE_SYNC_SCHEMA])await db.prepare(sql).run();
|
|
// No automatic appointments, account rewrites, rank resets or game grants.
|
|
}
|
|
|
|
export function staffScopes(){
|
|
return [{type:'organisation',key:'dunerealms',label:'DuneRealms'},...ORGANISATION.flatMap(d=>[
|
|
{type:'department',key:d.key,label:d.label},...d.branches.map(b=>({type:'branch',key:b.key,label:b.label,department:d.key}))
|
|
])];
|
|
}
|
|
export function validStaffRole(type,key,role){
|
|
return staffScopes().some(s=>s.type===type&&s.key===key)&&ASSIGNMENT_ROLES[type]?.some(r=>r.key===role);
|
|
}
|

Staff