unit(D4): blackout CRUD + isUnavailable(user, date)

Blackout / unavailability (ARCHITECTURE §4.7 / §5.3 rule 5 / §5.4):
- src/domain/availability.ts — pure isUnavailable(records, date); a record
  covers the date if active and its inclusive range [starts_on, ends_on]
  contains it OR its rrule matches. No DB imports.
- src/domain/availability.test.ts — 11 tests named after the rule, incl.
  the inclusive-end case (2026-11-21..2026-11-28 covers 11-28).
- src/db/schema.ts — unavailability table: CHECK ends_on >= starts_on,
  CHECK span <= 365 days, unique (user_id, starts_on, ends_on) WHERE active.
- src/db/unavailability.ts — list/get/create/update/cancel (soft-delete);
  range + rrule validated on write.
- src/app/api/dev/unavailability/route.ts — GET/POST/PATCH; bad range/rrule
  -> 400, cancel flips status=cancelled.
- drizzle/0008_needy_stryfe.sql + migration applied to DB on :5433.

date columns stored as strings (mode: string) to match the YYYY-MM-DD
domain; Postgres date is mode-agnostic so the migration is unchanged.
Constraints enforced twice: validateUnavailabilityRange at the API,
chk_unavailability_* at the DB.

.env added to .gitignore (real secret was not ignored; .env.* missed the
bare .env).
This commit is contained in:
Tiel Coder
2026-11-18 11:25:43 +00:00
parent 8ee339b235
commit deb4f35fb8
10 changed files with 3012 additions and 2 deletions
+92
View File
@@ -0,0 +1,92 @@
import { describe, expect, it } from "vitest";
import {
isUnavailable,
overlapsRange,
recordCovers,
type UnavailabilityRecord,
} from "./availability";
const record = (partial: Partial<UnavailabilityRecord>): UnavailabilityRecord => ({
id: "u1",
userId: "user-1",
kind: "blackout",
startsOn: "2026-11-21",
endsOn: "2026-11-28",
allDay: true,
reason: null,
rrule: null,
status: "active",
createdAt: new Date(),
createdBy: null,
...partial,
});
describe("overlapsRange — inclusive on both ends", () => {
it("covers the start date", () => {
expect(overlapsRange("2026-11-21", "2026-11-21", "2026-11-28")).toBe(true);
});
it("covers the END date (inclusive-end case)", () => {
expect(overlapsRange("2026-11-28", "2026-11-21", "2026-11-28")).toBe(true);
});
it("excludes the day before start and the day after end", () => {
expect(overlapsRange("2026-11-20", "2026-11-21", "2026-11-28")).toBe(false);
expect(overlapsRange("2026-11-29", "2026-11-21", "2026-11-28")).toBe(false);
});
it("a same-day blackout covers exactly that day", () => {
expect(overlapsRange("2026-12-25", "2026-12-25", "2026-12-25")).toBe(true);
expect(overlapsRange("2026-12-24", "2026-12-25", "2026-12-25")).toBe(false);
});
});
describe("recordCovers", () => {
it("honours an active range", () => {
expect(recordCovers(record({}), "2026-11-25")).toBe(true);
});
it("a cancelled record never covers", () => {
expect(recordCovers(record({ status: "cancelled" }), "2026-11-25")).toBe(false);
});
it("a recurring rule matches its weekday", () => {
// Every other Sunday starting 2026-11-22: 11-22, 12-06 match; 11-29 does
// not (it is the skipped Sunday of the 2-week interval).
const r = record({
startsOn: "2026-11-22",
endsOn: "2027-12-31",
rrule: "FREQ=WEEKLY;INTERVAL=2;BYDAY=SU",
});
expect(recordCovers(r, "2026-11-22")).toBe(true);
expect(recordCovers(r, "2026-12-06")).toBe(true);
// The intervening Sunday is outside the 2-week interval.
expect(recordCovers(r, "2026-11-29")).toBe(false);
});
it("a malformed rrule does not throw and does not match", () => {
const r = record({ rrule: "FREQ=MONTHLY;BOGUS" });
expect(() => recordCovers(r, "2026-11-25")).not.toThrow();
expect(recordCovers(r, "2026-11-25")).toBe(false);
});
});
describe("isUnavailable", () => {
it("true when any active record covers the date", () => {
const records = [
record({ id: "a", startsOn: "2026-11-01", endsOn: "2026-11-10" }),
record({ id: "b", startsOn: "2026-11-25", endsOn: "2026-11-28" }),
];
expect(isUnavailable(records, "2026-11-28")).toBe(true);
expect(isUnavailable(records, "2026-11-05")).toBe(true);
});
it("false when no record covers the date", () => {
const records = [record({ startsOn: "2026-11-25", endsOn: "2026-11-28" })];
expect(isUnavailable(records, "2026-12-25")).toBe(false);
});
it("false for an empty record set", () => {
expect(isUnavailable([], "2026-11-25")).toBe(false);
});
});
+63
View File
@@ -0,0 +1,63 @@
/**
* Availability — unit D4 (ARCHITECTURE §"unavailability — blackout dates").
*
* Pure: no DB, no `src/db` imports. `isUnavailable(userRecords, date)` answers
* whether a volunteer is out on a given local date (YYYY-MM-DD), so the assign
* picker and auto-fill can exclude them.
*
* A record covers the date if it is `active` and either:
* - its inclusive range `[starts_on, ends_on]` contains the date, or
* - its `rrule` (RFC 5545 subset) matches the date.
*
* Range bounds are INCLUSIVE on both ends: a blackout 2026-11-21..2026-11-28
* covers 2026-11-28 (the end date), not just up to 2026-11-27.
*/
import { parseRecurrence, matchesRecurrence, validateRecurrence } from "./recurrence";
export type UnavailabilityKind = "blackout" | "vacation" | "recurring" | "preference";
export type UnavailabilityStatus = "active" | "cancelled";
export interface UnavailabilityRecord {
id: string;
userId: string;
kind: UnavailabilityKind;
/** Inclusive local-date range; same day = one day. */
startsOn: string;
endsOn: string;
allDay: boolean;
reason: string | null;
/** Recurring pattern (RFC 5545 subset), e.g. "FREQ=WEEKLY;INTERVAL=2". */
rrule: string | null;
status: UnavailabilityStatus;
createdAt: Date;
createdBy: string | null;
}
/**
* Inclusive range check: `startsOn <= date <= endsOn`. ISO (YYYY-MM-DD) strings
* compare correctly lexicographically, so no date parsing is needed here.
*/
export function overlapsRange(date: string, startsOn: string, endsOn: string): boolean {
return date >= startsOn && date <= endsOn;
}
/** Is this single record active and covering `date`? */
export function recordCovers(record: UnavailabilityRecord, date: string): boolean {
if (record.status !== "active") return false;
// A recurring rule is authoritative for coverage; the range (if any) is just
// metadata and is not used to over-cover non-matching dates.
if (record.rrule) {
// Guard the rrule: parseRecurrence() throws on malformed input, so only
// match when validateRecurrence() reports the rule is well-formed.
return validateRecurrence(record.rrule).length === 0
&& matchesRecurrence(date, parseRecurrence(record.rrule));
}
// Range-only blackout: inclusive on both ends.
return overlapsRange(date, record.startsOn, record.endsOn);
}
/** Is the user unavailable on `date` (YYYY-MM-DD)? */
export function isUnavailable(records: UnavailabilityRecord[], date: string): boolean {
return records.some((record) => recordCovers(record, date));
}