Add tasks to customer jobs

Phase 1, item 7, in the Jobs module (tasks are part of a job, and my jobs
needs their assignees):
- Tasks with name, work type, assignees, status (waiting, in progress,
  in review, done), due date, estimated hours and description.
- Tasks can be added once the job is a quote request or later, not to
  an archived job. The work type must belong to the job's service units
  or the shared ones and be in use; assignees must be active. What a
  task already has stays allowed after being taken out of use.
- Job page: a Tehtävät section with a status select per task (announced,
  focus kept), edit links and delete with a confirmation dialog. Task
  form with work types grouped by unit and service.
- Access: adding, changing and removing for everyone but subcontractors;
  subcontractors may change task statuses on jobs they are attached to.
- Task assignees now count in my jobs (list, Kanban, job access).
- Spec and CLAUDE.md updated.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
2026-11-23 19:26:31 +02:00
co-authored by Claude Opus 5.5
parent 7f3b6dff78
commit 02a62eb16e
37 changed files with 1895 additions and 8 deletions
+46
View File
@@ -2,8 +2,10 @@
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\CustomerJobModel;
use App\Modules\Jobs\Infrastructure\Persistence\Eloquent\TaskModel;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\DB;
@@ -145,3 +147,47 @@ it('moves a card by dragging it to another column', function () {
expect($this->job->fresh()->status->value)->toBe('order');
});
it('has no accessibility issues on a job with tasks and on the task form', function (string $page, string $device, string $colorScheme) {
$this->job->update(['status' => 'order']);
$workType = WorkTypeModel::query()->where('name', 'videokuvaus')->sole();
$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]);
$url = match ($page) {
'job page' => route('jobs.show', $this->job->id),
'new task' => route('jobs.tasks.create', $this->job->id),
'edit task' => route('jobs.tasks.edit', [$this->job->id, $task->id]),
};
$visit = visit($url);
$visit = $device === 'mobile' ? $visit->on()->mobile() : $visit->on()->desktop();
$visit = $colorScheme === 'dark' ? $visit->inDarkMode() : $visit->inLightMode();
$visit->assertNoJavaScriptErrors()
->assertNoAccessibilityIssues(level: 2);
})->with(['job page', 'new task', 'edit task'])->with([
'desktop, light' => ['desktop', 'light'],
'desktop, dark' => ['desktop', 'dark'],
'mobile, light' => ['mobile', 'light'],
'mobile, dark' => ['mobile', 'dark'],
]);
it('changes a task status and removes a task 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ä']);
visit(route('jobs.show', $this->job->id))
->select('#task-status-'.$task->id, 'in_progress')
->assertSee('Tehtävän Kuvauspäivä tila on nyt Työn alla.')
->assertScript('document.activeElement.id', 'task-status-'.$task->id)
->keys('[aria-label="Poista tehtävä Kuvauspäivä"]', 'Enter')
->assertScript('document.getElementById("remove-task-dialog").open', true)
->assertScript('document.getElementById("remove-task-dialog").contains(document.activeElement)', true)
->click('#remove-task-dialog [wire\\:click="removeTask"]')
->assertSee('Tehtävä Kuvauspäivä poistettu.')
->assertScript('document.activeElement.id', 'tasks-heading');
});
+18 -1
View File
@@ -4,6 +4,7 @@ namespace Tests\Fakes;
use App\Modules\Catalog\Application\Contracts\CatalogQueries;
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;
@@ -15,6 +16,14 @@ class FakeCatalogQueries implements CatalogQueries
/** @var array<int, ServiceUnitSummary> */
public array $serviceUnits = [];
/** @var array<int, list<WorkTypeSummary>> active work types directly under each unit */
public array $workTypes = [];
public function addWorkType(int $id, int $serviceUnitId, string $name): void
{
$this->workTypes[$serviceUnitId][] = new WorkTypeSummary($id, $serviceUnitId, null, $name, true);
}
public function addServiceUnit(int $id, string $name, bool $active = true, bool $shared = false): void
{
$this->serviceUnits[$id] = new ServiceUnitSummary($id, $name, $shared, $active);
@@ -32,7 +41,15 @@ class FakeCatalogQueries implements CatalogQueries
public function activeWorkTypesFor(array $serviceUnitIds): array
{
return [];
$nodes = [];
foreach ($this->serviceUnits as $id => $unit) {
if ($unit->isActive && (in_array($id, $serviceUnitIds, true) || $unit->isShared)) {
$nodes[] = new ServiceUnitNode($unit, [], $this->workTypes[$id] ?? []);
}
}
return $nodes;
}
public function findServiceUnit(int $id): ?ServiceUnitSummary
+45
View File
@@ -0,0 +1,45 @@
<?php
namespace Tests\Fakes;
use App\Modules\Jobs\Domain\Contracts\TaskRepository;
use App\Modules\Jobs\Domain\Entities\Task;
class InMemoryTaskRepository implements TaskRepository
{
/** @var array<int, Task> */
public array $tasks = [];
private int $nextId = 1;
public function findForJob(int $jobId, int $taskId): ?Task
{
$task = $this->tasks[$taskId] ?? null;
return $task !== null && $task->jobId() === $jobId ? clone $task : null;
}
public function save(Task $task): Task
{
$id = $task->id() ?? $this->nextId++;
$this->tasks[$id] = Task::reconstitute(
$id,
$task->jobId(),
$task->workTypeId(),
$task->title(),
$task->description(),
$task->status(),
$task->dueOn(),
$task->estimatedHours(),
$task->assigneeUserIds(),
$task->createdBy(),
);
return clone $this->tasks[$id];
}
public function delete(Task $task): void
{
unset($this->tasks[$task->id()]);
}
}
+119
View File
@@ -0,0 +1,119 @@
<?php
use App\Models\User;
use App\Modules\Catalog\Infrastructure\Persistence\Eloquent\ServiceUnitModel;
use App\Modules\Catalog\Infrastructure\Persistence\Eloquent\WorkTypeModel;
use App\Modules\Jobs\Infrastructure\Persistence\Eloquent\CustomerJobModel;
use App\Modules\Jobs\Infrastructure\Persistence\Eloquent\TaskModel;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\DB;
use Livewire\Livewire;
uses(RefreshDatabase::class);
beforeEach(function () {
$this->employee = User::factory()->create(['name' => 'Eero Työntekijä']);
$this->actingAs($this->employee);
$this->marketing = ServiceUnitModel::query()->where('name', 'Markkinointi')->sole();
$this->job = CustomerJobModel::factory()->create(['name' => 'Kampanja', 'status' => 'order', 'responsible_user_id' => $this->employee->id]);
DB::table('customer_job_service_unit')->where('customer_job_id', $this->job->id)->update(['service_unit_id' => $this->marketing->id]);
$this->graphic = WorkTypeModel::query()->where('name', 'graafinen työ')->sole();
$this->planning = WorkTypeModel::query()->where('name', 'suunnittelu')->sole();
});
it('adds a task with work types of the job and the shared ones on offer', function () {
$this->get(route('jobs.tasks.create', $this->job->id))
->assertOk()
->assertSee('graafinen työ')
->assertSee('suunnittelu')
->assertDontSee('painotyö');
Livewire::test('jobs::task-form', ['job' => $this->job->id])
->assertSet('form.assignee_user_ids', [(string) $this->employee->id])
->set('form.title', 'Mainoskuvat')
->set('form.work_type_id', (string) $this->graphic->id)
->set('form.due_on', '2026-05-20')
->call('save')
->assertHasNoErrors()
->assertRedirect(route('jobs.show', $this->job->id).'#tasks');
$task = TaskModel::query()->sole();
expect($task->work_type_id)->toBe($this->graphic->id)
->and(DB::table('task_assignees')->where('task_id', $task->id)->pluck('user_id')->all())->toBe([$this->employee->id]);
$this->get(route('jobs.show', $this->job->id))
->assertSee('Mainoskuvat')
->assertSee('graafinen työ')
->assertSee('Eero Työntekijä');
});
it('shows Finnish errors on the task form', function () {
Livewire::test('jobs::task-form', ['job' => $this->job->id])
->call('save')
->assertHasErrors(['form.title', 'form.work_type_id'])
->assertSee('Anna tehtävän nimi.')
->assertSee('Valitse työtehtävä.');
});
it('does not add tasks before the job is a quote request', function () {
$this->job->update(['status' => 'tentative']);
$this->get(route('jobs.tasks.create', $this->job->id))->assertNotFound();
$this->get(route('jobs.show', $this->job->id))
->assertSee('Tehtäviä voi lisätä, kun työ on tarjouspyyntö-tilassa')
->assertDontSee('Lisää tehtävä');
});
it('edits, changes the status of and removes a task on the job page', function () {
$task = TaskModel::factory()->create(['customer_job_id' => $this->job->id, 'work_type_id' => $this->graphic->id, 'title' => 'Mainoskuvat']);
Livewire::test('jobs::task-form', ['job' => $this->job->id, 'task' => $task->id])
->assertSet('form.title', 'Mainoskuvat')
->set('form.work_type_id', (string) $this->planning->id)
->call('save')
->assertHasNoErrors();
Livewire::test('jobs::show', ['job' => $this->job->id])
->call('changeTaskStatus', $task->id, 'in_review')
->assertSee('Tehtävän Mainoskuvat tila on nyt Tarkastettavana.')
->call('confirmTaskRemoval', $task->id)
->assertSee('Poistetaanko tehtävä Mainoskuvat?')
->call('removeTask')
->assertSee('Tehtävä Mainoskuvat poistettu.');
expect(TaskModel::query()->count())->toBe(0);
});
it('counts task assignees as members in my jobs', function () {
$colleague = User::factory()->create();
TaskModel::factory()->assignedTo([$colleague->id])->create(['customer_job_id' => $this->job->id, 'work_type_id' => $this->graphic->id]);
$this->actingAs($colleague);
Livewire::test('jobs::index')->assertSee('Kampanja');
});
it('lets a subcontractor change task statuses on their jobs, but not add or change tasks', function () {
$subcontractor = User::factory()->subcontractor()->create();
$task = TaskModel::factory()->assignedTo([$subcontractor->id])->create(['customer_job_id' => $this->job->id, 'work_type_id' => $this->graphic->id]);
$this->actingAs($subcontractor);
$this->get(route('jobs.show', $this->job->id))->assertOk()->assertDontSee('Lisää tehtävä');
$this->get(route('jobs.tasks.create', $this->job->id))->assertForbidden();
$this->get(route('jobs.tasks.edit', [$this->job->id, $task->id]))->assertForbidden();
Livewire::test('jobs::show', ['job' => $this->job->id])
->call('changeTaskStatus', $task->id, 'done')
->assertHasNoErrors()
->call('confirmTaskRemoval', $task->id)
->assertForbidden();
expect($task->fresh()->status->value)->toBe('done');
});
it('returns 404 for a task of another job', function () {
$other = TaskModel::factory()->create();
$this->get(route('jobs.tasks.edit', [$this->job->id, $other->id]))->assertNotFound();
});
@@ -0,0 +1,125 @@
<?php
use App\Modules\Jobs\Application\DTOs\CustomerJobData;
use App\Modules\Jobs\Application\DTOs\TaskData;
use App\Modules\Jobs\Application\Services\JobDetailsFactory;
use App\Modules\Jobs\Application\Services\TaskReferenceChecker;
use App\Modules\Jobs\Application\UseCases\AddTask;
use App\Modules\Jobs\Application\UseCases\ChangeJobStatus;
use App\Modules\Jobs\Application\UseCases\ChangeTaskStatus;
use App\Modules\Jobs\Application\UseCases\CreateCustomerJob;
use App\Modules\Jobs\Application\UseCases\RemoveTask;
use App\Modules\Jobs\Application\UseCases\UpdateTask;
use App\Modules\Jobs\Domain\Enums\JobStatus;
use App\Modules\Jobs\Domain\Enums\TaskStatus;
use App\Modules\Jobs\Domain\Exceptions\InvalidTaskReference;
use App\Modules\Jobs\Domain\Exceptions\InvalidTaskTitle;
use App\Modules\Jobs\Domain\Exceptions\TaskNotFound;
use App\Modules\Jobs\Domain\Exceptions\TasksNotAllowedYet;
use App\Modules\Jobs\Domain\ValueObjects\TaskTitle;
use Tests\Fakes\FakeCatalogQueries;
use Tests\Fakes\FakeClock;
use Tests\Fakes\FakeCustomerQueries;
use Tests\Fakes\FakeTransactionManager;
use Tests\Fakes\FakeUserAccountQueries;
use Tests\Fakes\InMemoryCustomerJobRepository;
use Tests\Fakes\InMemoryTaskRepository;
beforeEach(function () {
$this->jobs = new InMemoryCustomerJobRepository;
$this->tasks = new InMemoryTaskRepository;
$customers = new FakeCustomerQueries;
$this->catalog = new FakeCatalogQueries;
$this->users = new FakeUserAccountQueries;
$transactions = new FakeTransactionManager;
$customers->customers = [1 => 'Mainos Oy'];
$this->catalog->addServiceUnit(1, 'Markkinointi');
$this->catalog->addServiceUnit(2, 'Painotuotanto');
$this->catalog->addServiceUnit(9, 'Kaikille yhteiset', shared: true);
$this->catalog->addWorkType(11, 1, 'graafinen työ');
$this->catalog->addWorkType(21, 2, 'painotyö');
$this->catalog->addWorkType(91, 9, 'suunnittelu');
$this->users->addUser(10, 'Eero');
$this->users->addUser(12, 'Pois', active: false);
$createJob = new CreateCustomerJob($this->jobs, $this->jobs, new JobDetailsFactory($customers, $this->catalog, $this->users), $transactions, new FakeClock(new DateTimeImmutable('2026-05-01')));
$this->jobId = $createJob->execute(new CustomerJobData('Kampanja', 1, [1], 10, 'one_off', 'quote_request', 'normal'), 10);
$this->tentativeJobId = $createJob->execute(new CustomerJobData('Alustava', 1, [1], 10, 'one_off', 'tentative', 'normal'), 10);
$checker = new TaskReferenceChecker($this->catalog, $this->users);
$this->addTask = new AddTask($this->jobs, $this->tasks, $checker, $transactions);
$this->updateTask = new UpdateTask($this->jobs, $this->tasks, $checker, $transactions);
$this->changeTaskStatus = new ChangeTaskStatus($this->tasks, $transactions);
$this->removeTask = new RemoveTask($this->tasks, $transactions);
$this->changeJobStatus = new ChangeJobStatus($this->jobs, $transactions);
});
it('allows tasks from a quote request on, not before and not when archived', function () {
expect(JobStatus::Tentative->allowsTasks())->toBeFalse()
->and(JobStatus::Prospect->allowsTasks())->toBeFalse()
->and(JobStatus::Archived->allowsTasks())->toBeFalse()
->and(JobStatus::QuoteRequest->allowsTasks())->toBeTrue()
->and(JobStatus::Billed->allowsTasks())->toBeTrue()
->and(fn () => new TaskTitle(' '))->toThrow(InvalidTaskTitle::class);
expect(fn () => $this->addTask->execute($this->tentativeJobId, new TaskData('Luonnos', 11, 'waiting'), 10))
->toThrow(TasksNotAllowedYet::class);
});
it('adds a task with a work type of the job or a shared one', function () {
$taskId = $this->addTask->execute($this->jobId, new TaskData('Mainoskuvat', 11, 'waiting', dueOn: '2026-05-20', estimatedHours: '3,5', assigneeUserIds: [10]), 10);
$this->addTask->execute($this->jobId, new TaskData('Suunnittelupalaveri', 91, 'waiting'), 10);
$task = $this->tasks->findForJob($this->jobId, $taskId);
expect($task?->dueOn()?->format('Y-m-d'))->toBe('2026-05-20')
->and($task?->estimatedHours()?->format())->toBe('3.5')
->and($task?->assigneeUserIds())->toBe([10])
->and($this->tasks->tasks)->toHaveCount(2);
});
it('rejects a work type of another unit and an inactive assignee', function (int $workTypeId, array $assignees, string $field) {
try {
$this->addTask->execute($this->jobId, new TaskData('Työ', $workTypeId, 'waiting', assigneeUserIds: $assignees), 10);
$this->fail('No exception');
} catch (InvalidTaskReference $exception) {
expect($exception->field)->toBe($field);
}
})->with([
'work type of another unit' => [21, [], 'work_type'],
'inactive assignee' => [11, [12], 'assignees'],
]);
it('changes, moves through statuses and removes a task', function () {
$taskId = $this->addTask->execute($this->jobId, new TaskData('Mainoskuvat', 11, 'waiting'), 10);
$this->updateTask->execute($this->jobId, $taskId, new TaskData('Mainoskuvat ja banneri', 11, 'in_progress'));
$this->changeTaskStatus->execute($this->jobId, $taskId, TaskStatus::Done);
$task = $this->tasks->findForJob($this->jobId, $taskId);
expect($task?->title()->value)->toBe('Mainoskuvat ja banneri')
->and($task?->status())->toBe(TaskStatus::Done);
$this->removeTask->execute($this->jobId, $taskId);
expect($this->tasks->tasks)->toBeEmpty();
});
it('can still change a task after the job moved back to an earlier status', function () {
$taskId = $this->addTask->execute($this->jobId, new TaskData('Mainoskuvat', 11, 'waiting'), 10);
$this->changeJobStatus->execute($this->jobId, JobStatus::Tentative, 10);
$this->updateTask->execute($this->jobId, $taskId, new TaskData('Mainoskuvat', 11, 'in_review'));
expect($this->tasks->findForJob($this->jobId, $taskId)?->status())->toBe(TaskStatus::InReview);
});
it('does not touch a task of another job', function (string $action) {
$taskId = $this->addTask->execute($this->jobId, new TaskData('Mainoskuvat', 11, 'waiting'), 10);
match ($action) {
'update' => $this->updateTask->execute($this->tentativeJobId, $taskId, new TaskData('X', 11, 'done')),
'status' => $this->changeTaskStatus->execute($this->tentativeJobId, $taskId, TaskStatus::Done),
'remove' => $this->removeTask->execute($this->tentativeJobId, $taskId),
};
})->throws(TaskNotFound::class)->with(['update', 'status', 'remove']);