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:
@@ -0,0 +1,19 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Modules\Jobs\Domain\Contracts;
|
||||
|
||||
use App\Modules\Jobs\Domain\Entities\TaskComment;
|
||||
|
||||
interface TaskCommentRepository
|
||||
{
|
||||
/**
|
||||
* Null when the comment does not exist or belongs to another task.
|
||||
*/
|
||||
public function findForTask(int $taskId, int $commentId): ?TaskComment;
|
||||
|
||||
public function save(TaskComment $comment): TaskComment;
|
||||
|
||||
public function delete(TaskComment $comment): void;
|
||||
}
|
||||
@@ -0,0 +1,110 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Modules\Jobs\Domain\Entities;
|
||||
|
||||
use App\Modules\Jobs\Domain\Exceptions\NotTaskCommentAuthor;
|
||||
use App\Modules\Jobs\Domain\ValueObjects\TaskCommentBody;
|
||||
use DateTimeImmutable;
|
||||
|
||||
/**
|
||||
* A comment on a task. Its author may edit and remove it; others may only
|
||||
* remove it, and only when allowed to remove anyone's comments.
|
||||
*/
|
||||
final class TaskComment
|
||||
{
|
||||
private function __construct(
|
||||
private ?int $id,
|
||||
private int $taskId,
|
||||
private ?int $authorId,
|
||||
private TaskCommentBody $body,
|
||||
private DateTimeImmutable $createdAt,
|
||||
private ?DateTimeImmutable $editedAt,
|
||||
) {}
|
||||
|
||||
public static function create(int $taskId, int $authorId, TaskCommentBody $body, DateTimeImmutable $at): self
|
||||
{
|
||||
return new self(null, $taskId, $authorId, $body, $at, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* The author is null when the user no longer exists.
|
||||
*/
|
||||
public static function reconstitute(int $id, int $taskId, ?int $authorId, TaskCommentBody $body, DateTimeImmutable $createdAt, ?DateTimeImmutable $editedAt): self
|
||||
{
|
||||
return new self($id, $taskId, $authorId, $body, $createdAt, $editedAt);
|
||||
}
|
||||
|
||||
/**
|
||||
* Saving the same text again changes nothing.
|
||||
*
|
||||
* @throws NotTaskCommentAuthor
|
||||
*/
|
||||
public function edit(int $userId, TaskCommentBody $body, DateTimeImmutable $at): void
|
||||
{
|
||||
$this->ensureAuthor($userId);
|
||||
|
||||
if ($body->value === $this->body->value) {
|
||||
return;
|
||||
}
|
||||
|
||||
$this->body = $body;
|
||||
$this->editedAt = $at;
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws NotTaskCommentAuthor
|
||||
*/
|
||||
public function ensureRemovableBy(int $userId, bool $mayRemoveOthers): void
|
||||
{
|
||||
if (! $mayRemoveOthers) {
|
||||
$this->ensureAuthor($userId);
|
||||
}
|
||||
}
|
||||
|
||||
public function isWrittenBy(int $userId): bool
|
||||
{
|
||||
return $this->authorId === $userId;
|
||||
}
|
||||
|
||||
public function id(): ?int
|
||||
{
|
||||
return $this->id;
|
||||
}
|
||||
|
||||
public function taskId(): int
|
||||
{
|
||||
return $this->taskId;
|
||||
}
|
||||
|
||||
public function authorId(): ?int
|
||||
{
|
||||
return $this->authorId;
|
||||
}
|
||||
|
||||
public function body(): TaskCommentBody
|
||||
{
|
||||
return $this->body;
|
||||
}
|
||||
|
||||
public function createdAt(): DateTimeImmutable
|
||||
{
|
||||
return $this->createdAt;
|
||||
}
|
||||
|
||||
public function editedAt(): ?DateTimeImmutable
|
||||
{
|
||||
return $this->editedAt;
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws NotTaskCommentAuthor
|
||||
*/
|
||||
private function ensureAuthor(int $userId): void
|
||||
{
|
||||
if (! $this->isWrittenBy($userId)) {
|
||||
throw new NotTaskCommentAuthor((int) $this->id, $userId);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Modules\Jobs\Domain\Exceptions;
|
||||
|
||||
use App\Modules\Shared\Domain\Exceptions\DomainException;
|
||||
|
||||
final class InvalidTaskCommentBody extends DomainException
|
||||
{
|
||||
public function __construct()
|
||||
{
|
||||
parent::__construct('A task comment must be 1–5000 characters.');
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Modules\Jobs\Domain\Exceptions;
|
||||
|
||||
use App\Modules\Shared\Domain\Exceptions\DomainException;
|
||||
|
||||
/**
|
||||
* Only the author edits a comment; others than the author may remove it
|
||||
* only when they may remove anyone's comments.
|
||||
*/
|
||||
final class NotTaskCommentAuthor extends DomainException
|
||||
{
|
||||
public function __construct(public readonly int $commentId, public readonly int $userId)
|
||||
{
|
||||
parent::__construct("User {$userId} is not the author of comment {$commentId}.");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Modules\Jobs\Domain\Exceptions;
|
||||
|
||||
use App\Modules\Shared\Domain\Exceptions\DomainException;
|
||||
|
||||
/**
|
||||
* The comment does not exist or belongs to another task or job.
|
||||
*/
|
||||
final class TaskCommentNotFound extends DomainException
|
||||
{
|
||||
public function __construct(public readonly int $taskId, public readonly int $commentId)
|
||||
{
|
||||
parent::__construct("Comment {$commentId} of task {$taskId} was not found.");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Modules\Jobs\Domain\ValueObjects;
|
||||
|
||||
use App\Modules\Jobs\Domain\Exceptions\InvalidTaskCommentBody;
|
||||
|
||||
/**
|
||||
* The text of a task comment: line breaks are kept, surrounding blank space
|
||||
* is not.
|
||||
*/
|
||||
final readonly class TaskCommentBody
|
||||
{
|
||||
public const int MAX_LENGTH = 5000;
|
||||
|
||||
public string $value;
|
||||
|
||||
/**
|
||||
* @throws InvalidTaskCommentBody
|
||||
*/
|
||||
public function __construct(string $value)
|
||||
{
|
||||
$value = trim(str_replace(["\r\n", "\r"], "\n", $value));
|
||||
|
||||
if ($value === '' || mb_strlen($value) > self::MAX_LENGTH) {
|
||||
throw new InvalidTaskCommentBody;
|
||||
}
|
||||
|
||||
$this->value = $value;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user