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:
@@ -0,0 +1,85 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Fakes;
|
||||
|
||||
use App\Modules\Catalog\Domain\Contracts\ServiceUnitRepository;
|
||||
use App\Modules\Catalog\Domain\Entities\CatalogItem;
|
||||
use App\Modules\Catalog\Domain\Entities\ServiceUnit;
|
||||
use App\Modules\Catalog\Domain\Enums\MoveDirection;
|
||||
|
||||
class InMemoryServiceUnitRepository implements ServiceUnitRepository
|
||||
{
|
||||
/** @var array<int, ServiceUnit> */
|
||||
public array $items = [];
|
||||
|
||||
private int $nextId = 1;
|
||||
|
||||
public function findById(int $id): ?ServiceUnit
|
||||
{
|
||||
return isset($this->items[$id]) ? clone $this->items[$id] : null;
|
||||
}
|
||||
|
||||
public function save(ServiceUnit $serviceUnit): ServiceUnit
|
||||
{
|
||||
$id = $serviceUnit->id() ?? $this->nextId++;
|
||||
$this->items[$id] = ServiceUnit::reconstitute($id, $serviceUnit->name(), $serviceUnit->isShared(), $serviceUnit->isActive(), $serviceUnit->sortOrder());
|
||||
|
||||
return clone $this->items[$id];
|
||||
}
|
||||
|
||||
public function isNameTaken(string $name, ?int $exceptId = null): bool
|
||||
{
|
||||
return self::nameTakenIn($this->items, $name, $exceptId);
|
||||
}
|
||||
|
||||
public function nextSortOrder(): int
|
||||
{
|
||||
return self::nextIn($this->items);
|
||||
}
|
||||
|
||||
public function neighbour(ServiceUnit $serviceUnit, MoveDirection $direction): ?ServiceUnit
|
||||
{
|
||||
return self::neighbourIn($this->items, $serviceUnit, $direction);
|
||||
}
|
||||
|
||||
/**
|
||||
* @template T of CatalogItem
|
||||
*
|
||||
* @param array<int, T> $siblings
|
||||
* @return T|null
|
||||
*/
|
||||
private static function neighbourIn(array $siblings, CatalogItem $item, MoveDirection $direction): ?CatalogItem
|
||||
{
|
||||
$candidates = array_filter($siblings, fn (CatalogItem $sibling): bool => $direction === MoveDirection::Up
|
||||
? $sibling->sortOrder() < $item->sortOrder()
|
||||
: $sibling->sortOrder() > $item->sortOrder());
|
||||
|
||||
usort($candidates, fn (CatalogItem $a, CatalogItem $b): int => $direction === MoveDirection::Up
|
||||
? $b->sortOrder() <=> $a->sortOrder()
|
||||
: $a->sortOrder() <=> $b->sortOrder());
|
||||
|
||||
return isset($candidates[0]) ? clone $candidates[0] : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<int, CatalogItem> $siblings
|
||||
*/
|
||||
private static function nameTakenIn(array $siblings, string $name, ?int $exceptId): bool
|
||||
{
|
||||
foreach ($siblings as $id => $sibling) {
|
||||
if ($id !== $exceptId && mb_strtolower($sibling->name()->value) === mb_strtolower($name)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<int, CatalogItem> $siblings
|
||||
*/
|
||||
private static function nextIn(array $siblings): int
|
||||
{
|
||||
return array_reduce($siblings, fn (int $max, CatalogItem $item): int => max($max, $item->sortOrder()), 0) + 1;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user