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]>
121 lines
5.3 KiB
PHP
121 lines
5.3 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Modules\Jobs\Application\UseCases;
|
|
|
|
use App\Modules\Catalog\Application\Contracts\WorkflowTemplateQueries;
|
|
use App\Modules\Catalog\Application\DTOs\WorkflowTemplateDetails;
|
|
use App\Modules\Jobs\Application\DTOs\TemplateTaskSchedule;
|
|
use App\Modules\Jobs\Application\DTOs\TemplateTasksResult;
|
|
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\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;
|
|
|
|
/**
|
|
* Makes a task of every step of a workflow template, in the template's
|
|
* order: waiting, with the step's work type and the due date, estimate and
|
|
* assignee entered for it (without an entry, the adder is the assignee).
|
|
* When asked, a task with a due date is scheduled to end on it, booked
|
|
* backwards from the due date (TaskPlanner::planBackwards). The tasks are
|
|
* copies, so later changes to the template do not touch them.
|
|
*/
|
|
final class AddTasksFromWorkflowTemplate
|
|
{
|
|
public function __construct(
|
|
private CustomerJobRepository $jobs,
|
|
private TaskRepository $tasks,
|
|
private WorkflowTemplateQueries $templates,
|
|
private TaskReferenceChecker $references,
|
|
private TaskPlanner $planner,
|
|
private Clock $clock,
|
|
private TransactionManager $transactions,
|
|
private EventDispatcher $events,
|
|
) {}
|
|
|
|
/**
|
|
* @param list<TemplateTaskSchedule> $schedules In step order; a missing one leaves the task without a due date and estimate, assigned to the adder.
|
|
* @param bool $scheduleToDueDates Whether tasks with a due date are scheduled to end on it.
|
|
*
|
|
* @throws CustomerJobNotFound
|
|
* @throws InvalidEstimatedHours
|
|
* @throws TasksNotAllowedYet
|
|
* @throws WorkflowTemplateNotAvailable
|
|
* @throws InvalidTaskReference when a step's work type is not available for the job, or the assignee is not active
|
|
*/
|
|
public function execute(int $jobId, int $templateId, int $actorUserId, array $schedules = [], bool $scheduleToDueDates = false): TemplateTasksResult
|
|
{
|
|
$dueDates = array_map(fn (TemplateTaskSchedule $schedule) => TaskInput::date($schedule->dueOn), $schedules);
|
|
$estimates = array_map(fn (TemplateTaskSchedule $schedule) => TaskInput::hours($schedule->estimatedHours), $schedules);
|
|
$assignees = array_map(fn (TemplateTaskSchedule $schedule): ?int => $schedule->assigneeUserId, $schedules);
|
|
|
|
return $this->transactions->run(function () use ($jobId, $templateId, $actorUserId, $dueDates, $estimates, $assignees, $scheduleToDueDates): TemplateTasksResult {
|
|
$job = $this->jobs->findById($jobId) ?? throw new CustomerJobNotFound($jobId);
|
|
$template = $this->availableTemplate($templateId, $job->details()->serviceUnitIds);
|
|
$today = $this->clock->today();
|
|
$scheduled = 0;
|
|
$late = 0;
|
|
|
|
foreach ($template->steps as $index => $step) {
|
|
$assigneeUserId = array_key_exists($index, $assignees) ? $assignees[$index] : $actorUserId;
|
|
$this->references->check($job, $step->effectiveWorkTypeId, $assigneeUserId);
|
|
|
|
$task = $this->tasks->save(Task::create(
|
|
$jobId,
|
|
$job->details()->status,
|
|
$step->effectiveWorkTypeId,
|
|
new TaskTitle($step->title),
|
|
$step->description,
|
|
TaskStatus::Waiting,
|
|
$dueDates[$index] ?? null,
|
|
$estimates[$index] ?? null,
|
|
$assigneeUserId,
|
|
[],
|
|
$actorUserId,
|
|
));
|
|
|
|
if ($scheduleToDueDates && $task->dueOn() !== null) {
|
|
$late += $this->planner->planBackwards($task, $today) ? 1 : 0;
|
|
$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);
|
|
});
|
|
}
|
|
|
|
/**
|
|
* @param list<int> $serviceUnitIds
|
|
*/
|
|
private function availableTemplate(int $templateId, array $serviceUnitIds): WorkflowTemplateDetails
|
|
{
|
|
foreach ($this->templates->activeFor($serviceUnitIds) as $template) {
|
|
if ($template->id === $templateId) {
|
|
return $template;
|
|
}
|
|
}
|
|
|
|
throw new WorkflowTemplateNotAvailable($templateId);
|
|
}
|
|
}
|