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>
98 lines
3.5 KiB
PHP
98 lines
3.5 KiB
PHP
<?php
|
|
|
|
namespace App\Tests\Patient;
|
|
|
|
use App\Appointment\Entity\Appointment;
|
|
use App\Doctor\Entity\Doctor;
|
|
use App\Patient\Entity\PatientRecord;
|
|
use App\Tests\ApiTestCase;
|
|
|
|
/**
|
|
* List Patient Appointments (تب نوبتها): shape (incl. version), ordering,
|
|
* empty boundary, and ownership scoping.
|
|
*/
|
|
class PatientAppointmentsTest extends ApiTestCase
|
|
{
|
|
/** @return array{0: \App\Auth\Entity\User, 1: Doctor, 2: PatientRecord} */
|
|
private function recordFor(): array
|
|
{
|
|
$owner = $this->createUser(['ROLE_DOCTOR']);
|
|
$doctor = new Doctor($owner, 'دکتر ژیلا فتحی');
|
|
$this->em->persist($doctor);
|
|
$this->em->flush();
|
|
|
|
$patient = $this->createUser(['ROLE_USER']);
|
|
$record = new PatientRecord('doctor', $doctor->getId(), $patient, 'doctor', $doctor->getId());
|
|
$this->em->persist($record);
|
|
$this->em->flush();
|
|
|
|
return [$owner, $doctor, $record];
|
|
}
|
|
|
|
private function appointment(Doctor $doctor, PatientRecord $record, int $slotStart): Appointment
|
|
{
|
|
$appt = $this->newAppointment($doctor, $record->getUser(), $slotStart, $slotStart + 1800);
|
|
$this->em->persist($appt);
|
|
$this->em->flush();
|
|
|
|
return $appt;
|
|
}
|
|
|
|
public function testListReturnsShapeWithVersion(): void
|
|
{
|
|
[$owner, $doctor, $record] = $this->recordFor();
|
|
$this->appointment($doctor, $record, 1_754_000_000);
|
|
|
|
$list = $this->authJson('GET', '/api/v1/patient/' . $record->getUuid() . '/appointments', $owner);
|
|
self::assertSame(200, $this->responseCode());
|
|
self::assertCount(1, $list['data']);
|
|
|
|
$row = $list['data'][0];
|
|
foreach (['uuid', 'starts_at', 'ends_at', 'status', 'version', 'doctor_name'] as $key) {
|
|
self::assertArrayHasKey($key, $row);
|
|
}
|
|
self::assertSame(1, $row['version']);
|
|
self::assertSame('pending', $row['status']);
|
|
self::assertSame('دکتر ژیلا فتحی', $row['doctor_name']);
|
|
self::assertSame(1_754_000_000, $row['starts_at']);
|
|
}
|
|
|
|
public function testSortedByStartDescending(): void
|
|
{
|
|
[$owner, $doctor, $record] = $this->recordFor();
|
|
$this->appointment($doctor, $record, 1_754_000_000);
|
|
$this->appointment($doctor, $record, 1_755_000_000);
|
|
|
|
$list = $this->authJson('GET', '/api/v1/patient/' . $record->getUuid() . '/appointments', $owner);
|
|
self::assertCount(2, $list['data']);
|
|
self::assertSame(1_755_000_000, $list['data'][0]['starts_at']);
|
|
self::assertSame(1_754_000_000, $list['data'][1]['starts_at']);
|
|
}
|
|
|
|
public function testEmptyWhenNoAppointments(): void
|
|
{
|
|
[$owner, , $record] = $this->recordFor();
|
|
|
|
$list = $this->authJson('GET', '/api/v1/patient/' . $record->getUuid() . '/appointments', $owner);
|
|
self::assertSame(200, $this->responseCode());
|
|
self::assertCount(0, $list['data']);
|
|
}
|
|
|
|
public function testNotFoundForUnknownRecord(): void
|
|
{
|
|
[$owner] = $this->recordFor();
|
|
$this->authJson('GET', '/api/v1/patient/00000000-0000-0000-0000-000000000000/appointments', $owner);
|
|
self::assertSame(404, $this->responseCode());
|
|
}
|
|
|
|
public function testOwnershipScoped(): void
|
|
{
|
|
[, $doctor, $record] = $this->recordFor();
|
|
$this->appointment($doctor, $record, 1_754_000_000);
|
|
|
|
[$other] = $this->recordFor();
|
|
$this->authJson('GET', '/api/v1/patient/' . $record->getUuid() . '/appointments', $other);
|
|
self::assertSame(404, $this->responseCode());
|
|
}
|
|
}
|