Files
Toiminnanohjaus/app/Modules/Jobs/Application/UseCases/AddTasksFromWorkflowTemplate.php
T
petriandClaude Opus 5.5 b4f2f1cfaa Add the task calendar, holidays, workflow templates and customer removal
Task calendar (/jobs/calendar) replaces per-unit task Kanban columns
(K34): a two-week Gantt of tasks per person. Bars move, resize and
change person by dragging or through a keyboard dialog. Each task's
estimate is booked evenly over working days into task_bookings, and
what exceeds the daily maximum (jobs.daily_capacity_hours, 8 h) moves
to the next working day. Tasks get planned_from/planned_to on the form.

Holidays (/settings/holidays): Finnish public holidays calculated per
year, which the admin can take out of use, plus company days off.

Also includes the earlier uncommitted work: workflow templates with
adding tasks from a template, and removing customers no module refers to.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-11-24 18:13:11 +02:00

93 lines
3.7 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\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\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\TransactionManager;
/**
* 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 and estimate
* entered for it, assigned to the user who adds them. 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 TransactionManager $transactions,
) {}
/**
* @param list<TemplateTaskSchedule> $schedules In step order; a missing one leaves the task without a due date and estimate.
* @return int How many tasks were added.
*
* @throws CustomerJobNotFound
* @throws InvalidEstimatedHours
* @throws TasksNotAllowedYet
* @throws WorkflowTemplateNotAvailable
* @throws InvalidTaskReference when a step's work type is not available for the job
*/
public function execute(int $jobId, int $templateId, int $actorUserId, array $schedules = []): int
{
$dueDates = array_map(fn (TemplateTaskSchedule $schedule) => TaskInput::date($schedule->dueOn), $schedules);
$estimates = array_map(fn (TemplateTaskSchedule $schedule) => TaskInput::hours($schedule->estimatedHours), $schedules);
return $this->transactions->run(function () use ($jobId, $templateId, $actorUserId, $dueDates, $estimates): int {
$job = $this->jobs->findById($jobId) ?? throw new CustomerJobNotFound($jobId);
$template = $this->availableTemplate($templateId, $job->details()->serviceUnitIds);
foreach ($template->steps as $index => $step) {
$this->references->check($job, $step->effectiveWorkTypeId, [$actorUserId]);
$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,
[$actorUserId],
$actorUserId,
));
}
return count($template->steps);
});
}
/**
* @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);
}
}