Phase 2 of the tenant-marking series. appointments, weekly_schedules and date_overrides kept their environment implicit in a nullable clinic_id, so every query that wanted "this environment's rows" had to rebuild clinic_id IS NULL ? doctor : clinic itself. The two calendar tables also depended on a MariaDB-only generated column, clinic_key = IFNULL(clinic_id, 0), purely to make a unique key work across NULLs. All three now carry the (entity_type, entity_id) pair that service_sections, patient_records and clinic_staff already use, via a shared TenantOwnedTrait. The pair is a deliberate denormalisation of clinic_id/doctor_id: the automatic tenant filter and the tenant-leading indexes both need a real column, and neither can be built on an IF() expression. - Unique keys keep doctor_id alongside the pair. A clinic has several doctors and each has their own schedule, so (entity_type, entity_id) alone would reject the second doctor. - clinic_key is gone from both calendar tables. - appointments gained tenant-leading indexes; EXPLAIN on the panel's list query now picks idx_appointments_tenant_slot. Deliberately unchanged, both with the reason already recorded in the code: active_slot_key stays keyed on doctor + slot, since adding the environment would let one doctor be booked in their own practice and a clinic at the same moment. holidays keeps its nullable clinic_id, where NULL means "every environment" rather than "personal practice" — a meaning the pair cannot carry. The migration adds the columns nullable, backfills, aborts if any row is left without an owner, and only then tightens to NOT NULL. It creates each replacement unique index before dropping the old one, so the tables are never left unprotected — MariaDB commits implicitly on DDL, so ordering is the only safety net. It runs its statements through the connection rather than addSql() because the guard has to sit between the backfill and the NOT NULL change. Columns are NOT NULL with no default on purpose: a construction site that forgets assignTenant() fails at flush instead of silently writing entity_id 0, which the phase 4 filter would then hide from everyone. Verified on the dev database: 0 rows without a tenant, 0 personal bookings mismatched against their doctor, 0 clinic bookings mismatched against their clinic. Tests: 819 passing (813 + 6 new in BookingTenantTest). PHPStan clean on every changed file. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
138 lines
5.2 KiB
PHP
138 lines
5.2 KiB
PHP
<?php
|
|
|
|
namespace App\Tests\Doctor;
|
|
|
|
use App\Appointment\Entity\WeeklySchedule;
|
|
use App\Clinic\Entity\Clinic;
|
|
use App\Doctor\Entity\Doctor;
|
|
use App\Doctor\Entity\DoctorAddress;
|
|
use App\Tests\ApiTestCase;
|
|
|
|
/**
|
|
* The public doctor payload must aggregate booking state across every weekly
|
|
* schedule (personal + each clinic). A doctor whose personal schedule is empty
|
|
* but who is bookable at a clinic used to be reported as «نوبتدهی غیرفعال».
|
|
*/
|
|
class DoctorBookingStateAggregationTest extends ApiTestCase
|
|
{
|
|
/** پاسخ عمومی doctor به شکل {data:{data:{…}}} است. */
|
|
private function doctorPayload(): array
|
|
{
|
|
$json = json_decode($this->client->getResponse()->getContent(), true) ?? [];
|
|
|
|
return $json['data']['data'] ?? [];
|
|
}
|
|
|
|
private function week(array $session): array
|
|
{
|
|
$days = array_fill_keys(range(0, 6), ['sessions' => []]);
|
|
$days[0] = ['sessions' => [$session]];
|
|
|
|
return $days;
|
|
}
|
|
|
|
private function session(bool $active, int $locationId): array
|
|
{
|
|
return [
|
|
'active' => $active,
|
|
'location_id' => $locationId,
|
|
'start_time' => '09:00',
|
|
'end_time' => '13:00',
|
|
'duration_per_patient' => 20,
|
|
];
|
|
}
|
|
|
|
/** @return array{doctor: Doctor, personal: WeeklySchedule, clinic: WeeklySchedule} */
|
|
private function makeDoctorWithInactivePersonalAndClinic(): array
|
|
{
|
|
$doctor = new Doctor($this->createUser(['ROLE_USER', 'ROLE_DOCTOR']), 'دکتر تجمیع');
|
|
$this->em->persist($doctor);
|
|
|
|
$clinic = new Clinic($this->createUser(['ROLE_USER', 'ROLE_CLINIC']));
|
|
$clinic->setName('کلینیک تجمیع');
|
|
$clinic->getDoctors()->add($doctor);
|
|
$this->em->persist($clinic);
|
|
$this->em->flush();
|
|
|
|
$personalAddress = DoctorAddress::forDoctor($doctor);
|
|
$this->em->persist($personalAddress);
|
|
$clinicAddress = DoctorAddress::forClinic($clinic->getId());
|
|
$this->em->persist($clinicAddress);
|
|
$this->em->flush();
|
|
|
|
$personal = $this->newWeeklySchedule($doctor, $this->week($this->session(false, $personalAddress->getId())));
|
|
$clinicSchedule = $this->newWeeklySchedule(
|
|
$doctor,
|
|
$this->week($this->session(true, $clinicAddress->getId())),
|
|
$clinic
|
|
);
|
|
$this->em->persist($personal);
|
|
$this->em->persist($clinicSchedule);
|
|
$this->em->flush();
|
|
|
|
return ['doctor' => $doctor, 'personal' => $personal, 'clinic' => $clinicSchedule];
|
|
}
|
|
|
|
public function testClinicScheduleKeepsDoctorBookableDespiteEmptyPersonalSchedule(): void
|
|
{
|
|
['doctor' => $doctor] = $this->makeDoctorWithInactivePersonalAndClinic();
|
|
|
|
$this->client->request('GET', '/api/v1/doctor/' . $doctor->getUuid());
|
|
$data = $this->doctorPayload();
|
|
|
|
$this->assertTrue($data['active']);
|
|
$this->assertStringContainsString('شنبه', $data['free_turn']);
|
|
}
|
|
|
|
public function testAllSchedulesDisabledReportsBookingDisabled(): void
|
|
{
|
|
['doctor' => $doctor, 'personal' => $personal, 'clinic' => $clinicSchedule]
|
|
= $this->makeDoctorWithInactivePersonalAndClinic();
|
|
|
|
$personal->setMeta(['online_booking_enabled' => false]);
|
|
$clinicSchedule->setMeta(['online_booking_enabled' => false]);
|
|
$this->em->flush();
|
|
|
|
$this->client->request('GET', '/api/v1/doctor/' . $doctor->getUuid());
|
|
$data = $this->doctorPayload();
|
|
|
|
$this->assertFalse($data['active']);
|
|
$this->assertSame('نوبتدهی آنلاین غیرفعال است', $data['free_turn']);
|
|
}
|
|
|
|
public function testDetailExposesRawIsActiveFlagForDeactivatedDoctor(): void
|
|
{
|
|
['doctor' => $doctor] = $this->makeDoctorWithInactivePersonalAndClinic();
|
|
|
|
// فلگ روشن (پیشفرض): is_active باید true باشد.
|
|
$this->client->request('GET', '/api/v1/doctor/' . $doctor->getUuid());
|
|
$this->assertTrue($this->doctorPayload()['is_active']);
|
|
|
|
// ادمین پزشک را غیرفعال میکند → is_active=false (مبنای 404 در سایت عمومی).
|
|
$doctor->setActiveDoctorAppointment(false);
|
|
$this->em->flush();
|
|
|
|
$this->client->request('GET', '/api/v1/doctor/' . $doctor->getUuid());
|
|
$data = $this->doctorPayload();
|
|
$this->assertFalse($data['is_active']);
|
|
$this->assertFalse($data['active']);
|
|
}
|
|
|
|
public function testDisabledClinicScheduleDoesNotMaskActivePersonalSchedule(): void
|
|
{
|
|
['doctor' => $doctor, 'personal' => $personal, 'clinic' => $clinicSchedule]
|
|
= $this->makeDoctorWithInactivePersonalAndClinic();
|
|
|
|
// برعکسِ سناریوی اول: شخصی فعال، کلینیکی خاموش — ترتیب ردیفها نباید مهم باشد.
|
|
$personal->setSetting($this->week($this->session(true, 0)));
|
|
$clinicSchedule->setMeta(['online_booking_enabled' => false]);
|
|
$this->em->flush();
|
|
|
|
$this->client->request('GET', '/api/v1/doctor/' . $doctor->getUuid());
|
|
$data = $this->doctorPayload();
|
|
|
|
$this->assertTrue($data['active']);
|
|
$this->assertStringContainsString('شنبه', $data['free_turn']);
|
|
}
|
|
}
|