where('customer_job_id', $jobId) ->orderByRaw('status = ?', [TaskStatus::Done->value]) ->orderByRaw('due_on IS NULL') ->orderBy('due_on') ->orderBy('id') ->get(); return $this->summaries($models); } public function findTask(int $jobId, int $taskId): ?TaskSummary { $model = TaskModel::query()->where('customer_job_id', $jobId)->find($taskId); return $model === null ? null : $this->summaries(collect([$model]))[0]; } /** * @param Collection $models * @return list */ private function summaries(Collection $models): array { $assignees = DB::table('task_assignees') ->whereIn('task_id', $models->pluck('id')->all()) ->get() ->groupBy('task_id'); $userNames = $this->users->namesByIds($assignees->flatten()->pluck('user_id')->map(fn ($id): int => (int) $id)->unique()->values()->all()); $workTypeNames = []; foreach ($models->pluck('work_type_id')->unique() as $workTypeId) { $workTypeNames[$workTypeId] = $this->catalog->findWorkType((int) $workTypeId)->name ?? ''; } return $models->map(fn (TaskModel $model): TaskSummary => new TaskSummary( id: $model->id, jobId: $model->customer_job_id, title: $model->title, description: $model->description, status: $model->status, dueOn: $model->due_on?->format('Y-m-d'), estimatedHours: $model->estimated_hours === null ? null : rtrim(rtrim($model->estimated_hours, '0'), '.'), workType: new NamedId($model->work_type_id, $workTypeNames[$model->work_type_id] ?? ''), assignees: ($assignees->get($model->id) ?? collect()) ->map(fn ($row): NamedId => new NamedId((int) $row->user_id, $userNames[(int) $row->user_id] ?? '')) ->sortBy('name') ->values() ->all(), ))->values()->all(); } }