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:
@@ -292,7 +292,7 @@ class AppointmentController extends BaseController
|
||||
'services' => $meta['booking_mode'] === WeeklySchedule::MODE_SERVICE
|
||||
? $this->bookableServices($doctor, $clinic)
|
||||
: [],
|
||||
'next_available_at' => $this->nextAvailableAt($doctor, $clinic),
|
||||
'next_available_at' => $this->slotCalculator->findNextAvailableStart($doctor, $clinic),
|
||||
];
|
||||
}
|
||||
|
||||
@@ -757,20 +757,6 @@ class AppointmentController extends BaseController
|
||||
return $hours;
|
||||
}
|
||||
|
||||
/** زودترین اسلات آزاد در ۳۰ روز آینده، یا null اگر ظرفیتی نباشد. */
|
||||
private function nextAvailableAt(Doctor $doctor, ?Clinic $clinic): ?int
|
||||
{
|
||||
for ($i = 0; $i < 30; $i++) {
|
||||
$date = date('Y-m-d', strtotime("today +{$i} day"));
|
||||
$slots = $this->slotCalculator->getAvailableSlots($doctor, $date, $clinic);
|
||||
if (!empty($slots)) {
|
||||
return (int) $slots[0]['start'];
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
#[OA\Patch(
|
||||
path: '/api/v1/appointment/{uuid}/status',
|
||||
|
||||
Reference in New Issue
Block a user