Also drops a redundant array_values() that Larastan flagged in MoveBoardCard. Co-Authored-By: Claude Opus 5.5 <[email protected]>
146 lines
5.3 KiB
PHP
146 lines
5.3 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Modules\Jobs\Application\UseCases;
|
|
|
|
use App\Modules\Jobs\Domain\Contracts\BoardRepository;
|
|
use App\Modules\Jobs\Domain\Contracts\CustomerJobRepository;
|
|
use App\Modules\Jobs\Domain\Contracts\TaskRepository;
|
|
use App\Modules\Jobs\Domain\Enums\BoardColumn;
|
|
use App\Modules\Jobs\Domain\Enums\JobStatus;
|
|
use App\Modules\Jobs\Domain\Enums\Priority;
|
|
use App\Modules\Jobs\Domain\Enums\TaskStatus;
|
|
use App\Modules\Jobs\Domain\Enums\UrgentThreshold;
|
|
use App\Modules\Jobs\Domain\Events\TaskCompleted;
|
|
use App\Modules\Jobs\Domain\Exceptions\CardCannotMoveThere;
|
|
use App\Modules\Jobs\Domain\Exceptions\CustomerJobNotFound;
|
|
use App\Modules\Jobs\Domain\Exceptions\TaskNotFound;
|
|
use App\Modules\Shared\Application\Contracts\EventDispatcher;
|
|
use App\Modules\Shared\Application\Contracts\TransactionManager;
|
|
use App\Modules\Users\Application\Contracts\Clock;
|
|
|
|
/**
|
|
* A Kanban card dropped into a column (docs/ui-kanban-board.md 3):
|
|
*
|
|
* - a job dropped into another status's column gets that status;
|
|
* - a job dropped into "Työn alla • Kiire" gets at least the threshold's
|
|
* priority, and one dropped into the normal "Työn alla" with an urgent
|
|
* priority gets the normal priority;
|
|
* - a pinned task card gets the status of its new column;
|
|
* - the column's cards get positions in the dropped order.
|
|
*
|
|
* Who may set a billing status or open a lock is checked by the caller.
|
|
*/
|
|
final class MoveBoardCard
|
|
{
|
|
public function __construct(
|
|
private CustomerJobRepository $jobs,
|
|
private TaskRepository $tasks,
|
|
private BoardRepository $board,
|
|
private TransactionManager $transactions,
|
|
private EventDispatcher $events,
|
|
private Clock $clock,
|
|
) {}
|
|
|
|
/**
|
|
* @param list<string> $orderedKeys the target column's card keys ("job-12", "task-34") in their new order
|
|
*
|
|
* @throws CustomerJobNotFound
|
|
* @throws TaskNotFound
|
|
* @throws CardCannotMoveThere
|
|
*/
|
|
public function execute(int $jobId, ?int $taskId, BoardColumn $target, array $orderedKeys, UrgentThreshold $threshold, int $actorUserId): void
|
|
{
|
|
$this->transactions->run(function () use ($jobId, $taskId, $target, $orderedKeys, $threshold, $actorUserId): void {
|
|
if ($taskId === null) {
|
|
$this->moveJob($jobId, $target, $threshold, $actorUserId);
|
|
} else {
|
|
$this->moveTask($jobId, $taskId, $target);
|
|
}
|
|
|
|
$this->reorder($orderedKeys);
|
|
});
|
|
}
|
|
|
|
/**
|
|
* The status a job gets when dropped into the column: its own when the
|
|
* column already holds it (e.g. moving between the quote statuses in
|
|
* "Tarjoukset"), the column's otherwise.
|
|
*/
|
|
public static function statusAfterDrop(JobStatus $current, BoardColumn $target): JobStatus
|
|
{
|
|
return in_array($current, $target->statuses(), true) ? $current : $target->targetStatus();
|
|
}
|
|
|
|
private function moveJob(int $jobId, BoardColumn $target, UrgentThreshold $threshold, int $actorUserId): void
|
|
{
|
|
$job = $this->jobs->findById($jobId) ?? throw new CustomerJobNotFound($jobId);
|
|
$current = $job->details()->status;
|
|
|
|
if (($current === JobStatus::Ongoing) !== $target->isOngoing()) {
|
|
throw new CardCannotMoveThere;
|
|
}
|
|
|
|
$changes = $job->changeStatus(self::statusAfterDrop($current, $target));
|
|
$priority = $job->details()->priority;
|
|
|
|
if ($target->isUrgentLane() && ! $threshold->includes($priority)) {
|
|
$changes = [...$changes, ...$job->changePriority($threshold->lowestPriority())];
|
|
} elseif ($target->isNormalProductionLane() && $threshold->includes($priority)) {
|
|
$changes = [...$changes, ...$job->changePriority(Priority::Normal)];
|
|
}
|
|
|
|
$job->recordCompletion($this->clock->now());
|
|
|
|
if ($changes !== []) {
|
|
$this->jobs->save($job);
|
|
$this->jobs->recordChanges($jobId, $actorUserId, $changes);
|
|
}
|
|
}
|
|
|
|
private function moveTask(int $jobId, int $taskId, BoardColumn $target): void
|
|
{
|
|
$status = $target->taskStatus() ?? throw new CardCannotMoveThere;
|
|
$task = $this->tasks->findForJob($jobId, $taskId) ?? throw new TaskNotFound($jobId, $taskId);
|
|
$wasDone = $task->status() === TaskStatus::Done;
|
|
|
|
if ($task->status() === $status || ($status === TaskStatus::InProgress && $task->status() === TaskStatus::InReview)) {
|
|
return;
|
|
}
|
|
|
|
$task->changeStatus($status);
|
|
$this->tasks->save($task);
|
|
|
|
if (! $wasDone && $status === TaskStatus::Done) {
|
|
$this->events->dispatch(new TaskCompleted($taskId, $jobId));
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @param list<string> $orderedKeys
|
|
*/
|
|
private function reorder(array $orderedKeys): void
|
|
{
|
|
$jobPositions = [];
|
|
$taskPositions = [];
|
|
|
|
foreach ($orderedKeys as $index => $key) {
|
|
if (preg_match('/^(job|task)-(\d+)$/', $key, $matches) !== 1) {
|
|
continue;
|
|
}
|
|
|
|
$position = ($index + 1) * 10;
|
|
|
|
if ($matches[1] === 'job') {
|
|
$jobPositions[(int) $matches[2]] = $position;
|
|
} else {
|
|
$taskPositions[(int) $matches[2]] = $position;
|
|
}
|
|
}
|
|
|
|
$this->board->setJobPositions($jobPositions);
|
|
$this->board->setTaskPositions($taskPositions);
|
|
}
|
|
}
|