Files
Toiminnanohjaus/app/Modules/Catalog/Application/Services/WorkflowStepBuilder.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

84 lines
2.8 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Modules\Catalog\Application\Services;
use App\Modules\Catalog\Application\DTOs\WorkflowStepData;
use App\Modules\Catalog\Domain\Contracts\ServiceUnitRepository;
use App\Modules\Catalog\Domain\Contracts\WorkTypeRepository;
use App\Modules\Catalog\Domain\Entities\WorkType;
use App\Modules\Catalog\Domain\Exceptions\InvalidWorkflowStepTitle;
use App\Modules\Catalog\Domain\Exceptions\WorkflowStepWorkTypeNotAllowed;
use App\Modules\Catalog\Domain\ValueObjects\WorkflowStep;
use App\Modules\Catalog\Domain\ValueObjects\WorkflowStepTitle;
/**
* Builds the steps of a workflow template from input and checks their own work types.
*/
final class WorkflowStepBuilder
{
public function __construct(
private WorkTypeRepository $workTypes,
private ServiceUnitRepository $serviceUnits,
) {}
/**
* @param list<WorkflowStepData> $input
* @return list<WorkflowStep>
*
* @throws InvalidWorkflowStepTitle with the index of the step
*/
public function build(array $input): array
{
$steps = [];
foreach ($input as $index => $step) {
try {
$title = new WorkflowStepTitle($step->title);
} catch (InvalidWorkflowStepTitle) {
throw new InvalidWorkflowStepTitle($index);
}
$steps[] = new WorkflowStep($title, $step->description, $step->workTypeId);
}
return $steps;
}
/**
* A step's own work type must be in use and belong to the template work
* type's unit or to the shared unit. A work type the template already
* uses stays allowed after it is taken out of use.
*
* @param list<WorkflowStep> $steps
* @param list<WorkflowStep> $currentSteps
*
* @throws WorkflowStepWorkTypeNotAllowed
*/
public function checkWorkTypes(WorkType $templateWorkType, array $steps, array $currentSteps = []): void
{
$currentIds = array_map(fn (WorkflowStep $step): ?int => $step->workTypeId, $currentSteps);
foreach ($steps as $index => $step) {
if ($step->workTypeId === null || $step->workTypeId === $templateWorkType->id()) {
continue;
}
$workType = $this->workTypes->findById($step->workTypeId);
if ($workType === null
|| (! $workType->isActive() && ! in_array($workType->id(), $currentIds, true))
|| ! $this->sameOrSharedUnit($templateWorkType, $workType)) {
throw new WorkflowStepWorkTypeNotAllowed($index);
}
}
}
private function sameOrSharedUnit(WorkType $templateWorkType, WorkType $workType): bool
{
return $workType->serviceUnitId() === $templateWorkType->serviceUnitId()
|| ($this->serviceUnits->findById($workType->serviceUnitId())?->isShared() ?? false);
}
}