fix(db): purge insurance config when a doctor/clinic is deleted (H5)

tenant_insurances, entity_insurance_pricing and tenant_service_coverages
reference their owner through a polymorphic (entity_type, entity_id) pair, so no
database FK can cascade their cleanup. Hard-deleting a doctor (DoctorController)
or clinic (AdminApiController) left these rows orphaned.

Add TenantInsuranceCleanupService::purgeForEntity() and call it from both delete
paths — removes coverage (via owning tenant_insurance ids), then tenant
insurances, then pricing.

Residual (separate, lower-freq paths): deleting an insurance category or a
service_item still orphans rows that reference them by id — tracked under the
medium-tier soft-ref findings.

Regression: tests/Insurance/TenantInsuranceCleanupTest.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
hamed
2026-06-28 19:16:10 +03:30
co-authored by Claude Opus 4.8
parent d14ac38da5
commit 4cf6873900
7 changed files with 105 additions and 1 deletions
@@ -34,6 +34,7 @@ class AdminApiController extends BaseController
public function __construct(
private readonly EntityManagerInterface $em,
private readonly \App\Appointment\Service\SlotCalculatorService $slotCalculator,
private readonly \App\Insurance\Service\TenantInsuranceCleanupService $insuranceCleanup,
) {}
// ── Users ─────────────────────────────────────────────────────────────────
@@ -553,6 +554,7 @@ class AdminApiController extends BaseController
$clinic = $this->em->getRepository(Clinic::class)->findOneBy(['uuid' => $uuid]);
if (!$clinic) return $this->error('CLINIC_NOT_FOUND', 'کلینیک یافت نشد', 404);
$this->insuranceCleanup->purgeForEntity(\App\Insurance\Entity\TenantInsurance::TYPE_CLINIC, $clinic->getId());
$this->em->remove($clinic);
$this->em->flush();