68 lines
2.8 KiB
TypeScript
68 lines
2.8 KiB
TypeScript
// AK-P16a · availability extraction (PURE) — the leak-protection boundary (DoD 3).
|
|
//
|
|
// External calendar events carry private content (title/description/attendees).
|
|
// Outside the owning user's own views, the ONLY thing other users (and the Booking
|
|
// slot calculator in AK-P16b) may learn is "busy from X to Y" — never a title or
|
|
// any other field. This module is the single chokepoint that turns rich events
|
|
// into anonymous busy intervals, so the leak-protection is enforced in one pure,
|
|
// unit-tested place rather than scattered across queries.
|
|
//
|
|
// No Prisma / no React import — pure functions over plain inputs.
|
|
|
|
/** The minimal event fields availability needs (a structural subset). */
|
|
export interface BusyCandidate {
|
|
readonly startsAt: Date;
|
|
readonly endsAt: Date;
|
|
readonly isBusyForAvailability: boolean;
|
|
readonly archivedAt: Date | null;
|
|
}
|
|
|
|
/** An anonymous busy interval — deliberately NO title/description/owner field. */
|
|
export interface BusyInterval {
|
|
readonly from: Date;
|
|
readonly to: Date;
|
|
}
|
|
|
|
/**
|
|
* Project events to anonymous busy intervals. Drops events that are not busy
|
|
* (provider "Free"), archived/cancelled, or zero/negative length. The result
|
|
* contains ONLY {from,to} — by construction it cannot leak event content (DoD 3).
|
|
*/
|
|
export function extractBusyIntervals(events: readonly BusyCandidate[]): BusyInterval[] {
|
|
return events
|
|
.filter((e) => e.isBusyForAvailability && e.archivedAt === null && e.endsAt.getTime() > e.startsAt.getTime())
|
|
.map((e) => ({ from: e.startsAt, to: e.endsAt }))
|
|
.sort((a, b) => a.from.getTime() - b.from.getTime());
|
|
}
|
|
|
|
/**
|
|
* Merge overlapping/adjacent busy intervals into a minimal set. Used by the
|
|
* Booking slot calculator (AK-P16b) so back-to-back external events read as one
|
|
* contiguous busy block. Input need not be sorted.
|
|
*/
|
|
export function mergeBusyIntervals(intervals: readonly BusyInterval[]): BusyInterval[] {
|
|
if (intervals.length === 0) {
|
|
return [];
|
|
}
|
|
const sorted = [...intervals].sort((a, b) => a.from.getTime() - b.from.getTime());
|
|
const merged: BusyInterval[] = [{ from: sorted[0].from, to: sorted[0].to }];
|
|
for (let i = 1; i < sorted.length; i += 1) {
|
|
const last = merged[merged.length - 1];
|
|
const cur = sorted[i];
|
|
if (cur.from.getTime() <= last.to.getTime()) {
|
|
// Overlapping or touching → extend the current block.
|
|
if (cur.to.getTime() > last.to.getTime()) {
|
|
merged[merged.length - 1] = { from: last.from, to: cur.to };
|
|
}
|
|
} else {
|
|
merged.push({ from: cur.from, to: cur.to });
|
|
}
|
|
}
|
|
return merged;
|
|
}
|
|
|
|
/** True when [from,to) overlaps any busy interval (used for anti-double-booking). */
|
|
export function overlapsBusy(from: Date, to: Date, intervals: readonly BusyInterval[]): boolean {
|
|
return intervals.some((iv) => from.getTime() < iv.to.getTime() && to.getTime() > iv.from.getTime());
|
|
}
|