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:
2026-11-24 16:38:08 +02:00
co-authored by Claude Opus 5.5
parent e521df647c
commit dfe4941f02
28 changed files with 1376 additions and 3 deletions
+37
View File
@@ -4,6 +4,7 @@ use App\Models\User;
use App\Modules\Catalog\Infrastructure\Persistence\Eloquent\ServiceUnitModel;
use App\Modules\Catalog\Infrastructure\Persistence\Eloquent\WorkTypeModel;
use App\Modules\Customers\Infrastructure\Persistence\Eloquent\CustomerModel;
use App\Modules\Jobs\Infrastructure\Persistence\Eloquent\ChecklistItemModel;
use App\Modules\Jobs\Infrastructure\Persistence\Eloquent\CustomerJobModel;
use App\Modules\Jobs\Infrastructure\Persistence\Eloquent\TaskModel;
use Illuminate\Foundation\Testing\RefreshDatabase;
@@ -155,6 +156,8 @@ it('has no accessibility issues on a job with tasks and on the task form', funct
$task = TaskModel::factory()
->assignedTo([$this->user->id])
->create(['customer_job_id' => $this->job->id, 'work_type_id' => $workType->id, 'title' => 'Kuvauspäivä', 'due_on' => now()->subDay()->toDateString(), 'estimated_hours' => 6]);
ChecklistItemModel::query()->create(['task_id' => $task->id, 'text' => 'Valot', 'sort_order' => 1, 'done_at' => now(), 'done_by' => $this->user->id]);
ChecklistItemModel::query()->create(['task_id' => $task->id, 'text' => 'Mikit', 'sort_order' => 2]);
$url = match ($page) {
'job page' => route('jobs.show', $this->job->id),
@@ -192,3 +195,37 @@ it('changes a task status and removes a task with the keyboard', function () {
->assertSee('Tehtävä Kuvauspäivä poistettu.')
->assertScript('document.activeElement.id', 'tasks-heading');
});
it('adds, checks, renames, moves and removes checklist items with the keyboard', function () {
$this->job->update(['status' => 'order']);
$workType = WorkTypeModel::query()->where('name', 'videokuvaus')->sole();
$task = TaskModel::factory()
->create(['customer_job_id' => $this->job->id, 'work_type_id' => $workType->id, 'title' => 'Kuvauspäivä']);
$page = visit(route('jobs.show', $this->job->id))
->type('#checklist-new-'.$task->id, 'Valot')
->keys('#checklist-new-'.$task->id, 'Enter')
->assertSee('Kohta Valot lisätty.')
->type('#checklist-new-'.$task->id, 'Mikit')
->keys('#checklist-new-'.$task->id, 'Enter')
->assertSee('Kohta Mikit lisätty.');
[$lights, $mics] = ChecklistItemModel::query()->orderBy('id')->pluck('id')->all();
$page->keys('#checklist-item-'.$lights, 'Space')
->assertSee('Kohta Valot merkitty tehdyksi.')
->assertSee('(1/2 tehty)')
->keys('#checklist-rename-'.$lights, 'Enter')
->assertScript('document.activeElement.id', 'checklist-edit-'.$lights)
->type('#checklist-edit-'.$lights, 'Valot ja jalustat')
->keys('#checklist-edit-'.$lights, 'Enter')
->assertSee('Kohta Valot ja jalustat tallennettu.')
->assertScript('document.activeElement.id', 'checklist-rename-'.$lights)
->keys('#checklist-move-up-'.$mics, 'Enter')
->assertSee('Kohta Mikit siirretty ylöspäin.')
->assertScript('document.activeElement.id', 'checklist-move-up-'.$mics)
->keys('[aria-label="Poista kohta Mikit"]', 'Enter')
->assertSee('Kohta Mikit poistettu.')
->assertScript('document.activeElement.id', 'checklist-new-'.$task->id)
->assertNoAccessibilityIssues(level: 2);
});
@@ -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);
}
}
@@ -0,0 +1,127 @@
<?php
use App\Models\User;
use App\Modules\Catalog\Infrastructure\Persistence\Eloquent\WorkTypeModel;
use App\Modules\Jobs\Infrastructure\Persistence\Eloquent\ChecklistItemModel;
use App\Modules\Jobs\Infrastructure\Persistence\Eloquent\CustomerJobModel;
use App\Modules\Jobs\Infrastructure\Persistence\Eloquent\TaskModel;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Livewire\Livewire;
uses(RefreshDatabase::class);
beforeEach(function () {
$this->employee = User::factory()->create();
$this->actingAs($this->employee);
$this->job = CustomerJobModel::factory()->create(['status' => 'order', 'responsible_user_id' => $this->employee->id]);
$workType = WorkTypeModel::query()->where('name', 'graafinen työ')->sole();
$this->task = TaskModel::factory()->create(['customer_job_id' => $this->job->id, 'work_type_id' => $workType->id, 'title' => 'Mainoskuvat']);
$this->params = ['jobId' => $this->job->id, 'taskId' => $this->task->id, 'taskTitle' => 'Mainoskuvat'];
});
/**
* @return list<string>
*/
function checklistTexts(int $taskId): array
{
return ChecklistItemModel::query()->where('task_id', $taskId)->orderBy('sort_order')->orderBy('id')->pluck('text')->all();
}
it('shows the checklist with progress on the job page', function () {
ChecklistItemModel::query()->create(['task_id' => $this->task->id, 'text' => 'Valot', 'sort_order' => 1, 'done_at' => now(), 'done_by' => $this->employee->id]);
ChecklistItemModel::query()->create(['task_id' => $this->task->id, 'text' => 'Mikit', 'sort_order' => 2]);
$this->get(route('jobs.show', $this->job->id))
->assertOk()
->assertSee('Tarkistuslista')
->assertSee('(1/2 tehty)')
->assertSeeInOrder(['Valot', 'Mikit']);
});
it('adds, checks, renames, moves and removes checklist items', function () {
$component = Livewire::test('jobs::task-checklist', $this->params)
->set('newItem', ' Valot ')
->call('add')
->assertHasNoErrors()
->assertSet('newItem', '')
->assertSee('Kohta Valot lisätty.')
->set('newItem', 'Mikit')
->call('add');
[$lights, $mics] = ChecklistItemModel::query()->orderBy('id')->pluck('id')->all();
$component->call('check', $lights, true)
->assertSee('Kohta Valot merkitty tehdyksi.')
->assertSee('(1/2 tehty)');
expect(ChecklistItemModel::query()->find($lights)->done_by)->toBe($this->employee->id);
$component->call('startRename', $lights)
->assertSet('editingItemId', $lights)
->set('editingText', 'Valot ja jalustat')
->call('saveRename')
->assertSee('Kohta Valot ja jalustat tallennettu.')
->assertSet('editingItemId', null)
->call('move', $mics, 'up')
->assertSee('Kohta Mikit siirretty ylöspäin.')
->call('move', $mics, 'up')
->assertSee('Kohta Mikit on jo ensimmäisenä.');
expect(checklistTexts($this->task->id))->toBe(['Mikit', 'Valot ja jalustat']);
$component->call('remove', $mics)->assertSee('Kohta Mikit poistettu.');
expect(checklistTexts($this->task->id))->toBe(['Valot ja jalustat']);
});
it('shows Finnish errors for empty and too long items', function () {
Livewire::test('jobs::task-checklist', $this->params)
->call('add')
->assertHasErrors('newItem')
->assertSee('Anna kohdan teksti.')
->set('newItem', str_repeat('a', 256))
->call('add')
->assertSee('Kohdan teksti voi olla enintään 255 merkkiä.');
expect(ChecklistItemModel::query()->count())->toBe(0);
});
it('does not change items of another task through this one', function () {
$other = ChecklistItemModel::query()->create(['task_id' => TaskModel::factory()->create()->id, 'text' => 'Vieras', 'sort_order' => 1]);
Livewire::test('jobs::task-checklist', $this->params)
->call('remove', $other->id)
->assertSee('Kohtaa ei löytynyt. Päivitä sivu.');
expect($other->fresh())->not->toBeNull();
});
it('lets an attached subcontractor check items but not manage them', function () {
$item = ChecklistItemModel::query()->create(['task_id' => $this->task->id, 'text' => 'Valot', 'sort_order' => 1]);
$subcontractor = User::factory()->subcontractor()->create();
TaskModel::factory()->assignedTo([$subcontractor->id])->create(['customer_job_id' => $this->job->id, 'work_type_id' => $this->task->work_type_id]);
$this->actingAs($subcontractor);
Livewire::test('jobs::task-checklist', $this->params)
->assertDontSee('Lisää kohta')
->call('check', $item->id, true)
->assertSee('Kohta Valot merkitty tehdyksi.')
->call('remove', $item->id)
->assertForbidden();
expect($item->fresh()->done_by)->toBe($subcontractor->id);
});
it('forbids the checklist of a job the subcontractor is not attached to', function () {
$this->actingAs(User::factory()->subcontractor()->create());
Livewire::test('jobs::task-checklist', $this->params)->assertForbidden();
});
it('removes the checklist with its task', function () {
ChecklistItemModel::query()->create(['task_id' => $this->task->id, 'text' => 'Valot', 'sort_order' => 1]);
$this->task->delete();
expect(ChecklistItemModel::query()->count())->toBe(0);
});
@@ -0,0 +1,90 @@
<?php
use App\Modules\Jobs\Application\Services\ChecklistItemFinder;
use App\Modules\Jobs\Application\UseCases\AddChecklistItem;
use App\Modules\Jobs\Application\UseCases\CheckChecklistItem;
use App\Modules\Jobs\Application\UseCases\MoveChecklistItem;
use App\Modules\Jobs\Application\UseCases\RemoveChecklistItem;
use App\Modules\Jobs\Application\UseCases\RenameChecklistItem;
use App\Modules\Jobs\Domain\Entities\Task;
use App\Modules\Jobs\Domain\Enums\MoveDirection;
use App\Modules\Jobs\Domain\Enums\TaskStatus;
use App\Modules\Jobs\Domain\Exceptions\ChecklistItemNotFound;
use App\Modules\Jobs\Domain\Exceptions\InvalidChecklistItemText;
use App\Modules\Jobs\Domain\Exceptions\TaskNotFound;
use App\Modules\Jobs\Domain\ValueObjects\ChecklistItemText;
use App\Modules\Jobs\Domain\ValueObjects\TaskTitle;
use Tests\Fakes\FakeClock;
use Tests\Fakes\FakeTransactionManager;
use Tests\Fakes\InMemoryChecklistItemRepository;
use Tests\Fakes\InMemoryTaskRepository;
beforeEach(function () {
$tasks = new InMemoryTaskRepository;
$this->items = new InMemoryChecklistItemRepository;
$transactions = new FakeTransactionManager;
$finder = new ChecklistItemFinder($tasks, $this->items);
$this->jobId = 5;
$this->taskId = (int) $tasks->save(Task::reconstitute(1, $this->jobId, 11, new TaskTitle('Kuvaus'), null, TaskStatus::Waiting, null, null, [], 10))->id();
$this->add = new AddChecklistItem($finder, $this->items, $transactions);
$this->rename = new RenameChecklistItem($finder, $this->items, $transactions);
$this->check = new CheckChecklistItem($finder, $this->items, $transactions, new FakeClock(new DateTimeImmutable('2026-11-24')));
$this->move = new MoveChecklistItem($finder, $this->items, $transactions);
$this->remove = new RemoveChecklistItem($finder, $this->items, $transactions);
});
it('normalises checklist item text and rejects empty and too long text', function () {
expect((new ChecklistItemText(" Tarkista valot \n"))->value)->toBe('Tarkista valot')
->and(fn () => new ChecklistItemText(' '))->toThrow(InvalidChecklistItemText::class)
->and(fn () => new ChecklistItemText(str_repeat('a', 256)))->toThrow(InvalidChecklistItemText::class);
});
it('adds items to the end of the task checklist', function () {
$this->add->execute($this->jobId, $this->taskId, 'Valot');
$this->add->execute($this->jobId, $this->taskId, 'Mikit');
expect($this->items->textsOf($this->taskId))->toBe(['Valot', 'Mikit']);
});
it('does not touch the checklist of a task through another job', function () {
$itemId = $this->add->execute($this->jobId, $this->taskId, 'Valot');
expect(fn () => $this->add->execute(99, $this->taskId, 'Mikit'))->toThrow(TaskNotFound::class)
->and(fn () => $this->remove->execute(99, $this->taskId, $itemId))->toThrow(TaskNotFound::class)
->and(fn () => $this->remove->execute($this->jobId, $this->taskId, 404))->toThrow(ChecklistItemNotFound::class);
});
it('checks an item with who and when, keeps them on a second check and clears them on uncheck', function () {
$itemId = $this->add->execute($this->jobId, $this->taskId, 'Valot');
$this->check->execute($this->jobId, $this->taskId, $itemId, true, 10);
$this->check->execute($this->jobId, $this->taskId, $itemId, true, 12);
$item = $this->items->items[$itemId];
expect($item->isDone())->toBeTrue()
->and($item->doneBy())->toBe(10)
->and($item->doneAt()?->format('Y-m-d H:i'))->toBe('2026-11-24 12:00');
$this->check->execute($this->jobId, $this->taskId, $itemId, false, 10);
expect($this->items->items[$itemId]->isDone())->toBeFalse()
->and($this->items->items[$itemId]->doneBy())->toBeNull();
});
it('renames, moves and removes items', function () {
$first = $this->add->execute($this->jobId, $this->taskId, 'Valot');
$second = $this->add->execute($this->jobId, $this->taskId, 'Mikit');
$this->rename->execute($this->jobId, $this->taskId, $first, 'Valot ja jalustat');
expect($this->move->execute($this->jobId, $this->taskId, $first, MoveDirection::Up))->toBeFalse()
->and($this->move->execute($this->jobId, $this->taskId, $second, MoveDirection::Down))->toBeFalse()
->and($this->move->execute($this->jobId, $this->taskId, $first, MoveDirection::Down))->toBeTrue()
->and($this->items->textsOf($this->taskId))->toBe(['Mikit', 'Valot ja jalustat']);
$this->remove->execute($this->jobId, $this->taskId, $second);
expect($this->items->textsOf($this->taskId))->toBe(['Valot ja jalustat']);
});