feat: implement home board for upcoming shifts and events

Introduced a new home board feature that displays shifts needing volunteers and upcoming events for the next two weeks. Updated the admin interface to show relevant shift details, including capacity and occupancy. Enhanced data fetching to support this feature and modified related components for better user experience. Added tests to ensure functionality works as expected.
This commit is contained in:
blake committed 2026-10-26 10:36:55 -05:00
1 parent bf661673c9
commit 7cfb984d63
7 files changed
+1962 -381

No files matched your search

+17 -6
View File
@@ -32,7 +32,7 @@ import {
getAdminEvent,
getAdminShift,
getAdminTemplate,
listAdminEvents,
listHomeBoard,
readEventRemoval,
readShiftRemoval,
readSignupRemoval,
@@ -285,7 +285,9 @@ function showHome(
const session = requireSession(c, services);
if (session instanceof Response) return session;
const notice = knownNotice(c.req.query('notice'));
const backup = describeBackup(backupDirectory(services.config.dataDir), services.now());
const now = services.now();
const backup = describeBackup(backupDirectory(services.config.dataDir), now);
const board = listHomeBoard(services.db, now, services.config.defaultTimeZone);
return html(
c,
services,
@@ -293,7 +295,9 @@ function showHome(
<AdminHome
config={services.config}
name={session.name}
events={listAdminEvents(services.db)}
events={board.events}
shifts={board.shifts}
now={now}
csrf={session.csrf}
notice={notice}
exportFrom={range?.from}
@@ -779,6 +783,7 @@ function showNewTemplate(c: Context, services: AdminServices, values?: TemplateV
csrf={session.csrf}
values={values ?? blankTemplate(localToday(event.tz, services.now()))}
eventId={event.id}
eventTitle={event.title}
error={error}
submitLabel="Save template"
/>,
@@ -806,8 +811,9 @@ function showTemplate(c: Context, services: AdminServices, values?: TemplateValu
const session = requireSession(c, services);
if (session instanceof Response) return session;
const eventId = idFrom(c);
const event = getAdminEvent(services.db, eventId);
const template = getAdminTemplate(services.db, eventId, templateIdFrom(c));
if (!template) return missing(c, services);
if (!event || !template) return missing(c, services);
return html(
c,
services,
@@ -829,6 +835,7 @@ function showTemplate(c: Context, services: AdminServices, values?: TemplateValu
}
}
eventId={eventId}
eventTitle={event.title}
ended={!template.active}
error={error}
submitLabel="Save template"
@@ -1104,6 +1111,8 @@ function showNewShift(c: Context, services: AdminServices, values?: ShiftValues,
action={appPath(services.config, `/admin/events/${event.id}/shifts`)}
csrf={session.csrf}
values={values ?? { role: '', date: '', start: '09:00', end: '12:00', capacity: '1' }}
eventId={event.id}
eventTitle={event.title}
error={error}
submitLabel="Create shift"
/>,
@@ -1135,6 +1144,7 @@ function showShift(
walkIn?: WalkInState,
shiftId = idFrom(c),
confirm?: ConfirmPrompt,
editError?: string,
) {
const session = requireSession(c, services);
if (session instanceof Response) return session;
@@ -1174,6 +1184,7 @@ function showShift(
contacts={showWalkIn ? loadContacts(services.db, '').people : []}
moves={confirmed ? { targets, tokens } : undefined}
confirm={confirm}
editError={editError}
/>,
{ account: session, status },
);
@@ -1249,10 +1260,10 @@ async function submitShift(c: Context, services: AdminServices): Promise<Respons
const id = idFrom(c);
const values = shiftValuesFrom(session.body);
const parsed = parseShift(values);
if (!parsed.ok) return showShift(c, services, parsed.error, 400);
if (!parsed.ok) return showShift(c, services, undefined, 400, undefined, id, undefined, parsed.error);
try {
const saved = updateShift(services.db, session.coordinatorId, id, parsed.value, services.now(), mailIdentity(services.db, services.config));
if (!saved.ok) return showShift(c, services, saved.error, 400);
if (!saved.ok) return showShift(c, services, undefined, 400, undefined, id, undefined, saved.error);
return c.redirect(appPath(services.config, `/admin/shifts/${id}?notice=saved`), 303);
} catch (err) {
return saveFailed(c, services, session, err);
+574 -356
View File
File diff suppressed because it is too large. Load diff
+71 -1
View File
@@ -8,10 +8,12 @@ import type { MailShift } from '../mail/templates.js';
import type { MailIdentity } from '../mail/types.js';
import { confirmedPersonOnShift, confirmedPersonShiftIds, reserveSignup, type ReserveInput, type ReserveResult } from '../signup.js';
import { isUniqueConstraint } from '../sqlite-error.js';
import { assertSameDayShift, assertValidTimeZone, formatInstantInZone, formatUtc, localShiftToUtc, LocalTimeError } from '../time.js';
import { addLocalDays, assertSameDayShift, assertValidTimeZone, formatInstantInZone, formatUtc, localShiftToUtc, LocalTimeError, todayInZone } from '../time.js';
const MANAGE_TOKEN_TTL_HOURS = 24 * 7;
const REPLACED_LINK = 'This link replaces the earlier one. The previous link no longer works.';
/** Today through this many days ahead. The event shift list uses the same window. */
export const DESK_WINDOW_DAYS = 14;
export type AdminEventSummary = {
id: number;
@@ -21,6 +23,22 @@ export type AdminEventSummary = {
shiftCount: number;
};
export type HomeShift = {
id: number;
eventId: number;
eventTitle: string;
role: string;
date: string;
timeLabel: string;
capacity: number;
occupied: number;
full: boolean;
};
export type HomeEvent = AdminEventSummary & {
nextShift: { date: string; timeLabel: string } | null;
};
export type AdminShiftSummary = {
id: number;
role: string;
@@ -149,6 +167,58 @@ export function listAdminEvents(db: Database.Database): AdminEventSummary[] {
}));
}
/** Open and full shifts in the next two weeks, plus the next open shift on each event. */
export function listHomeBoard(db: Database.Database, now: string, timeZone: string): { shifts: HomeShift[]; events: HomeEvent[] } {
const today = todayInZone(timeZone, Temporal.Instant.from(now));
const cutoff = addLocalDays(today, DESK_WINDOW_DAYS);
const rows = db
.prepare(
`SELECT s.id AS id, s.role AS role, s.starts_at_utc AS starts_at_utc, s.capacity AS capacity,
e.id AS event_id, e.title AS title, e.tz AS tz,
COALESCE(SUM(CASE WHEN su.status = 'confirmed' THEN su.group_size ELSE 0 END), 0) AS occupied
FROM shifts s
JOIN events e ON e.id = s.event_id
LEFT JOIN signups su ON su.shift_id = s.id
WHERE s.cancelled = 0 AND s.ends_at_utc > ?
GROUP BY s.id
ORDER BY s.starts_at_utc, s.id`,
)
.all(now) as Array<{
id: number;
role: string;
starts_at_utc: string;
capacity: number;
event_id: number;
title: string;
tz: string;
occupied: number;
}>;
const nextByEvent = new Map<number, { date: string; timeLabel: string }>();
const shifts: HomeShift[] = [];
for (const row of rows) {
const local = formatInstantInZone(row.starts_at_utc, row.tz);
if (!nextByEvent.has(row.event_id)) nextByEvent.set(row.event_id, { date: local.date, timeLabel: local.timeLabel });
if (local.date < today || local.date > cutoff) continue;
const capacity = Number(row.capacity);
const occupied = Number(row.occupied);
shifts.push({
id: row.id,
eventId: row.event_id,
eventTitle: row.title,
role: row.role,
date: local.date,
timeLabel: local.timeLabel,
capacity,
occupied,
full: occupied >= capacity,
});
}
return {
shifts,
events: listAdminEvents(db).map((event) => ({ ...event, nextShift: nextByEvent.get(event.id) ?? null })),
};
}
export function getAdminEvent(db: Database.Database, id: number): AdminEvent | null {
const event = db
.prepare(