fix(booking): carry the clinic context through the panel and drop phantom locations

Two faults, one root: the per-context booking work updated ScheduleSection but
left the rest of the panel calling slot endpoints without clinic_uuid. Absent
clinic_uuid means the personal practice, so the panel asked about a schedule the
doctor barely uses and got nothing back.

- useClinicContext() resolves the current environment once and is used by the
  appointments page, useDoctorBookingServices, ServiceSlotPicker and both
  queries in NewAppointmentDrawer (a fifth call site a sweep turned up). It
  returns null in a doctor's personal environment so the mirror-image bug — a
  doctor seeing the clinic's schedule at their own practice — cannot appear.
  clinicUuid is part of every query key; without it the cache leaks across
  environments.
- appointment-slots returns empty_reason (no_schedule | holiday | day_off |
  outside_window). TurnsTimeline rendered «این روز تعطیل است» for any empty day,
  which is what the bug report actually saw; it now says which of the four it is.
- booking-locations lists a location only when the context has an address and an
  active shift points at it. The dev data had three "personal" schedules whose
  shifts referenced the clinic's address, so the public site advertised a
  personal practice that could never be booked.
- ?date= adds available_on_date per location, validated as a real calendar date.
- MyAppointmentsController and AdminApiController resolved the appointment
  address with no context and could store the wrong one. Both now go through the
  new BookingContextResolver, which also replaces AppointmentController's private
  copy of the same membership check.
- app:schedule:audit-locations reports shifts pointing at a missing or foreign
  address; --fix deactivates them rather than deleting.

Verified against the reported doctor: same date, no clinic_uuid -> 0 sessions,
with it -> 1 session; a full week matches the configured Sat/Tue/Wed/Thu.

Suite: 417 tests, 2 failures — both pre-existing and unrelated.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
hamed
2026-07-18 14:35:31 +03:30
co-authored by Claude Opus 4.8
parent 8d31ebb3cb
commit 7a0654f8ba
15 changed files with 906 additions and 42 deletions
@@ -0,0 +1,111 @@
<?php
namespace App\Appointment\Command;
use App\Appointment\Entity\WeeklySchedule;
use App\Appointment\Repository\WeeklyScheduleRepository;
use App\Doctor\Repository\DoctorAddressRepository;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
/**
* Finds active shifts whose location_id is empty or points outside their own
* booking context.
*
* The API rejects such shifts today (validateSessions), but rows created before
* that validation are still around. They are invisible in the panel yet make a
* location look bookable when nothing can actually be reserved there — the
* public site used to advertise a "personal practice" built entirely out of one.
*/
#[AsCommand(name: 'app:schedule:audit-locations', description: 'Report (and optionally disable) schedule shifts pointing at no valid address')]
class AuditScheduleLocationsCommand extends Command
{
public function __construct(
private readonly WeeklyScheduleRepository $scheduleRepo,
private readonly DoctorAddressRepository $addressRepo,
private readonly EntityManagerInterface $em,
) {
parent::__construct();
}
protected function configure(): void
{
$this->addOption('fix', null, InputOption::VALUE_NONE, 'Deactivate the offending shifts instead of only reporting them');
}
protected function execute(InputInterface $input, OutputInterface $output): int
{
$io = new SymfonyStyle($input, $output);
$fix = (bool) $input->getOption('fix');
$rows = [];
$touched = 0;
foreach ($this->scheduleRepo->findAll() as $schedule) {
$clinic = $schedule->getClinic();
$allowed = [];
foreach ($this->addressRepo->findForContext($schedule->getDoctor(), $clinic?->getId()) as $address) {
$allowed[(int) $address->getId()] = true;
}
$setting = $schedule->getSetting();
$changed = false;
foreach ($setting as $dayKey => $day) {
foreach (($day['sessions'] ?? []) as $index => $session) {
if (!($session['active'] ?? false)) {
continue;
}
$locationId = (int) ($session['location_id'] ?? 0);
if ($locationId !== 0 && isset($allowed[$locationId])) {
continue;
}
$rows[] = [
$schedule->getDoctor()->getUuid(),
$clinic === null ? 'personal' : ($clinic->getName() ?? 'clinic'),
WeeklySchedule::DAYS[(int) $dayKey] ?? (string) $dayKey,
$locationId === 0 ? '—' : (string) $locationId,
$locationId === 0 ? 'no address' : 'address outside context',
];
if ($fix) {
$setting[$dayKey]['sessions'][$index]['active'] = false;
$changed = true;
}
}
}
if ($changed) {
// داده‌ی کاربر حذف نمی‌شود؛ فقط غیرفعال می‌شود تا قابل بازیابی بماند.
$schedule->setSetting($setting);
$touched++;
}
}
if ($rows === []) {
$io->success('No schedule shift points at a missing or foreign address.');
return Command::SUCCESS;
}
$io->table(['doctor', 'context', 'day', 'location_id', 'problem'], $rows);
if (!$fix) {
$io->warning(sprintf('%d offending shift(s). Re-run with --fix to deactivate them.', count($rows)));
return Command::SUCCESS;
}
$this->em->flush();
$io->success(sprintf('Deactivated %d shift(s) across %d schedule(s).', count($rows), $touched));
return Command::SUCCESS;
}
}