Add notifications: header bell, task start run and assignment notices

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]>
This commit is contained in:
2026-11-25 08:28:01 +02:00
co-authored by Claude Sonnet 5.5
parent ae8172898d
commit f906f9a43f
48 changed files with 1665 additions and 31 deletions
@@ -0,0 +1,21 @@
<?php
declare(strict_types=1);
namespace App\Modules\Jobs\Application\Contracts;
use App\Modules\Jobs\Application\DTOs\UpcomingTask;
/**
* Tasks about to start on the task calendar, for notifying their people.
*/
interface UpcomingTasks
{
/**
* Tasks not done, on jobs not archived, whose planned start is after the
* first date and at most the second (Y-m-d), in start date order.
*
* @return list<UpcomingTask>
*/
public function startingBetween(string $after, string $to): array;
}
@@ -0,0 +1,25 @@
<?php
declare(strict_types=1);
namespace App\Modules\Jobs\Application\DTOs;
/**
* A task whose planned start is near, with whom to tell about it: the
* assignee and the members.
*/
final readonly class UpcomingTask
{
/**
* @param list<int> $recipientIds
*/
public function __construct(
public int $taskId,
public string $title,
public int $jobId,
public string $jobNumber,
public string $jobName,
public string $plannedFrom,
public array $recipientIds,
) {}
}
@@ -11,11 +11,13 @@ use App\Modules\Jobs\Domain\Contracts\CustomerJobRepository;
use App\Modules\Jobs\Domain\Contracts\TaskRepository;
use App\Modules\Jobs\Domain\Entities\Task;
use App\Modules\Jobs\Domain\Enums\TaskStatus;
use App\Modules\Jobs\Domain\Events\PeopleAddedToTask;
use App\Modules\Jobs\Domain\Exceptions\CustomerJobNotFound;
use App\Modules\Jobs\Domain\Exceptions\InvalidTaskReference;
use App\Modules\Jobs\Domain\Exceptions\PlanEndsBeforeStart;
use App\Modules\Jobs\Domain\Exceptions\TasksNotAllowedYet;
use App\Modules\Jobs\Domain\ValueObjects\TaskTitle;
use App\Modules\Shared\Application\Contracts\EventDispatcher;
use App\Modules\Shared\Application\Contracts\TransactionManager;
final class AddTask
@@ -26,6 +28,7 @@ final class AddTask
private TaskReferenceChecker $references,
private TaskPlanner $planner,
private TransactionManager $transactions,
private EventDispatcher $events,
) {}
/**
@@ -66,6 +69,12 @@ final class AddTask
$task = $this->tasks->save($task);
}
$added = PeopleAddedToTask::between(null, $task, $job, $actorUserId);
if ($added !== null) {
$this->events->dispatch($added);
}
return (int) $task->id();
});
}
@@ -14,12 +14,14 @@ use App\Modules\Jobs\Domain\Contracts\CustomerJobRepository;
use App\Modules\Jobs\Domain\Contracts\TaskRepository;
use App\Modules\Jobs\Domain\Entities\Task;
use App\Modules\Jobs\Domain\Enums\TaskStatus;
use App\Modules\Jobs\Domain\Events\PeopleAddedToTask;
use App\Modules\Jobs\Domain\Exceptions\CustomerJobNotFound;
use App\Modules\Jobs\Domain\Exceptions\InvalidEstimatedHours;
use App\Modules\Jobs\Domain\Exceptions\InvalidTaskReference;
use App\Modules\Jobs\Domain\Exceptions\TasksNotAllowedYet;
use App\Modules\Jobs\Domain\Exceptions\WorkflowTemplateNotAvailable;
use App\Modules\Jobs\Domain\ValueObjects\TaskTitle;
use App\Modules\Shared\Application\Contracts\EventDispatcher;
use App\Modules\Shared\Application\Contracts\TransactionManager;
use App\Modules\Users\Application\Contracts\Clock;
@@ -41,6 +43,7 @@ final class AddTasksFromWorkflowTemplate
private TaskPlanner $planner,
private Clock $clock,
private TransactionManager $transactions,
private EventDispatcher $events,
) {}
/**
@@ -89,6 +92,12 @@ final class AddTasksFromWorkflowTemplate
$scheduled++;
$this->tasks->save($task);
}
$added = PeopleAddedToTask::between(null, $task, $job, $actorUserId);
if ($added !== null) {
$this->events->dispatch($added);
}
}
return new TemplateTasksResult(count($template->steps), $scheduled, $late);
@@ -9,8 +9,10 @@ use App\Modules\Jobs\Application\Services\JobDetailsFactory;
use App\Modules\Jobs\Domain\Contracts\CustomerJobRepository;
use App\Modules\Jobs\Domain\Contracts\JobNumberSequence;
use App\Modules\Jobs\Domain\Entities\CustomerJob;
use App\Modules\Jobs\Domain\Events\PeopleAddedToJob;
use App\Modules\Jobs\Domain\Exceptions\InvalidJobReference;
use App\Modules\Jobs\Domain\ValueObjects\FieldChange;
use App\Modules\Shared\Application\Contracts\EventDispatcher;
use App\Modules\Shared\Application\Contracts\TransactionManager;
use App\Modules\Users\Application\Contracts\Clock;
@@ -26,6 +28,7 @@ final class CreateCustomerJob
private JobDetailsFactory $detailsFactory,
private TransactionManager $transactions,
private Clock $clock,
private EventDispatcher $events,
) {}
/**
@@ -41,10 +44,17 @@ final class CreateCustomerJob
return $this->transactions->run(function () use ($details, $year, $actorUserId): int {
$job = CustomerJob::open($this->numbers->next($year), $details, $actorUserId);
$job->recordCompletion($this->clock->now());
$jobId = (int) $this->jobs->save($job)->id();
$job = $this->jobs->save($job);
$jobId = (int) $job->id();
$this->jobs->recordChanges($jobId, $actorUserId, [new FieldChange('created', null, $job->number()->format())]);
$added = PeopleAddedToJob::between(null, $job, $actorUserId);
if ($added !== null) {
$this->events->dispatch($added);
}
return $jobId;
});
}
@@ -9,10 +9,12 @@ use App\Modules\Jobs\Application\Services\TaskPlanner;
use App\Modules\Jobs\Application\Services\TaskReferenceChecker;
use App\Modules\Jobs\Domain\Contracts\CustomerJobRepository;
use App\Modules\Jobs\Domain\Contracts\TaskRepository;
use App\Modules\Jobs\Domain\Events\PeopleAddedToTask;
use App\Modules\Jobs\Domain\Exceptions\CustomerJobNotFound;
use App\Modules\Jobs\Domain\Exceptions\InvalidTaskReference;
use App\Modules\Jobs\Domain\Exceptions\PlanEndsBeforeStart;
use App\Modules\Jobs\Domain\Exceptions\TaskNotFound;
use App\Modules\Shared\Application\Contracts\EventDispatcher;
use App\Modules\Shared\Application\Contracts\TransactionManager;
/**
@@ -29,6 +31,7 @@ final class ScheduleTask
private TaskReferenceChecker $references,
private TaskPlanner $planner,
private TransactionManager $transactions,
private EventDispatcher $events,
) {}
/**
@@ -37,14 +40,15 @@ final class ScheduleTask
* @throws InvalidTaskReference
* @throws PlanEndsBeforeStart
*/
public function execute(int $jobId, int $taskId, ?int $fromUserId, ?int $toUserId, string $plannedFrom, ?string $plannedTo): ScheduleResult
public function execute(int $jobId, int $taskId, ?int $fromUserId, ?int $toUserId, string $plannedFrom, ?string $plannedTo, int $actorUserId): ScheduleResult
{
$from = TaskInput::date($plannedFrom) ?? throw new PlanEndsBeforeStart;
$to = TaskInput::date($plannedTo);
return $this->transactions->run(function () use ($jobId, $taskId, $fromUserId, $toUserId, $from, $to): ScheduleResult {
return $this->transactions->run(function () use ($jobId, $taskId, $fromUserId, $toUserId, $from, $to, $actorUserId): ScheduleResult {
$job = $this->jobs->findById($jobId) ?? throw new CustomerJobNotFound($jobId);
$task = $this->tasks->findForJob($jobId, $taskId) ?? throw new TaskNotFound($jobId, $taskId);
$peopleBefore = PeopleAddedToTask::peopleOf($task);
if ($fromUserId !== $toUserId) {
$this->references->check($job, $task->workTypeId(), $toUserId, [], $task);
@@ -57,6 +61,12 @@ final class ScheduleTask
$extended = $this->planner->plan($task);
$task = $this->tasks->save($task);
$added = PeopleAddedToTask::between($peopleBefore, $task, $job, $actorUserId);
if ($added !== null) {
$this->events->dispatch($added);
}
return new ScheduleResult(
(string) $task->plannedFrom()?->format('Y-m-d'),
(string) $task->plannedTo()?->format('Y-m-d'),
@@ -7,8 +7,10 @@ namespace App\Modules\Jobs\Application\UseCases;
use App\Modules\Jobs\Application\DTOs\CustomerJobData;
use App\Modules\Jobs\Application\Services\JobDetailsFactory;
use App\Modules\Jobs\Domain\Contracts\CustomerJobRepository;
use App\Modules\Jobs\Domain\Events\PeopleAddedToJob;
use App\Modules\Jobs\Domain\Exceptions\CustomerJobNotFound;
use App\Modules\Jobs\Domain\Exceptions\InvalidJobReference;
use App\Modules\Shared\Application\Contracts\EventDispatcher;
use App\Modules\Shared\Application\Contracts\TransactionManager;
use App\Modules\Users\Application\Contracts\Clock;
@@ -23,6 +25,7 @@ final class UpdateCustomerJob
private JobDetailsFactory $detailsFactory,
private TransactionManager $transactions,
private Clock $clock,
private EventDispatcher $events,
) {}
/**
@@ -34,15 +37,22 @@ final class UpdateCustomerJob
$this->transactions->run(function () use ($jobId, $data, $actorUserId): void {
$job = $this->jobs->findById($jobId) ?? throw new CustomerJobNotFound($jobId);
$changes = $job->changeDetails($this->detailsFactory->build($data, $job->details()));
$before = $job->details();
$changes = $job->changeDetails($this->detailsFactory->build($data, $before));
$job->recordCompletion($this->clock->now());
if ($changes === []) {
return;
}
$this->jobs->save($job);
$job = $this->jobs->save($job);
$this->jobs->recordChanges($jobId, $actorUserId, $changes);
$added = PeopleAddedToJob::between($before, $job, $actorUserId);
if ($added !== null) {
$this->events->dispatch($added);
}
});
}
}
@@ -11,11 +11,13 @@ use App\Modules\Jobs\Domain\Contracts\CustomerJobRepository;
use App\Modules\Jobs\Domain\Contracts\TaskRepository;
use App\Modules\Jobs\Domain\Entities\Task;
use App\Modules\Jobs\Domain\Enums\TaskStatus;
use App\Modules\Jobs\Domain\Events\PeopleAddedToTask;
use App\Modules\Jobs\Domain\Exceptions\CustomerJobNotFound;
use App\Modules\Jobs\Domain\Exceptions\InvalidTaskReference;
use App\Modules\Jobs\Domain\Exceptions\PlanEndsBeforeStart;
use App\Modules\Jobs\Domain\Exceptions\TaskNotFound;
use App\Modules\Jobs\Domain\ValueObjects\TaskTitle;
use App\Modules\Shared\Application\Contracts\EventDispatcher;
use App\Modules\Shared\Application\Contracts\TransactionManager;
final class UpdateTask
@@ -26,6 +28,7 @@ final class UpdateTask
private TaskReferenceChecker $references,
private TaskPlanner $planner,
private TransactionManager $transactions,
private EventDispatcher $events,
) {}
/**
@@ -34,17 +37,18 @@ final class UpdateTask
* @throws InvalidTaskReference
* @throws PlanEndsBeforeStart
*/
public function execute(int $jobId, int $taskId, TaskData $data): void
public function execute(int $jobId, int $taskId, TaskData $data, int $actorUserId): void
{
$title = new TaskTitle($data->title);
$this->transactions->run(function () use ($jobId, $taskId, $data, $title): void {
$this->transactions->run(function () use ($jobId, $taskId, $data, $title, $actorUserId): void {
$job = $this->jobs->findById($jobId) ?? throw new CustomerJobNotFound($jobId);
$task = $this->tasks->findForJob($jobId, $taskId) ?? throw new TaskNotFound($jobId, $taskId);
$this->references->check($job, $data->workTypeId, $data->assigneeUserId, $data->memberUserIds, $task);
$before = self::planningInput($task);
$peopleBefore = PeopleAddedToTask::peopleOf($task);
$task->changeDetails(
$data->workTypeId,
@@ -65,6 +69,12 @@ final class UpdateTask
$this->planner->plan($task);
$this->tasks->save($task);
}
$added = PeopleAddedToTask::between($peopleBefore, $task, $job, $actorUserId);
if ($added !== null) {
$this->events->dispatch($added);
}
});
}
@@ -0,0 +1,82 @@
<?php
declare(strict_types=1);
namespace App\Modules\Jobs\Domain\Events;
use App\Modules\Jobs\Domain\Entities\CustomerJob;
use App\Modules\Jobs\Domain\ValueObjects\JobDetails;
/**
* Someone got a role on a customer job: responsible person, contact
* person, biller or member. Published after the change is saved, for
* notifying them (not the one who made it).
*/
final readonly class PeopleAddedToJob
{
public const string RESPONSIBLE = 'responsible';
public const string CONTACT = 'contact';
public const string BILLER = 'biller';
public const string MEMBER = 'member';
/**
* @param array<int, list<string>> $newRoles The new roles by user id.
*/
public function __construct(
public int $jobId,
public string $jobNumber,
public string $jobName,
public array $newRoles,
public int $actorUserId,
) {}
/**
* The roles the saved job gives that the details before did not (a new
* job has none before); null when nobody got a new role.
*/
public static function between(?JobDetails $before, CustomerJob $after, int $actorUserId): ?self
{
$old = $before === null ? [] : self::rolesOf($before);
$newRoles = [];
foreach (self::rolesOf($after->details()) as $userId => $roles) {
$added = array_values(array_diff($roles, $old[$userId] ?? []));
if ($added !== []) {
$newRoles[$userId] = $added;
}
}
if ($newRoles === []) {
return null;
}
return new self((int) $after->id(), $after->number()->format(), $after->details()->name->value, $newRoles, $actorUserId);
}
/**
* @return array<int, list<string>>
*/
private static function rolesOf(JobDetails $details): array
{
$roles = [];
$roles[$details->responsibleUserId][] = self::RESPONSIBLE;
if ($details->contactUserId !== null) {
$roles[$details->contactUserId][] = self::CONTACT;
}
if ($details->billerUserId !== null) {
$roles[$details->billerUserId][] = self::BILLER;
}
foreach ($details->memberUserIds as $memberId) {
$roles[$memberId][] = self::MEMBER;
}
return $roles;
}
}
@@ -0,0 +1,67 @@
<?php
declare(strict_types=1);
namespace App\Modules\Jobs\Domain\Events;
use App\Modules\Jobs\Domain\Entities\CustomerJob;
use App\Modules\Jobs\Domain\Entities\Task;
/**
* Someone became a task's assignee or one of its members. Published after
* the change is saved, for notifying them (not the one who made it).
*/
final readonly class PeopleAddedToTask
{
/**
* @param list<int> $newMemberIds
*/
public function __construct(
public int $taskId,
public string $taskTitle,
public int $jobId,
public string $jobNumber,
public string $jobName,
public ?int $newAssigneeId,
public array $newMemberIds,
public int $actorUserId,
) {}
/**
* The people the saved task has that it did not have before (a new task
* has none before); null when nobody was added.
*
* @param array{assignee: ?int, members: list<int>}|null $before
*/
public static function between(?array $before, Task $after, CustomerJob $job, int $actorUserId): ?self
{
$assignee = $after->assigneeUserId();
$newAssignee = $assignee !== null && $assignee !== ($before['assignee'] ?? null) ? $assignee : null;
$newMembers = array_values(array_diff($after->memberUserIds(), $before['members'] ?? [], $newAssignee === null ? [] : [$newAssignee]));
if ($newAssignee === null && $newMembers === []) {
return null;
}
return new self(
(int) $after->id(),
$after->title()->value,
$job->id() ?? $after->jobId(),
$job->number()->format(),
$job->details()->name->value,
$newAssignee,
$newMembers,
$actorUserId,
);
}
/**
* Who the task has now, to compare with after a change.
*
* @return array{assignee: ?int, members: list<int>}
*/
public static function peopleOf(Task $task): array
{
return ['assignee' => $task->assigneeUserId(), 'members' => $task->memberUserIds()];
}
}
@@ -0,0 +1,50 @@
<?php
namespace App\Modules\Jobs\Infrastructure\Persistence\Repositories;
use App\Modules\Jobs\Application\Contracts\UpcomingTasks;
use App\Modules\Jobs\Application\DTOs\UpcomingTask;
use App\Modules\Jobs\Domain\Enums\JobStatus;
use App\Modules\Jobs\Domain\Enums\TaskStatus;
use Carbon\CarbonImmutable;
use Illuminate\Support\Facades\DB;
class EloquentUpcomingTasks implements UpcomingTasks
{
public function startingBetween(string $after, string $to): array
{
// Half-open bounds on the next days: SQLite keeps dates as "Y-m-d 00:00:00" strings.
$rows = DB::table('tasks')
->join('customer_jobs', 'customer_jobs.id', '=', 'tasks.customer_job_id')
->where('tasks.planned_from', '>=', CarbonImmutable::parse($after)->addDay()->toDateString())
->where('tasks.planned_from', '<', CarbonImmutable::parse($to)->addDay()->toDateString())
->where('tasks.status', '!=', TaskStatus::Done->value)
->where('customer_jobs.status', '!=', JobStatus::Archived->value)
->orderBy('tasks.planned_from')
->orderBy('tasks.id')
->get(['tasks.id', 'tasks.title', 'tasks.planned_from', 'tasks.assignee_user_id', 'customer_jobs.id as job_id', 'customer_jobs.number', 'customer_jobs.name']);
$members = DB::table('task_members')
->whereIn('task_id', $rows->pluck('id')->all())
->get(['task_id', 'user_id'])
->groupBy('task_id');
return $rows->map(function (object $row) use ($members): UpcomingTask {
$recipients = ($members->get($row->id) ?? collect())->pluck('user_id');
if ($row->assignee_user_id !== null) {
$recipients->prepend($row->assignee_user_id);
}
return new UpcomingTask(
(int) $row->id,
(string) $row->title,
(int) $row->job_id,
(string) $row->number,
(string) $row->name,
substr((string) $row->planned_from, 0, 10),
$recipients->map(fn ($id): int => (int) $id)->unique()->values()->all(),
);
})->values()->all();
}
}
@@ -13,6 +13,7 @@ use App\Modules\Jobs\Application\Contracts\TaskImageFiles;
use App\Modules\Jobs\Application\Contracts\TaskImageQueries;
use App\Modules\Jobs\Application\Contracts\TaskQueries;
use App\Modules\Jobs\Application\Contracts\TaskUsage;
use App\Modules\Jobs\Application\Contracts\UpcomingTasks;
use App\Modules\Jobs\Domain\Contracts\ChecklistItemRepository;
use App\Modules\Jobs\Domain\Contracts\CustomerJobRepository;
use App\Modules\Jobs\Domain\Contracts\JobNumberSequence;
@@ -37,6 +38,7 @@ use App\Modules\Jobs\Infrastructure\Persistence\Repositories\EloquentTaskImageQu
use App\Modules\Jobs\Infrastructure\Persistence\Repositories\EloquentTaskImageRepository;
use App\Modules\Jobs\Infrastructure\Persistence\Repositories\EloquentTaskQueries;
use App\Modules\Jobs\Infrastructure\Persistence\Repositories\EloquentTaskRepository;
use App\Modules\Jobs\Infrastructure\Persistence\Repositories\EloquentUpcomingTasks;
use App\Modules\Users\Domain\Enums\Role;
use Illuminate\Support\Facades\Gate;
use Illuminate\Support\ServiceProvider;
@@ -67,6 +69,7 @@ class JobsServiceProvider extends ServiceProvider
TaskImageRepository::class => EloquentTaskImageRepository::class,
TaskImageQueries::class => EloquentTaskImageQueries::class,
TaskImageFiles::class => GdTaskImageFiles::class,
UpcomingTasks::class => EloquentUpcomingTasks::class,
// Replaced by the TimeTracking module, which knows which tasks have time entries.
TaskUsage::class => NothingUsesTasks::class,
];
@@ -410,7 +410,7 @@ new class extends Component
$task = $this->findTask($taskId);
try {
$result = $scheduleTask->execute($jobId, $taskId, $fromUserId, $toUserId, $plannedFrom, $plannedTo);
$result = $scheduleTask->execute($jobId, $taskId, $fromUserId, $toUserId, $plannedFrom, $plannedTo, (int) auth()->id());
} catch (CustomerJobNotFound|TaskNotFound|InvalidArgumentException) {
$this->statusMessage = __('jobs.calendar.not_changed');
@@ -378,9 +378,13 @@ new class extends Component
<ul class="space-y-3">
@foreach ($this->tasks as $task)
{{-- A notification links to #task-<id>: that card opens and comes into view. --}}
<li
id="task-{{ $task->id }}"
class="scroll-mt-24"
wire:key="task-{{ $task->id }}"
x-data="{ open: false }"
x-data="{ open: location.hash === '#task-{{ $task->id }}' }"
x-init="if (open) $nextTick(() => $el.scrollIntoView({ block: 'start' }))"
x-on:tasks-expand.window="open = $event.detail"
x-on:focusin="if ($event.target.closest('[data-task-details]')) open = true"
>
@@ -135,7 +135,7 @@ new class extends Component
$message = __('jobs.tasks.created', ['title' => trim($data->title)]);
} else {
$taskId = $this->taskId;
$updateTask->execute($this->jobId, $taskId, $data);
$updateTask->execute($this->jobId, $taskId, $data, (int) auth()->id());
$message = __('jobs.tasks.updated', ['title' => trim($data->title)]);
}
} catch (CustomerJobNotFound|TaskNotFound) {