Add checklists to tasks on the job page
Tasks get an ordered checklist (spec 8.2, phase 3): items are checked done with who and when recorded, also by subcontractors attached to the job; others add, rename, reorder and remove items. Items go with their task. The spec gets a phase 3 chapter (18.1), and open questions move to chapter 19. Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
@@ -0,0 +1,64 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Fakes;
|
||||
|
||||
use App\Modules\Jobs\Domain\Contracts\ChecklistItemRepository;
|
||||
use App\Modules\Jobs\Domain\Entities\ChecklistItem;
|
||||
use App\Modules\Jobs\Domain\Enums\MoveDirection;
|
||||
|
||||
class InMemoryChecklistItemRepository implements ChecklistItemRepository
|
||||
{
|
||||
/** @var array<int, ChecklistItem> */
|
||||
public array $items = [];
|
||||
|
||||
private int $nextId = 1;
|
||||
|
||||
public function findForTask(int $taskId, int $itemId): ?ChecklistItem
|
||||
{
|
||||
$item = $this->items[$itemId] ?? null;
|
||||
|
||||
return $item !== null && $item->taskId() === $taskId ? clone $item : null;
|
||||
}
|
||||
|
||||
public function neighbour(ChecklistItem $item, MoveDirection $direction): ?ChecklistItem
|
||||
{
|
||||
$siblings = array_values(array_filter($this->items, fn (ChecklistItem $other): bool => $other->taskId() === $item->taskId()));
|
||||
usort($siblings, fn (ChecklistItem $a, ChecklistItem $b): int => [$a->sortOrder(), $a->id()] <=> [$b->sortOrder(), $b->id()]);
|
||||
$ids = array_map(fn (ChecklistItem $other): ?int => $other->id(), $siblings);
|
||||
$index = array_search($item->id(), $ids, true);
|
||||
$neighbour = $siblings[$direction === MoveDirection::Up ? $index - 1 : $index + 1] ?? null;
|
||||
|
||||
return $neighbour === null ? null : clone $neighbour;
|
||||
}
|
||||
|
||||
public function nextSortOrder(int $taskId): int
|
||||
{
|
||||
$orders = array_map(fn (ChecklistItem $item): int => $item->sortOrder(), array_filter($this->items, fn (ChecklistItem $item): bool => $item->taskId() === $taskId));
|
||||
|
||||
return ($orders === [] ? 0 : max($orders)) + 1;
|
||||
}
|
||||
|
||||
public function save(ChecklistItem $item): ChecklistItem
|
||||
{
|
||||
$id = $item->id() ?? $this->nextId++;
|
||||
$this->items[$id] = ChecklistItem::reconstitute($id, $item->taskId(), $item->text(), $item->sortOrder(), $item->doneAt(), $item->doneBy());
|
||||
|
||||
return clone $this->items[$id];
|
||||
}
|
||||
|
||||
public function delete(ChecklistItem $item): void
|
||||
{
|
||||
unset($this->items[$item->id()]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return list<string>
|
||||
*/
|
||||
public function textsOf(int $taskId): array
|
||||
{
|
||||
$items = array_values(array_filter($this->items, fn (ChecklistItem $item): bool => $item->taskId() === $taskId));
|
||||
usort($items, fn (ChecklistItem $a, ChecklistItem $b): int => [$a->sortOrder(), $a->id()] <=> [$b->sortOrder(), $b->id()]);
|
||||
|
||||
return array_map(fn (ChecklistItem $item): string => $item->text()->value, $items);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user