perf(appointment): resolve next_available_at in one pass per location
next_available_at called getAvailableSlots() once per day for up to 30 days, and that helper re-read the schedule, holidays and overrides on every call and then issued an isSlotTaken() query per candidate slot. Cost grew with both the days scanned and the slots per day, multiplied by the number of locations. findNextAvailableStart() fetches the schedule, holidays, overrides and blocking intervals once for the whole window and walks the days in memory. Measured on the dev data (a doctor with two locations, first opening several days out): 73 -> 20 queries for one request. The gap widens as locations or the distance to the first opening grow. Reserve appointments must keep blocking here: findBusyIntervals() filters isReserve = false, so reusing it would have reported a reserved slot as free. Added findBlockingIntervals(), which mirrors isSlotTaken()'s predicate, and factored both onto a shared builder. Verified the endpoint returns identical timestamps before and after. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -773,7 +773,12 @@ location — picking one and hiding the rest removes real capacity from the doct
|
||||
| `booking_mode` | `"slot" \| "service"` | per-context — the same doctor can differ between locations |
|
||||
| `opening_hours` | `array` | active weekly shifts of that context, flattened; `day` is the English weekday name so it maps straight onto schema.org `openingHoursSpecification` |
|
||||
| `services` | `array` | populated only in `service` mode, scoped to that context's owner |
|
||||
| `next_available_at` | `int\|null` | Unix timestamp of the earliest free slot within 30 days |
|
||||
| `next_available_at` | `int\|null` | Unix timestamp of the earliest free slot within 30 days, capped by the context's booking window |
|
||||
|
||||
`next_available_at` is resolved by `SlotCalculatorService::findNextAvailableStart()`, which fetches
|
||||
the schedule, holidays, overrides and taken appointments once per location and walks the days in
|
||||
memory. It counts a reserve appointment as blocking, matching `isSlotTaken()` — the looser
|
||||
`findBusyIntervals()` used by service-mode slot generation would report such a slot as free.
|
||||
|
||||
Sorted by `next_available_at` ascending, so `booking_locations[0]` is the sensible default
|
||||
selection; locations with no capacity sort last. Deep links should carry the chosen location
|
||||
|
||||
Reference in New Issue
Block a user