Introducing a new way to review your PRs.

Read the announcement

calicastle / cali.so

Personal website for Cali Castle

View on GitHub

21 documents · Updated Sep 21, 2026

docs / ama-availability

AMA availability

The AMA availability engine turns Cali's working plan and current time conflicts into bookable times for paid AMA Sessions. It combines recurring Availability Windows, Date Overrides, the Schedule Time Zone, booking policy, Google Calendar free/busy data, Slot Holds, and existing Bookings before the public booking flow can show a time. See AMA booking lifecycle for what happens after a guest chooses a time and AMA external integrations for provider behavior.

Availability model

Schedule inputs

The schedule is the owner-side plan for when AMA Sessions may take place. The repository returns one Schedule Time Zone, weekday states, recurring Availability Windows, and Date Overrides as one schedule view for the engine.

The Schedule Time Zone is an IANA time zone used to interpret the schedule. It is separate from the Guest's display time zone. If no saved time zone exists, the repository uses Asia/Taipei.

InputPurpose
Availability WindowRecurring same-day hours for one ISO weekday.
Weekday stateEnables or disables a recurring weekday while preserving its saved windows.
Date OverrideReplaces the recurring windows for one schedule date. An empty override closes that date.
Schedule Time ZoneGives local meaning to weekday, date, and clock-minute values.

The admin availability interface changes these inputs through schedule mutations. It can replace a weekday's hours, copy one weekday to others, enable or disable a weekday, set the Schedule Time Zone, and save or remove a Date Override. The service validates dates, time zones, same-day intervals, and non-overlapping weekday intervals before the repository saves them.

References: lib/…/engine.ts• L5-26 lib/…/service.ts• L19-49 lib/…/service.ts• L193-240 lib/…/repository.ts• L34-84 lib/…/repository.ts• L121-176 lib/…/http.ts• L260-339 db/…/0002_ama_availability.sql• L1-14 db/…/0014_ama_availability_overrides.sql• L1-31 db/…/0015_ama_availability_weekdays.sql• L1-7

Booking policy

The booking policy limits which schedule times can become bookable. It gives each AMA Session a fixed duration and keeps the public list useful for near-term planning without allowing last-minute starts.

RuleValueEffect
Session length60 minutesA candidate must fit inside its schedule interval.
Minimum notice24 hoursA start before this point is not offered.
Booking horizon30 daysA start at or after this limit is not offered.
Start cadence30 minutesCandidate starts use half-hour steps.
Buffer before15 minutesA conflict before a session can remove the candidate.
Buffer after15 minutesA conflict after a session can remove the candidate.

The engine applies these rules after it resolves schedule dates in the Schedule Time Zone. This keeps the policy stable even when the schedule crosses daylight-saving changes or the guest views the result in another time zone.

References: lib/…/policy.ts• L1-9 lib/…/engine.ts• L138-145 lib/…/engine.ts• L193-239

Calendar and booking blockers

The engine treats a scheduled hour as only a possible time. It removes a candidate when its buffered interval overlaps Google Calendar busy time, an active Slot Hold, or a Booking. Expired Slot Holds do not block availability.

Google free/busy is read for the policy range around the current time. The calendar service refreshes the stored connection before it asks Google for busy intervals. If the connection or provider cannot supply trusted free/busy data, the availability service does not present times.

The booking service supplies current Slot Holds and Bookings from its claim store for the same calculation. This means the public list reflects both outside calendar commitments and claims already made inside the AMA system.

References: lib/…/engine.ts• L171-179 lib/…/engine.ts• L221-239 lib/…/service.ts• L261-289 lib/…/service.ts• L216-269 lib/…/service.ts• L177-193

Availability evaluation

Candidate generation

For each date from now through the booking horizon, the engine chooses the date's effective hours. A Date Override replaces the recurring week for that date. If there is no override, a disabled weekday has no hours; otherwise the engine uses that weekday's recurring windows.

The engine converts each local boundary into an instant in the Schedule Time Zone. It handles daylight-saving gaps and repeated clock times so that the returned interval is real and ordered. It then tests half-hour candidate starts and keeps only starts whose full 60-minute session fits inside the effective hours and policy limits.

The result is a sorted, duplicate-free list of startsAt and endsAt instants. The list does not contain owner schedule metadata or guest identity.

References: lib/…/engine.ts• L57-101 lib/…/engine.ts• L162-169 lib/…/engine.ts• L180-247

Preview diagnosis

The availability service uses a preview sequence to explain why no time can be shown. It loads schedule data and calendar free/busy data in parallel, then evaluates the same engine in stages.

  1. It checks whether any recurring or override hours exist.
  2. It requires a connected calendar before it exposes availability.
  3. It checks for times that pass schedule and booking policy without calendar blockers.
  4. It applies Google busy intervals.
  5. It applies active Slot Holds and Bookings.

The diagnosis distinguishes an empty schedule from a calendar outage and from hours removed by policy or conflicts. The public booking service keeps only the connected or unavailable result and the final list. Admin preview consumers can use the diagnosis to tell an owner what needs attention.

This diagram shows how stored schedule data and live blockers become the final availability result.

