Files
clinicpro/tests/ClinicService/ServiceItemDeleteCleanupTest.php
T
hamedandClaude Opus 4.8 b1aaaf3d55 fix(db): purge service-item config rows on delete (M20)
Tariff and TenantServiceCoverage reference a service item by a raw int (no FK),
so deleting an item orphaned its tariffs and tenant-coverage config. Delete them
in deleteItem() before removing the item. (The in-use FK guard for invoice/claim
usage is preserved.)

Remaining M20 refs (ClinicStaff/SmsWallet/DoctorAddress.clinicId/Claim.insurance_id
on rare owner deletions) are accepted as harmless unreferenced rows; SmsWallet is
intentionally retained as a financial record. Documented in docs/audit-backlog.md.

Regression: tests/ClinicService/ServiceItemDeleteCleanupTest.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-06-28 21:17:30 +03:30

45 lines
1.6 KiB
PHP

<?php
namespace App\Tests\ClinicService;
use App\ClinicService\Entity\ServiceItem;
use App\ClinicService\Entity\ServiceSection;
use App\ClinicService\Entity\Tariff;
use App\Doctor\Entity\Doctor;
use App\Insurance\Entity\TenantServiceCoverage;
use App\Tests\ApiTestCase;
/**
* Deleting a service item must also remove its config rows (tariffs and
* tenant-coverage), which reference it by a raw int with no FK and would
* otherwise orphan.
*/
class ServiceItemDeleteCleanupTest extends ApiTestCase
{
public function testDeletingItemPurgesTariffAndCoverage(): void
{
$owner = $this->createUser(['ROLE_DOCTOR']);
$doctor = new Doctor($owner, 'دکتر');
$this->em->persist($doctor);
$this->em->flush();
$section = new ServiceSection('doctor', $doctor->getId(), 'بخش');
$item = new ServiceItem($section, 'خدمت');
$this->em->persist($section);
$this->em->persist($item);
$this->em->flush();
$itemId = $item->getId();
$this->em->persist(new Tariff($itemId, 1404, 1000));
$this->em->persist(new TenantServiceCoverage(999_999, $itemId));
$this->em->flush();
$this->authJson('DELETE', '/api/v1/service-item/' . $item->getUuid(), $owner);
$this->assertSame(200, $this->responseCode());
$this->em->clear();
$this->assertCount(0, $this->em->getRepository(Tariff::class)->findBy(['serviceItemId' => $itemId]));
$this->assertCount(0, $this->em->getRepository(TenantServiceCoverage::class)->findBy(['serviceItemId' => $itemId]));
}
}