fix(insurance): let a clinic owner save the visit price of a member doctor

The pricing rows are keyed by the *target* tenant, but the tenant filter pins
every read to the environment of whoever is asking. A clinic owner setting the
free-visit price for one of their doctors was therefore blind to the row that
already existed: each save inserted another one — the unique key does not stop
it, because insurance_id is NULL for the free-visit row and MySQL does not treat
NULLs as equal — and the following read was blind in the same way, so the panel
kept showing the old value. From the outside it simply looked like the field
would not save.

Reads now run outside the filter, the same exception the tenant-insurance
repository already makes for the same reason, with authorization still coming
from resolveTargetEntity(). findOneForInsurance() takes the newest row so a
tenant that already accumulated duplicates converges on the last value the user
entered, and a migration collapses those leftovers.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
hamed
2026-08-18 16:52:04 +03:30
co-authored by Claude Opus 5
parent 5d2594ff87
commit 1f64b516d2
4 changed files with 183 additions and 7 deletions
@@ -0,0 +1,90 @@
<?php
namespace App\Tests\Insurance;
use App\Auth\Entity\User;
use App\Auth\Entity\UserActiveContext;
use App\Clinic\Entity\Clinic;
use App\Doctor\Entity\Doctor;
use App\Tests\ApiTestCase;
/**
* قیمت ویزیت آزادِ پزشکِ عضو کلینیک، وقتی مالک کلینیک محیطِ خودش را انتخاب کرده.
*
* محیطِ فعالِ مالک «کلینیک» است ولی مقصدِ این تنظیم «پزشک» — و TenantFilter روی
* `EntityInsurancePricing` بسته می‌شد. کوئریِ خواندن ردیفِ موجود را نمی‌دید، پس هر
* ذخیره ردیفِ تازه‌ای می‌ساخت (قید یکتا ردیفِ ویزیت آزاد را نمی‌گیرد چون
* `insurance_id` آنجا NULL است) و خواندنِ بعدی هم همان‌طور کور بود: از دید کاربر
* «ثبت نمی‌شود».
*/
class VisitPriceForMemberDoctorTest extends ApiTestCase
{
/** @return array{0: User, 1: Doctor} */
private function clinicOwnerWithMemberDoctor(): 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];
}
private function save(User $owner, Doctor $doctor, int $rials): array
{
return $this->authJson('PUT', '/api/v1/insurance-pricing', $owner, [
'doctor_uuid' => $doctor->getUuid(),
'free_visit_price_rials' => $rials,
]);
}
private function read(User $owner, Doctor $doctor): array
{
return $this->authJson('GET', '/api/v1/insurance-pricing?doctor_uuid=' . $doctor->getUuid(), $owner);
}
public function testTheSavedVisitPriceComesBack(): void
{
[$owner, $doctor] = $this->clinicOwnerWithMemberDoctor();
$this->save($owner, $doctor, 3_000_000);
self::assertSame(200, $this->responseCode());
$body = $this->read($owner, $doctor);
self::assertSame(3_000_000, $body['data']['free_visit_price_rials']);
}
public function testSavingTwiceUpdatesTheSameRowInsteadOfPilingUpNewOnes(): void
{
[$owner, $doctor] = $this->clinicOwnerWithMemberDoctor();
$this->save($owner, $doctor, 3_000_000);
$this->save($owner, $doctor, 4_500_000);
$rows = $this->em->getConnection()->fetchAllAssociative(
'SELECT patient_share_rials FROM entity_insurance_pricing
WHERE entity_type = ? AND entity_id = ? AND insurance_id IS NULL',
['doctor', $doctor->getId()],
);
self::assertCount(1, $rows, 'ذخیرهٔ دوباره نباید ردیف تازه بسازد');
self::assertSame(4_500_000, (int) $rows[0]['patient_share_rials']);
self::assertSame(4_500_000, $this->read($owner, $doctor)['data']['free_visit_price_rials']);
}
}