A new Notifications module shows a bell beside the profile menu with the number of unseen notifications and a panel of the last 7 days; opening it marks them seen, and each notification links to its job or task. - A daily run at 14.00 (notifications:task-starts) tells the assignee and members of tasks that start by the next working day, skipping holidays and days off, done tasks and archived jobs. - Jobs publishes PeopleAddedToTask and PeopleAddedToJob when someone else makes a user a task's assignee or member or gives them a role on a job; those users are notified at once. UpdateTask and ScheduleTask now take the acting user. - A notification link to #task-<id> opens that task card on the job page. Also: simplifying job fields per service unit (K31) moves to the end of phase 4, and the workflow template browser test waits for Livewire's progress bar before its last accessibility check. Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
246 lines
9.4 KiB
PHP
246 lines
9.4 KiB
PHP
<?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 Carbon\CarbonImmutable;
|
||
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_id = (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) {
|
||
$taskId = $addTask->execute($this->jobId, $data, (int) auth()->id());
|
||
$message = __('jobs.tasks.created', ['title' => trim($data->title)]);
|
||
} else {
|
||
$taskId = $this->taskId;
|
||
$updateTask->execute($this->jobId, $taskId, $data, (int) auth()->id());
|
||
$message = __('jobs.tasks.updated', ['title' => trim($data->title)]);
|
||
}
|
||
} catch (CustomerJobNotFound|TaskNotFound) {
|
||
abort(404);
|
||
} catch (DomainException $exception) {
|
||
$this->form->showDomainError($exception);
|
||
|
||
return;
|
||
}
|
||
|
||
$plannedTo = app(TaskQueries::class)->findTask($this->jobId, $taskId)?->plannedTo;
|
||
|
||
if ($data->plannedFrom !== null && $plannedTo !== null && $plannedTo !== ($data->plannedTo ?? $data->plannedFrom)) {
|
||
$message .= ' '.__('jobs.tasks.extended', ['to' => CarbonImmutable::parse($plannedTo)->format('j.n.Y')]);
|
||
}
|
||
|
||
session()->flash('status', $message);
|
||
|
||
$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-select.native wire:model="form.assignee_user_id" :label="__('jobs.tasks.fields.assignee')" :aria-label="__('jobs.tasks.fields.assignee')" :hint="__('jobs.tasks.fields.assignee_hint')">
|
||
<option value="">{{ __('jobs.calendar.no_assignee') }}</option>
|
||
@foreach ($this->userOptions as $user)
|
||
<option value="{{ $user['value'] }}">{{ $user['note'] === null ? $user['label'] : $user['label'].' ('.$user['note'].')' }}</option>
|
||
@endforeach
|
||
</x-select.native>
|
||
|
||
<x-checkbox-group
|
||
:legend="__('jobs.tasks.fields.members')"
|
||
model="form.member_user_ids"
|
||
:options="$this->userOptions"
|
||
:hint="__('jobs.tasks.fields.members_hint')"
|
||
/>
|
||
|
||
<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>
|
||
|
||
<fieldset class="space-y-2">
|
||
<legend class="text-sm font-semibold">{{ __('jobs.tasks.schedule') }}</legend>
|
||
<p class="text-sm text-zinc-600 dark:text-zinc-300">{{ __('jobs.tasks.fields.planned_hint') }}</p>
|
||
<div class="grid gap-6 sm:grid-cols-2">
|
||
<x-input wire:model="form.planned_from" type="date" :label="__('jobs.tasks.fields.planned_from')" />
|
||
<x-input wire:model="form.planned_to" type="date" :label="__('jobs.tasks.fields.planned_to')" />
|
||
</div>
|
||
</fieldset>
|
||
|
||
<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>
|