Files
clinicpro/tests/ClinicService/ServiceItemDeleteCleanupTest.php
T
hamedandClaude Opus 4.8 aa842b883d fix(services): forbid deleting service items/sections — deactivate only
Services are referenced by appointments, sessions, invoices and payment history,
so deleting one orphans/corrupts those records (deleting a section cascaded to
its services too). Make deletion impossible:

- Backend: DELETE /service-item/{uuid} and DELETE /service-section/{uuid} now
  always return 409 (ERR_SERVICE_ITEM_IN_USE) with a message pointing to
  deactivate; no rows are touched. Deactivate stays via PATCH active=false.
- Frontend: removed the section delete button, its confirm dialog, the delete
  mutation, and the now-unused delete state/flag/icon from ClinicServicesPage.
  Section and item deactivate toggles are unchanged.

Tests: ServiceItemDeleteCleanupTest rewritten — delete of item and section both
rejected (409) and the row survives. docs/api/clinic-services.md updated.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-07-23 19:20:40 +03:30

58 lines
2.1 KiB
PHP

<?php
namespace App\Tests\ClinicService;
use App\ClinicService\Entity\ServiceItem;
use App\ClinicService\Entity\ServiceSection;
use App\Doctor\Entity\Doctor;
use App\Tests\ApiTestCase;
/**
* سرویس‌ها اصلاً حذف نمی‌شوند — نوبت/جلسه/فاکتور/سوابق پرداخت به سرویس ارجاع
* دارند. DELETE با ۴۰۹ رد می‌شود و سرویس دست‌نخورده می‌ماند؛ فقط غیرفعال‌کردن
* (PATCH active=false) مجاز است.
*/
class ServiceItemDeleteCleanupTest extends ApiTestCase
{
public function testDeletingItemIsRejected(): 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();
$itemUuid = $item->getUuid();
$this->authJson('DELETE', '/api/v1/service-item/' . $itemUuid, $owner);
$this->assertSame(409, $this->responseCode());
// سرویس باید همچنان وجود داشته باشد.
$this->em->clear();
$this->assertNotNull($this->em->getRepository(ServiceItem::class)->findOneBy(['uuid' => $itemUuid]));
}
public function testDeletingSectionIsRejected(): void
{
$owner = $this->createUser(['ROLE_DOCTOR']);
$doctor = new Doctor($owner, 'دکتر');
$this->em->persist($doctor);
$this->em->flush();
$section = new ServiceSection('doctor', $doctor->getId(), 'بخش');
$this->em->persist($section);
$this->em->flush();
$sectionUuid = $section->getUuid();
$this->authJson('DELETE', '/api/v1/service-section/' . $sectionUuid, $owner);
$this->assertSame(409, $this->responseCode());
$this->em->clear();
$this->assertNotNull($this->em->getRepository(ServiceSection::class)->findOneBy(['uuid' => $sectionUuid]));
}
}