feat: staff and the schedule on v2; managers give staff passwords (platform-v2 step 6.11, KI-014)

- staff: PUT /api/waiters/{id}/password (set, change, or take away; 6+ chars)
  so the password sign-in mode works for everyone; PUT /{id}/pin with the PIN in
  the body (was in the address: reset-pin?pin= removed); PINs 4-6 digits when
  adding; a password can be given when adding; has_password on staff; editing
  clears emptied fields and keeps usernames unique; delete only without history
  (orders, items, shifts) and never yourself
- schedule: times checked (schedule.bad_time), one person's overlapping shifts
  refused (schedule.overlap, past midnight understood), everyone active can be
  scheduled (the kitchen too), changes need core.staff.manage
- manager: src/pages/staff (list with search and filters, person dialog with
  photo, PIN / password dialog, zones) and src/pages/schedule (by person, by
  hour with opening hours, shift dialog with clash check, view settings kept in
  the browser); the never-used "automatic start/close" switches dropped; v1
  StaffTab and SchedulePage removed; ApprovalsPage loads with useLiveQuery
- tests: test_staff_v2.py (done-when on a paired screen), staff.test.mjs, e2e
  manager.mjs (add, password, sign-in on a paired till, a shift and a clash)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-19 08:05:01 +02:00
co-authored by Claude Opus 5.5
parent f31bdf42aa
commit 6643614ff6
27 changed files with 1554 additions and 1794 deletions
@@ -0,0 +1,147 @@
// Staff (plan step 6.11; replaces the v1 Management → Staff table): everyone who works here,
// with their role, whether they can sign in (blocked or not; PIN, password), what they may do
// (permission keys, "custom" when they differ from the role) and their zones. New person, edit
// (and photo), a new PIN, a password (KI-014: the password sign-in mode works for everyone now),
// zones, permissions, block / unblock, delete (only someone without history).
import { useState } from 'react'
import { useMutation, useQueryClient } from '@tanstack/react-query'
import toast from 'react-hot-toast'
import { Ban, CheckCircle2, KeyRound, LockKeyhole, MapPin, Pencil, Plus, Search, ShieldCheck, Trash2, Users } from 'lucide-react'
import { useAccess } from '@xenia/access'
import { errorMessage } from '@xenia/api'
import { useTranslation } from '@xenia/i18n'
import { Button, Dialog, EmptyState, IconButton, Input, Select, Spinner, cx } from '@xenia/ui'
import client from '../../api/client'
import { useLiveQuery } from '../../live/useLiveQuery'
import { Avatar, Pill } from '../overview/parts'
import PermissionEditor from './PermissionEditor'
import SecretDialog from './SecretDialog'
import StaffDialog from './StaffDialog'
import ZonesDialog from './ZonesDialog'
import { GROUPS, ROLES, customKeys, displayName, filterStaff, groupCounts, zonesSummary } from './staffLogic'
export default function StaffPage() {
const { t } = useTranslation('manager')
const qc = useQueryClient()
const { user: me } = useAccess()
const [filter, setFilter] = useState({ role: '', status: '', q: '' })
const [dialog, setDialog] = useState(null) // { kind: 'new' | 'edit' | 'pin' | 'password' | 'zones' | 'perms' | 'delete', member }
const { data: staff, isPending } = useLiveQuery({ queryKey: ['staff'], queryFn: () => client.get('/api/waiters/').then(r => r.data) })
const { data: catalog } = useLiveQuery({ queryKey: ['permission-catalog'], queryFn: () => client.get('/api/waiters/permission-catalog').then(r => r.data), staleTime: 300_000 })
const { data: groups = [] } = useLiveQuery({ queryKey: ['table-groups'], queryFn: () => client.get('/api/tables/groups').then(r => r.data), staleTime: 60_000 })
const changed = () => qc.invalidateQueries({ queryKey: ['staff'] })
const onError = err => toast.error(errorMessage(err, t))
const block = useMutation({
mutationFn: m => client.put(`/api/waiters/${m.id}/block`).then(r => r.data),
onSuccess: (r, m) => { toast.success(t(r.is_active ? 'staff.unblocked' : 'staff.blocked', { name: displayName(m) })); changed() }, onError,
})
const remove = useMutation({
mutationFn: m => client.delete(`/api/waiters/${m.id}`),
onSuccess: (_r, m) => { toast.success(t('staff.deleted', { name: displayName(m) })); setDialog(null); changed() }, onError,
})
const set = (k, v) => setFilter(f => ({ ...f, [k]: v }))
const shown = filterStaff(staff ?? [], filter)
const counts = groupCounts(staff ?? [])
const open = (kind, member = null) => setDialog({ kind, member })
return (
<div className="h-full overflow-y-auto p-4 lg:p-6" data-testid="staff-page">
<div className="max-w-6xl mx-auto grid gap-4">
<header className="flex flex-wrap items-center gap-3">
<div className="flex-1 min-w-0">
<h1 className="text-title font-semibold text-ink">{t('staff.title')}</h1>
<p className="text-label text-ink-2">
{GROUPS.filter(g => counts[g]).map(g => t(`staff.groups.${g}`, { count: counts[g] })).join(' · ')}
</p>
</div>
<Button icon={<Plus size={16} />} onClick={() => open('new')} data-testid="staff-new">{t('staff.new')}</Button>
</header>
<div className="flex flex-wrap items-center gap-2">
<div className="relative flex-1 min-w-56">
<Search size={16} className="absolute left-3 top-1/2 -translate-y-1/2 text-ink-3" aria-hidden="true" />
<Input value={filter.q} onChange={e => set('q', e.target.value)} placeholder={t('staff.search')} aria-label={t('staff.search')} className="pl-9" data-testid="staff-search" />
</div>
<Select size="sm" value={filter.role} onChange={e => set('role', e.target.value)} aria-label={t('staff.role')}
options={[{ value: '', label: t('staff.allRoles') }, ...ROLES.map(r => ({ value: r, label: t(`roles.${r}`) }))]} />
<Select size="sm" value={filter.status} onChange={e => set('status', e.target.value)} aria-label={t('staff.status')}
options={[{ value: '', label: t('staff.anyStatus') }, { value: 'active', label: t('staff.active') }, { value: 'blocked', label: t('staff.blockedState') }]} />
</div>
{isPending ? <div className="py-10 flex justify-center"><Spinner /></div>
: !shown.length ? <EmptyState icon={<Users size={28} />} title={t('staff.none')} />
: (
<ul className="rounded-card border border-line bg-surface shadow-raised divide-y divide-line overflow-hidden">
{shown.map(m => {
const support = m.role === 'superadmin'
const zones = zonesSummary(m, groups)
return (
<li key={m.id} className={cx('px-4 py-3 flex flex-wrap items-center gap-3', !m.is_active && 'bg-surface-2')} data-testid="staff-row" data-username={m.username}>
<button type="button" className="flex items-center gap-3 flex-1 min-w-60 text-left" onClick={() => !support && open('edit', m)} disabled={support}>
<Avatar name={displayName(m)} url={m.avatar_url} />
<span className="grid min-w-0">
<span className={cx('text-body font-semibold truncate', m.is_active ? 'text-ink' : 'text-ink-3 line-through')}>{displayName(m)}</span>
<span className="flex flex-wrap items-center gap-1.5 text-caption text-ink-2">
<Pill tone="accent">{t(`roles.${m.role}`, { defaultValue: m.role })}</Pill>
<span>@{m.username}</span>
{m.nickname && m.nickname !== m.full_name && <span>· {m.nickname}</span>}
</span>
</span>
</button>
<span className="flex flex-wrap items-center gap-1.5 w-full sm:w-auto">
{!m.is_active && <Pill tone="danger">{t('staff.blockedState')}</Pill>}
<Pill tone={m.has_password ? 'success' : 'neutral'} title={t('staff.signInTip')}>{m.has_password ? t('staff.pinAndPassword') : t('staff.pinOnly')}</Pill>
{!support && (
<span data-testid={`staff-perms-${m.id}`}>
<button type="button" onClick={() => open('perms', m)} className="inline-flex items-center gap-1 text-caption text-ink-2 hover:text-accent-ink">
<ShieldCheck size={13} aria-hidden="true" />{t('permissions.count', { count: m.permissions?.length || 0 })}
{customKeys(m, catalog?.role_templates) && <Pill tone="warning">{t('permissions.custom')}</Pill>}
</button>
</span>
)}
{support && <span className="text-caption text-ink-3">{t('permissions.all')}</span>}
{(m.zone_assignments?.length > 0 || m.permissions?.includes('waiter_app.access')) && (
<span className="inline-flex items-center gap-1 text-caption text-ink-2" title={t('staff.zones')}>
<MapPin size={13} aria-hidden="true" />{zones === 'all' ? t('staff.allZones') : zones === 'none' ? t('staff.noZones') : zones}
</span>
)}
</span>
{!support && (
<span className="flex gap-0.5 ml-auto">
<IconButton size="sm" variant="ghost" label={t('staff.edit', { name: displayName(m) })} onClick={() => open('edit', m)}><Pencil size={15} /></IconButton>
<IconButton size="sm" variant="ghost" label={t('staff.pin', { name: displayName(m) })} onClick={() => open('pin', m)} data-testid="staff-pin"><KeyRound size={15} /></IconButton>
<IconButton size="sm" variant="ghost" label={t('staff.password', { name: displayName(m) })} onClick={() => open('password', m)} data-testid="staff-password"><LockKeyhole size={15} /></IconButton>
<IconButton size="sm" variant="ghost" label={t('staff.zonesOf', { name: displayName(m) })} onClick={() => open('zones', m)}><MapPin size={15} /></IconButton>
{m.id !== me?.id && (<>
<IconButton size="sm" variant="ghost" label={t(m.is_active ? 'staff.block' : 'staff.unblock', { name: displayName(m) })}
onClick={() => block.mutate(m)} data-testid="staff-block">{m.is_active ? <Ban size={15} /> : <CheckCircle2 size={15} />}</IconButton>
<IconButton size="sm" variant="ghost" label={t('staff.delete', { name: displayName(m) })} onClick={() => open('delete', m)}><Trash2 size={15} /></IconButton>
</>)}
</span>
)}
</li>
)
})}
</ul>
)}
</div>
{(dialog?.kind === 'new' || dialog?.kind === 'edit') && (
<StaffDialog member={dialog.member} onClose={() => setDialog(null)} onSaved={() => { setDialog(null); changed() }} />
)}
{(dialog?.kind === 'pin' || dialog?.kind === 'password') && (
<SecretDialog kind={dialog.kind} member={dialog.member} self={dialog.member.id === me?.id} onClose={() => setDialog(null)} onSaved={() => { setDialog(null); changed() }} />
)}
{dialog?.kind === 'zones' && <ZonesDialog member={dialog.member} groups={groups} onClose={() => setDialog(null)} onSaved={() => { setDialog(null); changed() }} />}
{dialog?.kind === 'perms' && <PermissionEditor member={dialog.member} onClose={() => { setDialog(null); changed() }} />}
{dialog?.kind === 'delete' && (
<Dialog open size="sm" onClose={() => setDialog(null)} title={t('staff.deleteTitle', { name: displayName(dialog.member) })} description={t('staff.deleteBody')}
footer={<>
<Button variant="secondary" onClick={() => setDialog(null)}>{t('staff.back')}</Button>
{dialog.member.is_active && <Button variant="secondary" onClick={() => { block.mutate(dialog.member); setDialog(null) }}>{t('staff.blockInstead')}</Button>}
<Button variant="danger" loading={remove.isPending} onClick={() => remove.mutate(dialog.member)} data-testid="staff-delete-confirm">{t('staff.deleteYes')}</Button>
</>} />
)}
</div>
)
}