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]>
This commit is contained in:
@@ -0,0 +1,34 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Modules\Catalog\Application\Contracts;
|
||||
|
||||
use App\Modules\Catalog\Application\DTOs\WorkflowTemplateDetails;
|
||||
use App\Modules\Catalog\Application\DTOs\WorkflowTemplateSummary;
|
||||
|
||||
/**
|
||||
* Read side of workflow templates (työnkulkupohjat). Customer jobs use it
|
||||
* to make tasks from a template.
|
||||
*/
|
||||
interface WorkflowTemplateQueries
|
||||
{
|
||||
/**
|
||||
* All templates, active or not, in the order of their work types in the
|
||||
* catalog (the shared unit last) and by name.
|
||||
*
|
||||
* @return list<WorkflowTemplateSummary>
|
||||
*/
|
||||
public function all(): array;
|
||||
|
||||
public function find(int $id): ?WorkflowTemplateDetails;
|
||||
|
||||
/**
|
||||
* Active templates whose work type is in use in the given service units
|
||||
* or in the shared unit, in catalog order and by name.
|
||||
*
|
||||
* @param list<int> $serviceUnitIds
|
||||
* @return list<WorkflowTemplateDetails>
|
||||
*/
|
||||
public function activeFor(array $serviceUnitIds): array;
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Modules\Catalog\Application\DTOs;
|
||||
|
||||
/**
|
||||
* A step of a workflow template as entered. A null work type means the template's own.
|
||||
*/
|
||||
final readonly class WorkflowStepData
|
||||
{
|
||||
public function __construct(
|
||||
public string $title,
|
||||
public ?string $description = null,
|
||||
public ?int $workTypeId = null,
|
||||
) {}
|
||||
|
||||
/**
|
||||
* @param array{title?: string, description?: string|null, work_type_id?: int|string|null} $input
|
||||
*/
|
||||
public static function fromArray(array $input): self
|
||||
{
|
||||
$workTypeId = $input['work_type_id'] ?? null;
|
||||
|
||||
return new self(
|
||||
(string) ($input['title'] ?? ''),
|
||||
$input['description'] ?? null,
|
||||
$workTypeId === null || $workTypeId === '' ? null : (int) $workTypeId,
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Modules\Catalog\Application\DTOs;
|
||||
|
||||
/**
|
||||
* A step of a workflow template. $workTypeId is the step's own work type
|
||||
* (null: the template's), $effectiveWorkTypeId the one its task gets.
|
||||
*/
|
||||
final readonly class WorkflowStepSummary
|
||||
{
|
||||
public function __construct(
|
||||
public string $title,
|
||||
public ?string $description,
|
||||
public ?int $workTypeId,
|
||||
public int $effectiveWorkTypeId,
|
||||
public string $workTypeName,
|
||||
) {}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Modules\Catalog\Application\DTOs;
|
||||
|
||||
/**
|
||||
* A workflow template as entered on its form.
|
||||
*/
|
||||
final readonly class WorkflowTemplateData
|
||||
{
|
||||
/**
|
||||
* @param list<WorkflowStepData> $steps
|
||||
*/
|
||||
public function __construct(
|
||||
public string $name,
|
||||
public int $workTypeId,
|
||||
public array $steps,
|
||||
) {}
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Modules\Catalog\Application\DTOs;
|
||||
|
||||
/**
|
||||
* A workflow template with its steps in order.
|
||||
*/
|
||||
final readonly class WorkflowTemplateDetails
|
||||
{
|
||||
/**
|
||||
* @param list<WorkflowStepSummary> $steps
|
||||
*/
|
||||
public function __construct(
|
||||
public int $id,
|
||||
public string $name,
|
||||
public int $workTypeId,
|
||||
public string $workTypeName,
|
||||
public int $serviceUnitId,
|
||||
public bool $isActive,
|
||||
public array $steps,
|
||||
) {}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Modules\Catalog\Application\DTOs;
|
||||
|
||||
/**
|
||||
* A workflow template on the template list.
|
||||
*/
|
||||
final readonly class WorkflowTemplateSummary
|
||||
{
|
||||
public function __construct(
|
||||
public int $id,
|
||||
public string $name,
|
||||
public int $workTypeId,
|
||||
public string $workTypeName,
|
||||
public int $serviceUnitId,
|
||||
public string $serviceUnitName,
|
||||
public bool $isActive,
|
||||
public int $stepCount,
|
||||
) {}
|
||||
}
|
||||
@@ -0,0 +1,83 @@
|
||||
<?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);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,64 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Modules\Catalog\Application\UseCases;
|
||||
|
||||
use App\Modules\Catalog\Application\DTOs\WorkflowTemplateData;
|
||||
use App\Modules\Catalog\Application\Services\WorkflowStepBuilder;
|
||||
use App\Modules\Catalog\Domain\Contracts\WorkflowTemplateRepository;
|
||||
use App\Modules\Catalog\Domain\Contracts\WorkTypeRepository;
|
||||
use App\Modules\Catalog\Domain\Entities\WorkflowTemplate;
|
||||
use App\Modules\Catalog\Domain\Enums\CatalogLevel;
|
||||
use App\Modules\Catalog\Domain\Exceptions\CatalogItemNotFound;
|
||||
use App\Modules\Catalog\Domain\Exceptions\InvalidCatalogName;
|
||||
use App\Modules\Catalog\Domain\Exceptions\InvalidWorkflowStepTitle;
|
||||
use App\Modules\Catalog\Domain\Exceptions\WorkflowStepWorkTypeNotAllowed;
|
||||
use App\Modules\Catalog\Domain\Exceptions\WorkflowTemplateNameTaken;
|
||||
use App\Modules\Catalog\Domain\Exceptions\WorkflowTemplateNeedsSteps;
|
||||
use App\Modules\Catalog\Domain\ValueObjects\CatalogName;
|
||||
use App\Modules\Shared\Application\Contracts\TransactionManager;
|
||||
|
||||
/**
|
||||
* Adds a workflow template to an active work type and returns its id.
|
||||
*/
|
||||
final class AddWorkflowTemplate
|
||||
{
|
||||
public function __construct(
|
||||
private WorkflowTemplateRepository $templates,
|
||||
private WorkTypeRepository $workTypes,
|
||||
private WorkflowStepBuilder $stepBuilder,
|
||||
private TransactionManager $transactions,
|
||||
) {}
|
||||
|
||||
/**
|
||||
* @throws CatalogItemNotFound when the work type is missing or not in use
|
||||
* @throws InvalidCatalogName
|
||||
* @throws InvalidWorkflowStepTitle
|
||||
* @throws WorkflowStepWorkTypeNotAllowed
|
||||
* @throws WorkflowTemplateNameTaken
|
||||
* @throws WorkflowTemplateNeedsSteps
|
||||
*/
|
||||
public function execute(WorkflowTemplateData $data): int
|
||||
{
|
||||
$name = new CatalogName($data->name);
|
||||
$steps = $this->stepBuilder->build($data->steps);
|
||||
$template = WorkflowTemplate::create($data->workTypeId, $name, $steps);
|
||||
|
||||
return $this->transactions->run(function () use ($data, $name, $steps, $template): int {
|
||||
$workType = $this->workTypes->findById($data->workTypeId);
|
||||
|
||||
if ($workType === null || ! $workType->isActive()) {
|
||||
throw new CatalogItemNotFound(CatalogLevel::WorkType, $data->workTypeId);
|
||||
}
|
||||
|
||||
if ($this->templates->isNameTaken($data->workTypeId, $name->value)) {
|
||||
throw new WorkflowTemplateNameTaken;
|
||||
}
|
||||
|
||||
$this->stepBuilder->checkWorkTypes($workType, $steps);
|
||||
|
||||
return (int) $this->templates->save($template)->id();
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Modules\Catalog\Application\UseCases;
|
||||
|
||||
use App\Modules\Catalog\Domain\Contracts\WorkflowTemplateRepository;
|
||||
use App\Modules\Catalog\Domain\Exceptions\WorkflowTemplateNotFound;
|
||||
use App\Modules\Shared\Application\Contracts\TransactionManager;
|
||||
|
||||
/**
|
||||
* Takes a workflow template out of use or back into use. A template out of
|
||||
* use is not offered on customer jobs.
|
||||
*/
|
||||
final class SetWorkflowTemplateActive
|
||||
{
|
||||
public function __construct(
|
||||
private WorkflowTemplateRepository $templates,
|
||||
private TransactionManager $transactions,
|
||||
) {}
|
||||
|
||||
/**
|
||||
* @throws WorkflowTemplateNotFound
|
||||
*/
|
||||
public function execute(int $templateId, bool $active): void
|
||||
{
|
||||
$this->transactions->run(function () use ($templateId, $active): void {
|
||||
$template = $this->templates->findById($templateId) ?? throw new WorkflowTemplateNotFound($templateId);
|
||||
|
||||
$active ? $template->activate() : $template->deactivate();
|
||||
$this->templates->save($template);
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,68 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Modules\Catalog\Application\UseCases;
|
||||
|
||||
use App\Modules\Catalog\Application\DTOs\WorkflowTemplateData;
|
||||
use App\Modules\Catalog\Application\Services\WorkflowStepBuilder;
|
||||
use App\Modules\Catalog\Domain\Contracts\WorkflowTemplateRepository;
|
||||
use App\Modules\Catalog\Domain\Contracts\WorkTypeRepository;
|
||||
use App\Modules\Catalog\Domain\Enums\CatalogLevel;
|
||||
use App\Modules\Catalog\Domain\Exceptions\CatalogItemNotFound;
|
||||
use App\Modules\Catalog\Domain\Exceptions\InvalidCatalogName;
|
||||
use App\Modules\Catalog\Domain\Exceptions\InvalidWorkflowStepTitle;
|
||||
use App\Modules\Catalog\Domain\Exceptions\WorkflowStepWorkTypeNotAllowed;
|
||||
use App\Modules\Catalog\Domain\Exceptions\WorkflowTemplateNameTaken;
|
||||
use App\Modules\Catalog\Domain\Exceptions\WorkflowTemplateNeedsSteps;
|
||||
use App\Modules\Catalog\Domain\Exceptions\WorkflowTemplateNotFound;
|
||||
use App\Modules\Catalog\Domain\ValueObjects\CatalogName;
|
||||
use App\Modules\Shared\Application\Contracts\TransactionManager;
|
||||
|
||||
/**
|
||||
* Saves a workflow template's name, work type and steps. Tasks already made
|
||||
* from the template do not change.
|
||||
*/
|
||||
final class UpdateWorkflowTemplate
|
||||
{
|
||||
public function __construct(
|
||||
private WorkflowTemplateRepository $templates,
|
||||
private WorkTypeRepository $workTypes,
|
||||
private WorkflowStepBuilder $stepBuilder,
|
||||
private TransactionManager $transactions,
|
||||
) {}
|
||||
|
||||
/**
|
||||
* @throws CatalogItemNotFound when a new work type is missing or not in use
|
||||
* @throws InvalidCatalogName
|
||||
* @throws InvalidWorkflowStepTitle
|
||||
* @throws WorkflowStepWorkTypeNotAllowed
|
||||
* @throws WorkflowTemplateNameTaken
|
||||
* @throws WorkflowTemplateNeedsSteps
|
||||
* @throws WorkflowTemplateNotFound
|
||||
*/
|
||||
public function execute(int $templateId, WorkflowTemplateData $data): void
|
||||
{
|
||||
$name = new CatalogName($data->name);
|
||||
$steps = $this->stepBuilder->build($data->steps);
|
||||
|
||||
$this->transactions->run(function () use ($templateId, $data, $name, $steps): void {
|
||||
$template = $this->templates->findById($templateId) ?? throw new WorkflowTemplateNotFound($templateId);
|
||||
$workType = $this->workTypes->findById($data->workTypeId);
|
||||
$workTypeChanged = $data->workTypeId !== $template->workTypeId();
|
||||
|
||||
if ($workType === null || ($workTypeChanged && ! $workType->isActive())) {
|
||||
throw new CatalogItemNotFound(CatalogLevel::WorkType, $data->workTypeId);
|
||||
}
|
||||
|
||||
if ($this->templates->isNameTaken($data->workTypeId, $name->value, $templateId)) {
|
||||
throw new WorkflowTemplateNameTaken;
|
||||
}
|
||||
|
||||
$this->stepBuilder->checkWorkTypes($workType, $steps, $template->steps());
|
||||
|
||||
$template->change($data->workTypeId, $name, $steps);
|
||||
$this->templates->save($template);
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Modules\Catalog\Domain\Contracts;
|
||||
|
||||
use App\Modules\Catalog\Domain\Entities\WorkflowTemplate;
|
||||
|
||||
interface WorkflowTemplateRepository
|
||||
{
|
||||
public function findById(int $id): ?WorkflowTemplate;
|
||||
|
||||
/**
|
||||
* Inserts a new template or updates a stored one, replacing its steps,
|
||||
* and returns the stored template.
|
||||
*/
|
||||
public function save(WorkflowTemplate $template): WorkflowTemplate;
|
||||
|
||||
/**
|
||||
* Whether another template of the work type than $exceptId has this name, ignoring case.
|
||||
*/
|
||||
public function isNameTaken(int $workTypeId, string $name, ?int $exceptId = null): bool;
|
||||
}
|
||||
@@ -0,0 +1,114 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Modules\Catalog\Domain\Entities;
|
||||
|
||||
use App\Modules\Catalog\Domain\Exceptions\WorkflowTemplateNeedsSteps;
|
||||
use App\Modules\Catalog\Domain\ValueObjects\CatalogName;
|
||||
use App\Modules\Catalog\Domain\ValueObjects\WorkflowStep;
|
||||
|
||||
/**
|
||||
* A workflow template (työnkulkupohja) of a work type, e.g. videokuvaus:
|
||||
* suunnittelu → kuvaus → editointi → tarkastus → toimitus. Its steps, in
|
||||
* order, become tasks of a customer job. A work type can have many
|
||||
* templates with different names. Templates are not deleted, only taken
|
||||
* out of use.
|
||||
*/
|
||||
final class WorkflowTemplate
|
||||
{
|
||||
/**
|
||||
* @param list<WorkflowStep> $steps
|
||||
*/
|
||||
private function __construct(
|
||||
private ?int $id,
|
||||
private int $workTypeId,
|
||||
private CatalogName $name,
|
||||
private bool $active,
|
||||
private array $steps,
|
||||
) {}
|
||||
|
||||
/**
|
||||
* @param list<WorkflowStep> $steps
|
||||
*
|
||||
* @throws WorkflowTemplateNeedsSteps
|
||||
*/
|
||||
public static function create(int $workTypeId, CatalogName $name, array $steps): self
|
||||
{
|
||||
self::ensureSteps($steps);
|
||||
|
||||
return new self(null, $workTypeId, $name, true, $steps);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param list<WorkflowStep> $steps
|
||||
*/
|
||||
public static function reconstitute(int $id, int $workTypeId, CatalogName $name, bool $active, array $steps): self
|
||||
{
|
||||
return new self($id, $workTypeId, $name, $active, $steps);
|
||||
}
|
||||
|
||||
public function id(): ?int
|
||||
{
|
||||
return $this->id;
|
||||
}
|
||||
|
||||
public function workTypeId(): int
|
||||
{
|
||||
return $this->workTypeId;
|
||||
}
|
||||
|
||||
public function name(): CatalogName
|
||||
{
|
||||
return $this->name;
|
||||
}
|
||||
|
||||
public function isActive(): bool
|
||||
{
|
||||
return $this->active;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return list<WorkflowStep>
|
||||
*/
|
||||
public function steps(): array
|
||||
{
|
||||
return $this->steps;
|
||||
}
|
||||
|
||||
/**
|
||||
* Changes the name, the work type and the steps at once, as the template form saves them.
|
||||
*
|
||||
* @param list<WorkflowStep> $steps
|
||||
*
|
||||
* @throws WorkflowTemplateNeedsSteps
|
||||
*/
|
||||
public function change(int $workTypeId, CatalogName $name, array $steps): void
|
||||
{
|
||||
self::ensureSteps($steps);
|
||||
|
||||
$this->workTypeId = $workTypeId;
|
||||
$this->name = $name;
|
||||
$this->steps = $steps;
|
||||
}
|
||||
|
||||
public function activate(): void
|
||||
{
|
||||
$this->active = true;
|
||||
}
|
||||
|
||||
public function deactivate(): void
|
||||
{
|
||||
$this->active = false;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param list<WorkflowStep> $steps
|
||||
*/
|
||||
private static function ensureSteps(array $steps): void
|
||||
{
|
||||
if ($steps === []) {
|
||||
throw new WorkflowTemplateNeedsSteps;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Modules\Catalog\Domain\Exceptions;
|
||||
|
||||
use App\Modules\Shared\Domain\Exceptions\DomainException;
|
||||
|
||||
/**
|
||||
* A step title is empty or longer than 255 characters. $stepIndex (from 0)
|
||||
* tells which step, when the caller knows it.
|
||||
*/
|
||||
final class InvalidWorkflowStepTitle extends DomainException
|
||||
{
|
||||
public function __construct(public readonly ?int $stepIndex = null)
|
||||
{
|
||||
parent::__construct('A workflow step title must be 1–255 characters long.');
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Modules\Catalog\Domain\Exceptions;
|
||||
|
||||
use App\Modules\Shared\Domain\Exceptions\DomainException;
|
||||
|
||||
/**
|
||||
* A step's own work type is not in use, or belongs neither to the template
|
||||
* work type's service unit nor to the shared unit. $stepIndex counts from 0.
|
||||
*/
|
||||
final class WorkflowStepWorkTypeNotAllowed extends DomainException
|
||||
{
|
||||
public function __construct(public readonly int $stepIndex)
|
||||
{
|
||||
parent::__construct("The work type of workflow step {$stepIndex} is not allowed.");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Modules\Catalog\Domain\Exceptions;
|
||||
|
||||
use App\Modules\Shared\Domain\Exceptions\DomainException;
|
||||
|
||||
final class WorkflowTemplateNameTaken extends DomainException
|
||||
{
|
||||
public function __construct()
|
||||
{
|
||||
parent::__construct('The work type already has a workflow template with this name.');
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Modules\Catalog\Domain\Exceptions;
|
||||
|
||||
use App\Modules\Shared\Domain\Exceptions\DomainException;
|
||||
|
||||
final class WorkflowTemplateNeedsSteps extends DomainException
|
||||
{
|
||||
public function __construct()
|
||||
{
|
||||
parent::__construct('A workflow template needs at least one step.');
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Modules\Catalog\Domain\Exceptions;
|
||||
|
||||
use App\Modules\Shared\Domain\Exceptions\DomainException;
|
||||
|
||||
final class WorkflowTemplateNotFound extends DomainException
|
||||
{
|
||||
public function __construct(public readonly int $id)
|
||||
{
|
||||
parent::__construct("Workflow template {$id} was not found.");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Modules\Catalog\Domain\ValueObjects;
|
||||
|
||||
/**
|
||||
* One step of a workflow template. A task is made of it: the title, the
|
||||
* description and the work type, which is the template's own when
|
||||
* $workTypeId is null.
|
||||
*/
|
||||
final readonly class WorkflowStep
|
||||
{
|
||||
public ?string $description;
|
||||
|
||||
public function __construct(
|
||||
public WorkflowStepTitle $title,
|
||||
?string $description = null,
|
||||
public ?int $workTypeId = null,
|
||||
) {
|
||||
$description = $description === null ? '' : trim($description);
|
||||
$this->description = $description === '' ? null : $description;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Modules\Catalog\Domain\ValueObjects;
|
||||
|
||||
use App\Modules\Catalog\Domain\Exceptions\InvalidWorkflowStepTitle;
|
||||
|
||||
/**
|
||||
* Title of a workflow step, e.g. "Editointi". It becomes a task title, so
|
||||
* its length limit is the same as a task title's.
|
||||
*/
|
||||
final readonly class WorkflowStepTitle
|
||||
{
|
||||
public const int MAX_LENGTH = 255;
|
||||
|
||||
public string $value;
|
||||
|
||||
public function __construct(string $value)
|
||||
{
|
||||
$value = trim(preg_replace('/\s+/u', ' ', $value) ?? $value);
|
||||
|
||||
if ($value === '' || mb_strlen($value) > self::MAX_LENGTH) {
|
||||
throw new InvalidWorkflowStepTitle;
|
||||
}
|
||||
|
||||
$this->value = $value;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
<?php
|
||||
|
||||
namespace App\Modules\Catalog\Infrastructure\Persistence\Eloquent;
|
||||
|
||||
use Illuminate\Database\Eloquent\Attributes\Fillable;
|
||||
use Illuminate\Database\Eloquent\Attributes\Table;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\HasMany;
|
||||
|
||||
/**
|
||||
* Persistence model, used only inside the Catalog Infrastructure layer.
|
||||
*
|
||||
* @property int $id
|
||||
* @property int $work_type_id
|
||||
* @property string $name
|
||||
* @property bool $is_active
|
||||
*/
|
||||
#[Table('workflow_templates')]
|
||||
#[Fillable(['work_type_id', 'name', 'is_active'])]
|
||||
class WorkflowTemplateModel extends Model
|
||||
{
|
||||
/**
|
||||
* @return HasMany<WorkflowTemplateStepModel, $this>
|
||||
*/
|
||||
public function steps(): HasMany
|
||||
{
|
||||
return $this->hasMany(WorkflowTemplateStepModel::class, 'workflow_template_id')->orderBy('sort_order');
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<string, string>
|
||||
*/
|
||||
protected function casts(): array
|
||||
{
|
||||
return [
|
||||
'work_type_id' => 'integer',
|
||||
'is_active' => 'boolean',
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
<?php
|
||||
|
||||
namespace App\Modules\Catalog\Infrastructure\Persistence\Eloquent;
|
||||
|
||||
use Illuminate\Database\Eloquent\Attributes\Fillable;
|
||||
use Illuminate\Database\Eloquent\Attributes\Table;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
/**
|
||||
* Persistence model, used only inside the Catalog Infrastructure layer.
|
||||
*
|
||||
* @property int $id
|
||||
* @property int $workflow_template_id
|
||||
* @property int|null $work_type_id
|
||||
* @property string $title
|
||||
* @property string|null $description
|
||||
* @property int $sort_order
|
||||
*/
|
||||
#[Table('workflow_template_steps')]
|
||||
#[Fillable(['workflow_template_id', 'work_type_id', 'title', 'description', 'sort_order'])]
|
||||
class WorkflowTemplateStepModel extends Model
|
||||
{
|
||||
/**
|
||||
* @return array<string, string>
|
||||
*/
|
||||
protected function casts(): array
|
||||
{
|
||||
return [
|
||||
'workflow_template_id' => 'integer',
|
||||
'work_type_id' => 'integer',
|
||||
'sort_order' => 'integer',
|
||||
];
|
||||
}
|
||||
}
|
||||
+128
@@ -0,0 +1,128 @@
|
||||
<?php
|
||||
|
||||
namespace App\Modules\Catalog\Infrastructure\Persistence\Repositories;
|
||||
|
||||
use App\Modules\Catalog\Application\Contracts\WorkflowTemplateQueries;
|
||||
use App\Modules\Catalog\Application\DTOs\WorkflowStepSummary;
|
||||
use App\Modules\Catalog\Application\DTOs\WorkflowTemplateDetails;
|
||||
use App\Modules\Catalog\Application\DTOs\WorkflowTemplateSummary;
|
||||
use App\Modules\Catalog\Infrastructure\Persistence\Eloquent\WorkflowTemplateModel;
|
||||
use App\Modules\Catalog\Infrastructure\Persistence\Eloquent\WorkflowTemplateStepModel;
|
||||
use App\Modules\Catalog\Infrastructure\Persistence\Eloquent\WorkTypeModel;
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
use Illuminate\Database\Eloquent\Collection;
|
||||
|
||||
class EloquentWorkflowTemplateQueries implements WorkflowTemplateQueries
|
||||
{
|
||||
public function all(): array
|
||||
{
|
||||
return $this->ordered()
|
||||
->withCount('steps')
|
||||
->get()
|
||||
->map(fn (WorkflowTemplateModel $model): WorkflowTemplateSummary => new WorkflowTemplateSummary(
|
||||
$model->id,
|
||||
$model->name,
|
||||
$model->work_type_id,
|
||||
(string) $model->getAttribute('work_type_name'),
|
||||
(int) $model->getAttribute('service_unit_id'),
|
||||
(string) $model->getAttribute('service_unit_name'),
|
||||
$model->is_active,
|
||||
(int) $model->getAttribute('steps_count'),
|
||||
))
|
||||
->values()
|
||||
->all();
|
||||
}
|
||||
|
||||
public function find(int $id): ?WorkflowTemplateDetails
|
||||
{
|
||||
$models = $this->ordered()->whereKey($id)->get();
|
||||
|
||||
return $this->toDetails($models)[0] ?? null;
|
||||
}
|
||||
|
||||
public function activeFor(array $serviceUnitIds): array
|
||||
{
|
||||
$models = $this->ordered()
|
||||
->where('workflow_templates.is_active', true)
|
||||
->where('work_types.is_active', true)
|
||||
->where('service_units.is_active', true)
|
||||
->where(fn (Builder $query) => $query->whereNull('services.id')->orWhere('services.is_active', true))
|
||||
->where(fn (Builder $query) => $query
|
||||
->whereIn('service_units.id', $serviceUnitIds)
|
||||
->orWhere('service_units.is_shared', true))
|
||||
->get();
|
||||
|
||||
return $this->toDetails($models);
|
||||
}
|
||||
|
||||
/**
|
||||
* Templates with their work type and unit, in catalog order: units (the
|
||||
* shared one last), services, work types under no service last, then by name.
|
||||
*
|
||||
* @return Builder<WorkflowTemplateModel>
|
||||
*/
|
||||
private function ordered(): Builder
|
||||
{
|
||||
return WorkflowTemplateModel::query()
|
||||
->join('work_types', 'work_types.id', '=', 'workflow_templates.work_type_id')
|
||||
->join('service_units', 'service_units.id', '=', 'work_types.service_unit_id')
|
||||
->leftJoin('services', 'services.id', '=', 'work_types.service_id')
|
||||
->select('workflow_templates.*')
|
||||
->addSelect([
|
||||
'work_types.name as work_type_name',
|
||||
'service_units.id as service_unit_id',
|
||||
'service_units.name as service_unit_name',
|
||||
])
|
||||
->orderBy('service_units.is_shared')
|
||||
->orderBy('service_units.sort_order')
|
||||
->orderByRaw('services.sort_order IS NULL')
|
||||
->orderBy('services.sort_order')
|
||||
->orderBy('work_types.sort_order')
|
||||
->orderBy('workflow_templates.name')
|
||||
->orderBy('workflow_templates.id');
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Collection<int, WorkflowTemplateModel> $models
|
||||
* @return list<WorkflowTemplateDetails>
|
||||
*/
|
||||
private function toDetails(Collection $models): array
|
||||
{
|
||||
if ($models->isEmpty()) {
|
||||
return [];
|
||||
}
|
||||
|
||||
$steps = WorkflowTemplateStepModel::query()
|
||||
->whereIn('workflow_template_id', $models->pluck('id'))
|
||||
->orderBy('sort_order')
|
||||
->get()
|
||||
->groupBy('workflow_template_id');
|
||||
|
||||
$workTypeNames = WorkTypeModel::query()
|
||||
->whereIn('id', $steps->flatten(1)->pluck('work_type_id')->filter()->unique())
|
||||
->pluck('name', 'id');
|
||||
|
||||
return $models->map(function (WorkflowTemplateModel $model) use ($steps, $workTypeNames): WorkflowTemplateDetails {
|
||||
$workTypeName = (string) $model->getAttribute('work_type_name');
|
||||
|
||||
return new WorkflowTemplateDetails(
|
||||
$model->id,
|
||||
$model->name,
|
||||
$model->work_type_id,
|
||||
$workTypeName,
|
||||
(int) $model->getAttribute('service_unit_id'),
|
||||
$model->is_active,
|
||||
($steps->get($model->id) ?? collect())
|
||||
->map(fn (WorkflowTemplateStepModel $step): WorkflowStepSummary => new WorkflowStepSummary(
|
||||
$step->title,
|
||||
$step->description,
|
||||
$step->work_type_id,
|
||||
$step->work_type_id ?? $model->work_type_id,
|
||||
$step->work_type_id === null ? $workTypeName : (string) $workTypeNames->get($step->work_type_id),
|
||||
))
|
||||
->values()
|
||||
->all(),
|
||||
);
|
||||
})->values()->all();
|
||||
}
|
||||
}
|
||||
+74
@@ -0,0 +1,74 @@
|
||||
<?php
|
||||
|
||||
namespace App\Modules\Catalog\Infrastructure\Persistence\Repositories;
|
||||
|
||||
use App\Modules\Catalog\Domain\Contracts\WorkflowTemplateRepository;
|
||||
use App\Modules\Catalog\Domain\Entities\WorkflowTemplate;
|
||||
use App\Modules\Catalog\Domain\ValueObjects\CatalogName;
|
||||
use App\Modules\Catalog\Domain\ValueObjects\WorkflowStep;
|
||||
use App\Modules\Catalog\Domain\ValueObjects\WorkflowStepTitle;
|
||||
use App\Modules\Catalog\Infrastructure\Persistence\Eloquent\WorkflowTemplateModel;
|
||||
use App\Modules\Catalog\Infrastructure\Persistence\Eloquent\WorkflowTemplateStepModel;
|
||||
|
||||
class EloquentWorkflowTemplateRepository implements WorkflowTemplateRepository
|
||||
{
|
||||
public function findById(int $id): ?WorkflowTemplate
|
||||
{
|
||||
$model = WorkflowTemplateModel::query()->with('steps')->find($id);
|
||||
|
||||
return $model === null ? null : $this->toEntity($model);
|
||||
}
|
||||
|
||||
public function save(WorkflowTemplate $template): WorkflowTemplate
|
||||
{
|
||||
$model = $template->id() === null ? new WorkflowTemplateModel : WorkflowTemplateModel::query()->findOrFail($template->id());
|
||||
|
||||
$model->fill([
|
||||
'work_type_id' => $template->workTypeId(),
|
||||
'name' => $template->name()->value,
|
||||
'is_active' => $template->isActive(),
|
||||
])->save();
|
||||
|
||||
// Steps have no identity of their own: the saved list replaces the old one.
|
||||
WorkflowTemplateStepModel::query()->where('workflow_template_id', $model->id)->delete();
|
||||
|
||||
foreach ($template->steps() as $index => $step) {
|
||||
WorkflowTemplateStepModel::query()->create([
|
||||
'workflow_template_id' => $model->id,
|
||||
'work_type_id' => $step->workTypeId,
|
||||
'title' => $step->title->value,
|
||||
'description' => $step->description,
|
||||
'sort_order' => $index + 1,
|
||||
]);
|
||||
}
|
||||
|
||||
return $this->toEntity($model->load('steps'));
|
||||
}
|
||||
|
||||
public function isNameTaken(int $workTypeId, string $name, ?int $exceptId = null): bool
|
||||
{
|
||||
return WorkflowTemplateModel::query()
|
||||
->where('work_type_id', $workTypeId)
|
||||
->whereRaw('LOWER(name) = ?', [mb_strtolower($name)])
|
||||
->when($exceptId !== null, fn ($query) => $query->whereKeyNot($exceptId))
|
||||
->exists();
|
||||
}
|
||||
|
||||
private function toEntity(WorkflowTemplateModel $model): WorkflowTemplate
|
||||
{
|
||||
return WorkflowTemplate::reconstitute(
|
||||
$model->id,
|
||||
$model->work_type_id,
|
||||
new CatalogName($model->name),
|
||||
$model->is_active,
|
||||
$model->steps
|
||||
->map(fn (WorkflowTemplateStepModel $step): WorkflowStep => new WorkflowStep(
|
||||
new WorkflowStepTitle($step->title),
|
||||
$step->description,
|
||||
$step->work_type_id,
|
||||
))
|
||||
->values()
|
||||
->all(),
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -5,10 +5,12 @@ namespace App\Modules\Catalog\Infrastructure\Providers;
|
||||
use App\Models\User;
|
||||
use App\Modules\Catalog\Application\Contracts\CatalogQueries;
|
||||
use App\Modules\Catalog\Application\Contracts\PriceListQueries;
|
||||
use App\Modules\Catalog\Application\Contracts\WorkflowTemplateQueries;
|
||||
use App\Modules\Catalog\Domain\Contracts\ProductRepository;
|
||||
use App\Modules\Catalog\Domain\Contracts\ServiceRepository;
|
||||
use App\Modules\Catalog\Domain\Contracts\ServiceUnitRepository;
|
||||
use App\Modules\Catalog\Domain\Contracts\VatRateRepository;
|
||||
use App\Modules\Catalog\Domain\Contracts\WorkflowTemplateRepository;
|
||||
use App\Modules\Catalog\Domain\Contracts\WorkTypeRepository;
|
||||
use App\Modules\Catalog\Infrastructure\Persistence\Repositories\EloquentCatalogQueries;
|
||||
use App\Modules\Catalog\Infrastructure\Persistence\Repositories\EloquentPriceListQueries;
|
||||
@@ -16,6 +18,8 @@ use App\Modules\Catalog\Infrastructure\Persistence\Repositories\EloquentProductR
|
||||
use App\Modules\Catalog\Infrastructure\Persistence\Repositories\EloquentServiceRepository;
|
||||
use App\Modules\Catalog\Infrastructure\Persistence\Repositories\EloquentServiceUnitRepository;
|
||||
use App\Modules\Catalog\Infrastructure\Persistence\Repositories\EloquentVatRateRepository;
|
||||
use App\Modules\Catalog\Infrastructure\Persistence\Repositories\EloquentWorkflowTemplateQueries;
|
||||
use App\Modules\Catalog\Infrastructure\Persistence\Repositories\EloquentWorkflowTemplateRepository;
|
||||
use App\Modules\Catalog\Infrastructure\Persistence\Repositories\EloquentWorkTypeRepository;
|
||||
use App\Modules\Users\Domain\Enums\Role;
|
||||
use Illuminate\Support\Facades\Gate;
|
||||
@@ -40,6 +44,8 @@ class CatalogServiceProvider extends ServiceProvider
|
||||
ProductRepository::class => EloquentProductRepository::class,
|
||||
PriceListQueries::class => EloquentPriceListQueries::class,
|
||||
VatRateRepository::class => EloquentVatRateRepository::class,
|
||||
WorkflowTemplateRepository::class => EloquentWorkflowTemplateRepository::class,
|
||||
WorkflowTemplateQueries::class => EloquentWorkflowTemplateQueries::class,
|
||||
];
|
||||
|
||||
public function boot(): void
|
||||
|
||||
@@ -0,0 +1,103 @@
|
||||
<?php
|
||||
|
||||
namespace App\Modules\Catalog\Presentation\Livewire\Forms;
|
||||
|
||||
use App\Modules\Catalog\Application\DTOs\WorkflowStepData;
|
||||
use App\Modules\Catalog\Application\DTOs\WorkflowTemplateData;
|
||||
use App\Modules\Catalog\Domain\Exceptions\CatalogItemNotFound;
|
||||
use App\Modules\Catalog\Domain\Exceptions\InvalidCatalogName;
|
||||
use App\Modules\Catalog\Domain\Exceptions\InvalidWorkflowStepTitle;
|
||||
use App\Modules\Catalog\Domain\Exceptions\WorkflowStepWorkTypeNotAllowed;
|
||||
use App\Modules\Catalog\Domain\Exceptions\WorkflowTemplateNameTaken;
|
||||
use App\Modules\Catalog\Domain\Exceptions\WorkflowTemplateNeedsSteps;
|
||||
use App\Modules\Shared\Domain\Exceptions\DomainException;
|
||||
use Livewire\Form;
|
||||
|
||||
/**
|
||||
* Name, work type and steps of a workflow template. Each step has a `key`
|
||||
* that stays the same while steps are moved, for wire:key and element ids.
|
||||
*/
|
||||
class WorkflowTemplateForm extends Form
|
||||
{
|
||||
public string $name = '';
|
||||
|
||||
public string $work_type_id = '';
|
||||
|
||||
/**
|
||||
* @var list<array{key: int, title: string, work_type_id: string, description: string}>
|
||||
*/
|
||||
public array $steps = [];
|
||||
|
||||
public int $nextKey = 1;
|
||||
|
||||
public function addStep(string $title = '', string $workTypeId = '', string $description = ''): int
|
||||
{
|
||||
$key = $this->nextKey++;
|
||||
$this->steps[] = ['key' => $key, 'title' => $title, 'work_type_id' => $workTypeId, 'description' => $description];
|
||||
|
||||
return $key;
|
||||
}
|
||||
|
||||
public function toData(): WorkflowTemplateData
|
||||
{
|
||||
$this->validate();
|
||||
|
||||
return new WorkflowTemplateData(
|
||||
$this->name,
|
||||
(int) $this->work_type_id,
|
||||
array_map(fn (array $step): WorkflowStepData => WorkflowStepData::fromArray($step), $this->steps),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Shows a broken business rule as an error on the field it concerns.
|
||||
*/
|
||||
public function showDomainError(DomainException $exception): void
|
||||
{
|
||||
[$field, $key] = match (true) {
|
||||
$exception instanceof InvalidCatalogName => ['name', 'catalog.errors.invalid_name'],
|
||||
$exception instanceof WorkflowTemplateNameTaken => ['name', 'catalog.workflows.errors.name_taken'],
|
||||
$exception instanceof CatalogItemNotFound => ['work_type_id', 'catalog.errors.work_type_unavailable'],
|
||||
$exception instanceof WorkflowTemplateNeedsSteps => ['steps', 'catalog.workflows.errors.steps_required'],
|
||||
$exception instanceof InvalidWorkflowStepTitle => ['steps.'.($exception->stepIndex ?? 0).'.title', 'catalog.workflows.errors.invalid_step_title'],
|
||||
$exception instanceof WorkflowStepWorkTypeNotAllowed => ['steps.'.$exception->stepIndex.'.work_type_id', 'catalog.workflows.errors.step_work_type_not_allowed'],
|
||||
default => throw $exception,
|
||||
};
|
||||
|
||||
$this->addError($field, __($key));
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<string, array<int, string>>
|
||||
*/
|
||||
protected function rules(): array
|
||||
{
|
||||
return [
|
||||
'name' => ['required', 'string', 'max:100'],
|
||||
'work_type_id' => ['required', 'integer'],
|
||||
'steps' => ['required', 'array', 'min:1'],
|
||||
'steps.*.title' => ['required', 'string', 'max:255'],
|
||||
'steps.*.work_type_id' => ['nullable', 'integer'],
|
||||
'steps.*.description' => ['nullable', 'string', 'max:5000'],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<string, string>
|
||||
*/
|
||||
protected function messages(): array
|
||||
{
|
||||
return [
|
||||
'name.required' => __('catalog.errors.name_required'),
|
||||
'name.max' => __('catalog.errors.invalid_name'),
|
||||
'work_type_id.required' => __('catalog.workflows.errors.work_type_required'),
|
||||
'work_type_id.integer' => __('catalog.errors.work_type_unavailable'),
|
||||
'steps.required' => __('catalog.workflows.errors.steps_required'),
|
||||
'steps.min' => __('catalog.workflows.errors.steps_required'),
|
||||
'steps.*.title.required' => __('catalog.workflows.errors.step_title_required'),
|
||||
'steps.*.title.max' => __('catalog.workflows.errors.invalid_step_title'),
|
||||
'steps.*.work_type_id.integer' => __('catalog.workflows.errors.step_work_type_not_allowed'),
|
||||
'steps.*.description.max' => __('catalog.workflows.errors.description_too_long'),
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -122,6 +122,7 @@ new class extends Component
|
||||
<div class="space-y-1 text-sm">
|
||||
<p class="text-zinc-600 dark:text-zinc-300">{{ __('catalog.intro') }}</p>
|
||||
<p><x-link :href="route('catalog.prices')" navigate>{{ __('catalog.prices_link') }}</x-link></p>
|
||||
<p><x-link :href="route('catalog.workflow-templates')" navigate>{{ __('catalog.workflows_link') }}</x-link></p>
|
||||
</div>
|
||||
|
||||
@if (session('status'))
|
||||
|
||||
@@ -0,0 +1,353 @@
|
||||
<?php
|
||||
|
||||
use App\Modules\Catalog\Application\Contracts\CatalogQueries;
|
||||
use App\Modules\Catalog\Application\Contracts\WorkflowTemplateQueries;
|
||||
use App\Modules\Catalog\Application\DTOs\WorkTypeSummary;
|
||||
use App\Modules\Catalog\Application\UseCases\AddWorkflowTemplate;
|
||||
use App\Modules\Catalog\Application\UseCases\UpdateWorkflowTemplate;
|
||||
use App\Modules\Catalog\Domain\Exceptions\WorkflowTemplateNotFound;
|
||||
use App\Modules\Catalog\Presentation\Livewire\Forms\WorkflowTemplateForm;
|
||||
use App\Modules\Shared\Domain\Exceptions\DomainException;
|
||||
use Livewire\Attributes\Computed;
|
||||
use Livewire\Attributes\Locked;
|
||||
use Livewire\Component;
|
||||
|
||||
/**
|
||||
* Add page and edit page (with {template}) of a workflow template: its name,
|
||||
* work type and steps. Steps are added, moved and removed with buttons, and
|
||||
* everything is saved at once.
|
||||
*/
|
||||
new class extends Component
|
||||
{
|
||||
#[Locked]
|
||||
public ?int $templateId = null;
|
||||
|
||||
/**
|
||||
* Work types the stored template uses, offered even if out of use.
|
||||
*
|
||||
* @var list<int>
|
||||
*/
|
||||
#[Locked]
|
||||
public array $storedWorkTypeIds = [];
|
||||
|
||||
public WorkflowTemplateForm $form;
|
||||
|
||||
public string $statusMessage = '';
|
||||
|
||||
public function mount(WorkflowTemplateQueries $templates, ?int $template = null): void
|
||||
{
|
||||
$this->authorize('catalog.manage');
|
||||
|
||||
if ($template === null) {
|
||||
$this->form->addStep();
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
$details = $templates->find($template);
|
||||
|
||||
abort_if($details === null, 404);
|
||||
|
||||
$this->templateId = $details->id;
|
||||
$this->form->name = $details->name;
|
||||
$this->form->work_type_id = (string) $details->workTypeId;
|
||||
$this->storedWorkTypeIds = [$details->workTypeId];
|
||||
|
||||
foreach ($details->steps as $step) {
|
||||
$this->form->addStep($step->title, $step->workTypeId === null ? '' : (string) $step->workTypeId, $step->description ?? '');
|
||||
|
||||
if ($step->workTypeId !== null) {
|
||||
$this->storedWorkTypeIds[] = $step->workTypeId;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Work types for the template, as optgroups "Unit" or "Unit – Service".
|
||||
*
|
||||
* @return array<string, list<WorkTypeSummary>>
|
||||
*/
|
||||
#[Computed]
|
||||
public function workTypeGroups(): array
|
||||
{
|
||||
return $this->groups(app(CatalogQueries::class)->tree(includeInactive: false));
|
||||
}
|
||||
|
||||
/**
|
||||
* Work types a step can have: those of the template work type's unit and the shared ones.
|
||||
*
|
||||
* @return array<string, list<WorkTypeSummary>>
|
||||
*/
|
||||
#[Computed]
|
||||
public function stepWorkTypeGroups(): array
|
||||
{
|
||||
$catalog = app(CatalogQueries::class);
|
||||
$workType = $this->form->work_type_id === '' ? null : $catalog->findWorkType((int) $this->form->work_type_id);
|
||||
|
||||
return $workType === null ? [] : $this->groups($catalog->activeWorkTypesFor([$workType->serviceUnitId]));
|
||||
}
|
||||
|
||||
public function addStep(): void
|
||||
{
|
||||
$this->authorize('catalog.manage');
|
||||
|
||||
$key = $this->form->addStep();
|
||||
|
||||
$this->statusMessage = __('catalog.workflows.status.step_added', ['number' => count($this->form->steps)]);
|
||||
$this->js("document.getElementById('step-{$key}-title')?.focus()");
|
||||
}
|
||||
|
||||
public function moveStep(int $index, string $direction): void
|
||||
{
|
||||
$this->authorize('catalog.manage');
|
||||
|
||||
$steps = array_values($this->form->steps);
|
||||
$target = $direction === 'up' ? $index - 1 : $index + 1;
|
||||
|
||||
if (! isset($steps[$index])) {
|
||||
return;
|
||||
}
|
||||
|
||||
$key = $steps[$index]['key'];
|
||||
|
||||
if (! isset($steps[$target])) {
|
||||
$this->statusMessage = __($direction === 'up' ? 'catalog.workflows.status.already_first' : 'catalog.workflows.status.already_last', ['number' => $index + 1]);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
[$steps[$index], $steps[$target]] = [$steps[$target], $steps[$index]];
|
||||
$this->form->steps = $steps;
|
||||
$this->resetErrorBag();
|
||||
|
||||
$this->statusMessage = __('catalog.workflows.status.step_moved', ['from' => $index + 1, 'to' => $target + 1]);
|
||||
$this->js("document.getElementById('step-{$key}-{$direction}')?.focus()");
|
||||
}
|
||||
|
||||
public function removeStep(int $index): void
|
||||
{
|
||||
$this->authorize('catalog.manage');
|
||||
|
||||
$steps = array_values($this->form->steps);
|
||||
|
||||
if (! isset($steps[$index]) || count($steps) === 1) {
|
||||
return;
|
||||
}
|
||||
|
||||
array_splice($steps, $index, 1);
|
||||
$this->form->steps = $steps;
|
||||
$this->resetErrorBag();
|
||||
|
||||
$this->statusMessage = __('catalog.workflows.status.step_removed', ['number' => $index + 1]);
|
||||
|
||||
// Focus the step that took its place, the new last one, or the add button.
|
||||
$next = $steps[$index] ?? $steps[count($steps) - 1];
|
||||
$this->js(count($steps) > 1
|
||||
? "document.getElementById('step-{$next['key']}-remove')?.focus()"
|
||||
: "document.getElementById('add-step')?.focus()");
|
||||
}
|
||||
|
||||
public function save(AddWorkflowTemplate $add, UpdateWorkflowTemplate $update): void
|
||||
{
|
||||
$this->authorize('catalog.manage');
|
||||
|
||||
$data = $this->form->toData();
|
||||
|
||||
try {
|
||||
if ($this->templateId === null) {
|
||||
$add->execute($data);
|
||||
session()->flash('status', __('catalog.status.created', ['name' => trim($data->name)]));
|
||||
} else {
|
||||
$update->execute($this->templateId, $data);
|
||||
session()->flash('status', __('catalog.status.updated', ['name' => trim($data->name)]));
|
||||
}
|
||||
} catch (WorkflowTemplateNotFound) {
|
||||
abort(404);
|
||||
} catch (DomainException $exception) {
|
||||
$this->form->showDomainError($exception);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
$this->redirectRoute('catalog.workflow-templates', navigate: true);
|
||||
}
|
||||
|
||||
public function heading(): string
|
||||
{
|
||||
return __($this->templateId === null ? 'catalog.workflows.create_title' : 'catalog.workflows.edit_title');
|
||||
}
|
||||
|
||||
public function render(): mixed
|
||||
{
|
||||
return $this->view()->title($this->heading());
|
||||
}
|
||||
|
||||
/**
|
||||
* @param list<\App\Modules\Catalog\Application\DTOs\ServiceUnitNode> $tree
|
||||
* @return array<string, list<WorkTypeSummary>>
|
||||
*/
|
||||
private function groups(array $tree): array
|
||||
{
|
||||
$groups = [];
|
||||
$offered = [];
|
||||
|
||||
foreach ($tree as $node) {
|
||||
foreach ($node->services as $serviceNode) {
|
||||
if ($serviceNode->workTypes !== []) {
|
||||
$groups[$node->serviceUnit->name.' – '.$serviceNode->service->name] = $serviceNode->workTypes;
|
||||
}
|
||||
}
|
||||
|
||||
if ($node->workTypes !== []) {
|
||||
$groups[$node->serviceUnit->name] = $node->workTypes;
|
||||
}
|
||||
}
|
||||
|
||||
foreach ($groups as $workTypes) {
|
||||
foreach ($workTypes as $workType) {
|
||||
$offered[] = $workType->id;
|
||||
}
|
||||
}
|
||||
|
||||
// Keep the stored choices, even when they are out of use now.
|
||||
$catalog = app(CatalogQueries::class);
|
||||
$inactive = [];
|
||||
|
||||
foreach (array_unique($this->storedWorkTypeIds) as $id) {
|
||||
if (! in_array($id, $offered, true) && ($workType = $catalog->findWorkType($id)) !== null) {
|
||||
$inactive[] = $workType;
|
||||
}
|
||||
}
|
||||
|
||||
if ($inactive !== []) {
|
||||
$groups[__('catalog.inactive')] = $inactive;
|
||||
}
|
||||
|
||||
return $groups;
|
||||
}
|
||||
};
|
||||
?>
|
||||
|
||||
<div class="mx-auto max-w-3xl space-y-6 p-4 sm:p-6">
|
||||
<h1 class="text-2xl font-semibold">{{ $this->heading() }}</h1>
|
||||
|
||||
<form wire:submit="save" class="space-y-6" novalidate>
|
||||
<x-input wire:model="form.name" :label="__('catalog.fields.name')" :hint="__('catalog.workflows.name_help')" 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.live="form.work_type_id"
|
||||
:label="__('catalog.workflows.work_type')"
|
||||
:aria-label="__('catalog.workflows.work_type')"
|
||||
:hint="__('catalog.workflows.work_type_help')"
|
||||
required
|
||||
>
|
||||
<option value="">{{ __('catalog.workflows.choose_work_type') }}</option>
|
||||
@foreach ($this->workTypeGroups as $group => $workTypes)
|
||||
<optgroup label="{{ $group }}">
|
||||
@foreach ($workTypes as $workType)
|
||||
<option wire:key="work-type-option-{{ $workType->id }}" value="{{ $workType->id }}">{{ $workType->name }}</option>
|
||||
@endforeach
|
||||
</optgroup>
|
||||
@endforeach
|
||||
</x-select.native>
|
||||
|
||||
<section aria-labelledby="steps-heading" class="space-y-4">
|
||||
<div class="space-y-1">
|
||||
<h2 id="steps-heading" class="text-xl font-semibold">{{ __('catalog.workflows.steps') }}</h2>
|
||||
<p class="text-sm text-zinc-600 dark:text-zinc-300">{{ __('catalog.workflows.steps_help') }}</p>
|
||||
</div>
|
||||
|
||||
<p role="status" aria-live="polite" class="text-sm">{{ $statusMessage }}</p>
|
||||
|
||||
@error('form.steps')
|
||||
<p role="alert" class="text-sm text-red-700 dark:text-red-400">{{ $message }}</p>
|
||||
@enderror
|
||||
|
||||
<ol class="space-y-4">
|
||||
@foreach ($form->steps as $index => $step)
|
||||
{{-- The index is part of the key: a moved step is rendered anew, so its fields bind to their new position. --}}
|
||||
<li wire:key="step-{{ $step['key'] }}-{{ $index }}">
|
||||
<fieldset class="space-y-4 rounded-lg border border-zinc-200 p-4 dark:border-zinc-700">
|
||||
<legend class="px-1 font-medium">{{ __('catalog.workflows.step_legend', ['number' => $index + 1]) }}</legend>
|
||||
|
||||
<x-input
|
||||
id="step-{{ $step['key'] }}-title"
|
||||
wire:model="form.steps.{{ $index }}.title"
|
||||
:label="__('catalog.workflows.step_title')"
|
||||
autocomplete="off"
|
||||
required
|
||||
/>
|
||||
|
||||
<x-select.native
|
||||
id="step-{{ $step['key'] }}-work-type"
|
||||
wire:model="form.steps.{{ $index }}.work_type_id"
|
||||
:label="__('catalog.workflows.step_work_type')"
|
||||
:aria-label="__('catalog.workflows.step_work_type_named', ['number' => $index + 1])"
|
||||
:hint="__('catalog.workflows.step_work_type_help')"
|
||||
>
|
||||
<option value="">{{ __('catalog.workflows.template_work_type') }}</option>
|
||||
@foreach ($this->stepWorkTypeGroups as $group => $workTypes)
|
||||
<optgroup label="{{ $group }}">
|
||||
@foreach ($workTypes as $workType)
|
||||
<option value="{{ $workType->id }}">{{ $workType->name }}</option>
|
||||
@endforeach
|
||||
</optgroup>
|
||||
@endforeach
|
||||
</x-select.native>
|
||||
|
||||
<x-textarea
|
||||
id="step-{{ $step['key'] }}-description"
|
||||
wire:model="form.steps.{{ $index }}.description"
|
||||
:label="__('catalog.workflows.step_description')"
|
||||
resize-auto
|
||||
/>
|
||||
|
||||
<div class="flex flex-wrap gap-2">
|
||||
<x-button
|
||||
id="step-{{ $step['key'] }}-up"
|
||||
wire:click="moveStep({{ $index }}, 'up')"
|
||||
subtle
|
||||
color="black"
|
||||
sm
|
||||
icon="arrow-up"
|
||||
:text="__('catalog.workflows.move_up')"
|
||||
:aria-label="__('catalog.workflows.move_up_named', ['number' => $index + 1])"
|
||||
/>
|
||||
<x-button
|
||||
id="step-{{ $step['key'] }}-down"
|
||||
wire:click="moveStep({{ $index }}, 'down')"
|
||||
subtle
|
||||
color="black"
|
||||
sm
|
||||
icon="arrow-down"
|
||||
:text="__('catalog.workflows.move_down')"
|
||||
:aria-label="__('catalog.workflows.move_down_named', ['number' => $index + 1])"
|
||||
/>
|
||||
@if (count($form->steps) > 1)
|
||||
<x-button
|
||||
id="step-{{ $step['key'] }}-remove"
|
||||
wire:click="removeStep({{ $index }})"
|
||||
subtle
|
||||
color="black"
|
||||
sm
|
||||
icon="trash"
|
||||
:text="__('catalog.workflows.remove_step')"
|
||||
:aria-label="__('catalog.workflows.remove_step_named', ['number' => $index + 1])"
|
||||
/>
|
||||
@endif
|
||||
</div>
|
||||
</fieldset>
|
||||
</li>
|
||||
@endforeach
|
||||
</ol>
|
||||
|
||||
<x-button id="add-step" wire:click="addStep" subtle color="black" icon="plus" :text="__('catalog.workflows.add_step')" />
|
||||
</section>
|
||||
|
||||
<div class="flex flex-wrap gap-3">
|
||||
<x-button submit :text="__('catalog.save')" />
|
||||
<x-button :href="route('catalog.workflow-templates')" wire:navigate subtle color="black" :text="__('catalog.cancel')" />
|
||||
</div>
|
||||
</form>
|
||||
</div>
|
||||
@@ -0,0 +1,152 @@
|
||||
<?php
|
||||
|
||||
use App\Modules\Catalog\Application\Contracts\WorkflowTemplateQueries;
|
||||
use App\Modules\Catalog\Application\DTOs\WorkflowTemplateSummary;
|
||||
use App\Modules\Catalog\Application\UseCases\SetWorkflowTemplateActive;
|
||||
use App\Modules\Catalog\Domain\Exceptions\WorkflowTemplateNotFound;
|
||||
use Livewire\Attributes\Computed;
|
||||
use Livewire\Component;
|
||||
|
||||
/**
|
||||
* Workflow templates (työnkulkupohjat) grouped by service unit: adding,
|
||||
* editing and taking out of use.
|
||||
*/
|
||||
new class extends Component
|
||||
{
|
||||
public string $statusMessage = '';
|
||||
|
||||
public function mount(): void
|
||||
{
|
||||
$this->authorize('catalog.manage');
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<string, list<WorkflowTemplateSummary>>
|
||||
*/
|
||||
#[Computed]
|
||||
public function groups(): array
|
||||
{
|
||||
$groups = [];
|
||||
|
||||
foreach (app(WorkflowTemplateQueries::class)->all() as $template) {
|
||||
$groups[$template->serviceUnitName][] = $template;
|
||||
}
|
||||
|
||||
return $groups;
|
||||
}
|
||||
|
||||
public function toggleActive(int $templateId, SetWorkflowTemplateActive $setActive): void
|
||||
{
|
||||
$this->authorize('catalog.manage');
|
||||
|
||||
$template = app(WorkflowTemplateQueries::class)->find($templateId);
|
||||
|
||||
if ($template === null) {
|
||||
$this->statusMessage = __('catalog.status.not_found');
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
$setActive->execute($templateId, ! $template->isActive);
|
||||
} catch (WorkflowTemplateNotFound) {
|
||||
$this->statusMessage = __('catalog.status.not_found');
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
unset($this->groups);
|
||||
|
||||
$this->statusMessage = __($template->isActive ? 'catalog.workflows.status.deactivated' : 'catalog.status.activated', ['name' => $template->name]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return list<array{index: string, label: string, sortable: bool}>
|
||||
*/
|
||||
public function headers(): array
|
||||
{
|
||||
return [
|
||||
['index' => 'name', 'label' => __('catalog.fields.name'), 'sortable' => false],
|
||||
['index' => 'work_type', 'label' => __('catalog.workflows.work_type'), 'sortable' => false],
|
||||
['index' => 'steps', 'label' => __('catalog.workflows.step_count'), 'sortable' => false],
|
||||
['index' => 'actions', 'label' => __('catalog.products.actions'), 'sortable' => false],
|
||||
];
|
||||
}
|
||||
|
||||
public function render(): mixed
|
||||
{
|
||||
return $this->view()->title(__('catalog.workflows.title'));
|
||||
}
|
||||
};
|
||||
?>
|
||||
|
||||
<div class="mx-auto max-w-5xl space-y-6 p-4 sm:p-6">
|
||||
<div class="flex flex-wrap items-center justify-between gap-4">
|
||||
<h1 class="text-2xl font-semibold">{{ __('catalog.workflows.title') }}</h1>
|
||||
|
||||
<x-button :href="route('catalog.workflow-templates.create')" wire:navigate icon="plus" :text="__('catalog.workflows.add')" />
|
||||
</div>
|
||||
|
||||
<div class="space-y-1 text-sm">
|
||||
<p class="text-zinc-600 dark:text-zinc-300">{{ __('catalog.workflows.intro') }}</p>
|
||||
<p><x-link :href="route('catalog.index')" navigate>{{ __('catalog.workflows.back_to_catalog') }}</x-link></p>
|
||||
</div>
|
||||
|
||||
@if (session('status'))
|
||||
<x-alert role="status" color="green" light icon="check-circle" :text="session('status')" />
|
||||
@endif
|
||||
|
||||
<p role="status" aria-live="polite" class="text-sm">{{ $statusMessage }}</p>
|
||||
|
||||
@forelse ($this->groups as $unitName => $templates)
|
||||
<section wire:key="unit-{{ $loop->index }}" aria-labelledby="unit-{{ $loop->index }}-heading" class="space-y-3">
|
||||
<h2 id="unit-{{ $loop->index }}-heading" class="text-xl font-semibold break-words">{{ $unitName }}</h2>
|
||||
|
||||
<x-table :headers="$this->headers()" :rows="$templates">
|
||||
@interact('column_name', $template)
|
||||
<span class="font-medium break-words">{{ $template->name }}</span>
|
||||
@unless ($template->isActive)
|
||||
<span class="ml-1 text-sm">({{ __('catalog.inactive') }})</span>
|
||||
@endunless
|
||||
@endinteract
|
||||
|
||||
@interact('column_work_type', $template)
|
||||
{{ $template->workTypeName }}
|
||||
@endinteract
|
||||
|
||||
@interact('column_steps', $template)
|
||||
{{ $template->stepCount }}
|
||||
@endinteract
|
||||
|
||||
@interact('column_actions', $template)
|
||||
<div class="flex flex-wrap items-center gap-2">
|
||||
<x-button
|
||||
:href="route('catalog.workflow-templates.edit', $template->id)"
|
||||
wire:navigate
|
||||
subtle
|
||||
color="black"
|
||||
sm
|
||||
icon="pencil"
|
||||
:text="__('catalog.actions.edit')"
|
||||
:aria-label="__('catalog.actions.edit_named', ['name' => $template->name])"
|
||||
/>
|
||||
|
||||
<label class="inline-flex min-h-6 cursor-pointer items-center gap-2 text-sm">
|
||||
<input
|
||||
type="checkbox"
|
||||
id="active-template-{{ $template->id }}"
|
||||
wire:click="toggleActive({{ $template->id }})"
|
||||
@checked($template->isActive)
|
||||
aria-label="{{ __('catalog.actions.active_named', ['name' => $template->name]) }}"
|
||||
class="size-4 rounded border-zinc-400 text-primary-600 focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-blue-700 dark:border-zinc-500 dark:bg-zinc-800"
|
||||
/>
|
||||
<span aria-hidden="true">{{ __('catalog.actions.active') }}</span>
|
||||
</label>
|
||||
</div>
|
||||
@endinteract
|
||||
</x-table>
|
||||
</section>
|
||||
@empty
|
||||
<p>{{ __('catalog.workflows.none') }}</p>
|
||||
@endforelse
|
||||
</div>
|
||||
@@ -27,6 +27,12 @@ Route::middleware(['web', 'auth', 'can:catalog.manage'])
|
||||
->whereNumber('workType')
|
||||
->name('work-types.edit');
|
||||
|
||||
Route::livewire('/workflow-templates', 'catalog::workflow-templates')->name('workflow-templates');
|
||||
Route::livewire('/workflow-templates/create', 'catalog::workflow-template-form')->name('workflow-templates.create');
|
||||
Route::livewire('/workflow-templates/{template}/edit', 'catalog::workflow-template-form')
|
||||
->whereNumber('template')
|
||||
->name('workflow-templates.edit');
|
||||
|
||||
Route::livewire('/prices', 'catalog::prices')->name('prices');
|
||||
Route::livewire('/products/create', 'catalog::product-form')->name('products.create');
|
||||
Route::livewire('/products/{product}/edit', 'catalog::product-form')
|
||||
|
||||
Reference in New Issue
Block a user