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,18 @@
<?php
declare(strict_types=1);
namespace App\Modules\Jobs\Application\Contracts;
use App\Modules\Jobs\Application\DTOs\TaskCommentSummary;
interface TaskCommentQueries
{
/**
* The task's comments, oldest first; empty when the task belongs to
* another job.
*
* @return list<TaskCommentSummary>
*/
public function commentsOf(int $jobId, int $taskId): array;
}
@@ -0,0 +1,21 @@
<?php
declare(strict_types=1);
namespace App\Modules\Jobs\Application\DTOs;
/**
* A task comment with its author's name. Times are in the business time
* zone as "Y-m-d H:i".
*/
final readonly class TaskCommentSummary
{
public function __construct(
public int $id,
public ?int $authorId,
public string $authorName,
public string $body,
public string $createdAt,
public ?string $editedAt,
) {}
}
@@ -27,5 +27,6 @@ final readonly class TaskSummary
public array $members,
public ?string $plannedFrom = null,
public ?string $plannedTo = null,
public int $commentCount = 0,
) {}
}
@@ -0,0 +1,44 @@
<?php
declare(strict_types=1);
namespace App\Modules\Jobs\Application\Services;
use App\Modules\Jobs\Domain\Contracts\TaskCommentRepository;
use App\Modules\Jobs\Domain\Contracts\TaskRepository;
use App\Modules\Jobs\Domain\Entities\TaskComment;
use App\Modules\Jobs\Domain\Exceptions\TaskCommentNotFound;
use App\Modules\Jobs\Domain\Exceptions\TaskNotFound;
/**
* Finds a comment only through its task and the task's job, so a page of
* one job never changes another job's comments.
*/
final readonly class TaskCommentFinder
{
public function __construct(
private TaskRepository $tasks,
private TaskCommentRepository $comments,
) {}
/**
* @throws TaskNotFound
* @throws TaskCommentNotFound
*/
public function find(int $jobId, int $taskId, int $commentId): TaskComment
{
$this->ensureTask($jobId, $taskId);
return $this->comments->findForTask($taskId, $commentId) ?? throw new TaskCommentNotFound($taskId, $commentId);
}
/**
* @throws TaskNotFound
*/
public function ensureTask(int $jobId, int $taskId): void
{
if ($this->tasks->findForJob($jobId, $taskId) === null) {
throw new TaskNotFound($jobId, $taskId);
}
}
}
@@ -0,0 +1,44 @@
<?php
declare(strict_types=1);
namespace App\Modules\Jobs\Application\UseCases;
use App\Modules\Jobs\Application\Services\TaskCommentFinder;
use App\Modules\Jobs\Domain\Contracts\TaskCommentRepository;
use App\Modules\Jobs\Domain\Entities\TaskComment;
use App\Modules\Jobs\Domain\Exceptions\InvalidTaskCommentBody;
use App\Modules\Jobs\Domain\Exceptions\TaskNotFound;
use App\Modules\Jobs\Domain\ValueObjects\TaskCommentBody;
use App\Modules\Shared\Application\Contracts\TransactionManager;
use App\Modules\Users\Application\Contracts\Clock;
/**
* Adds a comment by the given user to a task.
*/
final class AddTaskComment
{
public function __construct(
private TaskCommentFinder $finder,
private TaskCommentRepository $comments,
private TransactionManager $transactions,
private Clock $clock,
) {}
/**
* @return int The new comment's id.
*
* @throws TaskNotFound
* @throws InvalidTaskCommentBody
*/
public function execute(int $jobId, int $taskId, int $userId, string $body): int
{
$body = new TaskCommentBody($body);
return $this->transactions->run(function () use ($jobId, $taskId, $userId, $body): int {
$this->finder->ensureTask($jobId, $taskId);
return (int) $this->comments->save(TaskComment::create($taskId, $userId, $body, $this->clock->now()))->id();
});
}
}
@@ -0,0 +1,46 @@
<?php
declare(strict_types=1);
namespace App\Modules\Jobs\Application\UseCases;
use App\Modules\Jobs\Application\Services\TaskCommentFinder;
use App\Modules\Jobs\Domain\Contracts\TaskCommentRepository;
use App\Modules\Jobs\Domain\Exceptions\InvalidTaskCommentBody;
use App\Modules\Jobs\Domain\Exceptions\NotTaskCommentAuthor;
use App\Modules\Jobs\Domain\Exceptions\TaskCommentNotFound;
use App\Modules\Jobs\Domain\Exceptions\TaskNotFound;
use App\Modules\Jobs\Domain\ValueObjects\TaskCommentBody;
use App\Modules\Shared\Application\Contracts\TransactionManager;
use App\Modules\Users\Application\Contracts\Clock;
/**
* Changes the text of the user's own comment and marks it edited.
*/
final class EditTaskComment
{
public function __construct(
private TaskCommentFinder $finder,
private TaskCommentRepository $comments,
private TransactionManager $transactions,
private Clock $clock,
) {}
/**
* @throws TaskNotFound
* @throws TaskCommentNotFound
* @throws NotTaskCommentAuthor
* @throws InvalidTaskCommentBody
*/
public function execute(int $jobId, int $taskId, int $commentId, int $userId, string $body): void
{
$body = new TaskCommentBody($body);
$this->transactions->run(function () use ($jobId, $taskId, $commentId, $userId, $body): void {
$comment = $this->finder->find($jobId, $taskId, $commentId);
$comment->edit($userId, $body, $this->clock->now());
$this->comments->save($comment);
});
}
}
@@ -0,0 +1,40 @@
<?php
declare(strict_types=1);
namespace App\Modules\Jobs\Application\UseCases;
use App\Modules\Jobs\Application\Services\TaskCommentFinder;
use App\Modules\Jobs\Domain\Contracts\TaskCommentRepository;
use App\Modules\Jobs\Domain\Exceptions\NotTaskCommentAuthor;
use App\Modules\Jobs\Domain\Exceptions\TaskCommentNotFound;
use App\Modules\Jobs\Domain\Exceptions\TaskNotFound;
use App\Modules\Shared\Application\Contracts\TransactionManager;
/**
* Removes the user's own comment, or anyone's when the user may remove
* others' comments (admin and management).
*/
final class RemoveTaskComment
{
public function __construct(
private TaskCommentFinder $finder,
private TaskCommentRepository $comments,
private TransactionManager $transactions,
) {}
/**
* @throws TaskNotFound
* @throws TaskCommentNotFound
* @throws NotTaskCommentAuthor
*/
public function execute(int $jobId, int $taskId, int $commentId, int $userId, bool $mayRemoveOthers): void
{
$this->transactions->run(function () use ($jobId, $taskId, $commentId, $userId, $mayRemoveOthers): void {
$comment = $this->finder->find($jobId, $taskId, $commentId);
$comment->ensureRemovableBy($userId, $mayRemoveOthers);
$this->comments->delete($comment);
});
}
}