Everyone who may change a task's status (attached subcontractors too) comments on it on the job page. Authors edit and remove their own comments; admin and management may remove anyone's (jobs.remove-any-task-comment). Edited comments show when they were edited, and task cards show the number of comments. Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
45 lines
1.7 KiB
PHP
45 lines
1.7 KiB
PHP
<?php
|
|
|
|
namespace App\Modules\Jobs\Infrastructure\Persistence\Repositories;
|
|
|
|
use App\Modules\Jobs\Application\Contracts\TaskCommentQueries;
|
|
use App\Modules\Jobs\Application\DTOs\TaskCommentSummary;
|
|
use App\Modules\Jobs\Infrastructure\Persistence\Eloquent\TaskCommentModel;
|
|
use App\Modules\Users\Application\Contracts\UserAccountQueries;
|
|
use Carbon\CarbonImmutable;
|
|
|
|
class EloquentTaskCommentQueries implements TaskCommentQueries
|
|
{
|
|
public function __construct(private UserAccountQueries $users) {}
|
|
|
|
public function commentsOf(int $jobId, int $taskId): array
|
|
{
|
|
$models = TaskCommentModel::query()
|
|
->where('task_id', $taskId)
|
|
->whereExists(fn ($query) => $query->selectRaw('1')->from('tasks')
|
|
->whereColumn('tasks.id', 'task_comments.task_id')
|
|
->where('tasks.customer_job_id', $jobId))
|
|
->orderBy('id')
|
|
->get();
|
|
|
|
$names = $this->users->namesByIds($models->pluck('user_id')->filter()->unique()->values()->all());
|
|
|
|
return $models
|
|
->map(fn (TaskCommentModel $model): TaskCommentSummary => new TaskCommentSummary(
|
|
$model->id,
|
|
$model->user_id,
|
|
$model->user_id === null ? '' : ($names[$model->user_id] ?? ''),
|
|
$model->body,
|
|
$this->localTime($model->created_at),
|
|
$model->edited_at === null ? null : $this->localTime($model->edited_at),
|
|
))
|
|
->values()
|
|
->all();
|
|
}
|
|
|
|
private function localTime(CarbonImmutable $moment): string
|
|
{
|
|
return $moment->setTimezone((string) config('app.business_timezone'))->format('Y-m-d H:i');
|
|
}
|
|
}
|