unit(D1): service CRUD + expandRRULE()
Service template CRUD (create/read/update/deactivate) and the pure expandRRULE() recurrence expander for the RFC 5545 subset (WEEKLY, BIWEEKLY via INTERVAL=2, MONTHLY nth weekday). - src/domain/recurrence.ts: expandRRULE, parseRecurrence, matchesRecurrence, validateRecurrence. Pure — no DB imports. - src/domain/recurrence.test.ts: 13 tests, incl. the 5 required recurrence cases (spring-forward / fall-back Sunday, monthly-nth across a year boundary, cancelled instance not regenerated, biweekly phase stable). - src/db/services.ts + src/app/api/dev/services/route.ts: list/get/ create/update/deactivate; rrule validated on write; deactivate flips active (never delete); 409 on duplicate name. No schema change (services table predates this unit). DST stays in the caller's localToUtc; interval phase anchored to a fixed epoch so biweekly never drifts.
This commit is contained in:
@@ -0,0 +1,104 @@
|
||||
/**
|
||||
* Service templates (ARCHITECTURE §4.3). D1 writes these — the recurring
|
||||
* gathering template that instances (D2) and slots (D3) are built from.
|
||||
*
|
||||
* Integrity rules:
|
||||
* - Deactivate, never delete — `active` flips to false; the row persists so
|
||||
* historical instances and their assignments are preserved.
|
||||
* - `rrule` is validated on write (RFC 5545 subset) so a malformed rule can
|
||||
* never reach the instance generator.
|
||||
* - `name` is unique and frozen on update (no update path accepts it).
|
||||
*
|
||||
* Read path: `listServices({ active: true })` returns only rows shown in the
|
||||
* admin services list; `active: false` returns the full set (audit).
|
||||
*/
|
||||
|
||||
import { eq } from "drizzle-orm";
|
||||
import { db } from "@/db";
|
||||
import { services } from "@/db/schema";
|
||||
import { uuidv7 } from "@/lib/uuid";
|
||||
import { validateRecurrence } from "@/domain/recurrence";
|
||||
|
||||
type ServiceInsert = typeof services.$inferInsert;
|
||||
|
||||
export type ServiceCreate = Omit<ServiceInsert, "id" | "active"> & { active?: boolean };
|
||||
export type ServiceUpdate = Partial<Omit<ServiceInsert, "id" | "active">>;
|
||||
|
||||
const notNull = <T>(value: T | null | undefined): T | undefined =>
|
||||
value === null || value === undefined ? undefined : value;
|
||||
|
||||
export async function listServices({ active }: { active?: boolean }) {
|
||||
const rows = await db
|
||||
.select()
|
||||
.from(services)
|
||||
.where(active === false ? undefined : eq(services.active, active ?? true))
|
||||
.orderBy(services.name);
|
||||
return rows;
|
||||
}
|
||||
|
||||
export async function getService(id: string) {
|
||||
const [row] = await db.select().from(services).where(eq(services.id, id));
|
||||
return row ?? null;
|
||||
}
|
||||
|
||||
export async function createService(fields: ServiceCreate) {
|
||||
const problems = validateRecurrence(fields.rrule ?? "");
|
||||
if (fields.rrule != null && problems.length > 0) {
|
||||
throw new Error(`invalid recurrence: ${problems.join("; ")}`);
|
||||
}
|
||||
|
||||
const values: ServiceInsert = {
|
||||
id: uuidv7(),
|
||||
name: fields.name,
|
||||
location: notNull(fields.location),
|
||||
timezone: notNull(fields.timezone),
|
||||
rrule: notNull(fields.rrule),
|
||||
startTimeLocal: fields.startTimeLocal,
|
||||
durationMinutes: notNull(fields.durationMinutes),
|
||||
doorsOpenMinutesBefore: notNull(fields.doorsOpenMinutesBefore),
|
||||
notes: notNull(fields.notes),
|
||||
active: fields.active ?? true,
|
||||
};
|
||||
|
||||
const [row] = await db.insert(services).values(values).returning();
|
||||
return row;
|
||||
}
|
||||
|
||||
export async function updateService(id: string, fields: ServiceUpdate) {
|
||||
// A recurrence change is validated before it is written so a bad rule never
|
||||
// reaches the generator.
|
||||
if (fields.rrule != null) {
|
||||
const problems = validateRecurrence(fields.rrule);
|
||||
if (problems.length > 0) {
|
||||
throw new Error(`invalid recurrence: ${problems.join("; ")}`);
|
||||
}
|
||||
}
|
||||
|
||||
const patch: Record<string, unknown> = {};
|
||||
const editable: (keyof ServiceUpdate)[] = [
|
||||
"location",
|
||||
"timezone",
|
||||
"rrule",
|
||||
"startTimeLocal",
|
||||
"durationMinutes",
|
||||
"doorsOpenMinutesBefore",
|
||||
"notes",
|
||||
];
|
||||
for (const key of editable) {
|
||||
if (fields[key] !== undefined) patch[key] = fields[key];
|
||||
}
|
||||
|
||||
if (Object.keys(patch).length === 0) return await getService(id);
|
||||
|
||||
const [row] = await db.update(services).set(patch).where(eq(services.id, id)).returning();
|
||||
return row;
|
||||
}
|
||||
|
||||
export async function deactivateService(id: string) {
|
||||
const [row] = await db
|
||||
.update(services)
|
||||
.set({ active: false })
|
||||
.where(eq(services.id, id))
|
||||
.returning();
|
||||
return row;
|
||||
}
|
||||
Reference in New Issue
Block a user