// The staff page as plain data (plan step 6.11), tested in tests/staff.test.mjs. import { parseNumber } from '@xenia/i18n/number' /** The roles in the order the page lists them (backend roles.py); names are i18n `roles.*`. */ export const ROLES = ['owner', 'store_manager', 'staff_manager', 'chef', 'assistant_chef', 'barista', 'barman', 'waiter', 'assistant_waiter', 'custom'] const GROUP = { superadmin: 'management', owner: 'management', store_manager: 'management', staff_manager: 'management', chef: 'kitchen', assistant_chef: 'kitchen', barista: 'kitchen', barman: 'kitchen', waiter: 'service', assistant_waiter: 'service', } export const GROUPS = ['management', 'kitchen', 'service', 'other'] export const groupOf = role => GROUP[role] ?? 'other' export const displayName = m => (m ? m.full_name || m.nickname || m.username : '') /** How many people in each group (the page's header). */ export function groupCounts(staff = []) { const out = Object.fromEntries(GROUPS.map(g => [g, 0])) for (const m of staff) out[groupOf(m.role)] += 1 return out } const fold = s => String(s ?? '').normalize('NFD').replace(/[\u0300-\u036f]/g, '').toLowerCase() /** Staff shown for a role ('' = all), a status ('' | 'active' | 'blocked') and a search * (name, nickname, username, phone; accents ignored); active first, then by name. */ export function filterStaff(staff = [], { role = '', status = '', q = '' } = {}) { const words = fold(q).split(/\s+/).filter(Boolean) return staff .filter(m => (!role || m.role === role) && (!status || (status === 'active') === !!m.is_active)) .filter(m => { if (!words.length) return true const text = fold([m.full_name, m.nickname, m.username, m.mobile_phone].join(' ')) return words.every(w => text.includes(w)) }) .sort((a, b) => Number(!!b.is_active) - Number(!!a.is_active) || displayName(a).localeCompare(displayName(b))) } // ── the person's form ─────────────────────────────────────────────────────── export const EMPTY_STAFF = { full_name: '', nickname: '', username: '', role: 'waiter', mobile_phone: '', email: '', note: '', hourly_rate: '', language: '', pin: '', password: '', } export function staffForm(m) { if (!m) return { ...EMPTY_STAFF } const out = { ...EMPTY_STAFF } for (const k of Object.keys(EMPTY_STAFF)) if (m[k] != null && k !== 'pin' && k !== 'password') out[k] = String(m[k]) return out } export const validPin = pin => /^\d{4,6}$/.test(pin ?? '') export const validPassword = pw => (pw ?? '').length >= 6 /** The fields that are wrong (empty = fine). A new person needs a PIN; a password is optional. */ export function staffProblems(form, isNew) { const out = [] if (!form.full_name.trim()) out.push('full_name') if (!form.nickname.trim()) out.push('nickname') if (!/^[^\s]{2,}$/.test(form.username.trim())) out.push('username') const rate = String(form.hourly_rate ?? '').trim() const rateValue = rate ? parseNumber(rate) : null if (rate && !(rateValue != null && rateValue >= 0)) out.push('hourly_rate') if (isNew && !validPin(form.pin)) out.push('pin') if (isNew && form.password && !validPassword(form.password)) out.push('password') return out } /** POST /api/waiters/ (new) or PUT /api/waiters/{id} (edit). */ export function staffBody(form, isNew) { const text = v => (v ?? '').trim() || null const rate = String(form.hourly_rate ?? '').trim() const body = { full_name: form.full_name.trim(), nickname: form.nickname.trim(), username: form.username.trim(), role: form.role, mobile_phone: text(form.mobile_phone), email: text(form.email), note: text(form.note), hourly_rate: rate ? parseNumber(rate, { decimals: 2 }) : null, language: form.language || '', } if (isNew) { body.pin = form.pin body.is_active = true body.language = form.language || null if (form.password) body.password = form.password } return body } // ── zones ─────────────────────────────────────────────────────────────────── /** A person's zones: { all, ids } (all = every zone). */ export function zonesOf(member) { const rows = member?.zone_assignments ?? [] return { all: rows.some(z => z.group_id == null), ids: rows.map(z => z.group_id).filter(id => id != null) } } /** 'all' | 'none' | the zones' names. */ export function zonesSummary(member, groups = []) { const { all, ids } = zonesOf(member) if (all) return 'all' if (!ids.length) return 'none' return ids.map(id => groups.find(g => g.id === id)?.name ?? `#${id}`).join(', ') } export const zonesBody = ({ all, ids }) => ({ all_zones: all, group_ids: all ? [] : ids }) /** Does the role's set of keys differ from the person's? (the "custom" mark) */ export function customKeys(member, templates) { const role = templates?.[member.role] if (!role || member.role === 'superadmin') return false const a = new Set(member.permissions ?? []) return a.size !== role.length || role.some(k => !a.has(k)) }