Add comments to tasks

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]>
This commit is contained in:
2026-11-25 07:31:53 +02:00
co-authored by Claude Sonnet 5.5
parent 60cab4b871
commit 4fb8ea1c5f
29 changed files with 1354 additions and 4 deletions
@@ -0,0 +1,38 @@
<?php
namespace App\Modules\Jobs\Infrastructure\Persistence\Eloquent;
use Carbon\CarbonImmutable;
use Illuminate\Database\Eloquent\Attributes\Fillable;
use Illuminate\Database\Eloquent\Attributes\Table;
use Illuminate\Database\Eloquent\Model;
/**
* Persistence model of a task comment, used only inside the Jobs
* Infrastructure layer.
*
* @property int $id
* @property int $task_id
* @property int|null $user_id
* @property string $body
* @property CarbonImmutable|null $edited_at
* @property CarbonImmutable $created_at
*/
#[Table('task_comments')]
#[Fillable(['task_id', 'user_id', 'body', 'edited_at', 'created_at'])]
class TaskCommentModel extends Model
{
/**
* @return array<string, string>
*/
protected function casts(): array
{
return [
'task_id' => 'integer',
'user_id' => 'integer',
'edited_at' => 'immutable_datetime',
'created_at' => 'immutable_datetime',
'updated_at' => 'immutable_datetime',
];
}
}
@@ -0,0 +1,44 @@
<?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');
}
}
@@ -0,0 +1,51 @@
<?php
namespace App\Modules\Jobs\Infrastructure\Persistence\Repositories;
use App\Modules\Jobs\Domain\Contracts\TaskCommentRepository;
use App\Modules\Jobs\Domain\Entities\TaskComment;
use App\Modules\Jobs\Domain\ValueObjects\TaskCommentBody;
use App\Modules\Jobs\Infrastructure\Persistence\Eloquent\TaskCommentModel;
use DateTimeImmutable;
class EloquentTaskCommentRepository implements TaskCommentRepository
{
public function findForTask(int $taskId, int $commentId): ?TaskComment
{
$model = TaskCommentModel::query()->where('task_id', $taskId)->find($commentId);
return $model === null ? null : $this->toEntity($model);
}
public function save(TaskComment $comment): TaskComment
{
$model = $comment->id() === null ? new TaskCommentModel : TaskCommentModel::query()->findOrFail($comment->id());
$model->fill([
'task_id' => $comment->taskId(),
'user_id' => $comment->authorId(),
'body' => $comment->body()->value,
'edited_at' => $comment->editedAt(),
'created_at' => $comment->createdAt(),
])->save();
return $this->toEntity($model->refresh());
}
public function delete(TaskComment $comment): void
{
TaskCommentModel::query()->whereKey($comment->id())->delete();
}
private function toEntity(TaskCommentModel $model): TaskComment
{
return TaskComment::reconstitute(
$model->id,
$model->task_id,
$model->user_id,
new TaskCommentBody($model->body),
DateTimeImmutable::createFromInterface($model->created_at),
$model->edited_at === null ? null : DateTimeImmutable::createFromInterface($model->edited_at),
);
}
}
@@ -147,6 +147,11 @@ class EloquentTaskQueries implements TaskQueries
$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');
$workTypeNames = [];
foreach ($models->pluck('work_type_id')->unique() as $workTypeId) {
@@ -170,6 +175,7 @@ class EloquentTaskQueries implements TaskQueries
->all(),
plannedFrom: $model->planned_from?->format('Y-m-d'),
plannedTo: $model->planned_to?->format('Y-m-d'),
commentCount: (int) ($commentCounts[$model->id] ?? 0),
))->values()->all();
}
}