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,40 @@
<?php
namespace App\Modules\Catalog\Infrastructure\Persistence\Eloquent;
use Database\Factories\ServiceFactory;
use Illuminate\Database\Eloquent\Attributes\Fillable;
use Illuminate\Database\Eloquent\Attributes\Table;
use Illuminate\Database\Eloquent\Attributes\UseFactory;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
/**
* Persistence model, used only inside the Catalog Infrastructure layer.
*
* @property int $id
* @property int $service_unit_id
* @property string $name
* @property bool $is_active
* @property int $sort_order
*/
#[Table('services')]
#[Fillable(['service_unit_id', 'name', 'is_active', 'sort_order'])]
#[UseFactory(ServiceFactory::class)]
class ServiceModel extends Model
{
/** @use HasFactory<ServiceFactory> */
use HasFactory;
/**
* @return array<string, string>
*/
protected function casts(): array
{
return [
'service_unit_id' => 'integer',
'is_active' => 'boolean',
'sort_order' => 'integer',
];
}
}
@@ -0,0 +1,40 @@
<?php
namespace App\Modules\Catalog\Infrastructure\Persistence\Eloquent;
use Database\Factories\ServiceUnitFactory;
use Illuminate\Database\Eloquent\Attributes\Fillable;
use Illuminate\Database\Eloquent\Attributes\Table;
use Illuminate\Database\Eloquent\Attributes\UseFactory;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
/**
* Persistence model, used only inside the Catalog Infrastructure layer.
*
* @property int $id
* @property string $name
* @property bool $is_shared
* @property bool $is_active
* @property int $sort_order
*/
#[Table('service_units')]
#[Fillable(['name', 'is_shared', 'is_active', 'sort_order'])]
#[UseFactory(ServiceUnitFactory::class)]
class ServiceUnitModel extends Model
{
/** @use HasFactory<ServiceUnitFactory> */
use HasFactory;
/**
* @return array<string, string>
*/
protected function casts(): array
{
return [
'is_shared' => 'boolean',
'is_active' => 'boolean',
'sort_order' => 'integer',
];
}
}
@@ -0,0 +1,42 @@
<?php
namespace App\Modules\Catalog\Infrastructure\Persistence\Eloquent;
use Database\Factories\WorkTypeFactory;
use Illuminate\Database\Eloquent\Attributes\Fillable;
use Illuminate\Database\Eloquent\Attributes\Table;
use Illuminate\Database\Eloquent\Attributes\UseFactory;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
/**
* Persistence model, used only inside the Catalog Infrastructure layer.
*
* @property int $id
* @property int $service_unit_id
* @property int|null $service_id
* @property string $name
* @property bool $is_active
* @property int $sort_order
*/
#[Table('work_types')]
#[Fillable(['service_unit_id', 'service_id', 'name', 'is_active', 'sort_order'])]
#[UseFactory(WorkTypeFactory::class)]
class WorkTypeModel extends Model
{
/** @use HasFactory<WorkTypeFactory> */
use HasFactory;
/**
* @return array<string, string>
*/
protected function casts(): array
{
return [
'service_unit_id' => 'integer',
'service_id' => 'integer',
'is_active' => 'boolean',
'sort_order' => 'integer',
];
}
}
@@ -0,0 +1,149 @@
<?php
namespace App\Modules\Catalog\Infrastructure\Persistence\Repositories;
use App\Modules\Catalog\Application\Contracts\CatalogQueries;
use App\Modules\Catalog\Application\DTOs\ServiceNode;
use App\Modules\Catalog\Application\DTOs\ServiceSummary;
use App\Modules\Catalog\Application\DTOs\ServiceUnitNode;
use App\Modules\Catalog\Application\DTOs\ServiceUnitSummary;
use App\Modules\Catalog\Application\DTOs\WorkTypeSummary;
use App\Modules\Catalog\Infrastructure\Persistence\Eloquent\ServiceModel;
use App\Modules\Catalog\Infrastructure\Persistence\Eloquent\ServiceUnitModel;
use App\Modules\Catalog\Infrastructure\Persistence\Eloquent\WorkTypeModel;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Support\Collection;
class EloquentCatalogQueries implements CatalogQueries
{
public function tree(bool $includeInactive = true): array
{
return $this->buildTree(ServiceUnitModel::query(), $includeInactive);
}
public function activeServiceUnits(): array
{
return ServiceUnitModel::query()
->where('is_active', true)
->where('is_shared', false)
->orderBy('sort_order')
->get()
->map(fn (ServiceUnitModel $model): ServiceUnitSummary => $this->toServiceUnitSummary($model))
->values()
->all();
}
public function activeWorkTypesFor(array $serviceUnitIds): array
{
$units = ServiceUnitModel::query()->where(fn (Builder $query) => $query
->whereIn('id', $serviceUnitIds)
->orWhere('is_shared', true));
return $this->buildTree($units, includeInactive: false);
}
public function findServiceUnit(int $id): ?ServiceUnitSummary
{
$model = ServiceUnitModel::query()->find($id);
return $model === null ? null : $this->toServiceUnitSummary($model);
}
public function findService(int $id): ?ServiceSummary
{
$model = ServiceModel::query()->find($id);
return $model === null ? null : $this->toServiceSummary($model);
}
public function findWorkType(int $id): ?WorkTypeSummary
{
$model = WorkTypeModel::query()->find($id);
return $model === null ? null : $this->toWorkTypeSummary($model);
}
public function servicesOf(int $serviceUnitId): array
{
return ServiceModel::query()
->where('service_unit_id', $serviceUnitId)
->orderBy('sort_order')
->get()
->map(fn (ServiceModel $model): ServiceSummary => $this->toServiceSummary($model))
->values()
->all();
}
/**
* Three queries for the whole tree, grouped in memory. The shared unit
* always comes last, after the units it serves.
*
* @param Builder<ServiceUnitModel> $units
* @return list<ServiceUnitNode>
*/
private function buildTree(Builder $units, bool $includeInactive): array
{
$unitModels = $units
->when(! $includeInactive, fn (Builder $query) => $query->where('is_active', true))
->orderBy('is_shared')
->orderBy('sort_order')
->get();
$unitIds = $unitModels->pluck('id')->all();
/** @var Collection<int, Collection<int, ServiceModel>> $services */
$services = ServiceModel::query()
->whereIn('service_unit_id', $unitIds)
->when(! $includeInactive, fn (Builder $query) => $query->where('is_active', true))
->orderBy('sort_order')
->get()
->groupBy('service_unit_id');
/** @var Collection<string, Collection<int, WorkTypeModel>> $workTypes Keyed by "unitId-serviceId" or "unitId-none". */
$workTypes = WorkTypeModel::query()
->whereIn('service_unit_id', $unitIds)
->when(! $includeInactive, fn (Builder $query) => $query->where('is_active', true))
->orderBy('sort_order')
->get()
->groupBy(fn (WorkTypeModel $model): string => $model->service_unit_id.'-'.($model->service_id ?? 'none'));
return $unitModels->map(fn (ServiceUnitModel $unit): ServiceUnitNode => new ServiceUnitNode(
$this->toServiceUnitSummary($unit),
($services->get($unit->id) ?? collect())
->map(fn (ServiceModel $service): ServiceNode => new ServiceNode(
$this->toServiceSummary($service),
$this->workTypeSummaries($workTypes->get($unit->id.'-'.$service->id)),
))
->values()
->all(),
$this->workTypeSummaries($workTypes->get($unit->id.'-none')),
))->values()->all();
}
/**
* @param Collection<int, WorkTypeModel>|null $models
* @return list<WorkTypeSummary>
*/
private function workTypeSummaries(?Collection $models): array
{
return ($models ?? collect())
->map(fn (WorkTypeModel $model): WorkTypeSummary => $this->toWorkTypeSummary($model))
->values()
->all();
}
private function toServiceUnitSummary(ServiceUnitModel $model): ServiceUnitSummary
{
return new ServiceUnitSummary($model->id, $model->name, $model->is_shared, $model->is_active);
}
private function toServiceSummary(ServiceModel $model): ServiceSummary
{
return new ServiceSummary($model->id, $model->service_unit_id, $model->name, $model->is_active);
}
private function toWorkTypeSummary(WorkTypeModel $model): WorkTypeSummary
{
return new WorkTypeSummary($model->id, $model->service_unit_id, $model->service_id, $model->name, $model->is_active);
}
}
@@ -0,0 +1,63 @@
<?php
namespace App\Modules\Catalog\Infrastructure\Persistence\Repositories;
use App\Modules\Catalog\Domain\Contracts\ServiceRepository;
use App\Modules\Catalog\Domain\Entities\Service;
use App\Modules\Catalog\Domain\Enums\MoveDirection;
use App\Modules\Catalog\Domain\ValueObjects\CatalogName;
use App\Modules\Catalog\Infrastructure\Persistence\Eloquent\ServiceModel;
class EloquentServiceRepository implements ServiceRepository
{
public function findById(int $id): ?Service
{
$model = ServiceModel::query()->find($id);
return $model === null ? null : $this->toEntity($model);
}
public function save(Service $service): Service
{
$model = $service->id() === null ? new ServiceModel : ServiceModel::query()->findOrFail($service->id());
$model->fill([
'service_unit_id' => $service->serviceUnitId(),
'name' => $service->name()->value,
'is_active' => $service->isActive(),
'sort_order' => $service->sortOrder(),
])->save();
return $this->toEntity($model);
}
public function isNameTaken(int $serviceUnitId, string $name, ?int $exceptId = null): bool
{
return ServiceModel::query()
->where('service_unit_id', $serviceUnitId)
->whereRaw('LOWER(name) = ?', [mb_strtolower($name)])
->when($exceptId !== null, fn ($query) => $query->whereKeyNot($exceptId))
->exists();
}
public function nextSortOrder(int $serviceUnitId): int
{
return (int) ServiceModel::query()->where('service_unit_id', $serviceUnitId)->max('sort_order') + 1;
}
public function neighbour(Service $service, MoveDirection $direction): ?Service
{
$model = ServiceModel::query()
->where('service_unit_id', $service->serviceUnitId())
->where('sort_order', $direction === MoveDirection::Up ? '<' : '>', $service->sortOrder())
->orderBy('sort_order', $direction === MoveDirection::Up ? 'desc' : 'asc')
->first();
return $model === null ? null : $this->toEntity($model);
}
private function toEntity(ServiceModel $model): Service
{
return Service::reconstitute($model->id, $model->service_unit_id, new CatalogName($model->name), $model->is_active, $model->sort_order);
}
}
@@ -0,0 +1,61 @@
<?php
namespace App\Modules\Catalog\Infrastructure\Persistence\Repositories;
use App\Modules\Catalog\Domain\Contracts\ServiceUnitRepository;
use App\Modules\Catalog\Domain\Entities\ServiceUnit;
use App\Modules\Catalog\Domain\Enums\MoveDirection;
use App\Modules\Catalog\Domain\ValueObjects\CatalogName;
use App\Modules\Catalog\Infrastructure\Persistence\Eloquent\ServiceUnitModel;
class EloquentServiceUnitRepository implements ServiceUnitRepository
{
public function findById(int $id): ?ServiceUnit
{
$model = ServiceUnitModel::query()->find($id);
return $model === null ? null : $this->toEntity($model);
}
public function save(ServiceUnit $serviceUnit): ServiceUnit
{
$model = $serviceUnit->id() === null ? new ServiceUnitModel : ServiceUnitModel::query()->findOrFail($serviceUnit->id());
$model->fill([
'name' => $serviceUnit->name()->value,
'is_shared' => $serviceUnit->isShared(),
'is_active' => $serviceUnit->isActive(),
'sort_order' => $serviceUnit->sortOrder(),
])->save();
return $this->toEntity($model);
}
public function isNameTaken(string $name, ?int $exceptId = null): bool
{
return ServiceUnitModel::query()
->whereRaw('LOWER(name) = ?', [mb_strtolower($name)])
->when($exceptId !== null, fn ($query) => $query->whereKeyNot($exceptId))
->exists();
}
public function nextSortOrder(): int
{
return (int) ServiceUnitModel::query()->max('sort_order') + 1;
}
public function neighbour(ServiceUnit $serviceUnit, MoveDirection $direction): ?ServiceUnit
{
$model = ServiceUnitModel::query()
->where('sort_order', $direction === MoveDirection::Up ? '<' : '>', $serviceUnit->sortOrder())
->orderBy('sort_order', $direction === MoveDirection::Up ? 'desc' : 'asc')
->first();
return $model === null ? null : $this->toEntity($model);
}
private function toEntity(ServiceUnitModel $model): ServiceUnit
{
return ServiceUnit::reconstitute($model->id, new CatalogName($model->name), $model->is_shared, $model->is_active, $model->sort_order);
}
}
@@ -0,0 +1,84 @@
<?php
namespace App\Modules\Catalog\Infrastructure\Persistence\Repositories;
use App\Modules\Catalog\Domain\Contracts\WorkTypeRepository;
use App\Modules\Catalog\Domain\Entities\WorkType;
use App\Modules\Catalog\Domain\Enums\MoveDirection;
use App\Modules\Catalog\Domain\ValueObjects\CatalogName;
use App\Modules\Catalog\Infrastructure\Persistence\Eloquent\WorkTypeModel;
use Illuminate\Database\Eloquent\Builder;
class EloquentWorkTypeRepository implements WorkTypeRepository
{
public function findById(int $id): ?WorkType
{
$model = WorkTypeModel::query()->find($id);
return $model === null ? null : $this->toEntity($model);
}
public function save(WorkType $workType): WorkType
{
$model = $workType->id() === null ? new WorkTypeModel : WorkTypeModel::query()->findOrFail($workType->id());
$model->fill([
'service_unit_id' => $workType->serviceUnitId(),
'service_id' => $workType->serviceId(),
'name' => $workType->name()->value,
'is_active' => $workType->isActive(),
'sort_order' => $workType->sortOrder(),
])->save();
return $this->toEntity($model);
}
public function isNameTaken(int $serviceUnitId, ?int $serviceId, string $name, ?int $exceptId = null): bool
{
return $this->siblings($serviceUnitId, $serviceId)
->whereRaw('LOWER(name) = ?', [mb_strtolower($name)])
->when($exceptId !== null, fn ($query) => $query->whereKeyNot($exceptId))
->exists();
}
public function nextSortOrder(int $serviceUnitId, ?int $serviceId): int
{
return (int) $this->siblings($serviceUnitId, $serviceId)->max('sort_order') + 1;
}
public function neighbour(WorkType $workType, MoveDirection $direction): ?WorkType
{
$model = $this->siblings($workType->serviceUnitId(), $workType->serviceId())
->where('sort_order', $direction === MoveDirection::Up ? '<' : '>', $workType->sortOrder())
->orderBy('sort_order', $direction === MoveDirection::Up ? 'desc' : 'asc')
->first();
return $model === null ? null : $this->toEntity($model);
}
/**
* @return Builder<WorkTypeModel>
*/
private function siblings(int $serviceUnitId, ?int $serviceId): Builder
{
return WorkTypeModel::query()
->where('service_unit_id', $serviceUnitId)
->when(
$serviceId === null,
fn (Builder $query) => $query->whereNull('service_id'),
fn (Builder $query) => $query->where('service_id', $serviceId),
);
}
private function toEntity(WorkTypeModel $model): WorkType
{
return WorkType::reconstitute(
$model->id,
$model->service_unit_id,
$model->service_id,
new CatalogName($model->name),
$model->is_active,
$model->sort_order,
);
}
}
@@ -0,0 +1,49 @@
<?php
namespace App\Modules\Catalog\Infrastructure\Providers;
use App\Models\User;
use App\Modules\Catalog\Application\Contracts\CatalogQueries;
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\Infrastructure\Persistence\Repositories\EloquentCatalogQueries;
use App\Modules\Catalog\Infrastructure\Persistence\Repositories\EloquentServiceRepository;
use App\Modules\Catalog\Infrastructure\Persistence\Repositories\EloquentServiceUnitRepository;
use App\Modules\Catalog\Infrastructure\Persistence\Repositories\EloquentWorkTypeRepository;
use App\Modules\Users\Domain\Enums\Role;
use Illuminate\Support\Facades\Gate;
use Illuminate\Support\ServiceProvider;
use Livewire\Livewire;
/**
* Wires the Catalog module (service units, services, work types) into
* Laravel: bindings, routes, the `catalog::` Livewire namespace and the
* `catalog.manage` ability.
*/
class CatalogServiceProvider extends ServiceProvider
{
/**
* @var array<class-string, class-string>
*/
public array $bindings = [
ServiceUnitRepository::class => EloquentServiceUnitRepository::class,
ServiceRepository::class => EloquentServiceRepository::class,
WorkTypeRepository::class => EloquentWorkTypeRepository::class,
CatalogQueries::class => EloquentCatalogQueries::class,
];
public function boot(): void
{
$presentationPath = dirname(__DIR__, 2).'/Presentation';
Livewire::addNamespace('catalog', viewPath: $presentationPath.'/Livewire');
if (! $this->app->routesAreCached()) {
$this->loadRoutesFrom($presentationPath.'/Routes/web.php');
}
// Catalog data (pohjatiedot) is managed by admins and management.
Gate::define('catalog.manage', fn (User $user): bool => in_array($user->role, [Role::Admin, Role::Management], true));
}
}