perf(insurance): batch-fetch service items in coverage list (H8)

listServiceCoverage called serviceItemRepo->find() once per coverage row (N+1).
Collect the ids and fetch them in one findBy(['id' => $ids]), then map by id.

Regression: tests/Insurance/ServiceCoverageNPlusOneTest (functional correctness —
every row resolves the right service_item_uuid).

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
hamed
2026-06-28 19:29:06 +03:30
co-authored by Claude Opus 4.8
parent 6b12f3ddb9
commit eb1997066e
3 changed files with 70 additions and 4 deletions
@@ -0,0 +1,57 @@
<?php
namespace App\Tests\Insurance;
use App\ClinicService\Entity\ServiceItem;
use App\ClinicService\Entity\ServiceSection;
use App\Doctor\Entity\Doctor;
use App\Insurance\Entity\TenantInsurance;
use App\Insurance\Entity\TenantServiceCoverage;
use App\Tests\ApiTestCase;
/**
* The service-coverage list batch-fetches the referenced service items in one
* query instead of find()-per-row. This guards the refactor's correctness: every
* row must still carry the right service_item_uuid.
*/
class ServiceCoverageNPlusOneTest extends ApiTestCase
{
public function testEveryRowResolvesItsServiceItemUuid(): void
{
$owner = $this->createUser(['ROLE_DOCTOR']);
$doctor = new Doctor($owner, 'دکتر تست');
$this->em->persist($doctor);
$this->em->flush();
$tenant = new TenantInsurance(TenantInsurance::TYPE_DOCTOR, $doctor->getId(), 1);
$section = new ServiceSection(TenantInsurance::TYPE_DOCTOR, $doctor->getId(), 'بخش');
$this->em->persist($tenant);
$this->em->persist($section);
$this->em->flush();
$expectedUuids = [];
for ($i = 0; $i < 4; $i++) {
$item = new ServiceItem($section, "خدمت $i");
$this->em->persist($item);
$this->em->flush();
$expectedUuids[] = $item->getUuid();
$this->em->persist(new TenantServiceCoverage($tenant->getId(), $item->getId()));
}
$this->em->flush();
$body = $this->authJson(
'GET',
'/api/v1/billing/tenant-insurances/' . $tenant->getUuid() . '/service-coverage',
$owner,
);
$this->assertSame(200, $this->responseCode());
$rows = $body['data']['data'] ?? $body['data'];
$this->assertCount(4, $rows);
$returnedUuids = array_column($rows, 'service_item_uuid');
sort($expectedUuids);
sort($returnedUuids);
$this->assertSame($expectedUuids, $returnedUuids);
}
}