$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 $orderedKeys */ private function reorder(array $orderedKeys): void { $jobPositions = []; $taskPositions = []; foreach (array_values($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); } }