Files
clinicpro/tests/Insurance/DoctorFirstThenClinicScopeTest.php
T
hamedandClaude Opus 5 b24f45cc83 fix(insurance): read a doctor's own settings first, then their clinic's
A clinic owner configures insurance on the doctor (`doctor_uuid`), but an
appointment booked at the clinic belongs to the clinic — so at confirm time
the engine looked for contracts under the clinic, found none, and the operator
had no insurance to pick and no way to save one ("this insurance has no active
contract"). The two sides were writing and reading different tenants.

Contracts, service kinds and the visit price now resolve doctor-first with the
appointment's clinic as fallback, each judged separately: a doctor who holds
their own contracts but leaves the visit price to the clinic gets each from the
right place. The confirm modal asks the same question the engine answers, via
`inherit=1` on the two read endpoints; the settings pages deliberately do not
send it, since editing must target the doctor's own row.

Two further things came out of the same sweep. The service-kind settings
repository had the tenant-filter blindness already fixed for contracts and
pricing — reads pinned to the caller's environment while the target is another
tenant — so it is now exempted the same way. And a coverage percentage of zero
is accepted as a real choice meaning "this contract does not cover that service
kind"; what is still rejected is leaving an enabled kind with no percentage at
all, inheriting a central default of zero included.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-08-18 17:18:35 +03:30

180 lines
7.1 KiB
PHP

<?php
namespace App\Tests\Insurance;
use App\Appointment\Entity\Appointment;
use App\Auth\Entity\User;
use App\Auth\Entity\UserActiveContext;
use App\Clinic\Entity\Clinic;
use App\Doctor\Entity\Doctor;
use App\Insurance\Entity\Insurance;
use App\Insurance\Enum\InsuranceType;
use App\Tests\ApiTestCase;
/**
* تنظیمات بیمه: اول خودِ پزشک، در نبودِ تنظیمِ او کلینیک.
*
* پنل کلینیک قرارداد را روی خودِ پزشک ذخیره می‌کند (`doctor_uuid`)، ولی نوبتِ
* ثبت‌شده در کلینیک محیطش «کلینیک» است. تا پیش از این، هنگام قطعی‌کردنِ نوبت لیست
* بیمه از محیط کلینیک خوانده می‌شد و خالی بود: کاربر بیمه‌ای برای انتخاب نداشت و
* انتخابِ دستی هم با «قرارداد فعال ندارد» رد می‌شد.
*/
class DoctorFirstThenClinicScopeTest extends ApiTestCase
{
/** @return array{0: User, 1: Doctor, 2: Clinic} */
private function clinicWithMemberDoctor(): array
{
$doctorUser = $this->createUser(['ROLE_USER', 'ROLE_DOCTOR']);
$doctor = new Doctor($doctorUser, 'دکتر عضو کلینیک بیمه');
$doctor->setMobileNumber($doctorUser->getMobileNumber());
$this->em->persist($doctor);
$owner = $this->createUser(['ROLE_USER', 'ROLE_CLINIC']);
$clinic = new Clinic($owner);
$clinic->setName('کلینیک محدودهٔ بیمه');
$clinic->getDoctors()->add($doctor);
$this->em->persist($clinic);
$this->em->flush();
$this->em->persist(new UserActiveContext(
$this->em->find(User::class, $owner->getId()),
$clinic->getUuid(),
'clinic',
));
$this->em->flush();
return [$owner, $doctor, $clinic];
}
private function insurance(): Insurance
{
$insurance = new Insurance('بیمه محدوده ' . random_int(1000, 9999), InsuranceType::Basic);
$this->em->persist($insurance);
$this->em->flush();
return $insurance;
}
private function appointmentAtClinic(Doctor $doctor, Clinic $clinic, User $patient): Appointment
{
$appointment = $this->newAppointment($doctor, $patient, time() + 3600, time() + 5400, $clinic);
$this->em->persist($appointment);
$this->em->flush();
return $appointment;
}
public function testAContractStoredOnTheDoctorAppliesToTheirClinicAppointment(): void
{
[$owner, $doctor, $clinic] = $this->clinicWithMemberDoctor();
$insurance = $this->insurance();
$this->authJson('POST', '/api/v1/billing/tenant-insurances', $owner, [
'doctor_uuid' => $doctor->getUuid(),
'insurance_id' => $insurance->getId(),
'coverage_percent' => 80,
]);
self::assertSame(201, $this->responseCode());
$appointment = $this->appointmentAtClinic($doctor, $clinic, $this->createUser(['ROLE_USER']));
$this->authJson('PATCH', '/api/v1/appointment/' . $appointment->getUuid(), $owner, [
'insurance_base_id' => $insurance->getId(),
]);
self::assertSame(200, $this->responseCode(), 'قرارداد پزشک باید روی نوبت کلینیک اعمال شود');
}
public function testTheClinicContractIsInheritedWhenTheDoctorHasNone(): void
{
[$owner, $doctor, $clinic] = $this->clinicWithMemberDoctor();
$insurance = $this->insurance();
// قرارداد روی خودِ کلینیک، بدون هیچ قراردادی برای پزشک.
$this->authJson('POST', '/api/v1/billing/tenant-insurances', $owner, [
'insurance_id' => $insurance->getId(),
'coverage_percent' => 60,
]);
self::assertSame(201, $this->responseCode());
$appointment = $this->appointmentAtClinic($doctor, $clinic, $this->createUser(['ROLE_USER']));
$this->authJson('PATCH', '/api/v1/appointment/' . $appointment->getUuid(), $owner, [
'insurance_base_id' => $insurance->getId(),
]);
self::assertSame(200, $this->responseCode(), 'در نبود قرارداد پزشک، قرارداد کلینیک اعمال می‌شود');
}
/**
* همان قاعده در خواندن: مودالِ قطعی‌کردن با `inherit=1` می‌پرسد «برای این پزشک
* چه چیزی اعمال می‌شود» و باید قرارداد پزشک را ببیند، نه فهرست خالیِ کلینیک.
*/
public function testInheritReadReturnsTheDoctorContractToTheClinicOwner(): void
{
[$owner, $doctor, ] = $this->clinicWithMemberDoctor();
$insurance = $this->insurance();
$this->authJson('POST', '/api/v1/billing/tenant-insurances', $owner, [
'doctor_uuid' => $doctor->getUuid(),
'insurance_id' => $insurance->getId(),
'coverage_percent' => 80,
]);
$body = $this->authJson(
'GET',
'/api/v1/billing/tenant-insurances?doctor_uuid=' . $doctor->getUuid() . '&inherit=1',
$owner,
);
$ids = array_map(static fn (array $row): int => (int) $row['insurance_id'], $body['data']['data']);
self::assertContains($insurance->getId(), $ids);
}
public function testTheVisitPriceOfTheDoctorWinsOverTheClinicOne(): void
{
[$owner, $doctor, ] = $this->clinicWithMemberDoctor();
$this->authJson('PUT', '/api/v1/insurance-pricing', $owner, ['free_visit_price_rials' => 1_000_000]);
$this->authJson('PUT', '/api/v1/insurance-pricing', $owner, [
'doctor_uuid' => $doctor->getUuid(),
'free_visit_price_rials' => 2_500_000,
]);
$inherited = $this->authJson(
'GET',
'/api/v1/insurance-pricing?doctor_uuid=' . $doctor->getUuid() . '&inherit=1',
$owner,
);
self::assertSame(2_500_000, $inherited['data']['free_visit_price_rials']);
}
public function testTheClinicVisitPriceIsInheritedWhenTheDoctorHasNone(): void
{
[$owner, $doctor, ] = $this->clinicWithMemberDoctor();
$this->authJson('PUT', '/api/v1/insurance-pricing', $owner, ['free_visit_price_rials' => 1_000_000]);
$inherited = $this->authJson(
'GET',
'/api/v1/insurance-pricing?doctor_uuid=' . $doctor->getUuid() . '&inherit=1',
$owner,
);
self::assertSame(1_000_000, $inherited['data']['free_visit_price_rials']);
}
/** بدون `inherit`، صفحهٔ تنظیمات باید ردیفِ خودِ پزشک را ببیند — حتی وقتی خالی است. */
public function testTheSettingsReadStaysOnTheDoctorWithoutTheInheritFlag(): void
{
[$owner, $doctor, ] = $this->clinicWithMemberDoctor();
$this->authJson('PUT', '/api/v1/insurance-pricing', $owner, ['free_visit_price_rials' => 1_000_000]);
$own = $this->authJson('GET', '/api/v1/insurance-pricing?doctor_uuid=' . $doctor->getUuid(), $owner);
self::assertSame(0, $own['data']['free_visit_price_rials']);
}
}