feat(manager): staff permission editor, signed-in sessions, approvals log (platform-v2 2.6e)

- Staff: the 13 perm_* tick columns become one Permissions column (count,
  "changed from role") opening an editor grouped by module, from
  /api/waiters/permission-catalog: switches labelled per key, keys you don't
  hold are disabled with the reason, reset to role defaults (names what it
  can't give), save the whole set; access.cannot_grant explained by label.
- "Signed in now" (/management/sessions): who, where (app or device), since,
  last active, your own session marked; end session; show ended + reason.
- "Manager approvals" (/management/approvals): the override audit with
  action label, asked by, approved by, device, used/not used, order link.
- Backend: sessions list adds device_name/device_app/current/expires_at;
  overrides list adds device_name/expires_at.
- Permission labels for every key in modules.json (en/el), guarded by
  test_module_texts; manager unit tests for the editor logic; e2e 66 checks.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-14 08:10:57 +02:00
co-authored by Claude Opus 5.5
parent cc00ec39c7
commit 92bc1b0113
20 changed files with 1135 additions and 189 deletions
+55 -1
View File
@@ -1,8 +1,11 @@
// Pure logic of the platform pages (plan steps 2.6c, 2.6d).
// Pure logic of the platform pages (plan steps 2.6c, 2.6d, 2.6e).
import assert from 'node:assert/strict'
import { test } from 'node:test'
import { deviceStatus, deviceUsage, hasRoom, linkTargets, normalizeCode } from '../src/pages/platform/devicesLogic.js'
import { moduleState, stoppedBy } from '../src/pages/platform/modulesLogic.js'
import {
approvalState, approvalTarget, canEndSession, canGive, diff, differsFromRole, editorGroups, permLabelKey, resetToRole,
} from '../src/pages/staff/permissionsLogic.js'
const mod = (key, extra = {}) => ({ key, depends: [], can_disable: true, entitled: true, enabled: true, effective: true, limits: [], device_app: null, ...extra })
@@ -62,3 +65,54 @@ test('link targets: not itself, not removed', () => {
const devices = [{ id: 1, revoked_at: null }, { id: 2, revoked_at: 'x' }, { id: 3, revoked_at: null }]
assert.deepEqual(linkTargets(devices, 1).map(d => d.id), [3])
})
// ── 2.6e ────────────────────────────────────────────────────────────────────
test('permission label keys follow the dots of the key under the module', () => {
assert.equal(permLabelKey('ordering.orders.cancel'), 'modules:ordering.permissions.orders.cancel')
assert.equal(permLabelKey('kds.access'), 'modules:kds.permissions.access')
})
test('editor groups: licensed modules only (core, on, off), keys no role has only for a superadmin', () => {
const catalog = [
{ key: 'core', permissions: [{ key: 'core.staff.manage', roles: ['owner'] }, { key: 'core.system.admin', roles: [] }] },
{ key: 'kds', permissions: [{ key: 'kds.access', roles: ['chef'] }] },
{ key: 'tabs', permissions: [{ key: 'tabs.manage', roles: ['owner'] }] },
]
catalog.unshift({ key: 'chat', permissions: [{ key: 'chat.use', roles: ['waiter'] }] })
const modules = [mod('chat', { effective: false }), mod('core'), mod('kds'), mod('tabs', { entitled: false, effective: false })]
const g = editorGroups(catalog, modules)
assert.deepEqual(g.map(x => [x.key, x.on, x.permissions.map(p => p.key)]), [['core', true, ['core.staff.manage']], ['kds', true, ['kds.access']], ['chat', false, ['chat.use']]])
assert.deepEqual(editorGroups(catalog, modules, { actorIsSuperadmin: true })[0].permissions.map(p => p.key), ['core.staff.manage', 'core.system.admin'])
})
test('giving: only what you hold or what they already had; reset skips the rest', () => {
const original = new Set(['kds.access'])
const mine = new Set(['kds.bump'])
assert.equal(canGive('kds.bump', original, mine), true)
assert.equal(canGive('kds.access', original, mine), true) // not new
assert.equal(canGive('reports.view', original, mine), false)
assert.equal(canGive('reports.view', original, null), true) // superadmin
const r = resetToRole(['kds.access', 'kds.bump', 'reports.view'], original, mine)
assert.deepEqual([...r.keys].sort(), ['kds.access', 'kds.bump'])
assert.deepEqual(r.skipped, ['reports.view'])
assert.deepEqual(diff(new Set(['kds.bump']), original), { added: ['kds.bump'], removed: ['kds.access'] })
assert.equal(differsFromRole(['a', 'b'], ['b', 'a']), false)
assert.equal(differsFromRole(['a'], ['a', 'b']), true)
})
test('approvals and sessions', () => {
const now = Date.parse('2026-11-13T12:00:00Z')
assert.equal(approvalState({ used_at: '2026-11-13T11:59:00Z', expires_at: '2026-11-13T11:59:30Z' }, { now }), 'used')
assert.equal(approvalState({ used_at: null, expires_at: '2026-11-13T11:59:30Z' }, { now }), 'expired')
assert.equal(approvalState({ used_at: null, expires_at: '2026-11-13T12:00:30Z' }, { now }), 'waiting')
assert.deepEqual(approvalTarget('order:12'), { order: 12 })
assert.deepEqual(approvalTarget('DELETE /api/x/3'), { text: 'DELETE /api/x/3' })
assert.equal(approvalTarget(null), null)
const s = { client: 'waiter', current: false, ended_at: null }
assert.equal(canEndSession(s), true)
assert.equal(canEndSession({ ...s, current: true }), false)
assert.equal(canEndSession({ ...s, ended_at: 'x' }), false)
assert.equal(canEndSession({ ...s, client: 'support' }), false)
assert.equal(canEndSession({ ...s, client: 'support' }, { actorIsSuperadmin: true }), true)
})