Files
Toiminnanohjaus/app/Modules/Jobs/Infrastructure/Persistence/Repositories/EloquentTaskQueries.php
T
petriandClaude Sonnet 5.5 ae8172898d Add images to tasks
Everyone who may comment on a task sends images to it, e.g. photos
taken with a phone. The server stores an upright, at most 2560 px JPEG
without EXIF data (no location) and a 400 px thumbnail on the private
disk, served only to those who see the job. Uploaders change captions
(also the alt text) and remove their own images; admin and management
may remove anyone's (jobs.remove-any-task-image). Removing a task also
removes its image files.

Livewire's temporary upload limit is raised to PHP's 40 MB so that the
page's own 12 MB limit gives a translated message.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
2026-11-25 07:46:53 +02:00

188 lines
7.4 KiB
PHP

<?php
namespace App\Modules\Jobs\Infrastructure\Persistence\Repositories;
use App\Modules\Catalog\Application\Contracts\CatalogQueries;
use App\Modules\Jobs\Application\Contracts\TaskQueries;
use App\Modules\Jobs\Application\DTOs\BookableTask;
use App\Modules\Jobs\Application\DTOs\NamedId;
use App\Modules\Jobs\Application\DTOs\TaskSummary;
use App\Modules\Jobs\Domain\Enums\JobStatus;
use App\Modules\Jobs\Domain\Enums\TaskStatus;
use App\Modules\Jobs\Infrastructure\Persistence\Eloquent\TaskModel;
use App\Modules\Jobs\Infrastructure\Persistence\JobInvolvement;
use App\Modules\Users\Application\Contracts\UserAccountQueries;
use Illuminate\Database\Query\Builder;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\DB;
class EloquentTaskQueries implements TaskQueries
{
public function __construct(
private CatalogQueries $catalog,
private UserAccountQueries $users,
) {}
public function tasksOf(int $jobId): array
{
$models = TaskModel::query()
->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];
}
public function bookableTask(int $taskId): ?BookableTask
{
return $this->bookableTasks([$taskId])[0] ?? null;
}
public function bookableTasks(array $taskIds): array
{
if ($taskIds === []) {
return [];
}
return $this->bookableRows(DB::table('tasks')->whereIn('tasks.id', $taskIds));
}
public function assignedOpenTaskIds(int $userId): array
{
return DB::table('tasks')
->join('customer_jobs', 'customer_jobs.id', '=', 'tasks.customer_job_id')
->where(fn ($query) => $query
->where('tasks.assignee_user_id', $userId)
->orWhereIn('tasks.id', DB::table('task_members')->select('task_id')->where('user_id', $userId)))
->where('tasks.status', '!=', TaskStatus::Done->value)
->whereNotIn('customer_jobs.status', self::lockedStatuses())
->orderBy('tasks.id')
->pluck('tasks.id')
->map(fn ($id): int => (int) $id)
->all();
}
public function searchBookableTasks(string $term, ?int $onlyMemberUserId = null, int $limit = 8): array
{
$term = trim($term);
if ($term === '') {
return [];
}
$pattern = '%'.addcslashes($term, '%_\\').'%';
$query = DB::table('tasks')
->join('customer_jobs', 'customer_jobs.id', '=', 'tasks.customer_job_id')
->whereNotIn('customer_jobs.status', self::lockedStatuses())
->where(fn ($query) => $query
->where('customer_jobs.number', 'like', $pattern)
->orWhere('customer_jobs.name', 'like', $pattern)
->orWhere('tasks.title', 'like', $pattern))
->limit($limit);
if ($onlyMemberUserId !== null) {
JobInvolvement::restrict($query, $onlyMemberUserId);
}
return $this->bookableRows(DB::table('tasks')->whereIn('tasks.id', $query->pluck('tasks.id')->all()));
}
/**
* @return list<BookableTask>
*/
private function bookableRows(Builder $tasks): array
{
$rows = $tasks
->join('customer_jobs', 'customer_jobs.id', '=', 'tasks.customer_job_id')
->orderBy('customer_jobs.number')
->orderBy('tasks.id')
->get(['tasks.id', 'tasks.customer_job_id', 'tasks.title', 'tasks.work_type_id', 'tasks.status', 'customer_jobs.number', 'customer_jobs.name', 'customer_jobs.status as job_status']);
$workTypeNames = [];
foreach ($rows->pluck('work_type_id')->unique() as $workTypeId) {
$workTypeNames[(int) $workTypeId] = $this->catalog->findWorkType((int) $workTypeId)->name ?? '';
}
return $rows->map(fn ($row): BookableTask => new BookableTask(
(int) $row->id,
(int) $row->customer_job_id,
(string) $row->number,
(string) $row->name,
(string) $row->title,
(int) $row->work_type_id,
$workTypeNames[(int) $row->work_type_id] ?? '',
JobStatus::from((string) $row->job_status),
TaskStatus::from((string) $row->status),
))->values()->all();
}
/**
* @return list<string>
*/
private static function lockedStatuses(): array
{
return array_values(array_map(fn (JobStatus $status): string => $status->value, array_filter(JobStatus::cases(), fn (JobStatus $status): bool => $status->locksTime())));
}
/**
* @param Collection<int, TaskModel> $models
* @return list<TaskSummary>
*/
private function summaries(Collection $models): array
{
$members = DB::table('task_members')
->whereIn('task_id', $models->pluck('id')->all())
->get()
->groupBy('task_id');
$userNames = $this->users->namesByIds($members->flatten()->pluck('user_id')
->merge($models->pluck('assignee_user_id')->filter())
->map(fn ($id): int => (int) $id)->unique()->values()->all());
$commentCounts = DB::table('task_comments')
->whereIn('task_id', $models->pluck('id')->all())
->groupBy('task_id')
->selectRaw('task_id, count(*) as aggregate')
->pluck('aggregate', 'task_id');
$imageCounts = DB::table('task_images')
->whereIn('task_id', $models->pluck('id')->all())
->groupBy('task_id')
->selectRaw('task_id, count(*) as aggregate')
->pluck('aggregate', 'task_id');
$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] ?? ''),
assignee: $model->assignee_user_id === null ? null : new NamedId($model->assignee_user_id, $userNames[$model->assignee_user_id] ?? ''),
members: ($members->get($model->id) ?? collect())
->map(fn ($row): NamedId => new NamedId((int) $row->user_id, $userNames[(int) $row->user_id] ?? ''))
->sortBy('name')
->values()
->all(),
plannedFrom: $model->planned_from?->format('Y-m-d'),
plannedTo: $model->planned_to?->format('Y-m-d'),
commentCount: (int) ($commentCounts[$model->id] ?? 0),
imageCount: (int) ($imageCounts[$model->id] ?? 0),
))->values()->all();
}
}