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]>
69 lines
2.3 KiB
PHP
69 lines
2.3 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\Enums\CatalogLevel;
|
|
use App\Modules\Catalog\Domain\Exceptions\CatalogItemNotFound;
|
|
use App\Modules\Catalog\Domain\Exceptions\CatalogNameTaken;
|
|
use App\Modules\Catalog\Domain\ValueObjects\CatalogName;
|
|
use App\Modules\Shared\Application\Contracts\TransactionManager;
|
|
use InvalidArgumentException;
|
|
|
|
/**
|
|
* Renames a service unit or a service. Work types change with UpdateWorkType,
|
|
* which can also move them to another service.
|
|
*/
|
|
final class RenameCatalogItem
|
|
{
|
|
public function __construct(
|
|
private ServiceUnitRepository $serviceUnits,
|
|
private ServiceRepository $services,
|
|
private TransactionManager $transactions,
|
|
) {}
|
|
|
|
/**
|
|
* @throws CatalogItemNotFound
|
|
* @throws CatalogNameTaken
|
|
*/
|
|
public function execute(CatalogLevel $level, int $id, string $name): void
|
|
{
|
|
$name = new CatalogName($name);
|
|
|
|
$this->transactions->run(function () use ($level, $id, $name): void {
|
|
match ($level) {
|
|
CatalogLevel::ServiceUnit => $this->renameServiceUnit($id, $name),
|
|
CatalogLevel::Service => $this->renameService($id, $name),
|
|
CatalogLevel::WorkType => throw new InvalidArgumentException('Use UpdateWorkType for work types.'),
|
|
};
|
|
});
|
|
}
|
|
|
|
private function renameServiceUnit(int $id, CatalogName $name): void
|
|
{
|
|
$serviceUnit = $this->serviceUnits->findById($id) ?? throw new CatalogItemNotFound(CatalogLevel::ServiceUnit, $id);
|
|
|
|
if ($this->serviceUnits->isNameTaken($name->value, $id)) {
|
|
throw new CatalogNameTaken;
|
|
}
|
|
|
|
$serviceUnit->rename($name);
|
|
$this->serviceUnits->save($serviceUnit);
|
|
}
|
|
|
|
private function renameService(int $id, CatalogName $name): void
|
|
{
|
|
$service = $this->services->findById($id) ?? throw new CatalogItemNotFound(CatalogLevel::Service, $id);
|
|
|
|
if ($this->services->isNameTaken($service->serviceUnitId(), $name->value, $id)) {
|
|
throw new CatalogNameTaken;
|
|
}
|
|
|
|
$service->rename($name);
|
|
$this->services->save($service);
|
|
}
|
|
}
|