feat: add optional inventory package association to service items and implement audit logging
- Added `inventory_package_uuid` and `inventory_package_title` fields to the `ServiceItem` interface. - Updated API documentation to reflect new fields in service item responses. - Implemented methods in `ClinicServiceController` to handle inventory package associations. - Created `ServiceItemAuditLog` entity and repository for tracking changes to service items. - Added functionality to log changes to service items, including inventory package associations. - Implemented tests for attaching/detaching inventory packages and auditing changes. - Created database migrations for new fields and audit log table.
This commit is contained in:
@@ -4,16 +4,20 @@ namespace App\ClinicService\Controller;
|
||||
|
||||
use App\Auth\Entity\User;
|
||||
use App\ClinicService\Entity\ServiceItem;
|
||||
use App\ClinicService\Entity\ServiceItemAuditLog;
|
||||
use App\ClinicService\Entity\ServiceSection;
|
||||
use App\Insurance\Entity\TenantServiceCoverage;
|
||||
use App\ClinicService\Entity\Tariff;
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
use App\ClinicService\Repository\ServiceItemAuditLogRepository;
|
||||
use App\ClinicService\Repository\ServiceItemRepository;
|
||||
use App\ClinicService\Repository\ServiceSectionRepository;
|
||||
use App\ClinicService\Repository\TariffRepository;
|
||||
use App\ClinicService\Service\ServiceItemAuditService;
|
||||
use App\ClinicService\Service\TariffService;
|
||||
use App\Clinic\Repository\ClinicRepository;
|
||||
use App\Doctor\Repository\DoctorRepository;
|
||||
use App\Inventory\Repository\InventoryPackageRepository;
|
||||
use App\Shared\Constant\ErrorCodes;
|
||||
use App\Shared\Controller\BaseController;
|
||||
use App\Shared\Exception\AppException;
|
||||
@@ -39,9 +43,61 @@ class ClinicServiceController extends BaseController
|
||||
private readonly ClinicRepository $clinicRepo,
|
||||
private readonly TariffRepository $tariffRepo,
|
||||
private readonly TariffService $tariffService,
|
||||
private readonly InventoryPackageRepository $packageRepo,
|
||||
private readonly ServiceItemAuditService $auditService,
|
||||
private readonly ServiceItemAuditLogRepository $auditLogRepo,
|
||||
private readonly EntityManagerInterface $em,
|
||||
) {}
|
||||
|
||||
/**
|
||||
* uuid و عنوان پکیج کالای هر سرویس را به آرایهی خروجی اضافه میکند. پکیجها با یک
|
||||
* کوئری واکشی میشوند تا فهرست سرویسها به N+1 نیفتد.
|
||||
*
|
||||
* @param ServiceItem[] $items
|
||||
* @return array<int, array<string, mixed>>
|
||||
*/
|
||||
private function serializeItems(array $items): array
|
||||
{
|
||||
$packages = $this->packageRepo->findMapByIds(
|
||||
array_map(fn(ServiceItem $i) => $i->getInventoryPackageId(), $items)
|
||||
);
|
||||
|
||||
return array_map(function (ServiceItem $i) use ($packages) {
|
||||
$row = $i->toArray();
|
||||
$package = $packages[$i->getInventoryPackageId()] ?? null;
|
||||
$row['inventory_package_uuid'] = $package?->getUuid();
|
||||
$row['inventory_package_title'] = $package?->getTitle();
|
||||
|
||||
return $row;
|
||||
}, $items);
|
||||
}
|
||||
|
||||
/**
|
||||
* `inventory_package_uuid` را به id داخلی تبدیل و روی سرویس ست میکند.
|
||||
* مقدار null یعنی قطع اتصال. خطای دسترسی/نبود پکیج را برمیگرداند.
|
||||
*/
|
||||
private function applyInventoryPackage(ServiceItem $item, array $data, string $entityType, ?int $entityId): ?JsonResponse
|
||||
{
|
||||
if (!array_key_exists('inventory_package_uuid', $data)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$uuid = $data['inventory_package_uuid'];
|
||||
if ($uuid === null || $uuid === '') {
|
||||
$item->setInventoryPackageId(null);
|
||||
return null;
|
||||
}
|
||||
|
||||
$package = $this->packageRepo->findByUuid((string) $uuid);
|
||||
if ($package === null || $package->getEntityType() !== $entityType || $package->getEntityId() !== $entityId) {
|
||||
return $this->error(ErrorCodes::ERR_VALIDATION_001, 'پکیج کالا یافت نشد', 422, 'inventory_package_uuid');
|
||||
}
|
||||
|
||||
$item->setInventoryPackageId($package->getId());
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
// ── Service Sections ─────────────────────────────────────────────────────
|
||||
|
||||
#[Route('/api/v1/service-sections', methods: ['GET'])]
|
||||
@@ -128,12 +184,9 @@ class ClinicServiceController extends BaseController
|
||||
{
|
||||
[$entityType, $entityId] = $this->resolveEntity($user);
|
||||
|
||||
$items = array_map(
|
||||
fn(ServiceItem $i) => $i->toArray(),
|
||||
return $this->success($this->serializeItems(
|
||||
$this->itemRepo->findByEntity($entityType, $entityId)
|
||||
);
|
||||
|
||||
return $this->success($items);
|
||||
));
|
||||
}
|
||||
|
||||
#[Route('/api/v1/service-items/{sectionUuid}', methods: ['GET'])]
|
||||
@@ -146,12 +199,7 @@ class ClinicServiceController extends BaseController
|
||||
return $this->error(ErrorCodes::ERR_SERVICE_NOT_FOUND, ErrorCodes::message(ErrorCodes::ERR_SERVICE_NOT_FOUND), 404);
|
||||
}
|
||||
|
||||
$items = array_map(
|
||||
fn(ServiceItem $i) => $i->toArray(),
|
||||
$this->itemRepo->findBySection($section)
|
||||
);
|
||||
|
||||
return $this->success($items);
|
||||
return $this->success($this->serializeItems($this->itemRepo->findBySection($section)));
|
||||
}
|
||||
|
||||
#[Route('/api/v1/service-item/{uuid}', methods: ['GET'])]
|
||||
@@ -164,7 +212,24 @@ class ClinicServiceController extends BaseController
|
||||
return $this->error(ErrorCodes::ERR_SERVICE_NOT_FOUND, ErrorCodes::message(ErrorCodes::ERR_SERVICE_NOT_FOUND), 404);
|
||||
}
|
||||
|
||||
return $this->success($item->toArray());
|
||||
return $this->success($this->serializeItems([$item])[0]);
|
||||
}
|
||||
|
||||
/** تاریخچهی تغییرات یک خدمت — تازهترین رویداد اول. */
|
||||
#[Route('/api/v1/service-item/{uuid}/audit-logs', methods: ['GET'])]
|
||||
public function listItemAuditLogs(string $uuid, #[CurrentUser] User $user): JsonResponse
|
||||
{
|
||||
[$entityType, $entityId] = $this->resolveEntity($user);
|
||||
|
||||
$item = $this->itemRepo->findByUuid($uuid);
|
||||
if ($item === null || !$this->ownsSection($item->getSection(), $entityType, $entityId)) {
|
||||
return $this->error(ErrorCodes::ERR_SERVICE_NOT_FOUND, ErrorCodes::message(ErrorCodes::ERR_SERVICE_NOT_FOUND), 404);
|
||||
}
|
||||
|
||||
return $this->success(array_map(
|
||||
fn(ServiceItemAuditLog $l) => $l->toArray(),
|
||||
$this->auditLogRepo->findByItem($item)
|
||||
));
|
||||
}
|
||||
|
||||
#[Route('/api/v1/service-item', methods: ['POST'])]
|
||||
@@ -206,13 +271,18 @@ class ClinicServiceController extends BaseController
|
||||
if (array_key_exists('bookable', $data)) {
|
||||
$item->setBookable((bool) $data['bookable']);
|
||||
}
|
||||
$packageError = $this->applyInventoryPackage($item, $data, $entityType, $entityId);
|
||||
if ($packageError !== null) {
|
||||
return $packageError;
|
||||
}
|
||||
|
||||
$this->itemRepo->save($item);
|
||||
|
||||
// قیمت سرویس همان تعرفهی سال جاری است؛ هنگام ساخت، تعرفهی سال جاری ثبت میشود.
|
||||
$this->tariffService->upsert($item->getId(), $this->tariffService->currentJalaliYear(), $item->getPriceRials());
|
||||
$this->auditService->logCreate($item, $user);
|
||||
|
||||
return $this->success($item->toArray(), 201);
|
||||
return $this->success($this->serializeItems([$item])[0], 201);
|
||||
}
|
||||
|
||||
#[Route('/api/v1/service-item/{uuid}', methods: ['PATCH'])]
|
||||
@@ -225,7 +295,8 @@ class ClinicServiceController extends BaseController
|
||||
return $this->error(ErrorCodes::ERR_SERVICE_NOT_FOUND, ErrorCodes::message(ErrorCodes::ERR_SERVICE_NOT_FOUND), 404);
|
||||
}
|
||||
|
||||
$data = json_decode($request->getContent(), true) ?? [];
|
||||
$data = json_decode($request->getContent(), true) ?? [];
|
||||
$before = $this->auditService->snapshot($item);
|
||||
|
||||
$priceChanged = false;
|
||||
if (isset($data['name']) && trim($data['name']) !== '') { $item->setName(trim($data['name'])); }
|
||||
@@ -250,6 +321,10 @@ class ClinicServiceController extends BaseController
|
||||
if (array_key_exists('bookable', $data)) {
|
||||
$item->setBookable((bool) $data['bookable']);
|
||||
}
|
||||
$packageError = $this->applyInventoryPackage($item, $data, $entityType, $entityId);
|
||||
if ($packageError !== null) {
|
||||
return $packageError;
|
||||
}
|
||||
|
||||
$this->itemRepo->save($item);
|
||||
|
||||
@@ -258,7 +333,9 @@ class ClinicServiceController extends BaseController
|
||||
$this->tariffService->upsert($item->getId(), $this->tariffService->currentJalaliYear(), $item->getPriceRials());
|
||||
}
|
||||
|
||||
return $this->success($item->toArray());
|
||||
$this->auditService->logChanges($item, $before, $this->auditService->snapshot($item), $user);
|
||||
|
||||
return $this->success($this->serializeItems([$item])[0]);
|
||||
}
|
||||
|
||||
#[Route('/api/v1/service-item/{uuid}', methods: ['DELETE'])]
|
||||
|
||||
Reference in New Issue
Block a user