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
@@ -405,10 +405,19 @@ class InsuranceController extends BaseController
$rows = $this->serviceCoverageRepo->findByContract($contract->getId());
$data = array_map(function ($r) {
// Batch-fetch the referenced service items once instead of one find()
// per coverage row (N+1).
$itemIds = array_values(array_unique(array_map(fn($r) => $r->getServiceItemId(), $rows)));
$uuidById = [];
if ($itemIds !== []) {
foreach ($this->serviceItemRepo->findBy(['id' => $itemIds]) as $item) {
$uuidById[$item->getId()] = $item->getUuid();
}
}
$data = array_map(function ($r) use ($uuidById) {
$row = $r->toArray();
$item = $this->serviceItemRepo->find($r->getServiceItemId());
$row['service_item_uuid'] = $item?->getUuid();
$row['service_item_uuid'] = $uuidById[$r->getServiceItemId()] ?? null;
return $row;
}, $rows);