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:
@@ -0,0 +1,111 @@
|
||||
// The weekly schedule as plain data (plan step 6.11), tested in tests/schedule.test.mjs. Dates
|
||||
// are venue calendar days "YYYY-MM-DD"; times "HH:MM"; a shift that ends at or before its start
|
||||
// runs past midnight (the backend reads them the same way).
|
||||
|
||||
const DAY_MS = 86_400_000
|
||||
const parse = key => Date.UTC(Number(key.slice(0, 4)), Number(key.slice(5, 7)) - 1, Number(key.slice(8, 10)))
|
||||
const keyOf = ms => new Date(ms).toISOString().slice(0, 10)
|
||||
|
||||
export const addDays = (key, n) => keyOf(parse(key) + n * DAY_MS)
|
||||
|
||||
/** 0 = Sunday … 6 = Saturday. */
|
||||
export const dayOfWeek = key => new Date(parse(key)).getUTCDay()
|
||||
|
||||
/** The first day of the week holding `key` (Monday or Sunday first). */
|
||||
export function weekStartOf(key, mondayFirst = true) {
|
||||
const dow = dayOfWeek(key)
|
||||
const back = mondayFirst ? (dow + 6) % 7 : dow
|
||||
return addDays(key, -back)
|
||||
}
|
||||
|
||||
export const weekDays = start => Array.from({ length: 7 }, (_, i) => addDays(start, i))
|
||||
|
||||
export function minutes(t) {
|
||||
const [h, m] = String(t || '0:0').split(':').map(Number)
|
||||
return h * 60 + m
|
||||
}
|
||||
|
||||
/** [start, end] in minutes from the day's midnight; the end past 1440 when it runs overnight. */
|
||||
export function span(start, end) {
|
||||
const a = minutes(start)
|
||||
let b = minutes(end)
|
||||
if (b <= a) b += 1440
|
||||
return [a, b]
|
||||
}
|
||||
|
||||
export const hoursOf = (start, end) => { const [a, b] = span(start, end); return Math.round(((b - a) / 60) * 100) / 100 }
|
||||
|
||||
export function overlaps(s1, e1, s2, e2) {
|
||||
const [a, b] = span(s1, e1)
|
||||
const [c, d] = span(s2, e2)
|
||||
return a < d && c < b
|
||||
}
|
||||
|
||||
/** The shift a new or changed one would clash with (same person, same day), or null. */
|
||||
export function clashWith(scheduled = [], { userId, date, start, end, skipId = null }) {
|
||||
return scheduled.find(s => s.user_id === userId && s.scheduled_date === date && s.id !== skipId
|
||||
&& overlaps(start, end, s.start_time, s.end_time)) ?? null
|
||||
}
|
||||
|
||||
/** { userId: { date: [shifts by start] } } */
|
||||
export function gridOf(scheduled = []) {
|
||||
const g = {}
|
||||
for (const s of scheduled) {
|
||||
((g[s.user_id] ??= {})[s.scheduled_date] ??= []).push(s)
|
||||
}
|
||||
for (const days of Object.values(g)) for (const list of Object.values(days)) list.sort((a, b) => minutes(a.start_time) - minutes(b.start_time))
|
||||
return g
|
||||
}
|
||||
|
||||
/** 'planned' (not started) | 'working' (started, not ended) | 'worked' (started and ended). */
|
||||
export function shiftState(shift) {
|
||||
const actual = shift.actual_shifts ?? []
|
||||
if (!actual.length) return 'planned'
|
||||
return actual.some(a => a.is_active) ? 'working' : 'worked'
|
||||
}
|
||||
|
||||
/** Hours and estimated pay per person for the week: { userId: { hours, pay } }, plus the totals. */
|
||||
export function weekTotals(scheduled = []) {
|
||||
const people = {}
|
||||
let hours = 0
|
||||
let pay = 0
|
||||
for (const s of scheduled) {
|
||||
const p = (people[s.user_id] ??= { hours: 0, pay: 0 })
|
||||
const h = s.duration_hours ?? hoursOf(s.start_time, s.end_time)
|
||||
p.hours += h
|
||||
hours += h
|
||||
if (s.estimated_pay != null) { p.pay += s.estimated_pay; pay += s.estimated_pay }
|
||||
}
|
||||
const r = n => Math.round(n * 100) / 100
|
||||
for (const p of Object.values(people)) { p.hours = r(p.hours); p.pay = r(p.pay) }
|
||||
return { people, hours: r(hours), pay: r(pay) }
|
||||
}
|
||||
|
||||
/** Where a shift sits on a day's timeline drawn from `firstHour`: { top, height } in hours. */
|
||||
export function placeOnDay(shift, firstHour = 6) {
|
||||
const [a, b] = span(shift.start_time, shift.end_time)
|
||||
const from = firstHour * 60
|
||||
const top = ((a - from + 1440) % 1440) / 60
|
||||
return { top, height: Math.max((b - a) / 60, 0.25) }
|
||||
}
|
||||
|
||||
/** The 24 hour marks of a timeline starting at `firstHour`. */
|
||||
export const hourMarks = firstHour => Array.from({ length: 24 }, (_, i) => (firstHour + i) % 24)
|
||||
|
||||
/** Is an hour mark inside the opening hours (start–end, past midnight allowed)? */
|
||||
export function inOpening(hour, open, close) {
|
||||
if (!open || !close) return false
|
||||
const a = Math.floor(minutes(open) / 60)
|
||||
const b = Math.floor(minutes(close) / 60)
|
||||
return a <= b ? hour >= a && hour < b : hour >= a || hour < b
|
||||
}
|
||||
|
||||
/** The schedule's view settings kept in this browser (a per-viewer convenience). */
|
||||
export const VIEW_DEFAULTS = { view: 'people', mondayFirst: true, firstHour: 6, open: '', close: '' }
|
||||
const VIEW_KEY = 'xenia:schedule:view'
|
||||
export function loadView(storage = globalThis.localStorage) {
|
||||
try { return { ...VIEW_DEFAULTS, ...JSON.parse(storage?.getItem(VIEW_KEY) || '{}') } } catch { return { ...VIEW_DEFAULTS } }
|
||||
}
|
||||
export function saveView(view, storage = globalThis.localStorage) {
|
||||
try { storage?.setItem(VIEW_KEY, JSON.stringify(view)) } catch { /* private window: keep it for this visit */ }
|
||||
}
|
||||
Reference in New Issue
Block a user