Add catalog module for service units, services and work types

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]>
This commit is contained in:
2026-11-23 18:27:58 +02:00
co-authored by Claude Opus 5.5
parent e0b8f4970f
commit 51b2d4a252
62 changed files with 3196 additions and 7 deletions
@@ -0,0 +1,49 @@
<?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\Entities\Service;
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\Exceptions\SharedUnitHasNoServices;
use App\Modules\Catalog\Domain\ValueObjects\CatalogName;
use App\Modules\Shared\Application\Contracts\TransactionManager;
final class AddService
{
public function __construct(
private ServiceUnitRepository $serviceUnits,
private ServiceRepository $services,
private TransactionManager $transactions,
) {}
/**
* @return int The new service's id.
*
* @throws CatalogItemNotFound
* @throws CatalogNameTaken
* @throws SharedUnitHasNoServices
*/
public function execute(int $serviceUnitId, string $name): int
{
$name = new CatalogName($name);
return $this->transactions->run(function () use ($serviceUnitId, $name): int {
$serviceUnit = $this->serviceUnits->findById($serviceUnitId)
?? throw new CatalogItemNotFound(CatalogLevel::ServiceUnit, $serviceUnitId);
if ($this->services->isNameTaken($serviceUnitId, $name->value)) {
throw new CatalogNameTaken;
}
$service = Service::create($serviceUnit, $name, $this->services->nextSortOrder($serviceUnitId));
return (int) $this->services->save($service)->id();
});
}
}
@@ -0,0 +1,39 @@
<?php
declare(strict_types=1);
namespace App\Modules\Catalog\Application\UseCases;
use App\Modules\Catalog\Domain\Contracts\ServiceUnitRepository;
use App\Modules\Catalog\Domain\Entities\ServiceUnit;
use App\Modules\Catalog\Domain\Exceptions\CatalogNameTaken;
use App\Modules\Catalog\Domain\ValueObjects\CatalogName;
use App\Modules\Shared\Application\Contracts\TransactionManager;
final class AddServiceUnit
{
public function __construct(
private ServiceUnitRepository $serviceUnits,
private TransactionManager $transactions,
) {}
/**
* @return int The new service unit's id.
*
* @throws CatalogNameTaken
*/
public function execute(string $name): int
{
$name = new CatalogName($name);
return $this->transactions->run(function () use ($name): int {
if ($this->serviceUnits->isNameTaken($name->value)) {
throw new CatalogNameTaken;
}
$serviceUnit = ServiceUnit::create($name, $this->serviceUnits->nextSortOrder());
return (int) $this->serviceUnits->save($serviceUnit)->id();
});
}
}
@@ -0,0 +1,53 @@
<?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\WorkType;
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\Exceptions\ServiceNotInUnit;
use App\Modules\Catalog\Domain\ValueObjects\CatalogName;
use App\Modules\Shared\Application\Contracts\TransactionManager;
final class AddWorkType
{
public function __construct(
private ServiceUnitRepository $serviceUnits,
private ServiceRepository $services,
private WorkTypeRepository $workTypes,
private TransactionManager $transactions,
) {}
/**
* @return int The new work type's id.
*
* @throws CatalogItemNotFound
* @throws CatalogNameTaken
* @throws ServiceNotInUnit
*/
public function execute(int $serviceUnitId, ?int $serviceId, string $name): int
{
$name = new CatalogName($name);
return $this->transactions->run(function () use ($serviceUnitId, $serviceId, $name): int {
$serviceUnit = $this->serviceUnits->findById($serviceUnitId)
?? throw new CatalogItemNotFound(CatalogLevel::ServiceUnit, $serviceUnitId);
$service = $serviceId === null ? null : ($this->services->findById($serviceId)
?? throw new CatalogItemNotFound(CatalogLevel::Service, $serviceId));
if ($this->workTypes->isNameTaken($serviceUnitId, $serviceId, $name->value)) {
throw new CatalogNameTaken;
}
$workType = WorkType::create($serviceUnit, $service, $name, $this->workTypes->nextSortOrder($serviceUnitId, $serviceId));
return (int) $this->workTypes->save($workType)->id();
});
}
}
@@ -0,0 +1,83 @@
<?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\Service;
use App\Modules\Catalog\Domain\Entities\ServiceUnit;
use App\Modules\Catalog\Domain\Entities\WorkType;
use App\Modules\Catalog\Domain\Enums\CatalogLevel;
use App\Modules\Catalog\Domain\Enums\MoveDirection;
use App\Modules\Catalog\Domain\Exceptions\CatalogItemNotFound;
use App\Modules\Shared\Application\Contracts\TransactionManager;
/**
* Moves an item one place up or down among its siblings.
*/
final class MoveCatalogItem
{
public function __construct(
private ServiceUnitRepository $serviceUnits,
private ServiceRepository $services,
private WorkTypeRepository $workTypes,
private TransactionManager $transactions,
) {}
/**
* @return bool False when the item already was first (up) or last (down).
*
* @throws CatalogItemNotFound
*/
public function execute(CatalogLevel $level, int $id, MoveDirection $direction): bool
{
return $this->transactions->run(fn (): bool => match ($level) {
CatalogLevel::ServiceUnit => $this->moveServiceUnit($id, $direction),
CatalogLevel::Service => $this->moveService($id, $direction),
CatalogLevel::WorkType => $this->moveWorkType($id, $direction),
});
}
private function moveServiceUnit(int $id, MoveDirection $direction): bool
{
$item = $this->serviceUnits->findById($id) ?? throw new CatalogItemNotFound(CatalogLevel::ServiceUnit, $id);
$neighbour = $this->serviceUnits->neighbour($item, $direction);
return $neighbour !== null && $this->swap($item, $neighbour, fn (ServiceUnit $unit) => $this->serviceUnits->save($unit));
}
private function moveService(int $id, MoveDirection $direction): bool
{
$item = $this->services->findById($id) ?? throw new CatalogItemNotFound(CatalogLevel::Service, $id);
$neighbour = $this->services->neighbour($item, $direction);
return $neighbour !== null && $this->swap($item, $neighbour, fn (Service $service) => $this->services->save($service));
}
private function moveWorkType(int $id, MoveDirection $direction): bool
{
$item = $this->workTypes->findById($id) ?? throw new CatalogItemNotFound(CatalogLevel::WorkType, $id);
$neighbour = $this->workTypes->neighbour($item, $direction);
return $neighbour !== null && $this->swap($item, $neighbour, fn (WorkType $workType) => $this->workTypes->save($workType));
}
/**
* @template T of ServiceUnit|Service|WorkType
*
* @param T $item
* @param T $neighbour
* @param callable(T): mixed $save
*/
private function swap(ServiceUnit|Service|WorkType $item, ServiceUnit|Service|WorkType $neighbour, callable $save): bool
{
$item->swapOrderWith($neighbour);
$save($item);
$save($neighbour);
return true;
}
}
@@ -0,0 +1,68 @@
<?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);
}
}
@@ -0,0 +1,58 @@
<?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;
}
}
@@ -0,0 +1,60 @@
<?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\Enums\CatalogLevel;
use App\Modules\Catalog\Domain\Exceptions\CatalogItemNotFound;
use App\Modules\Catalog\Domain\Exceptions\CatalogNameTaken;
use App\Modules\Catalog\Domain\Exceptions\ServiceNotInUnit;
use App\Modules\Catalog\Domain\ValueObjects\CatalogName;
use App\Modules\Shared\Application\Contracts\TransactionManager;
/**
* Renames a work type and moves it under another service of its unit (or
* under no service).
*/
final class UpdateWorkType
{
public function __construct(
private ServiceUnitRepository $serviceUnits,
private ServiceRepository $services,
private WorkTypeRepository $workTypes,
private TransactionManager $transactions,
) {}
/**
* @throws CatalogItemNotFound
* @throws CatalogNameTaken
* @throws ServiceNotInUnit
*/
public function execute(int $workTypeId, ?int $serviceId, string $name): void
{
$name = new CatalogName($name);
$this->transactions->run(function () use ($workTypeId, $serviceId, $name): void {
$workType = $this->workTypes->findById($workTypeId)
?? throw new CatalogItemNotFound(CatalogLevel::WorkType, $workTypeId);
if ($this->workTypes->isNameTaken($workType->serviceUnitId(), $serviceId, $name->value, $workTypeId)) {
throw new CatalogNameTaken;
}
if ($serviceId !== $workType->serviceId()) {
$serviceUnit = $this->serviceUnits->findById($workType->serviceUnitId())
?? throw new CatalogItemNotFound(CatalogLevel::ServiceUnit, $workType->serviceUnitId());
$service = $serviceId === null ? null : ($this->services->findById($serviceId)
?? throw new CatalogItemNotFound(CatalogLevel::Service, $serviceId));
$workType->moveToService($serviceUnit, $service, $this->workTypes->nextSortOrder($workType->serviceUnitId(), $serviceId));
}
$workType->rename($name);
$this->workTypes->save($workType);
});
}
}