unit(D3): slot materialization (headcount, call time)
Slot materialization (ARCHITECTURE §5.2): for each non-published instance, plan the seats planSlots() computes and upsert them on (service_instance_id, role_id, slot_index). onConflictDoNothing makes it idempotent; published weeks are skipped (owned by the publish flow). - src/domain/slots.ts: pure planSlots()/planSlotsForRole(); headcount fills slotIndex 1..N, call_time = start + offset (null if no offset). - src/domain/slots.test.ts: 5 tests (Audio headcount 2 -> 2 slots with correct call_time, clamp to 1, null offset, multi-role flatten). - src/db/slots.ts: materializeSlotsForInstance() + materializeSlots(). - src/app/api/dev/cron/slots/route.ts: GET drives the daily job. No schema change (slots unique index predates this unit; position_skills stays deferred to D5). Verified: draft instance + Audio headcount 2 -> 4 seats (Audio x2 + Livestream + Slides), Audio call_time 09:30 EST; re-run creates 0 rows. Seed restored to canonical 4 instances / 12 slots after testing.
This commit is contained in:
@@ -0,0 +1,52 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { planSlots, planSlotsForRole } from "./slots";
|
||||
|
||||
const audioRole = {
|
||||
id: "audio",
|
||||
callTimeOffsetMinutes: -90,
|
||||
defaultHeadcount: 2,
|
||||
};
|
||||
|
||||
describe("planSlotsForRole — headcount", () => {
|
||||
it("Audio headcount 2 creates 2 slots with correct call_time", () => {
|
||||
const startsAt = new Date(Date.UTC(2026, 10, 22, 16, 0, 0)); // 11:00 EST
|
||||
const seats = planSlotsForRole(startsAt, audioRole);
|
||||
expect(seats.map((s) => s.slotIndex)).toEqual([1, 2]);
|
||||
expect(seats).toHaveLength(2);
|
||||
// call_time = start + (-90 min) = 09:30 EST = 14:30 UTC.
|
||||
expect(seats.every((s) => s.callTime !== null)).toBe(true);
|
||||
expect(seats.map((s) => s.callTime!.getUTCHours())).toEqual([14, 14]);
|
||||
expect(seats.map((s) => s.callTime!.getUTCMinutes())).toEqual([30, 30]);
|
||||
});
|
||||
|
||||
it("clamps headcount to at least 1", () => {
|
||||
const seats = planSlotsForRole(new Date(), {
|
||||
id: "coffee",
|
||||
callTimeOffsetMinutes: null,
|
||||
defaultHeadcount: 0,
|
||||
});
|
||||
expect(seats).toHaveLength(1);
|
||||
expect(seats[0].slotIndex).toBe(1);
|
||||
});
|
||||
|
||||
it("call_time is null when the role has no offset", () => {
|
||||
const seats = planSlotsForRole(new Date(Date.UTC(2026, 10, 22, 16, 0, 0)), {
|
||||
id: "welcome",
|
||||
callTimeOffsetMinutes: null,
|
||||
defaultHeadcount: 1,
|
||||
});
|
||||
expect(seats[0].callTime).toBeNull();
|
||||
});
|
||||
});
|
||||
|
||||
describe("planSlots — multiple roles", () => {
|
||||
it("flattens every role's seats in role order", () => {
|
||||
const startsAt = new Date(Date.UTC(2026, 10, 22, 16, 0, 0));
|
||||
const seats = planSlots(startsAt, [
|
||||
{ id: "audio", callTimeOffsetMinutes: -90, defaultHeadcount: 2 },
|
||||
{ id: "livestream", callTimeOffsetMinutes: -60, defaultHeadcount: 1 },
|
||||
]);
|
||||
expect(seats.map((s) => s.slotIndex)).toEqual([1, 2, 1]);
|
||||
expect(seats.map((s) => s.roleId)).toEqual(["audio", "audio", "livestream"]);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,54 @@
|
||||
/**
|
||||
* Slot planning — unit D3 (ARCHITECTURE §5.2).
|
||||
*
|
||||
* Pure: no DB, no `src/db` imports. `planSlots()` turns an instance's start
|
||||
* instant plus the role requirements into the seats that role fills. The data
|
||||
* layer (`src/db/slots.ts`) upserts these; the cron route drives it.
|
||||
*
|
||||
* A role with headcount N fills N seats (slotIndex 1..N). Each seat's call time
|
||||
* is the instance start plus the role's call-time offset (null when the role
|
||||
* has no offset), matching the seed.
|
||||
*/
|
||||
|
||||
export interface RolePlanInput {
|
||||
id: string;
|
||||
callTimeOffsetMinutes: number | null;
|
||||
defaultHeadcount: number;
|
||||
}
|
||||
|
||||
export interface SlotPlan {
|
||||
roleId: string;
|
||||
/** 1..headcount, for labels like "Audio 2". */
|
||||
slotIndex: number;
|
||||
/** instance.startsAt + offset; null when the role has no offset. */
|
||||
callTime: Date | null;
|
||||
}
|
||||
|
||||
/**
|
||||
* The seats a single role fills for an instance. Headcount is clamped to >= 1
|
||||
* so a role always produces at least one seat.
|
||||
*/
|
||||
export function planSlotsForRole(
|
||||
instanceStartsAt: Date,
|
||||
role: RolePlanInput,
|
||||
): SlotPlan[] {
|
||||
const headcount = Math.max(1, role.defaultHeadcount);
|
||||
const offsetMs = role.callTimeOffsetMinutes != null
|
||||
? role.callTimeOffsetMinutes * 60_000
|
||||
: 0;
|
||||
return Array.from({ length: headcount }, (_, i) => ({
|
||||
roleId: role.id,
|
||||
slotIndex: i + 1,
|
||||
callTime: role.callTimeOffsetMinutes != null
|
||||
? new Date(instanceStartsAt.getTime() + offsetMs)
|
||||
: null,
|
||||
}));
|
||||
}
|
||||
|
||||
/** Every seat for every role in an instance. */
|
||||
export function planSlots(
|
||||
instanceStartsAt: Date,
|
||||
roles: RolePlanInput[],
|
||||
): SlotPlan[] {
|
||||
return roles.flatMap((role) => planSlotsForRole(instanceStartsAt, role));
|
||||
}
|
||||
Reference in New Issue
Block a user