Files
glenloch-media/drizzle/0008_needy_stryfe.sql
T
Tiel Coder deb4f35fb8 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).
2026-11-18 11:25:43 +00:00

19 lines
1.1 KiB
SQL

CREATE TABLE "unavailability" (
"id" text PRIMARY KEY NOT NULL,
"user_id" text NOT NULL,
"kind" text DEFAULT 'blackout' NOT NULL,
"starts_on" date NOT NULL,
"ends_on" date NOT NULL,
"all_day" boolean DEFAULT true NOT NULL,
"reason" text,
"rrule" text,
"status" text DEFAULT 'active' NOT NULL,
"created_at" timestamp with time zone DEFAULT now(),
"created_by" text,
CONSTRAINT "chk_unavailability_span" CHECK ("unavailability"."ends_on" >= "unavailability"."starts_on"),
CONSTRAINT "chk_unavailability_max_span" CHECK ("unavailability"."ends_on" - "unavailability"."starts_on" <= 365)
);
--> statement-breakpoint
ALTER TABLE "unavailability" ADD CONSTRAINT "unavailability_user_id_users_id_fk" FOREIGN KEY ("user_id") REFERENCES "public"."users"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
CREATE UNIQUE INDEX "uq_unavailability_range" ON "unavailability" USING btree ("user_id","starts_on","ends_on") WHERE "unavailability"."status" = 'active';--> statement-breakpoint
CREATE INDEX "unavailability_user_idx" ON "unavailability" USING btree ("user_id");