Shapes: rectangle, circle, triangle, polygon, star or own corners, fill or gradient, border. Countdown: to a time every day or a date, from now / a span before / a set time, in venue time (the slide shows what is left right now). Opening hours, Wi-Fi (with a join-the-network code) and contact details read the venue's identity and opening hours (GET/PUT /api/identity/hours; Schedule -> Opening hours, shaded on its timeline); the editor warns with a link when they're missing. Plan 6R.15, F-112. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
121 lines
5.0 KiB
JavaScript
121 lines
5.0 KiB
JavaScript
// 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
|
||
}
|
||
|
||
/** Is an hour mark inside any of a day's opening spans ([{from, to}], 6R.15: the venue's hours, kept on the server)? */
|
||
export const inSpans = (hour, spans) => (spans || []).some(x => inOpening(hour, x.from, x.to))
|
||
|
||
/** Monday 0 … Sunday 6 of a "YYYY-MM-DD". */
|
||
export const weekdayOf = d => (new Date(`${d}T12:00:00Z`).getUTCDay() + 6) % 7
|
||
|
||
/** Opening hours that can be saved: every span has both times and they differ. */
|
||
export const hoursValid = days => days.every(d => d.every(x => /^\d\d:\d\d$/.test(x.from || '') && /^\d\d:\d\d$/.test(x.to || '') && x.from !== x.to))
|
||
|
||
/** The schedule's view settings kept in this browser (a per-viewer convenience). */
|
||
export const VIEW_DEFAULTS = { view: 'people', mondayFirst: true, firstHour: 6 }
|
||
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 */ }
|
||
}
|