D7: read-only schedule grid UI (rows=instances, cols=positions, week nav)
Server-component schedule page backed by a pure data layer (src/db/grid.ts): gridForWeek() returns positions + instances (->slots->assignments), and defaultWeekStart() defaults to the earliest upcoming published instance so a seeded DB shows data on first load. Week nav via ?week=YYYY-MM-DD (server rendered, no client state). Fixes two latent bugs that kept the grid empty/incorrect: - tz: weekdayInTz()/nextSundaysInTz() built Date.UTC(y,mo-1,d) from a LOCAL date string and read its weekday; for America/New_York (UTC-5) UTC midnight lands the previous evening, so the seed materialized instances on Mondays and the default week was off by one. weekdayInTz now builds the local-midnight instant via utcOffsetMs; nextSundaysInTz uses weekdayInTz + addDaysISO. - grid: occursOn (date mode) was run through dateInTimezone(), reinterpreting the UTC instant as wall-clock and shifting a day early; now extracts the UTC calendar date directly (occursOnISO). - schedule-grid: <table className=... collapse ...> set visibility: collapse (Tailwind 'collapse'), hiding the whole grid server-rendered-but-invisible; changed to border-collapse. E2E: new schedule-grid.spec.ts + shell.spec.ts grid assertion. Full suite green: tsc, eslint, 178 unit tests, next build, 6/6 Playwright.
This commit is contained in:
+202
@@ -0,0 +1,202 @@
|
||||
/**
|
||||
* Grid data — unit D7 (ARCHITECTURE §5.8.4).
|
||||
*
|
||||
* The read-only scheduling grid: rows = service instances, cols = active
|
||||
* positions (roles, by `sort_order`), each cell = one slot with its
|
||||
* assignment. This module is the single data source for the grid — the page
|
||||
* fetches a week and the component renders it. Pure data, no JSX.
|
||||
*/
|
||||
|
||||
import { and, eq, gte, lte } from "drizzle-orm";
|
||||
import { db } from "@/db";
|
||||
import {
|
||||
assignments,
|
||||
roles,
|
||||
serviceInstances,
|
||||
services,
|
||||
slots,
|
||||
users,
|
||||
} from "@/db/schema";
|
||||
import { addDaysISO, CHURCH_TIMEZONE, dateInTimezone, weekStartOn } from "@/lib/tz";
|
||||
|
||||
export interface GridAssignment {
|
||||
userId: string;
|
||||
displayName: string;
|
||||
status: string;
|
||||
}
|
||||
|
||||
export interface GridSlot {
|
||||
id: string;
|
||||
roleId: string;
|
||||
roleSlug: string;
|
||||
roleName: string;
|
||||
slotIndex: number;
|
||||
callTime: Date | null;
|
||||
status: string;
|
||||
isTraineeSlot: boolean;
|
||||
assignments: GridAssignment[];
|
||||
}
|
||||
|
||||
export interface GridInstance {
|
||||
id: string;
|
||||
serviceId: string;
|
||||
serviceName: string;
|
||||
/** Local date (YYYY-MM-DD) the service occurs. */
|
||||
occursOn: string;
|
||||
startsAt: Date;
|
||||
titleOverride: string | null;
|
||||
status: string;
|
||||
slots: GridSlot[];
|
||||
}
|
||||
|
||||
export interface GridPosition {
|
||||
slug: string;
|
||||
name: string;
|
||||
colorHex: string | null;
|
||||
}
|
||||
|
||||
export interface GridWeek {
|
||||
weekStart: string;
|
||||
weekEnd: string;
|
||||
positions: GridPosition[];
|
||||
instances: GridInstance[];
|
||||
}
|
||||
|
||||
/**
|
||||
* Grid data for one week (Sunday–Saturday). `weekStart` is a YYYY-MM-DD
|
||||
* Sunday. Instances are ordered by date then start time; slots within an
|
||||
* instance by slot index; positions by role sort order.
|
||||
*/
|
||||
function occursOnISO(d: Date): string {
|
||||
const y = d.getUTCFullYear();
|
||||
const m = String(d.getUTCMonth() + 1).padStart(2, "0");
|
||||
const day = String(d.getUTCDate()).padStart(2, "0");
|
||||
return `${y}-${m}-${day}`;
|
||||
}
|
||||
|
||||
/**
|
||||
* The `occurs_on` column stores a church-local calendar date. Drizzle returns a
|
||||
* `date` column as UTC midnight of that date, so the local date *is* the UTC
|
||||
* calendar date. Extract it directly rather than running it through
|
||||
* `dateInTimezone`, which would reinterpret the UTC-midnight instant as a wall
|
||||
* clock in the church timezone and shift the date a day early (e.g. 2026-11-23
|
||||
* stored as 2026-11-23T00:00:00Z would render as 2026-11-22 in America/New_York).
|
||||
*/
|
||||
export async function gridForWeek(weekStart: string): Promise<GridWeek> {
|
||||
const weekEnd = addDaysISO(weekStart, 6);
|
||||
const weekStartDate = new Date(`${weekStart}T00:00:00Z`);
|
||||
const weekEndDate = new Date(`${weekEnd}T00:00:00Z`);
|
||||
|
||||
const instances = await db
|
||||
.select()
|
||||
.from(serviceInstances)
|
||||
.where(
|
||||
and(
|
||||
gte(serviceInstances.occursOn, weekStartDate),
|
||||
lte(serviceInstances.occursOn, weekEndDate),
|
||||
),
|
||||
)
|
||||
.orderBy(serviceInstances.occursOn, serviceInstances.startsAt);
|
||||
|
||||
const roleRows = await db
|
||||
.select()
|
||||
.from(roles)
|
||||
.where(eq(roles.active, true))
|
||||
.orderBy(roles.sortOrder);
|
||||
const positions: GridPosition[] = roleRows.map((r) => ({
|
||||
slug: r.slug,
|
||||
name: r.name,
|
||||
colorHex: r.colorHex,
|
||||
}));
|
||||
const roleBySlug = new Map(positions.map((p) => [p.slug, p]));
|
||||
|
||||
const serviceById = new Map(
|
||||
(await db.select().from(services)).map((s) => [s.id, s.name]),
|
||||
);
|
||||
|
||||
const result: GridInstance[] = [];
|
||||
for (const inst of instances) {
|
||||
const slotRows = await db
|
||||
.select()
|
||||
.from(slots)
|
||||
.where(eq(slots.serviceInstanceId, inst.id))
|
||||
.orderBy(slots.slotIndex);
|
||||
|
||||
const slotsOut: GridSlot[] = [];
|
||||
for (const slot of slotRows) {
|
||||
const role = roleBySlug.get(slot.roleId);
|
||||
const assignRows = await db
|
||||
.select()
|
||||
.from(assignments)
|
||||
.where(eq(assignments.slotId, slot.id));
|
||||
const assignmentsOut: GridAssignment[] = [];
|
||||
for (const a of assignRows) {
|
||||
const [user] = await db
|
||||
.select()
|
||||
.from(users)
|
||||
.where(eq(users.id, a.userId));
|
||||
assignmentsOut.push({
|
||||
userId: a.userId,
|
||||
displayName: user?.displayName ?? "",
|
||||
status: a.status,
|
||||
});
|
||||
}
|
||||
slotsOut.push({
|
||||
id: slot.id,
|
||||
roleId: slot.roleId,
|
||||
roleSlug: role?.slug ?? "",
|
||||
roleName: role?.name ?? "",
|
||||
slotIndex: slot.slotIndex,
|
||||
callTime: slot.callTime,
|
||||
status: slot.status,
|
||||
isTraineeSlot: slot.isTraineeSlot,
|
||||
assignments: assignmentsOut,
|
||||
});
|
||||
}
|
||||
|
||||
result.push({
|
||||
id: inst.id,
|
||||
serviceId: inst.serviceId,
|
||||
serviceName: serviceById.get(inst.serviceId) ?? "",
|
||||
occursOn: inst.occursOn
|
||||
? occursOnISO(inst.occursOn)
|
||||
: "",
|
||||
startsAt: inst.startsAt ?? new Date(),
|
||||
titleOverride: inst.titleOverride ?? null,
|
||||
status: inst.status,
|
||||
slots: slotsOut,
|
||||
});
|
||||
}
|
||||
|
||||
return { weekStart, weekEnd, positions, instances: result };
|
||||
}
|
||||
|
||||
/**
|
||||
* The Sunday that starts the week of the earliest published instance at/after
|
||||
* today — the grid's default week, so a freshly seeded DB shows data on first
|
||||
* load. Falls back to the current week when there is no upcoming instance.
|
||||
*/
|
||||
export async function defaultWeekStart(): Promise<string> {
|
||||
const todayStart = new Date(
|
||||
`${dateInTimezone(new Date(), CHURCH_TIMEZONE)}T00:00:00Z`,
|
||||
);
|
||||
const [earliest] = await db
|
||||
.select({ occursOn: serviceInstances.occursOn })
|
||||
.from(serviceInstances)
|
||||
.where(
|
||||
and(
|
||||
eq(serviceInstances.status, "published"),
|
||||
gte(serviceInstances.occursOn, todayStart),
|
||||
),
|
||||
)
|
||||
.orderBy(serviceInstances.occursOn)
|
||||
.limit(1);
|
||||
|
||||
if (!earliest?.occursOn) {
|
||||
return weekStartOn(
|
||||
dateInTimezone(new Date(), CHURCH_TIMEZONE),
|
||||
CHURCH_TIMEZONE,
|
||||
);
|
||||
}
|
||||
return weekStartOn(occursOnISO(earliest.occursOn), CHURCH_TIMEZONE);
|
||||
}
|
||||
Reference in New Issue
Block a user