Phase 1, item 3: the hierarchy customer jobs, tasks and time entries will refer to. - New Catalog module: service unit > optional service > work type. Names are unique per level (case-insensitive); items are never deleted, only deactivated, and foreign keys restrict deletes. - Pohjatiedot page (/catalog, admins and management): add, rename, reorder with move up/down buttons (keyboard-friendly, focus kept and announced) and an "in use" checkbox per row. Work types can move to another service of the same unit. - A migration inserts the starting catalog from the specification when the tables are empty, so every installation has it. - CatalogQueries gives other modules the active service units and the work types of chosen units plus the shared ones. - Spec and CLAUDE.md updated. Co-Authored-By: Claude Opus 5.5 <[email protected]>
59 lines
2.0 KiB
PHP
59 lines
2.0 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Modules\Catalog\Application\UseCases;
|
|
|
|
use App\Modules\Catalog\Domain\Contracts\ServiceRepository;
|
|
use App\Modules\Catalog\Domain\Contracts\ServiceUnitRepository;
|
|
use App\Modules\Catalog\Domain\Contracts\WorkTypeRepository;
|
|
use App\Modules\Catalog\Domain\Entities\CatalogItem;
|
|
use App\Modules\Catalog\Domain\Enums\CatalogLevel;
|
|
use App\Modules\Catalog\Domain\Exceptions\CatalogItemNotFound;
|
|
use App\Modules\Shared\Application\Contracts\TransactionManager;
|
|
|
|
/**
|
|
* Takes an item out of use or back into use. Items are never deleted, so
|
|
* customer jobs and time entries that refer to them keep working.
|
|
*/
|
|
final class SetCatalogItemActive
|
|
{
|
|
public function __construct(
|
|
private ServiceUnitRepository $serviceUnits,
|
|
private ServiceRepository $services,
|
|
private WorkTypeRepository $workTypes,
|
|
private TransactionManager $transactions,
|
|
) {}
|
|
|
|
/**
|
|
* @throws CatalogItemNotFound
|
|
*/
|
|
public function execute(CatalogLevel $level, int $id, bool $active): void
|
|
{
|
|
$this->transactions->run(function () use ($level, $id, $active): void {
|
|
match ($level) {
|
|
CatalogLevel::ServiceUnit => $this->serviceUnits->save($this->toggled($this->serviceUnits->findById($id), $level, $id, $active)),
|
|
CatalogLevel::Service => $this->services->save($this->toggled($this->services->findById($id), $level, $id, $active)),
|
|
CatalogLevel::WorkType => $this->workTypes->save($this->toggled($this->workTypes->findById($id), $level, $id, $active)),
|
|
};
|
|
});
|
|
}
|
|
|
|
/**
|
|
* @template T of CatalogItem
|
|
*
|
|
* @param T|null $item
|
|
* @return T
|
|
*/
|
|
private function toggled(?CatalogItem $item, CatalogLevel $level, int $id, bool $active): CatalogItem
|
|
{
|
|
if ($item === null) {
|
|
throw new CatalogItemNotFound($level, $id);
|
|
}
|
|
|
|
$active ? $item->activate() : $item->deactivate();
|
|
|
|
return $item;
|
|
}
|
|
}
|