// Pure logic of the staff permission editor, the sessions list and the approvals log // (plan step 2.6e). No React here, so tests/platform.test.mjs can run it with node --test. /** The module a permission key belongs to: its first segment ("ordering.orders.cancel" → "ordering"). */ export const moduleOf = key => key.split('.', 1)[0] /** i18n key of a permission's label: modules:.permissions., nested by the dots. */ export const permLabelKey = key => `modules:${moduleOf(key)}.permissions.${key.slice(moduleOf(key).length + 1)}` /** * The groups the editor shows. * catalog GET /api/waiters/permission-catalog → .modules * modules GET /api/platform/modules → .modules (entitled / effective per module) * A module that isn't licensed is left out (its keys are kept as they are when saving). * Keys no role has (core.system.admin) are only shown to a superadmin. * Order: core, then the modules that are on, then those turned off (catalogue order within). */ export function editorGroups(catalog, modules, { actorIsSuperadmin = false } = {}) { const byKey = Object.fromEntries((modules || []).map(m => [m.key, m])) return catalog .filter(m => byKey[m.key]?.entitled !== false) .map(m => ({ key: m.key, on: byKey[m.key] ? !!byKey[m.key].effective : true, permissions: m.permissions.filter(p => actorIsSuperadmin || p.roles.length > 0), })) .filter(g => g.permissions.length > 0) .map((g, i) => ({ g, rank: (g.key === 'core' ? 0 : g.on ? 1 : 2) * 1000 + i })) .sort((a, b) => a.rank - b.rank) .map(x => x.g) } /** Keys added and removed by the draft, compared with what the user has now. */ export function diff(draft, original) { return { added: [...draft].filter(k => !original.has(k)).sort(), removed: [...original].filter(k => !draft.has(k)).sort(), } } /** * May the editor switch this key ON? Removing is always allowed. Giving is allowed for a key * the user already had (it isn't new) or one the editor holds; `grantable` null = superadmin. * Same rule as the backend's access.cannot_grant. */ export function canGive(key, original, grantable) { return grantable == null || original.has(key) || grantable.has(key) } /** * "Reset to role defaults": the role's template, minus the keys the editor may not give. * Returns { keys, skipped } so the editor can say what it couldn't add. */ export function resetToRole(template, original, grantable) { const keys = new Set() const skipped = [] for (const k of template) { if (canGive(k, original, grantable)) keys.add(k) else skipped.push(k) } return { keys, skipped: skipped.sort() } } /** Does this user's key set differ from their role's template? */ export function differsFromRole(keys, template) { const a = new Set(keys) const b = new Set(template) return a.size !== b.size || [...a].some(k => !b.has(k)) } /** An approval's outcome: used / expired (never used) / waiting (still valid). */ export function approvalState(row, { now = Date.now() } = {}) { if (row.used_at) return 'used' return Date.parse(row.expires_at) <= now ? 'expired' : 'waiting' } /** An approval's target as something to show: { order: id } or { text } or null. */ export function approvalTarget(target) { if (!target) return null const m = /^order:(\d+)$/.exec(target) return m ? { order: Number(m[1]) } : { text: target } } /** May this manager end this session? Not their own (that's "sign out"), not support unless superadmin. */ export function canEndSession(session, { actorIsSuperadmin = false } = {}) { if (session.ended_at || session.current) return false return session.client !== 'support' || actorIsSuperadmin }