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
@@ -0,0 +1,109 @@
<?php
namespace App\Modules\Jobs\Presentation\Livewire\Forms;
use App\Modules\Jobs\Application\DTOs\NamedId;
use App\Modules\Jobs\Application\DTOs\TaskData;
use App\Modules\Jobs\Application\DTOs\TaskSummary;
use App\Modules\Jobs\Domain\Enums\TaskStatus;
use App\Modules\Jobs\Domain\Exceptions\InvalidEstimatedHours;
use App\Modules\Jobs\Domain\Exceptions\InvalidTaskReference;
use App\Modules\Jobs\Domain\Exceptions\InvalidTaskTitle;
use App\Modules\Jobs\Domain\Exceptions\TasksNotAllowedYet;
use App\Modules\Shared\Domain\Exceptions\DomainException;
use Illuminate\Validation\Rule;
use Livewire\Form;
/**
* Task form shared by the add and edit pages.
*/
class TaskForm extends Form
{
public string $title = '';
public string $work_type_id = '';
public string $status = TaskStatus::Waiting->value;
public string $due_on = '';
public string $estimated_hours = '';
public string $description = '';
/** @var list<string> */
public array $assignee_user_ids = [];
public function fillFromSummary(TaskSummary $task): void
{
$this->title = $task->title;
$this->work_type_id = (string) $task->workType->id;
$this->status = $task->status->value;
$this->due_on = $task->dueOn ?? '';
$this->estimated_hours = $task->estimatedHours ?? '';
$this->description = $task->description ?? '';
$this->assignee_user_ids = array_map(fn (NamedId $user): string => (string) $user->id, $task->assignees);
}
public function toData(): TaskData
{
$this->validate();
return new TaskData(
title: $this->title,
workTypeId: (int) $this->work_type_id,
status: $this->status,
description: trim($this->description) === '' ? null : $this->description,
dueOn: $this->due_on === '' ? null : $this->due_on,
estimatedHours: trim($this->estimated_hours) === '' ? null : $this->estimated_hours,
assigneeUserIds: array_map('intval', $this->assignee_user_ids),
);
}
public function showDomainError(DomainException $exception): void
{
[$field, $key] = match (true) {
$exception instanceof InvalidTaskTitle => ['title', 'jobs.tasks.errors.invalid_title'],
$exception instanceof InvalidEstimatedHours => ['estimated_hours', 'jobs.errors.invalid_hours'],
$exception instanceof TasksNotAllowedYet => ['title', 'jobs.tasks.not_allowed_yet'],
$exception instanceof InvalidTaskReference => [$exception->field === 'work_type' ? 'work_type_id' : 'assignee_user_ids', 'jobs.errors.invalid_reference'],
default => throw $exception,
};
$this->addError($field, __($key));
}
/**
* @return array<string, array<int, mixed>>
*/
protected function rules(): array
{
return [
'title' => ['required', 'string', 'max:255'],
'work_type_id' => ['required', 'integer'],
'status' => ['required', Rule::enum(TaskStatus::class)],
'due_on' => ['nullable', 'date_format:Y-m-d'],
'estimated_hours' => ['nullable', 'numeric', 'min:0', 'max:99999'],
'description' => ['nullable', 'string', 'max:10000'],
'assignee_user_ids' => ['array'],
'assignee_user_ids.*' => ['integer'],
];
}
/**
* @return array<string, string>
*/
protected function messages(): array
{
return [
'title.required' => __('jobs.tasks.errors.title_required'),
'title.max' => __('jobs.tasks.errors.invalid_title'),
'work_type_id.required' => __('jobs.tasks.errors.work_type_required'),
'due_on.date_format' => __('jobs.errors.invalid_date'),
'estimated_hours.numeric' => __('jobs.errors.invalid_hours'),
'estimated_hours.min' => __('jobs.errors.invalid_hours'),
'estimated_hours.max' => __('jobs.errors.invalid_hours'),
'description.max' => __('jobs.errors.too_long', ['max' => 10000]),
];
}
}
@@ -4,9 +4,16 @@ use App\Modules\Catalog\Application\Contracts\CatalogQueries;
use App\Modules\Customers\Application\Contracts\CustomerQueries;
use App\Modules\Jobs\Application\Contracts\CustomerJobQueries;
use App\Modules\Jobs\Application\DTOs\CustomerJobDetails;
use App\Modules\Jobs\Application\Contracts\TaskQueries;
use App\Modules\Jobs\Application\DTOs\JobChangeEntry;
use App\Modules\Jobs\Application\DTOs\TaskSummary;
use App\Modules\Jobs\Application\UseCases\ChangeTaskStatus;
use App\Modules\Jobs\Application\UseCases\RemoveTask;
use App\Modules\Jobs\Domain\Enums\TaskStatus;
use App\Modules\Jobs\Domain\Exceptions\TaskNotFound;
use App\Modules\Users\Application\Contracts\UserAccountQueries;
use Carbon\CarbonImmutable;
use Illuminate\Support\Facades\Gate;
use Livewire\Attributes\Computed;
use Livewire\Attributes\Locked;
use Livewire\Component;
@@ -19,6 +26,17 @@ new class extends Component
#[Locked]
public int $jobId;
/**
* The task the remove dialog asks about.
*/
#[Locked]
public ?int $removalTaskId = null;
#[Locked]
public string $removalTaskTitle = '';
public string $taskStatusMessage = '';
public function mount(CustomerJobQueries $jobQueries, int $job): void
{
$this->authorize('jobs.view');
@@ -45,6 +63,82 @@ new class extends Component
return app(CustomerJobQueries::class)->changes($this->jobId);
}
/**
* @return list<TaskSummary>
*/
#[Computed]
public function tasks(): array
{
return app(TaskQueries::class)->tasksOf($this->jobId);
}
public function changeTaskStatus(int $taskId, string $status, ChangeTaskStatus $changeStatus): void
{
$this->authorize('jobs.update-task-status', $this->jobId);
$newStatus = TaskStatus::tryFrom($status);
$task = app(TaskQueries::class)->findTask($this->jobId, $taskId);
if ($newStatus === null || $task === null) {
$this->taskStatusMessage = __('jobs.tasks.not_found');
return;
}
try {
$changeStatus->execute($this->jobId, $taskId, $newStatus);
} catch (TaskNotFound) {
$this->taskStatusMessage = __('jobs.tasks.not_found');
return;
}
unset($this->tasks);
$this->taskStatusMessage = __('jobs.tasks.status_changed', ['title' => $task->title, 'status' => __('jobs.tasks.statuses.'.$newStatus->value)]);
// The list is re-ordered (done tasks last): keep focus on the same select.
$this->js("document.getElementById('task-status-{$taskId}')?.focus()");
}
public function confirmTaskRemoval(int $taskId): void
{
$this->authorize('jobs.manage-tasks', $this->jobId);
$task = app(TaskQueries::class)->findTask($this->jobId, $taskId);
if ($task === null) {
$this->taskStatusMessage = __('jobs.tasks.not_found');
return;
}
$this->removalTaskId = $task->id;
$this->removalTaskTitle = $task->title;
$this->js("document.getElementById('remove-task-dialog').showModal()");
}
public function removeTask(RemoveTask $removeTask): void
{
$this->authorize('jobs.manage-tasks', $this->jobId);
if ($this->removalTaskId === null) {
return;
}
try {
$removeTask->execute($this->jobId, $this->removalTaskId);
$this->taskStatusMessage = __('jobs.tasks.removed', ['title' => $this->removalTaskTitle]);
} catch (TaskNotFound) {
$this->taskStatusMessage = __('jobs.tasks.not_found');
}
unset($this->tasks);
$this->reset('removalTaskId', 'removalTaskTitle');
$this->js("document.getElementById('remove-task-dialog').close(); document.getElementById('tasks-heading').focus()");
}
public function formatDate(?string $date): string
{
return $date === null ? __('jobs.not_set') : CarbonImmutable::parse($date)->format('j.n.Y');
@@ -188,6 +282,113 @@ new class extends Component
</dl>
</section>
<section id="tasks" aria-labelledby="tasks-heading" class="space-y-4">
<div class="flex flex-wrap items-center justify-between gap-4">
<h2 id="tasks-heading" tabindex="-1" class="text-xl font-semibold focus:outline-none">{{ __('jobs.tasks.title') }}</h2>
@can('jobs.manage-tasks', $job->id)
@if ($job->status->allowsTasks())
<x-button :href="route('jobs.tasks.create', $job->id)" wire:navigate icon="plus" :text="__('jobs.tasks.new')" />
@endif
@endcan
</div>
@can('jobs.manage-tasks', $job->id)
@unless ($job->status->allowsTasks())
<p class="text-sm text-zinc-600 dark:text-zinc-300">{{ __('jobs.tasks.not_allowed_yet') }}</p>
@endunless
@endcan
<p role="status" aria-live="polite" class="text-sm">{{ $taskStatusMessage }}</p>
@if ($this->tasks === [])
<p class="text-sm text-zinc-600 dark:text-zinc-300">{{ __('jobs.tasks.empty') }}</p>
@else
@php($canChangeStatus = Gate::allows('jobs.update-task-status', $job->id))
@php($canManageTasks = Gate::allows('jobs.manage-tasks', $job->id))
<ul class="space-y-3">
@foreach ($this->tasks as $task)
<li wire:key="task-{{ $task->id }}">
<article aria-labelledby="task-{{ $task->id }}-title" class="space-y-3 rounded-lg border border-zinc-200 p-4 text-sm dark:border-zinc-700">
<div class="flex flex-wrap items-start justify-between gap-3">
<h3 id="task-{{ $task->id }}-title" class="text-base font-medium break-words">{{ $task->title }}</h3>
<span>{{ __('jobs.tasks.statuses.'.$task->status->value) }}</span>
</div>
<dl class="grid gap-x-6 gap-y-1 sm:grid-cols-[max-content_1fr]">
<dt class="font-medium">{{ __('jobs.tasks.fields.work_type') }}</dt>
<dd>{{ $task->workType->name }}</dd>
<dt class="font-medium">{{ __('jobs.tasks.fields.assignees') }}</dt>
<dd>{{ $task->assignees === [] ? __('jobs.not_set') : collect($task->assignees)->pluck('name')->join(', ') }}</dd>
<dt class="font-medium">{{ __('jobs.tasks.fields.due_on') }}</dt>
<dd><x-jobs.due-date :date="$task->dueOn" :status="$task->status" /></dd>
<dt class="font-medium">{{ __('jobs.tasks.fields.estimated_hours') }}</dt>
<dd>{{ $task->estimatedHours === null ? __('jobs.not_set') : str_replace('.', ',', $task->estimatedHours).' h' }}</dd>
</dl>
@if ($task->description !== null)
<p class="whitespace-pre-line">{{ $task->description }}</p>
@endif
<div class="flex flex-wrap items-end gap-3">
@if ($canChangeStatus)
<div class="w-full sm:w-56">
{{-- TallStackUI's native select does not tie its label to the control, so aria-label names it. --}}
<x-select.native
id="task-status-{{ $task->id }}"
wire:change="changeTaskStatus({{ $task->id }}, $event.target.value)"
:label="__('jobs.tasks.fields.status')"
:aria-label="__('jobs.tasks.status_of', ['title' => $task->title])"
>
@foreach (TaskStatus::cases() as $status)
<option value="{{ $status->value }}" @selected($status === $task->status)>{{ __('jobs.tasks.statuses.'.$status->value) }}</option>
@endforeach
</x-select.native>
</div>
@endif
@if ($canManageTasks)
<x-button
:href="route('jobs.tasks.edit', [$job->id, $task->id])"
wire:navigate
subtle
color="black"
sm
icon="pencil"
:text="__('jobs.edit')"
:aria-label="__('jobs.tasks.edit_named', ['title' => $task->title])"
/>
<x-button
wire:click="confirmTaskRemoval({{ $task->id }})"
subtle
color="black"
sm
icon="trash"
:text="__('customers.remove')"
:aria-label="__('jobs.tasks.remove_named', ['title' => $task->title])"
/>
@endif
</div>
</article>
</li>
@endforeach
</ul>
@endif
<x-confirm-dialog id="remove-task-dialog" :title="__('customers.removal.title')">
<p>
@if ($removalTaskId !== null)
{{ __('jobs.tasks.remove_question', ['title' => $removalTaskTitle]) }}
@endif
</p>
<x-slot:actions>
<x-button wire:click="removeTask" color="red" icon="trash" :text="__('customers.remove')" />
</x-slot:actions>
</x-confirm-dialog>
</section>
<section aria-labelledby="links-heading" class="space-y-3">
<h2 id="links-heading" class="text-xl font-semibold">{{ __('jobs.sections.links') }}</h2>
@@ -0,0 +1,218 @@
<?php
use App\Modules\Catalog\Application\Contracts\CatalogQueries;
use App\Modules\Jobs\Application\Contracts\CustomerJobQueries;
use App\Modules\Jobs\Application\Contracts\TaskQueries;
use App\Modules\Jobs\Application\DTOs\NamedId;
use App\Modules\Jobs\Application\UseCases\AddTask;
use App\Modules\Jobs\Application\UseCases\UpdateTask;
use App\Modules\Jobs\Domain\Enums\TaskStatus;
use App\Modules\Jobs\Domain\Exceptions\CustomerJobNotFound;
use App\Modules\Jobs\Domain\Exceptions\TaskNotFound;
use App\Modules\Jobs\Presentation\Livewire\Forms\TaskForm;
use App\Modules\Shared\Domain\Exceptions\DomainException;
use App\Modules\Users\Application\Contracts\UserAccountQueries;
use App\Modules\Users\Domain\Enums\Role;
use Livewire\Attributes\Computed;
use Livewire\Attributes\Locked;
use Livewire\Component;
/**
* Add page (no {task}) and edit page (with {task}) of a job's task.
*/
new class extends Component
{
#[Locked]
public int $jobId;
#[Locked]
public ?int $taskId = null;
#[Locked]
public string $jobTitle = '';
/** @var list<int> */
#[Locked]
public array $serviceUnitIds = [];
/**
* The task's work type when editing, offered even if taken out of use.
*/
#[Locked]
public ?int $storedWorkTypeId = null;
public TaskForm $form;
public function mount(CustomerJobQueries $jobQueries, TaskQueries $taskQueries, int $job, ?int $task = null): void
{
$details = $jobQueries->findDetails($job);
abort_if($details === null, 404);
$this->authorize('jobs.manage-tasks', $details->id);
$this->jobId = $details->id;
$this->jobTitle = $details->number.' '.$details->name;
$this->serviceUnitIds = array_map(fn (NamedId $unit): int => $unit->id, $details->serviceUnits);
if ($task === null) {
abort_unless($details->status->allowsTasks(), 404);
$this->form->assignee_user_ids = [(string) auth()->id()];
return;
}
$summary = $taskQueries->findTask($details->id, $task);
abort_if($summary === null, 404);
$this->taskId = $summary->id;
$this->storedWorkTypeId = $summary->workType->id;
$this->form->fillFromSummary($summary);
}
/**
* Work types of the job's service units and the shared ones, grouped
* as "Unit – Service" for <optgroup>.
*
* @return array<string, list<array{id: int, name: string}>>
*/
#[Computed]
public function workTypeGroups(): array
{
$catalog = app(CatalogQueries::class);
$groups = [];
$seen = [];
foreach ($catalog->activeWorkTypesFor($this->serviceUnitIds) as $node) {
if ($node->workTypes !== []) {
$groups[$node->serviceUnit->name] = array_map(fn ($workType): array => ['id' => $workType->id, 'name' => $workType->name], $node->workTypes);
}
foreach ($node->services as $serviceNode) {
if ($serviceNode->workTypes !== []) {
$groups[$node->serviceUnit->name.' – '.$serviceNode->service->name] = array_map(fn ($workType): array => ['id' => $workType->id, 'name' => $workType->name], $serviceNode->workTypes);
}
}
}
foreach ($groups as $group) {
foreach ($group as $workType) {
$seen[$workType['id']] = true;
}
}
if ($this->storedWorkTypeId !== null && ! isset($seen[$this->storedWorkTypeId]) && ($stored = $catalog->findWorkType($this->storedWorkTypeId)) !== null) {
$groups[__('catalog.inactive')] = [['id' => $stored->id, 'name' => $stored->name]];
}
return $groups;
}
/**
* @return list<array{value: int, label: string, note: ?string}>
*/
#[Computed]
public function userOptions(): array
{
return array_map(fn ($user): array => [
'value' => $user->id,
'label' => $user->name,
'note' => $user->role === Role::Subcontractor ? __('users.roles.subcontractor') : null,
], app(UserAccountQueries::class)->activeUserOptions());
}
public function save(AddTask $addTask, UpdateTask $updateTask): void
{
$this->authorize('jobs.manage-tasks', $this->jobId);
$data = $this->form->toData();
try {
if ($this->taskId === null) {
$addTask->execute($this->jobId, $data, (int) auth()->id());
session()->flash('status', __('jobs.tasks.created', ['title' => trim($data->title)]));
} else {
$updateTask->execute($this->jobId, $this->taskId, $data);
session()->flash('status', __('jobs.tasks.updated', ['title' => trim($data->title)]));
}
} catch (CustomerJobNotFound|TaskNotFound) {
abort(404);
} catch (DomainException $exception) {
$this->form->showDomainError($exception);
return;
}
$this->redirect(route('jobs.show', $this->jobId).'#tasks', navigate: true);
}
public function heading(): string
{
return __($this->taskId === null ? 'jobs.tasks.create_title' : 'jobs.tasks.edit_title');
}
public function render(): mixed
{
return $this->view()->title($this->heading().' – '.$this->jobTitle);
}
};
?>
<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>
<p class="text-sm text-zinc-600 dark:text-zinc-300">
{{ __('jobs.tasks.job_label') }}:
<x-link :href="route('jobs.show', $jobId)" navigate>{{ $jobTitle }}</x-link>
</p>
</div>
<form wire:submit="save" class="space-y-6" novalidate>
<x-input wire:model="form.title" :label="__('jobs.tasks.fields.title')" autocomplete="off" required />
{{-- TallStackUI's native select does not tie its label to the control, so aria-label names it. --}}
<x-select.native wire:model="form.work_type_id" :label="__('jobs.tasks.fields.work_type')" :aria-label="__('jobs.tasks.fields.work_type')" :hint="__('jobs.tasks.fields.work_type_description')" required>
<option value="">{{ __('jobs.fields.none_selected') }}</option>
@foreach ($this->workTypeGroups as $group => $workTypes)
<optgroup label="{{ $group }}">
@foreach ($workTypes as $workType)
<option value="{{ $workType['id'] }}">{{ $workType['name'] }}</option>
@endforeach
</optgroup>
@endforeach
</x-select.native>
<x-checkbox-group
:legend="__('jobs.tasks.fields.assignees')"
model="form.assignee_user_ids"
:options="$this->userOptions"
/>
<div class="grid gap-6 sm:grid-cols-3">
<x-select.native wire:model="form.status" :label="__('jobs.tasks.fields.status')" :aria-label="__('jobs.tasks.fields.status')">
@foreach (TaskStatus::cases() as $status)
<option value="{{ $status->value }}">{{ __('jobs.tasks.statuses.'.$status->value) }}</option>
@endforeach
</x-select.native>
<x-input wire:model="form.due_on" type="date" :label="__('jobs.tasks.fields.due_on')" />
<x-input
wire:model="form.estimated_hours"
type="number"
step="0.25"
min="0"
inputmode="decimal"
:label="__('jobs.tasks.fields.estimated_hours')"
/>
</div>
<x-textarea wire:model="form.description" :label="__('jobs.tasks.fields.description')" resize-auto />
<div class="flex flex-wrap gap-3">
<x-button submit :text="__('jobs.save')" />
<x-button :href="route('jobs.show', $jobId).'#tasks'" wire:navigate subtle color="black" :text="__('jobs.cancel')" />
</div>
</form>
</div>