flowchart TD
    A[Schedule repository] --> C[Availability preview]
    B[Google free/busy] --> C
    D[Active Slot Holds and Bookings] --> C
    C --> E[Apply Date Overrides and weekday states]
    E --> F[Apply session policy]
    F --> G[Remove calendar conflicts]
    G --> H[Remove holds and bookings]
    H --> I[Sorted bookable times]
    C --> J[Unavailable or empty diagnosis]

References: lib/…/service.ts• L66-72 lib/…/service.ts• L261-361 lib/…/service.ts• L177-193

Schedule administration

The owner changes schedule inputs through the admin availability route. The service is the boundary that checks values before they reach stored schedule data, while the repository keeps weekday states, windows, and overrides consistent.

Weekday edits replace all intervals for that weekday as one operation. A Date Override is also replaced as one date record: saving it removes its old intervals before saving the new set. This supports both a closed date and custom hours without changing the recurring schedule.

References: lib/…/http.ts• L243-344 lib/…/service.ts• L101-178 lib/…/repository.ts• L86-119 lib/…/repository.ts• L145-176 lib/…/repository.ts• L241-255

Public slot interface

Slot API

The public GET /api/ama/slots route asks the booking service for the current availability list. The route returns ISO timestamps for each bookable time when the calendar is connected; when availability is not safe to publish, it returns an unavailable status with an empty list.

The API does not promise that a returned time remains free. A guest must still obtain a Slot Hold before payment. The hold step protects the selected time against changes that occur after the list was read.

ResultMeaning
status: availableThe response contains current candidate times.
status: unavailableThe system cannot safely present times. The response contains no candidates.
HTTP 503A dependency failed while the route was serving the request.

References: app/…/route.ts• L1-7 lib/…/http.ts• L104-125 lib/…/service.ts• L177-193

Guest time display

The booking flow fetches the slot list and groups its ISO instants by calendar date in the guest's selected display time zone. The guest can change that display zone without changing the Schedule Time Zone or recalculating the owner's hours in a new local clock.

The picker enables only dates that have returned times. It shows the times for the selected date and keeps the selected start only while it remains in the latest response. If the API is unavailable or returns no usable times, the flow offers an Alternate Time Request instead of presenting an empty choice as bookable.

References: components/…/booking-flow.tsx• L353-423 components/…/booking-flow.tsx• L846-872 components/…/slot-picker.tsx• L18-31 components/…/slot-picker.tsx• L70-192 components/…/slot-picker.tsx• L227-280

Change checklist

When these files or sections are changed, remember to consider these:

  • Keep the policy range used for Google free/busy wide enough to cover the engine's notice, horizon, session, and buffer boundaries.
  • Keep the precedence between recurring windows, weekday states, and Date Overrides aligned in the engine and in the admin schedule model.
  • Keep the preview diagnosis mapping aligned with the booking service's public available or unavailable contract.
  • Keep ISO instant output aligned with the picker grouping and guest time-zone display rules.
  • Keep interval validation aligned across service checks and the database constraints so invalid or overlapping schedule data cannot reach candidate generation.
  • When changing Slot Hold or Booking claim data, preserve the blocker shape and expiry behavior consumed by computeSlots.

Important failure modes

TriggerConsequenceGuard
Google Calendar is disconnected, expired, revoked, or unavailableThe preview returns no slots and the public API reports unavailable.Require a connected calendar result before publishing times.
A Date Override has no intervalsThe date is closed even if its recurring weekday has hours.Treat an empty override as a full replacement, not as “use the week.”
A Slot Hold is still active or a Booking overlaps a candidate's buffered intervalThe candidate is removed from the public list.Filter expired holds and test all blockers with the before and after buffers.
The schedule has no policy-eligible timeThe preview reports no-policy-eligible-hours and returns no slots.Apply notice, horizon, session length, and cadence before checking live conflicts.
A daylight-saving boundary makes a local clock time missing or repeatedA candidate can have an invalid or ambiguous instant.Resolve boundaries and possible starts in the Schedule Time Zone, then keep only ordered real intervals.
The slot response is stale when the guest submits intakeThe selected time may be rejected as stale or taken during hold creation.Treat the public list as a read view and require a fresh Slot Hold before checkout.

Key files

FileWhy to read it
lib/…/engine.tsDefines the high-level conversion from schedule inputs and blockers to bookable times.
lib/…/policy.tsDefines the shared session and time-bound rules.
lib/…/service.tsCoordinates repository data, calendar preview, validation, and diagnosis.
lib/…/repository.tsShows how schedule data is read and how owner changes are persisted.
lib/…/service.tsConnects availability preview to current Slot Holds, Bookings, and the public booking contract.
lib/…/service.tsShows how calendar connection state becomes trusted free/busy input.
app/…/route.tsIdentifies the public slot endpoint.
lib/…/http.tsDefines the public response shape and unavailable behavior.
components/…/slot-picker.tsxShows how returned instants become guest-facing dates and times.
db/…/0014_ama_availability_overrides.sqlDefines stored Date Overrides and the Schedule Time Zone record.
db/…/0015_ama_availability_weekdays.sqlDefines persisted weekday enable states.