Admin-side booking (POST /api/v1/my/appointment and /api/v1/admin/appointment) resolved the patient User by mobile only, so one person booked under two mobiles produced two User rows — and two case-files, since PatientRecord is keyed on user_id. National code is the real unique identity (User.national_code is already unique); a person may have several mobiles. Booking now requires + validates patient_national_code and resolves the patient national-code-first (then mobile) via a shared PatientResolver, so the case-file stays unique per national code even across mobiles. Reusing a mobile already bound to a different national code returns 422 ERR_PROFILE_MOBILE_TAKEN. The admin create form and NewAppointmentDrawer gain a national-code field and send it; both had a dead patient-picker URL (/api/v1/patient) fixed to the real /api/v1/patients, whose payload already carries user_national_code for autofill. Docs (appointment.md, admin.md) and tests updated; new AppointmentNationalCodeTest covers success, single-file reuse, missing, invalid, and identity-conflict cases. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
119 lines
4.4 KiB
PHP
119 lines
4.4 KiB
PHP
<?php
|
|
|
|
namespace App\Tests\Appointment;
|
|
|
|
use App\Auth\Entity\User;
|
|
use App\Doctor\Entity\Doctor;
|
|
use App\Shared\Constant\ErrorCodes;
|
|
use App\Tests\ApiTestCase;
|
|
|
|
/**
|
|
* POST /api/v1/my/appointment must identify the patient by national code:
|
|
* it is required + validated, and the patient User (hence the case-file) is
|
|
* resolved by national code first so one person keeps a single record even
|
|
* when booked under a different mobile.
|
|
*/
|
|
class AppointmentNationalCodeTest extends ApiTestCase
|
|
{
|
|
/** @return array{0: User, 1: Doctor} */
|
|
private function doctor(): array
|
|
{
|
|
$owner = $this->createUser(['ROLE_DOCTOR']);
|
|
$doctor = new Doctor($owner, 'دکتر');
|
|
$this->em->persist($doctor);
|
|
$this->em->flush();
|
|
|
|
return [$owner, $doctor];
|
|
}
|
|
|
|
private function nationalCode(): string
|
|
{
|
|
return '00' . str_pad((string) random_int(0, 99_999_999), 8, '0', STR_PAD_LEFT);
|
|
}
|
|
|
|
private function mobile(): string
|
|
{
|
|
return '09' . str_pad((string) random_int(0, 999_999_999), 9, '0', STR_PAD_LEFT);
|
|
}
|
|
|
|
private function body(string $doctorUuid, string $mobile, string $nationalCode): array
|
|
{
|
|
$start = time() + 86_400 + random_int(0, 3_600) * 100;
|
|
|
|
return [
|
|
'doctor_uuid' => $doctorUuid,
|
|
'slot_start' => $start,
|
|
'slot_end' => $start + 1_800,
|
|
'patient_mobile' => $mobile,
|
|
'patient_name' => 'بیمار تست',
|
|
'patient_national_code' => $nationalCode,
|
|
];
|
|
}
|
|
|
|
public function testCreatesUserWithNationalCode(): void
|
|
{
|
|
[$owner, $doctor] = $this->doctor();
|
|
$nc = $this->nationalCode();
|
|
|
|
$this->authJson('POST', '/api/v1/my/appointment', $owner, $this->body($doctor->getUuid(), $this->mobile(), $nc));
|
|
|
|
self::assertSame(201, $this->responseCode());
|
|
$patient = $this->em->getRepository(User::class)->findOneBy(['nationalCode' => $nc]);
|
|
self::assertNotNull($patient);
|
|
self::assertSame($nc, $patient->getNationalCode());
|
|
}
|
|
|
|
public function testSameNationalCodeDifferentMobileReusesSinglePatient(): void
|
|
{
|
|
[$owner, $doctor] = $this->doctor();
|
|
$nc = $this->nationalCode();
|
|
|
|
// First booking under mobile A creates the patient.
|
|
$this->authJson('POST', '/api/v1/my/appointment', $owner, $this->body($doctor->getUuid(), $this->mobile(), $nc));
|
|
self::assertSame(201, $this->responseCode());
|
|
|
|
// Second booking under a *different* mobile but the same national code
|
|
// must resolve to the same patient — the case-file stays unique.
|
|
$this->authJson('POST', '/api/v1/my/appointment', $owner, $this->body($doctor->getUuid(), $this->mobile(), $nc));
|
|
self::assertSame(201, $this->responseCode());
|
|
|
|
$patients = $this->em->getRepository(User::class)->findBy(['nationalCode' => $nc]);
|
|
self::assertCount(1, $patients);
|
|
}
|
|
|
|
public function testMissingNationalCodeIs422(): void
|
|
{
|
|
[$owner, $doctor] = $this->doctor();
|
|
$body = $this->body($doctor->getUuid(), $this->mobile(), $this->nationalCode());
|
|
unset($body['patient_national_code']);
|
|
|
|
$this->authJson('POST', '/api/v1/my/appointment', $owner, $body);
|
|
|
|
self::assertSame(422, $this->responseCode());
|
|
}
|
|
|
|
public function testInvalidNationalCodeIs422(): void
|
|
{
|
|
[$owner, $doctor] = $this->doctor();
|
|
|
|
$this->authJson('POST', '/api/v1/my/appointment', $owner, $this->body($doctor->getUuid(), $this->mobile(), '123'));
|
|
|
|
self::assertSame(422, $this->responseCode());
|
|
}
|
|
|
|
public function testMobileBelongingToAnotherNationalCodeIsRejected(): void
|
|
{
|
|
[$owner, $doctor] = $this->doctor();
|
|
$mobile = $this->mobile();
|
|
|
|
// First booking binds this mobile to national code A.
|
|
$this->authJson('POST', '/api/v1/my/appointment', $owner, $this->body($doctor->getUuid(), $mobile, $this->nationalCode()));
|
|
self::assertSame(201, $this->responseCode());
|
|
|
|
// Same mobile, a *different* national code → identity conflict.
|
|
$res = $this->authJson('POST', '/api/v1/my/appointment', $owner, $this->body($doctor->getUuid(), $mobile, $this->nationalCode()));
|
|
self::assertSame(422, $this->responseCode());
|
|
self::assertSame(ErrorCodes::ERR_PROFILE_MOBILE_TAKEN, $res['errors'][0]['code']);
|
|
}
|
|
}
|