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();
}
}
@@ -8,12 +8,14 @@ use App\Modules\Customers\Application\Contracts\CustomerRowUsage;
use App\Modules\Jobs\Application\Contracts\ChecklistQueries;
use App\Modules\Jobs\Application\Contracts\CustomerJobQueries;
use App\Modules\Jobs\Application\Contracts\TaskCalendarQueries;
use App\Modules\Jobs\Application\Contracts\TaskCommentQueries;
use App\Modules\Jobs\Application\Contracts\TaskQueries;
use App\Modules\Jobs\Application\Contracts\TaskUsage;
use App\Modules\Jobs\Domain\Contracts\ChecklistItemRepository;
use App\Modules\Jobs\Domain\Contracts\CustomerJobRepository;
use App\Modules\Jobs\Domain\Contracts\JobNumberSequence;
use App\Modules\Jobs\Domain\Contracts\TaskBookingRepository;
use App\Modules\Jobs\Domain\Contracts\TaskCommentRepository;
use App\Modules\Jobs\Domain\Contracts\TaskRepository;
use App\Modules\Jobs\Infrastructure\CustomerReferencesFromJobs;
use App\Modules\Jobs\Infrastructure\CustomerRowUsageFromJobs;
@@ -25,6 +27,8 @@ use App\Modules\Jobs\Infrastructure\Persistence\Repositories\EloquentCustomerJob
use App\Modules\Jobs\Infrastructure\Persistence\Repositories\EloquentCustomerJobRepository;
use App\Modules\Jobs\Infrastructure\Persistence\Repositories\EloquentTaskBookingRepository;
use App\Modules\Jobs\Infrastructure\Persistence\Repositories\EloquentTaskCalendarQueries;
use App\Modules\Jobs\Infrastructure\Persistence\Repositories\EloquentTaskCommentQueries;
use App\Modules\Jobs\Infrastructure\Persistence\Repositories\EloquentTaskCommentRepository;
use App\Modules\Jobs\Infrastructure\Persistence\Repositories\EloquentTaskQueries;
use App\Modules\Jobs\Infrastructure\Persistence\Repositories\EloquentTaskRepository;
use App\Modules\Users\Domain\Enums\Role;
@@ -52,6 +56,8 @@ class JobsServiceProvider extends ServiceProvider
TaskCalendarQueries::class => EloquentTaskCalendarQueries::class,
ChecklistItemRepository::class => EloquentChecklistItemRepository::class,
ChecklistQueries::class => EloquentChecklistQueries::class,
TaskCommentRepository::class => EloquentTaskCommentRepository::class,
TaskCommentQueries::class => EloquentTaskCommentQueries::class,
// Replaced by the TimeTracking module, which knows which tasks have time entries.
TaskUsage::class => NothingUsesTasks::class,
];
@@ -96,6 +102,10 @@ class JobsServiceProvider extends ServiceProvider
Gate::define('jobs.update-task-status', fn (User $user, int $jobId): bool => $user->role !== Role::Subcontractor
|| app(CustomerJobQueries::class)->isMember($jobId, $user->id));
// Commenting on tasks follows jobs.update-task-status (spec 2.4); authors edit and remove
// their own comments, and admin and management may remove anyone's.
Gate::define('jobs.remove-any-task-comment', $isManager);
// "Lasku luotu" and "Laskutettu" are set when invoicing: management or the job's biller.
// The argument is the job's biller (user id), when known.
Gate::define('jobs.set-billing-status', fn (User $user, ?int $billerUserId = null): bool => $isManager($user)