Files
petvity/lib/domain/scheduling.ts
T
GandClaude Fable 5 bb5615c0fa feat(bookings,shop): calendar conflict blocking + Stripe payment integration (#25)
* feat(bookings,shop): calendar conflict blocking + Stripe payment integration

Tier-1 scheduling: the booking API now rejects (409) any request that
overlaps a professional's active bookings or their self-declared blocked
dates; vets/sitters manage blocked ranges at /portal/professional-profile
(new professional_blocked_dates table + /api/availability CRUD).

Payments: full Stripe Checkout flow, env-gated like Resend — without
STRIPE_SECRET_KEY nothing changes; with it, checkout redirects to Stripe,
the signed webhook (/api/payments/webhook) marks orders paid, and unpaid
orders stay payable from /portal/orders (Pay now + paid/awaiting badges).
orders gains paid_at + checkout_session_id.

Critical bug found by the smoke test: the login JWT callback wrote
resolveRole(email) back to the DB, silently DEMOTING every vet/sitter to
pet_owner at their next sign-in (residents were safe only because they
never log in). ADMIN_EMAILS now only promotes; professional roles survive.

Verified end-to-end locally: sitter registers → blocks dates → owner
booking overlap 409, blocked-dates 409, disjoint 201; checkout w/o keys
falls back to recorded orders. verify green (1209 tests incl. 12 new).

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* chore: nudge CI (pull_request event was never delivered)

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
2026-08-13 13:49:47 +00:00

30 lines
1.1 KiB
TypeScript

/**
* Pure scheduling logic for professional bookings.
* Timestamps are half-open intervals [start, end); blocked dates are
* inclusive date-only ranges (whole days off).
*/
/** Booking statuses that occupy the professional's time. */
export const BLOCKING_BOOKING_STATUSES = ["pending", "confirmed"] as const;
/** Half-open interval overlap: [aStart, aEnd) ∩ [bStart, bEnd) ≠ ∅ */
export function intervalsOverlap(aStart: Date, aEnd: Date, bStart: Date, bEnd: Date): boolean {
return aStart < bEnd && bStart < aEnd;
}
/**
* Does the requested [start, end) touch an inclusive date-only blocked range?
* A range blocked 2026-08-20..2026-08-21 occupies those two whole days in UTC.
*/
export function overlapsBlockedRange(
start: Date,
end: Date,
blockedStartDate: string,
blockedEndDate: string,
): boolean {
const blockStart = new Date(`${blockedStartDate}T00:00:00.000Z`);
const blockEnd = new Date(`${blockedEndDate}T00:00:00.000Z`);
blockEnd.setUTCDate(blockEnd.getUTCDate() + 1); // inclusive → exclusive upper bound
return intervalsOverlap(start, end, blockStart, blockEnd);
}