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,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>