ofTask($jobId, $taskId)->orderBy('id')->get(); $names = $this->users->namesByIds($models->pluck('user_id')->filter()->unique()->values()->all()); return $models ->map(fn (TaskImageModel $model): TaskImageSummary => new TaskImageSummary( $model->id, $model->user_id, $model->user_id === null ? '' : ($names[$model->user_id] ?? ''), $model->caption, $model->original_name, $model->created_at->setTimezone((string) config('app.business_timezone'))->format('Y-m-d H:i'), $model->width, $model->height, )) ->values() ->all(); } public function fileOf(int $jobId, int $taskId, int $imageId, bool $thumbnail): ?TaskImageFile { $model = $this->ofTask($jobId, $taskId)->find($imageId); if ($model === null) { return null; } // The stored file is always a JPEG, whatever the original was. $name = pathinfo($model->original_name, PATHINFO_FILENAME); return new TaskImageFile($thumbnail ? $model->thumbnail_path : $model->path, ($name === '' ? 'kuva' : $name).'.jpg'); } /** * @return Builder */ private function ofTask(int $jobId, int $taskId): Builder { return TaskImageModel::query() ->where('task_id', $taskId) ->whereExists(fn ($query) => $query->selectRaw('1')->from('tasks') ->whereColumn('tasks.id', 'task_images.task_id') ->where('tasks.customer_job_id', $jobId)); } }