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,36 @@
<?php
declare(strict_types=1);
namespace App\Modules\Catalog\Domain\Contracts;
use App\Modules\Catalog\Domain\Entities\Service;
use App\Modules\Catalog\Domain\Enums\MoveDirection;
/**
* Siblings are the services of one unit.
*/
interface ServiceRepository
{
public function findById(int $id): ?Service;
/**
* Inserts a new item or updates a stored one, and returns the stored item.
*/
public function save(Service $service): Service;
/**
* Whether a sibling other than $exceptId has this name, ignoring case.
*/
public function isNameTaken(int $serviceUnitId, string $name, ?int $exceptId = null): bool;
/**
* The sort order that puts a new item last among its siblings.
*/
public function nextSortOrder(int $serviceUnitId): int;
/**
* The sibling just before or after the item, or null at either end.
*/
public function neighbour(Service $service, MoveDirection $direction): ?Service;
}
@@ -0,0 +1,36 @@
<?php
declare(strict_types=1);
namespace App\Modules\Catalog\Domain\Contracts;
use App\Modules\Catalog\Domain\Entities\ServiceUnit;
use App\Modules\Catalog\Domain\Enums\MoveDirection;
/**
* Siblings are all service units.
*/
interface ServiceUnitRepository
{
public function findById(int $id): ?ServiceUnit;
/**
* Inserts a new item or updates a stored one, and returns the stored item.
*/
public function save(ServiceUnit $serviceUnit): ServiceUnit;
/**
* Whether a sibling other than $exceptId has this name, ignoring case.
*/
public function isNameTaken(string $name, ?int $exceptId = null): bool;
/**
* The sort order that puts a new item last among its siblings.
*/
public function nextSortOrder(): int;
/**
* The sibling just before or after the item, or null at either end.
*/
public function neighbour(ServiceUnit $serviceUnit, MoveDirection $direction): ?ServiceUnit;
}
@@ -0,0 +1,36 @@
<?php
declare(strict_types=1);
namespace App\Modules\Catalog\Domain\Contracts;
use App\Modules\Catalog\Domain\Entities\WorkType;
use App\Modules\Catalog\Domain\Enums\MoveDirection;
/**
* Siblings are the work types under one service, or directly under the unit when $serviceId is null.
*/
interface WorkTypeRepository
{
public function findById(int $id): ?WorkType;
/**
* Inserts a new item or updates a stored one, and returns the stored item.
*/
public function save(WorkType $workType): WorkType;
/**
* Whether a sibling other than $exceptId has this name, ignoring case.
*/
public function isNameTaken(int $serviceUnitId, ?int $serviceId, string $name, ?int $exceptId = null): bool;
/**
* The sort order that puts a new item last among its siblings.
*/
public function nextSortOrder(int $serviceUnitId, ?int $serviceId): int;
/**
* The sibling just before or after the item, or null at either end.
*/
public function neighbour(WorkType $workType, MoveDirection $direction): ?WorkType;
}
@@ -0,0 +1,65 @@
<?php
declare(strict_types=1);
namespace App\Modules\Catalog\Domain\Entities;
use App\Modules\Catalog\Domain\ValueObjects\CatalogName;
/**
* What service units, services and work types share: a name that is unique
* among its siblings, an order among them, and an active flag. Items are
* never deleted, only deactivated, so old references keep working.
*/
abstract class CatalogItem
{
public function __construct(
protected ?int $id,
protected CatalogName $name,
protected bool $active,
protected int $sortOrder,
) {}
public function id(): ?int
{
return $this->id;
}
public function name(): CatalogName
{
return $this->name;
}
public function isActive(): bool
{
return $this->active;
}
public function sortOrder(): int
{
return $this->sortOrder;
}
public function rename(CatalogName $name): void
{
$this->name = $name;
}
public function activate(): void
{
$this->active = true;
}
public function deactivate(): void
{
$this->active = false;
}
/**
* Swaps places with a sibling.
*/
public function swapOrderWith(self $sibling): void
{
[$this->sortOrder, $sibling->sortOrder] = [$sibling->sortOrder, $this->sortOrder];
}
}
@@ -0,0 +1,42 @@
<?php
declare(strict_types=1);
namespace App\Modules\Catalog\Domain\Entities;
use App\Modules\Catalog\Domain\Exceptions\SharedUnitHasNoServices;
use App\Modules\Catalog\Domain\ValueObjects\CatalogName;
/**
* A service (palvelu) inside a service unit, e.g. Videotuotanto in
* Mediatuotanto. Optional level: most units have work types directly.
*/
final class Service extends CatalogItem
{
private function __construct(?int $id, private int $serviceUnitId, CatalogName $name, bool $active, int $sortOrder)
{
parent::__construct($id, $name, $active, $sortOrder);
}
/**
* @throws SharedUnitHasNoServices
*/
public static function create(ServiceUnit $serviceUnit, CatalogName $name, int $sortOrder): self
{
if ($serviceUnit->isShared()) {
throw new SharedUnitHasNoServices;
}
return new self(null, (int) $serviceUnit->id(), $name, true, $sortOrder);
}
public static function reconstitute(int $id, int $serviceUnitId, CatalogName $name, bool $active, int $sortOrder): self
{
return new self($id, $serviceUnitId, $name, $active, $sortOrder);
}
public function serviceUnitId(): int
{
return $this->serviceUnitId;
}
}
@@ -0,0 +1,35 @@
<?php
declare(strict_types=1);
namespace App\Modules\Catalog\Domain\Entities;
use App\Modules\Catalog\Domain\ValueObjects\CatalogName;
/**
* A service unit (palveluyksikkö), e.g. Mediatuotanto. The shared unit
* ("Kaikille yhteiset") holds work types offered in every unit and has no
* services.
*/
final class ServiceUnit extends CatalogItem
{
private function __construct(?int $id, CatalogName $name, private bool $shared, bool $active, int $sortOrder)
{
parent::__construct($id, $name, $active, $sortOrder);
}
public static function create(CatalogName $name, int $sortOrder, bool $shared = false): self
{
return new self(null, $name, $shared, true, $sortOrder);
}
public static function reconstitute(int $id, CatalogName $name, bool $shared, bool $active, int $sortOrder): self
{
return new self($id, $name, $shared, $active, $sortOrder);
}
public function isShared(): bool
{
return $this->shared;
}
}
@@ -0,0 +1,76 @@
<?php
declare(strict_types=1);
namespace App\Modules\Catalog\Domain\Entities;
use App\Modules\Catalog\Domain\Exceptions\ServiceNotInUnit;
use App\Modules\Catalog\Domain\ValueObjects\CatalogName;
/**
* A work type (työtehtävä), e.g. editointi. Tasks and time entries are
* recorded against work types, and hourly rates (phase 2) belong to them.
*/
final class WorkType extends CatalogItem
{
private function __construct(
?int $id,
private int $serviceUnitId,
private ?int $serviceId,
CatalogName $name,
bool $active,
int $sortOrder,
) {
parent::__construct($id, $name, $active, $sortOrder);
}
/**
* @throws ServiceNotInUnit
*/
public static function create(ServiceUnit $serviceUnit, ?Service $service, CatalogName $name, int $sortOrder): self
{
self::ensureServiceInUnit($serviceUnit, $service);
return new self(null, (int) $serviceUnit->id(), $service?->id(), $name, true, $sortOrder);
}
public static function reconstitute(int $id, int $serviceUnitId, ?int $serviceId, CatalogName $name, bool $active, int $sortOrder): self
{
return new self($id, $serviceUnitId, $serviceId, $name, $active, $sortOrder);
}
/**
* Moves the work type under another service of the same unit (or under
* no service), last among its new siblings.
*
* @throws ServiceNotInUnit
*/
public function moveToService(ServiceUnit $serviceUnit, ?Service $service, int $sortOrder): void
{
if ($serviceUnit->id() !== $this->serviceUnitId) {
throw new ServiceNotInUnit;
}
self::ensureServiceInUnit($serviceUnit, $service);
$this->serviceId = $service?->id();
$this->sortOrder = $sortOrder;
}
public function serviceUnitId(): int
{
return $this->serviceUnitId;
}
public function serviceId(): ?int
{
return $this->serviceId;
}
private static function ensureServiceInUnit(ServiceUnit $serviceUnit, ?Service $service): void
{
if ($service !== null && $service->serviceUnitId() !== $serviceUnit->id()) {
throw new ServiceNotInUnit;
}
}
}
@@ -0,0 +1,15 @@
<?php
declare(strict_types=1);
namespace App\Modules\Catalog\Domain\Enums;
/**
* The three levels of the catalog: service unit > (service) > work type.
*/
enum CatalogLevel: string
{
case ServiceUnit = 'service_unit';
case Service = 'service';
case WorkType = 'work_type';
}
@@ -0,0 +1,11 @@
<?php
declare(strict_types=1);
namespace App\Modules\Catalog\Domain\Enums;
enum MoveDirection: string
{
case Up = 'up';
case Down = 'down';
}
@@ -0,0 +1,16 @@
<?php
declare(strict_types=1);
namespace App\Modules\Catalog\Domain\Exceptions;
use App\Modules\Catalog\Domain\Enums\CatalogLevel;
use App\Modules\Shared\Domain\Exceptions\DomainException;
final class CatalogItemNotFound extends DomainException
{
public function __construct(public readonly CatalogLevel $level, public readonly int $id)
{
parent::__construct("Catalog {$level->value} {$id} was not found.");
}
}
@@ -0,0 +1,15 @@
<?php
declare(strict_types=1);
namespace App\Modules\Catalog\Domain\Exceptions;
use App\Modules\Shared\Domain\Exceptions\DomainException;
final class CatalogNameTaken extends DomainException
{
public function __construct()
{
parent::__construct('Another item on the same level already has this name.');
}
}
@@ -0,0 +1,15 @@
<?php
declare(strict_types=1);
namespace App\Modules\Catalog\Domain\Exceptions;
use App\Modules\Shared\Domain\Exceptions\DomainException;
final class InvalidCatalogName extends DomainException
{
public function __construct()
{
parent::__construct('The name must be 1–100 characters.');
}
}
@@ -0,0 +1,15 @@
<?php
declare(strict_types=1);
namespace App\Modules\Catalog\Domain\Exceptions;
use App\Modules\Shared\Domain\Exceptions\DomainException;
final class ServiceNotInUnit extends DomainException
{
public function __construct()
{
parent::__construct('The service belongs to another service unit.');
}
}
@@ -0,0 +1,15 @@
<?php
declare(strict_types=1);
namespace App\Modules\Catalog\Domain\Exceptions;
use App\Modules\Shared\Domain\Exceptions\DomainException;
final class SharedUnitHasNoServices extends DomainException
{
public function __construct()
{
parent::__construct('The shared service unit cannot have services.');
}
}
@@ -0,0 +1,28 @@
<?php
declare(strict_types=1);
namespace App\Modules\Catalog\Domain\ValueObjects;
use App\Modules\Catalog\Domain\Exceptions\InvalidCatalogName;
/**
* Name of a service unit, service or work type, e.g. "Videotuotanto".
*/
final readonly class CatalogName
{
public const int MAX_LENGTH = 100;
public string $value;
public function __construct(string $value)
{
$value = trim(preg_replace('/\s+/u', ' ', $value) ?? $value);
if ($value === '' || mb_strlen($value) > self::MAX_LENGTH) {
throw new InvalidCatalogName;
}
$this->value = $value;
}
}