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>
150 lines
5.3 KiB
PHP
150 lines
5.3 KiB
PHP
<?php
|
|
|
|
namespace App\Tests\Patient;
|
|
|
|
use App\Appointment\Entity\Appointment;
|
|
use App\Appointment\Service\AppointmentConfirmationService;
|
|
use App\Clinic\Entity\Clinic;
|
|
use App\Doctor\Entity\Doctor;
|
|
use App\Patient\Entity\PatientRecord;
|
|
use App\Patient\Entity\PatientSession;
|
|
use App\Tests\ApiTestCase;
|
|
|
|
/**
|
|
* Confirming an appointment files exactly one case file, in the practice where
|
|
* the booking actually happened.
|
|
*
|
|
* Two records for one appointment means a single visit's revenue is counted
|
|
* twice, and inferring the practice from the address (rather than the booking
|
|
* context) files it under the wrong one.
|
|
*/
|
|
class AutoCreateSessionOnConfirmTest extends ApiTestCase
|
|
{
|
|
private function confirmation(): AppointmentConfirmationService
|
|
{
|
|
return static::getContainer()->get(AppointmentConfirmationService::class);
|
|
}
|
|
|
|
private function makeDoctor(): Doctor
|
|
{
|
|
$doctor = new Doctor($this->createUser(['ROLE_DOCTOR']), 'دکتر آزمون');
|
|
$this->em->persist($doctor);
|
|
$this->em->flush();
|
|
|
|
return $doctor;
|
|
}
|
|
|
|
private function makeClinic(Doctor $doctor): Clinic
|
|
{
|
|
$clinic = new Clinic($this->createUser(['ROLE_CLINIC']));
|
|
$clinic->setName('کلینیک آزمون');
|
|
$clinic->getDoctors()->add($doctor);
|
|
$this->em->persist($clinic);
|
|
$this->em->flush();
|
|
|
|
return $clinic;
|
|
}
|
|
|
|
private function makeAppointment(Doctor $doctor, ?Clinic $clinic = null): Appointment
|
|
{
|
|
$appointment = $this->newAppointment($doctor, $this->createUser(['ROLE_USER']), 1_790_000_000, 1_790_001_800, $clinic);
|
|
$appointment->transitionTo(Appointment::STATUS_CONFIRMED);
|
|
$this->em->persist($appointment);
|
|
$this->em->flush();
|
|
|
|
return $appointment;
|
|
}
|
|
|
|
/** @return PatientRecord[] */
|
|
private function recordsFor(Appointment $appointment): array
|
|
{
|
|
return $this->em->getRepository(PatientRecord::class)
|
|
->findBy(['user' => $appointment->getUser()]);
|
|
}
|
|
|
|
/** @return PatientSession[] */
|
|
private function sessionsFor(Appointment $appointment): array
|
|
{
|
|
return $this->em->getRepository(PatientSession::class)
|
|
->findBy(['appointment' => $appointment]);
|
|
}
|
|
|
|
public function testClinicBookingFilesOnlyTheClinicRecord(): void
|
|
{
|
|
$doctor = $this->makeDoctor();
|
|
$clinic = $this->makeClinic($doctor);
|
|
$appointment = $this->makeAppointment($doctor, $clinic);
|
|
|
|
$this->confirmation()->onConfirmed($appointment);
|
|
$this->em->flush();
|
|
|
|
$records = $this->recordsFor($appointment);
|
|
self::assertCount(1, $records, 'یک نوبت باید دقیقاً یک پرونده بسازد');
|
|
self::assertSame('clinic', $records[0]->getEntityType());
|
|
self::assertSame($clinic->getId(), $records[0]->getEntityId());
|
|
self::assertCount(1, $this->sessionsFor($appointment));
|
|
}
|
|
|
|
public function testPersonalBookingFilesOnlyTheDoctorRecord(): void
|
|
{
|
|
$doctor = $this->makeDoctor();
|
|
$this->makeClinic($doctor); // عضویت کلینیک نباید نوبت شخصی را بدزدد
|
|
$appointment = $this->makeAppointment($doctor, null);
|
|
|
|
$this->confirmation()->onConfirmed($appointment);
|
|
$this->em->flush();
|
|
|
|
$records = $this->recordsFor($appointment);
|
|
self::assertCount(1, $records);
|
|
self::assertSame('doctor', $records[0]->getEntityType());
|
|
self::assertSame($doctor->getId(), $records[0]->getEntityId());
|
|
}
|
|
|
|
public function testExistingRecordGetsAnotherSessionInsteadOfAnotherRecord(): void
|
|
{
|
|
$doctor = $this->makeDoctor();
|
|
$first = $this->makeAppointment($doctor, null);
|
|
$patient = $first->getUser();
|
|
|
|
$this->confirmation()->onConfirmed($first);
|
|
$this->em->flush();
|
|
|
|
$second = $this->newAppointment($doctor, $patient, 1_790_100_000, 1_790_101_800);
|
|
$second->transitionTo(Appointment::STATUS_CONFIRMED);
|
|
$this->em->persist($second);
|
|
$this->em->flush();
|
|
|
|
$this->confirmation()->onConfirmed($second);
|
|
$this->em->flush();
|
|
|
|
self::assertCount(1, $this->recordsFor($first), 'بیمار قبلاً پرونده دارد؛ پروندهٔ دوم ساخته نشود');
|
|
self::assertCount(1, $this->sessionsFor($second), 'ولی مراجعهٔ جدید باید ثبت شود');
|
|
}
|
|
|
|
public function testConfirmingTwiceDoesNotDuplicateTheSession(): void
|
|
{
|
|
$doctor = $this->makeDoctor();
|
|
$appointment = $this->makeAppointment($doctor, null);
|
|
|
|
$this->confirmation()->onConfirmed($appointment);
|
|
$this->em->flush();
|
|
$this->confirmation()->onConfirmed($appointment);
|
|
$this->em->flush();
|
|
|
|
self::assertCount(1, $this->sessionsFor($appointment));
|
|
}
|
|
|
|
public function testDayLevelReserveIsNotFiled(): void
|
|
{
|
|
$doctor = $this->makeDoctor();
|
|
$appointment = $this->makeAppointment($doctor, null);
|
|
$appointment->rescheduleTo(1_790_000_000, 1_790_001_800, true);
|
|
$this->em->flush();
|
|
|
|
$this->confirmation()->onConfirmed($appointment);
|
|
$this->em->flush();
|
|
|
|
self::assertCount(0, $this->sessionsFor($appointment), 'نوبت رزروِ روز-محور ساعت ندارد؛ مراجعه نمیسازد');
|
|
}
|
|
}
|