fix(appointment): one weekly schedule per doctor, place chosen per shift

A doctor working both at their own practice and at a clinic had to write two
independent schedules and neither panel could see the other, so the clinic
showed an empty form even though the doctor had configured their practice.

The schedule is now a single record owned by the doctor. What varies between
days is the place: the context of a shift is read from its location_id, not
from the record it lives in. Booking in a context therefore sees only that
context's days, so a personal-practice secretary still cannot book a clinic
day. The caller's own context decides which addresses they may assign: the
doctor gets every place of theirs, a clinic manager only its own, and shifts
outside their reach are returned for display but preserved verbatim on save.

Existing per-clinic rows are merged by migration; location_id was already
stored on every shift, so no context information is lost.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
hamed
2026-08-20 17:20:53 +03:30
co-authored by Claude Opus 5
parent 2f030edef1
commit a0b4a969fc
15 changed files with 789 additions and 84 deletions
@@ -10,9 +10,11 @@ use App\Doctor\Entity\DoctorAddress;
use App\Tests\ApiTestCase;
/**
* تنظیمات نوبت‌دهی per-context است: مطب شخصی پزشک (بدون clinic_uuid) فقط برای خود
* پزشک و ادمین باز است، و کلینیک با clinic_uuid فقط برنامهٔ همان کلینیک را
* می‌بیند/می‌نویسد. این دو برنامهٔ جدا هستند و روی هم اثر نمی‌گذارند.
* برنامهٔ هفتگی یکی است و مالِ خودِ پزشک؛ آنچه بین روزها فرق می‌کند مکان است.
*
* دسترسی همچنان per-context است، ولی روی *مکان* اعمال می‌شود نه روی *رکورد*: کلینیک
* فقط آدرس خودش را روی برنامه می‌نشاند و شیفت‌های مطب شخصی را نه می‌بیندشان که پاک
* کند نه اجازهٔ ویرایششان دارد؛ خودِ پزشک هر دو را دارد.
*/
class ClinicOwnerScheduleAccessTest extends ApiTestCase
{
@@ -117,14 +119,27 @@ class ClinicOwnerScheduleAccessTest extends ApiTestCase
self::assertSame(422, $this->responseCode());
}
public function testPersonalContextRejectsClinicAddress(): void
public function testDoctorMayPlaceTheirClinicAddressOnTheirOwnSchedule(): void
{
$doctor = $this->makeDoctor('دکتر عضو');
[, $clinic] = $this->makeClinicWith($doctor);
$clinicAddress = $this->clinicAddress($clinic);
// پزشک روز به روز جا عوض می‌کند؛ برنامه‌اش یکی است، پس هر دو آدرسش انتخاب‌شدنی‌اند.
$this->authJson('POST', '/api/v1/appointment-settings/weekly-schedule', $doctor->getUser(), $this->schedulePayload($doctor, $clinicAddress->getId(), '09:00'));
self::assertSame(201, $this->responseCode());
}
public function testDoctorCannotUseAnAddressOfAClinicTheyDoNotBelongTo(): void
{
$doctor = $this->makeDoctor('دکتر مستقل');
$outsider = $this->makeDoctor('دکتر دیگر');
[, $clinic] = $this->makeClinicWith($outsider);
$foreign = $this->clinicAddress($clinic);
$this->authJson('POST', '/api/v1/appointment-settings/weekly-schedule', $doctor->getUser(), $this->schedulePayload($doctor, $foreign->getId(), '09:00'));
self::assertSame(422, $this->responseCode());
}
@@ -140,7 +155,7 @@ class ClinicOwnerScheduleAccessTest extends ApiTestCase
self::assertSame(422, $this->responseCode(), 'پزشک عضو این کلینیک نیست');
}
public function testPersonalAndClinicSchedulesCoexistIndependently(): void
public function testOneScheduleHoldsShiftsOfBothPlaces(): void
{
$doctor = $this->makeDoctor('دکتر دو-محیطی');
[$owner, $clinic] = $this->makeClinicWith($doctor);
@@ -157,15 +172,16 @@ class ClinicOwnerScheduleAccessTest extends ApiTestCase
$reloadedDoctor = $this->em->getRepository(Doctor::class)->find($doctor->getId());
$schedules = $this->em->getRepository(WeeklySchedule::class)->findAllByDoctor($reloadedDoctor);
self::assertCount(2, $schedules, 'یک برنامه به ازای هر محیط');
self::assertCount(1, $schedules, 'یک برنامه برای هر پزشک، نه یکی به ازای هر محیط');
$byContext = [];
foreach ($schedules as $schedule) {
$byContext[$schedule->getClinic() === null ? 'personal' : 'clinic'] = $schedule->getSetting()[0]['sessions'][0]['start'];
$byLocation = [];
foreach ($schedules[0]->getSetting()[0]['sessions'] as $session) {
$byLocation[(int) $session['location_id']] = $session['start'];
}
self::assertSame('08:00', $byContext['personal']);
self::assertSame('16:00', $byContext['clinic']);
// نوشتنِ کلینیک، شیفت مطب شخصی را پاک نکرده — همان چیزی که ادغام باید تضمین کند.
self::assertSame('08:00', $byLocation[$personal->getId()] ?? null);
self::assertSame('16:00', $byLocation[$inClinic->getId()] ?? null);
}
public function testDoctorKeepsFullAccessToOwnSchedule(): void