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,74 @@
<?php
namespace App\Modules\Catalog\Presentation\Livewire\Forms;
use App\Modules\Catalog\Domain\Exceptions\CatalogNameTaken;
use App\Modules\Catalog\Domain\Exceptions\InvalidCatalogName;
use App\Modules\Catalog\Domain\Exceptions\ServiceNotInUnit;
use App\Modules\Catalog\Domain\Exceptions\SharedUnitHasNoServices;
use App\Modules\Shared\Domain\Exceptions\DomainException;
use Livewire\Form;
/**
* Name (and for work types, service) of a service unit, service or work type.
*/
class CatalogItemForm extends Form
{
public string $name = '';
/**
* Only for work types: '' means directly under the service unit.
*/
public string $service_id = '';
public function validatedName(): string
{
$this->validate();
return $this->name;
}
public function serviceId(): ?int
{
return $this->service_id === '' ? null : (int) $this->service_id;
}
/**
* Shows a broken business rule as an error on the field it concerns.
*/
public function showDomainError(DomainException $exception): void
{
[$field, $key] = match ($exception::class) {
InvalidCatalogName::class => ['name', 'catalog.errors.invalid_name'],
CatalogNameTaken::class => ['name', 'catalog.errors.name_taken'],
ServiceNotInUnit::class => ['service_id', 'catalog.errors.service_not_in_unit'],
SharedUnitHasNoServices::class => ['name', 'catalog.errors.shared_unit_has_no_services'],
default => throw $exception,
};
$this->addError($field, __($key));
}
/**
* @return array<string, array<int, string>>
*/
protected function rules(): array
{
return [
'name' => ['required', 'string', 'max:100'],
'service_id' => ['nullable', 'integer'],
];
}
/**
* @return array<string, string>
*/
protected function messages(): array
{
return [
'name.required' => __('catalog.errors.name_required'),
'name.max' => __('catalog.errors.invalid_name'),
'service_id.integer' => __('catalog.errors.service_not_in_unit'),
];
}
}
@@ -0,0 +1,216 @@
<?php
use App\Modules\Catalog\Application\Contracts\CatalogQueries;
use App\Modules\Catalog\Application\DTOs\ServiceUnitNode;
use App\Modules\Catalog\Application\UseCases\MoveCatalogItem;
use App\Modules\Catalog\Application\UseCases\SetCatalogItemActive;
use App\Modules\Catalog\Domain\Enums\CatalogLevel;
use App\Modules\Catalog\Domain\Enums\MoveDirection;
use App\Modules\Catalog\Domain\Exceptions\CatalogItemNotFound;
use Livewire\Attributes\Computed;
use Livewire\Component;
/**
* Catalog management (pohjatiedot): service units, services and work types.
*/
new class extends Component
{
public bool $showInactive = true;
public string $statusMessage = '';
protected CatalogQueries $catalogQueries;
public function boot(CatalogQueries $catalogQueries): void
{
$this->catalogQueries = $catalogQueries;
}
public function mount(): void
{
$this->authorize('catalog.manage');
}
/**
* @return list<ServiceUnitNode>
*/
#[Computed]
public function tree(): array
{
return $this->catalogQueries->tree($this->showInactive);
}
public function move(string $level, int $id, string $direction, MoveCatalogItem $moveItem): void
{
$this->authorize('catalog.manage');
$catalogLevel = CatalogLevel::from($level);
$moveDirection = MoveDirection::from($direction);
$name = $this->nameOf($catalogLevel, $id);
try {
$moved = $moveItem->execute($catalogLevel, $id, $moveDirection);
} catch (CatalogItemNotFound) {
$this->statusMessage = __('catalog.status.not_found');
return;
}
$this->statusMessage = __(match (true) {
$moved && $moveDirection === MoveDirection::Up => 'catalog.status.moved_up',
$moved => 'catalog.status.moved_down',
$moveDirection === MoveDirection::Up => 'catalog.status.already_first',
default => 'catalog.status.already_last',
}, ['name' => $name]);
// The row moved in the DOM: keep focus on the same button.
$this->js("document.getElementById('move-{$direction}-{$level}-{$id}')?.focus()");
}
public function toggleActive(string $level, int $id, SetCatalogItemActive $setActive): void
{
$this->authorize('catalog.manage');
$catalogLevel = CatalogLevel::from($level);
$active = ! $this->isActive($catalogLevel, $id);
try {
$setActive->execute($catalogLevel, $id, $active);
} catch (CatalogItemNotFound) {
$this->statusMessage = __('catalog.status.not_found');
return;
}
$this->statusMessage = __($active ? 'catalog.status.activated' : 'catalog.status.deactivated', [
'name' => $this->nameOf($catalogLevel, $id),
]);
}
public function render(): mixed
{
return $this->view()->title(__('catalog.title'));
}
private function nameOf(CatalogLevel $level, int $id): string
{
return match ($level) {
CatalogLevel::ServiceUnit => $this->catalogQueries->findServiceUnit($id)?->name,
CatalogLevel::Service => $this->catalogQueries->findService($id)?->name,
CatalogLevel::WorkType => $this->catalogQueries->findWorkType($id)?->name,
} ?? '';
}
private function isActive(CatalogLevel $level, int $id): bool
{
return match ($level) {
CatalogLevel::ServiceUnit => $this->catalogQueries->findServiceUnit($id)?->isActive,
CatalogLevel::Service => $this->catalogQueries->findService($id)?->isActive,
CatalogLevel::WorkType => $this->catalogQueries->findWorkType($id)?->isActive,
} ?? false;
}
};
?>
<div class="mx-auto max-w-5xl space-y-6 p-4 sm:p-6">
<div class="flex flex-wrap items-center justify-between gap-4">
<h1 class="text-2xl font-semibold">{{ __('catalog.title') }}</h1>
<x-button :href="route('catalog.service-units.create')" wire:navigate icon="plus" :text="__('catalog.actions.add_service_unit')" />
</div>
<p class="text-sm text-zinc-600 dark:text-zinc-300">{{ __('catalog.intro') }}</p>
@if (session('status'))
<x-alert role="status" color="green" light icon="check-circle" :text="session('status')" />
@endif
<x-checkbox id="show-inactive" wire:model.live="showInactive" :label="__('catalog.show_inactive')" />
<p role="status" aria-live="polite" class="text-sm">{{ $statusMessage }}</p>
@foreach ($this->tree as $node)
@php($unit = $node->serviceUnit)
<section
wire:key="service-unit-{{ $unit->id }}"
aria-labelledby="service-unit-{{ $unit->id }}-heading"
class="space-y-4 rounded-lg border border-zinc-200 p-4 dark:border-zinc-700"
>
<div class="flex flex-wrap items-center justify-between gap-3">
<h2 id="service-unit-{{ $unit->id }}-heading" class="text-xl font-semibold break-words">
{{ $unit->name }}
@unless ($unit->isActive)
<span class="ml-2 text-sm font-normal">({{ __('catalog.inactive') }})</span>
@endunless
</h2>
<x-catalog.item-actions
level="service_unit"
:id="$unit->id"
:name="$unit->name"
:active="$unit->isActive"
:edit-url="route('catalog.service-units.edit', $unit->id)"
/>
</div>
@if ($unit->isShared)
<p class="text-sm text-zinc-600 dark:text-zinc-300">{{ __('catalog.shared') }}</p>
@endif
@foreach ($node->services as $serviceNode)
@php($service = $serviceNode->service)
<div wire:key="service-{{ $service->id }}" class="space-y-2 border-l-2 border-zinc-200 pl-4 dark:border-zinc-700">
<div class="flex flex-wrap items-center justify-between gap-3">
<h3 class="text-lg font-medium break-words">
{{ $service->name }}
@unless ($service->isActive)
<span class="ml-2 text-sm font-normal">({{ __('catalog.inactive') }})</span>
@endunless
</h3>
<x-catalog.item-actions
level="service"
:id="$service->id"
:name="$service->name"
:active="$service->isActive"
:edit-url="route('catalog.services.edit', $service->id)"
/>
</div>
<x-catalog.work-type-list :work-types="$serviceNode->workTypes" />
</div>
@endforeach
@if ($node->workTypes !== [] || $node->services === [])
<x-catalog.work-type-list :work-types="$node->workTypes" />
@endif
<div class="flex flex-wrap gap-2">
@unless ($unit->isShared)
<x-button
:href="route('catalog.services.create', $unit->id)"
wire:navigate
subtle
color="black"
sm
icon="plus"
:text="__('catalog.actions.add_service')"
:aria-label="__('catalog.actions.add_service_to', ['name' => $unit->name])"
/>
@endunless
<x-button
:href="route('catalog.work-types.create', $unit->id)"
wire:navigate
subtle
color="black"
sm
icon="plus"
:text="__('catalog.actions.add_work_type')"
:aria-label="__('catalog.actions.add_work_type_to', ['name' => $unit->name])"
/>
</div>
</section>
@endforeach
</div>
@@ -0,0 +1,104 @@
<?php
use App\Modules\Catalog\Application\Contracts\CatalogQueries;
use App\Modules\Catalog\Application\UseCases\AddService;
use App\Modules\Catalog\Application\UseCases\RenameCatalogItem;
use App\Modules\Catalog\Domain\Enums\CatalogLevel;
use App\Modules\Catalog\Domain\Exceptions\CatalogItemNotFound;
use App\Modules\Catalog\Presentation\Livewire\Forms\CatalogItemForm;
use App\Modules\Shared\Domain\Exceptions\DomainException;
use Livewire\Attributes\Locked;
use Livewire\Component;
/**
* Add page (with {serviceUnit}) and edit page (with {service}) of a service.
*/
new class extends Component
{
#[Locked]
public ?int $serviceUnitId = null;
#[Locked]
public ?int $serviceId = null;
#[Locked]
public string $unitName = '';
public CatalogItemForm $form;
public function mount(CatalogQueries $catalogQueries, ?int $serviceUnit = null, ?int $service = null): void
{
$this->authorize('catalog.manage');
if ($service !== null) {
$summary = $catalogQueries->findService($service);
abort_if($summary === null, 404);
$this->serviceId = $summary->id;
$this->form->name = $summary->name;
$serviceUnit = $summary->serviceUnitId;
}
$unit = $serviceUnit === null ? null : $catalogQueries->findServiceUnit($serviceUnit);
abort_if($unit === null || ($unit->isShared && $this->serviceId === null), 404);
$this->serviceUnitId = $unit->id;
$this->unitName = $unit->name;
}
public function save(AddService $addService, RenameCatalogItem $renameItem): void
{
$this->authorize('catalog.manage');
$name = $this->form->validatedName();
try {
if ($this->serviceId === null) {
$addService->execute((int) $this->serviceUnitId, $name);
session()->flash('status', __('catalog.status.created', ['name' => trim($name)]));
} else {
$renameItem->execute(CatalogLevel::Service, $this->serviceId, $name);
session()->flash('status', __('catalog.status.updated', ['name' => trim($name)]));
}
} catch (CatalogItemNotFound) {
abort(404);
} catch (DomainException $exception) {
$this->form->showDomainError($exception);
return;
}
$this->redirectRoute('catalog.index', navigate: true);
}
public function heading(): string
{
return __($this->serviceId === null ? 'catalog.services.create_title' : 'catalog.services.edit_title');
}
public function render(): mixed
{
return $this->view()->title($this->heading().' – '.$this->unitName);
}
};
?>
<div class="mx-auto max-w-2xl space-y-6 p-4 sm:p-6">
<div class="space-y-1">
<h1 class="text-2xl font-semibold">{{ $this->heading() }}</h1>
@if ($unitName !== '')
<p class="text-sm text-zinc-600 dark:text-zinc-300">{{ __('catalog.fields.service_unit') }}: {{ $unitName }}</p>
@endif
</div>
<form wire:submit="save" class="space-y-6" novalidate>
<x-input wire:model="form.name" :label="__('catalog.fields.name')" autocomplete="off" required />
<div class="flex flex-wrap gap-3">
<x-button submit :text="__('catalog.save')" />
<x-button :href="route('catalog.index')" wire:navigate subtle color="black" :text="__('catalog.cancel')" />
</div>
</form>
</div>
@@ -0,0 +1,93 @@
<?php
use App\Modules\Catalog\Application\Contracts\CatalogQueries;
use App\Modules\Catalog\Application\UseCases\AddServiceUnit;
use App\Modules\Catalog\Application\UseCases\RenameCatalogItem;
use App\Modules\Catalog\Domain\Enums\CatalogLevel;
use App\Modules\Catalog\Domain\Exceptions\CatalogItemNotFound;
use App\Modules\Catalog\Presentation\Livewire\Forms\CatalogItemForm;
use App\Modules\Shared\Domain\Exceptions\DomainException;
use Livewire\Attributes\Locked;
use Livewire\Component;
/**
* Add page (no {serviceUnit}) and edit page (with {serviceUnit}) of a service unit.
*/
new class extends Component
{
#[Locked]
public ?int $serviceUnitId = null;
#[Locked]
public string $unitName = '';
public CatalogItemForm $form;
public function mount(CatalogQueries $catalogQueries, ?int $serviceUnit = null): void
{
$this->authorize('catalog.manage');
if ($serviceUnit === null) {
return;
}
$summary = $catalogQueries->findServiceUnit($serviceUnit);
abort_if($summary === null, 404);
$this->serviceUnitId = $summary->id;
$this->form->name = $summary->name;
}
public function save(AddServiceUnit $addServiceUnit, RenameCatalogItem $renameItem): void
{
$this->authorize('catalog.manage');
$name = $this->form->validatedName();
try {
if ($this->serviceUnitId === null) {
$addServiceUnit->execute($name);
session()->flash('status', __('catalog.status.created', ['name' => trim($name)]));
} else {
$renameItem->execute(CatalogLevel::ServiceUnit, $this->serviceUnitId, $name);
session()->flash('status', __('catalog.status.updated', ['name' => trim($name)]));
}
} catch (CatalogItemNotFound) {
abort(404);
} catch (DomainException $exception) {
$this->form->showDomainError($exception);
return;
}
$this->redirectRoute('catalog.index', navigate: true);
}
public function heading(): string
{
return __($this->serviceUnitId === null ? 'catalog.service_units.create_title' : 'catalog.service_units.edit_title');
}
public function render(): mixed
{
return $this->view()->title($this->heading());
}
};
?>
<div class="mx-auto max-w-2xl space-y-6 p-4 sm:p-6">
<div class="space-y-1">
<h1 class="text-2xl font-semibold">{{ $this->heading() }}</h1>
</div>
<form wire:submit="save" class="space-y-6" novalidate>
<x-input wire:model="form.name" :label="__('catalog.fields.name')" autocomplete="off" required />
<div class="flex flex-wrap gap-3">
<x-button submit :text="__('catalog.save')" />
<x-button :href="route('catalog.index')" wire:navigate subtle color="black" :text="__('catalog.cancel')" />
</div>
</form>
</div>
@@ -0,0 +1,135 @@
<?php
use App\Modules\Catalog\Application\Contracts\CatalogQueries;
use App\Modules\Catalog\Application\DTOs\ServiceSummary;
use App\Modules\Catalog\Application\UseCases\AddWorkType;
use App\Modules\Catalog\Application\UseCases\UpdateWorkType;
use App\Modules\Catalog\Domain\Exceptions\CatalogItemNotFound;
use App\Modules\Catalog\Presentation\Livewire\Forms\CatalogItemForm;
use App\Modules\Shared\Domain\Exceptions\DomainException;
use Livewire\Attributes\Computed;
use Livewire\Attributes\Locked;
use Livewire\Component;
/**
* Add page (with {serviceUnit}) and edit page (with {workType}) of a work type.
*/
new class extends Component
{
#[Locked]
public ?int $serviceUnitId = null;
#[Locked]
public ?int $workTypeId = null;
#[Locked]
public string $unitName = '';
public CatalogItemForm $form;
public function mount(CatalogQueries $catalogQueries, ?int $serviceUnit = null, ?int $workType = null): void
{
$this->authorize('catalog.manage');
if ($workType !== null) {
$summary = $catalogQueries->findWorkType($workType);
abort_if($summary === null, 404);
$this->workTypeId = $summary->id;
$this->form->name = $summary->name;
$this->form->service_id = $summary->serviceId === null ? '' : (string) $summary->serviceId;
$serviceUnit = $summary->serviceUnitId;
}
$unit = $serviceUnit === null ? null : $catalogQueries->findServiceUnit($serviceUnit);
abort_if($unit === null, 404);
$this->serviceUnitId = $unit->id;
$this->unitName = $unit->name;
}
/**
* @return list<ServiceSummary>
*/
#[Computed]
public function services(): array
{
return app(CatalogQueries::class)->servicesOf((int) $this->serviceUnitId);
}
public function save(AddWorkType $addWorkType, UpdateWorkType $updateWorkType): void
{
$this->authorize('catalog.manage');
$name = $this->form->validatedName();
$serviceId = $this->form->serviceId();
try {
if ($this->workTypeId === null) {
$addWorkType->execute((int) $this->serviceUnitId, $serviceId, $name);
session()->flash('status', __('catalog.status.created', ['name' => trim($name)]));
} else {
$updateWorkType->execute($this->workTypeId, $serviceId, $name);
session()->flash('status', __('catalog.status.updated', ['name' => trim($name)]));
}
} catch (CatalogItemNotFound) {
$this->form->addError('form.service_id', __('catalog.errors.service_not_in_unit'));
return;
} catch (DomainException $exception) {
$this->form->showDomainError($exception);
return;
}
$this->redirectRoute('catalog.index', navigate: true);
}
public function heading(): string
{
return __($this->workTypeId === null ? 'catalog.work_types.create_title' : 'catalog.work_types.edit_title');
}
public function render(): mixed
{
return $this->view()->title($this->heading().' – '.$this->unitName);
}
};
?>
<div class="mx-auto max-w-2xl space-y-6 p-4 sm:p-6">
<div class="space-y-1">
<h1 class="text-2xl font-semibold">{{ $this->heading() }}</h1>
@if ($unitName !== '')
<p class="text-sm text-zinc-600 dark:text-zinc-300">{{ __('catalog.fields.service_unit') }}: {{ $unitName }}</p>
@endif
</div>
<form wire:submit="save" class="space-y-6" novalidate>
<x-input wire:model="form.name" :label="__('catalog.fields.name')" autocomplete="off" required />
@if ($this->services !== [])
{{-- TallStackUI's native select does not tie its label to the control, so aria-label names it. --}}
<x-select.native
wire:model="form.service_id"
:label="__('catalog.work_types.service')"
:aria-label="__('catalog.work_types.service')"
:hint="__('catalog.work_types.service_description')"
>
<option value="">{{ __('catalog.work_types.no_service') }}</option>
@foreach ($this->services as $service)
<option wire:key="service-option-{{ $service->id }}" value="{{ $service->id }}">
{{ $service->name }}@unless ($service->isActive) ({{ __('catalog.inactive') }})@endunless
</option>
@endforeach
</x-select.native>
@endif
<div class="flex flex-wrap gap-3">
<x-button submit :text="__('catalog.save')" />
<x-button :href="route('catalog.index')" wire:navigate subtle color="black" :text="__('catalog.cancel')" />
</div>
</form>
</div>
@@ -0,0 +1,29 @@
<?php
use Illuminate\Support\Facades\Route;
Route::middleware(['web', 'auth', 'can:catalog.manage'])
->prefix('catalog')
->name('catalog.')
->group(function () {
Route::livewire('/', 'catalog::index')->name('index');
Route::livewire('/service-units/create', 'catalog::service-unit-form')->name('service-units.create');
Route::livewire('/service-units/{serviceUnit}/edit', 'catalog::service-unit-form')
->whereNumber('serviceUnit')
->name('service-units.edit');
Route::livewire('/service-units/{serviceUnit}/services/create', 'catalog::service-form')
->whereNumber('serviceUnit')
->name('services.create');
Route::livewire('/services/{service}/edit', 'catalog::service-form')
->whereNumber('service')
->name('services.edit');
Route::livewire('/service-units/{serviceUnit}/work-types/create', 'catalog::work-type-form')
->whereNumber('serviceUnit')
->name('work-types.create');
Route::livewire('/work-types/{workType}/edit', 'catalog::work-type-form')
->whereNumber('workType')
->name('work-types.edit');
});