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
+36
View File
@@ -9,6 +9,7 @@ use App\Modules\Catalog\Infrastructure\Persistence\Eloquent\WorkTypeModel;
use App\Modules\Customers\Infrastructure\Persistence\Eloquent\CustomerModel;
use App\Modules\Jobs\Infrastructure\Persistence\Eloquent\ChecklistItemModel;
use App\Modules\Jobs\Infrastructure\Persistence\Eloquent\CustomerJobModel;
use App\Modules\Jobs\Infrastructure\Persistence\Eloquent\TaskCommentModel;
use App\Modules\Jobs\Infrastructure\Persistence\Eloquent\TaskModel;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\DB;
@@ -161,6 +162,8 @@ it('has no accessibility issues on a job with tasks and on the task form', funct
->create(['customer_job_id' => $this->job->id, 'work_type_id' => $workType->id, 'title' => 'Kuvauspäivä', 'due_on' => now()->subDay()->toDateString(), 'estimated_hours' => 6]);
ChecklistItemModel::query()->create(['task_id' => $task->id, 'text' => 'Valot', 'sort_order' => 1, 'done_at' => now(), 'done_by' => $this->user->id]);
ChecklistItemModel::query()->create(['task_id' => $task->id, 'text' => 'Mikit', 'sort_order' => 2]);
TaskCommentModel::query()->create(['task_id' => $task->id, 'user_id' => $this->user->id, 'body' => 'Valot ok.
Mikit puuttuu.', 'edited_at' => now()]);
$url = match ($page) {
'job page' => route('jobs.show', $this->job->id),
@@ -279,6 +282,39 @@ it('adds, checks, renames, moves and removes checklist items with the keyboard',
->assertNoAccessibilityIssues(level: 2);
});
it('adds, edits and removes a task comment with the keyboard', function () {
$this->job->update(['status' => 'order']);
$workType = WorkTypeModel::query()->where('name', 'videokuvaus')->sole();
$task = TaskModel::factory()
->create(['customer_job_id' => $this->job->id, 'work_type_id' => $workType->id, 'title' => 'Kuvauspäivä']);
$page = visit(route('jobs.show', $this->job->id))
->click('#task-toggle-'.$task->id)
->type('#comment-new-'.$task->id, 'Valot ok.')
->keys('[aria-label="Lisää kommentti tehtävälle Kuvauspäivä"]', 'Enter')
->assertSee('Kommentti lisätty.')
->assertSeeIn('#task-'.$task->id.'-title + dl', 'Kommentit:');
$commentId = TaskCommentModel::query()->sole()->id;
$page->keys('#comment-edit-button-'.$commentId, 'Enter')
->assertScript('document.activeElement.id', 'comment-edit-'.$commentId)
->type('#comment-edit-'.$commentId, 'Valot ja mikit ok.')
->click('Tallenna')
->assertSee('Kommentti tallennettu.')
->assertSee('Valot ja mikit ok.')
->assertScript('document.activeElement.id', 'comment-edit-button-'.$commentId)
->keys('[aria-label^="Poista kommentti (Johanna Johto"]', 'Enter')
->assertScript("document.getElementById('remove-comment-dialog-{$task->id}').open", true)
->assertNoAccessibilityIssues(level: 2)
->click('#remove-comment-confirm-'.$task->id)
->assertSee('Kommentti poistettu.')
->assertScript('document.activeElement.id', 'comment-new-'.$task->id)
->assertNoAccessibilityIssues(level: 2);
expect(TaskCommentModel::query()->count())->toBe(0);
});
it('adds tasks from a workflow template with the keyboard, without accessibility issues', function (string $device, string $colorScheme) {
$this->job->update(['status' => 'order']);
$workType = WorkTypeModel::query()->where('name', 'videokuvaus')->sole();
@@ -0,0 +1,34 @@
<?php
namespace Tests\Fakes;
use App\Modules\Jobs\Domain\Contracts\TaskCommentRepository;
use App\Modules\Jobs\Domain\Entities\TaskComment;
class InMemoryTaskCommentRepository implements TaskCommentRepository
{
/** @var array<int, TaskComment> */
public array $comments = [];
private int $nextId = 1;
public function findForTask(int $taskId, int $commentId): ?TaskComment
{
$comment = $this->comments[$commentId] ?? null;
return $comment !== null && $comment->taskId() === $taskId ? clone $comment : null;
}
public function save(TaskComment $comment): TaskComment
{
$id = $comment->id() ?? $this->nextId++;
$this->comments[$id] = TaskComment::reconstitute($id, $comment->taskId(), $comment->authorId(), $comment->body(), $comment->createdAt(), $comment->editedAt());
return clone $this->comments[$id];
}
public function delete(TaskComment $comment): void
{
unset($this->comments[$comment->id()]);
}
}
@@ -0,0 +1,148 @@
<?php
use App\Models\User;
use App\Modules\Catalog\Infrastructure\Persistence\Eloquent\WorkTypeModel;
use App\Modules\Jobs\Infrastructure\Persistence\Eloquent\CustomerJobModel;
use App\Modules\Jobs\Infrastructure\Persistence\Eloquent\TaskCommentModel;
use App\Modules\Jobs\Infrastructure\Persistence\Eloquent\TaskModel;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Livewire\Livewire;
uses(RefreshDatabase::class);
beforeEach(function () {
$this->employee = User::factory()->create(['name' => 'Eetu Esimerkki']);
$this->actingAs($this->employee);
$this->job = CustomerJobModel::factory()->create(['status' => 'order', 'responsible_user_id' => $this->employee->id]);
$workType = WorkTypeModel::query()->where('name', 'graafinen työ')->sole();
$this->task = TaskModel::factory()->create(['customer_job_id' => $this->job->id, 'work_type_id' => $workType->id, 'title' => 'Mainoskuvat']);
$this->params = ['jobId' => $this->job->id, 'taskId' => $this->task->id, 'taskTitle' => 'Mainoskuvat'];
});
function taskComment(int $taskId, int $userId, string $body): TaskCommentModel
{
return TaskCommentModel::query()->create(['task_id' => $taskId, 'user_id' => $userId, 'body' => $body]);
}
it('shows the comments with author, time and their number on the job page', function () {
$this->travelTo(now()->setTimezone('Europe/Helsinki')->setDate(2026, 11, 25)->setTime(14, 5));
taskComment($this->task->id, $this->employee->id, 'Valot ok.');
taskComment($this->task->id, $this->employee->id, 'Mikit puuttuu.');
$this->get(route('jobs.show', $this->job->id))
->assertOk()
->assertSee('Kommentit:')
->assertSeeInOrder(['Eetu Esimerkki', '25.11.2026 klo 14.05', 'Valot ok.', 'Mikit puuttuu.']);
});
it('adds, edits and removes the user\'s own comment', function () {
$component = Livewire::test('jobs::task-comments', $this->params)
->set('newComment', " Valot ok.\nMikit puuttuu. ")
->call('add')
->assertHasNoErrors()
->assertSet('newComment', '')
->assertSee('Kommentti lisätty.')
->assertDispatched('task-comments-changed');
$comment = TaskCommentModel::query()->sole();
expect($comment->body)->toBe("Valot ok.\nMikit puuttuu.")
->and($comment->user_id)->toBe($this->employee->id);
$component->call('startEdit', $comment->id)
->assertSet('editingBody', "Valot ok.\nMikit puuttuu.")
->set('editingBody', 'Kaikki ok.')
->call('saveEdit')
->assertSee('Kommentti tallennettu.')
->assertSee('muokattu')
->assertSet('editingCommentId', null);
expect($comment->fresh()->body)->toBe('Kaikki ok.')
->and($comment->fresh()->edited_at)->not->toBeNull();
$component->call('confirmRemoval', $comment->id)
->assertSet('removalCommentId', $comment->id)
->call('remove')
->assertSee('Kommentti poistettu.');
expect(TaskCommentModel::query()->count())->toBe(0);
});
it('shows Finnish errors for empty and too long comments', function () {
Livewire::test('jobs::task-comments', $this->params)
->call('add')
->assertHasErrors('newComment')
->assertSee('Kirjoita kommentti.')
->set('newComment', str_repeat('a', 5001))
->call('add')
->assertSee('Kommentti voi olla enintään 5000 merkkiä.');
expect(TaskCommentModel::query()->count())->toBe(0);
});
it('does not let an employee edit or remove another user\'s comment', function () {
$comment = taskComment($this->task->id, User::factory()->create()->id, 'Toisen kommentti');
Livewire::test('jobs::task-comments', $this->params)
->assertDontSee('Muokkaa kommenttia')
->assertDontSee('Poista kommentti')
->call('startEdit', $comment->id)
->assertSet('editingCommentId', null)
->assertSee('Voit muokata ja poistaa vain omia kommenttejasi.')
->call('confirmRemoval', $comment->id)
->assertSet('removalCommentId', null);
expect($comment->fresh())->not->toBeNull();
});
it('lets management remove anyone\'s comment but not edit it', function () {
$comment = taskComment($this->task->id, $this->employee->id, 'Työntekijän kommentti');
$this->actingAs(User::factory()->management()->create());
Livewire::test('jobs::task-comments', $this->params)
->assertSee('Poista kommentti')
->assertDontSee('Muokkaa kommenttia')
->call('confirmRemoval', $comment->id)
->call('remove')
->assertSee('Kommentti poistettu.');
expect($comment->fresh())->toBeNull();
});
it('does not change comments of another task through this one', function () {
$other = taskComment(TaskModel::factory()->create()->id, $this->employee->id, 'Vieras');
Livewire::test('jobs::task-comments', $this->params)
->call('confirmRemoval', $other->id)
->assertSee('Kommenttia ei löytynyt. Päivitä sivu.')
->assertSet('removalCommentId', null);
expect($other->fresh())->not->toBeNull();
});
it('lets an attached subcontractor comment', function () {
$subcontractor = User::factory()->subcontractor()->create();
TaskModel::factory()->assignedTo([$subcontractor->id])->create(['customer_job_id' => $this->job->id, 'work_type_id' => $this->task->work_type_id]);
$this->actingAs($subcontractor);
Livewire::test('jobs::task-comments', $this->params)
->set('newComment', 'Aineisto toimitettu.')
->call('add')
->assertSee('Kommentti lisätty.');
expect(TaskCommentModel::query()->sole()->user_id)->toBe($subcontractor->id);
});
it('forbids the comments of a job the subcontractor is not attached to', function () {
$this->actingAs(User::factory()->subcontractor()->create());
Livewire::test('jobs::task-comments', $this->params)->assertForbidden();
});
it('removes the comments with their task', function () {
taskComment($this->task->id, $this->employee->id, 'Valot ok.');
$this->task->delete();
expect(TaskCommentModel::query()->count())->toBe(0);
});
@@ -0,0 +1,83 @@
<?php
use App\Modules\Jobs\Application\Services\TaskCommentFinder;
use App\Modules\Jobs\Application\UseCases\AddTaskComment;
use App\Modules\Jobs\Application\UseCases\EditTaskComment;
use App\Modules\Jobs\Application\UseCases\RemoveTaskComment;
use App\Modules\Jobs\Domain\Entities\Task;
use App\Modules\Jobs\Domain\Enums\TaskStatus;
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\Jobs\Domain\ValueObjects\TaskTitle;
use Tests\Fakes\FakeClock;
use Tests\Fakes\FakeTransactionManager;
use Tests\Fakes\InMemoryTaskCommentRepository;
use Tests\Fakes\InMemoryTaskRepository;
beforeEach(function () {
$tasks = new InMemoryTaskRepository;
$this->comments = new InMemoryTaskCommentRepository;
$transactions = new FakeTransactionManager;
$finder = new TaskCommentFinder($tasks, $this->comments);
$this->clock = new FakeClock(new DateTimeImmutable('2026-11-25'));
$this->jobId = 5;
$this->taskId = (int) $tasks->save(Task::reconstitute(1, $this->jobId, 11, new TaskTitle('Kuvaus'), null, TaskStatus::Waiting, null, null, null, [], 10))->id();
$this->add = new AddTaskComment($finder, $this->comments, $transactions, $this->clock);
$this->edit = new EditTaskComment($finder, $this->comments, $transactions, $this->clock);
$this->remove = new RemoveTaskComment($finder, $this->comments, $transactions);
});
it('keeps line breaks of a comment and rejects empty and too long comments', function () {
expect((new TaskCommentBody(" Valot ok.\r\nMikit puuttuu. \n"))->value)->toBe("Valot ok.\nMikit puuttuu.")
->and(fn () => new TaskCommentBody(" \n "))->toThrow(InvalidTaskCommentBody::class)
->and(fn () => new TaskCommentBody(str_repeat('a', 5001)))->toThrow(InvalidTaskCommentBody::class);
});
it('adds a comment with its author and time', function () {
$commentId = $this->add->execute($this->jobId, $this->taskId, 10, 'Valot ok.');
$comment = $this->comments->comments[$commentId];
expect($comment->authorId())->toBe(10)
->and($comment->body()->value)->toBe('Valot ok.')
->and($comment->createdAt()->format('Y-m-d H:i'))->toBe('2026-11-25 12:00')
->and($comment->editedAt())->toBeNull();
});
it('lets only the author edit a comment and marks it edited', function () {
$commentId = $this->add->execute($this->jobId, $this->taskId, 10, 'Valot ok.');
$this->edit->execute($this->jobId, $this->taskId, $commentId, 10, 'Valot ok.');
expect($this->comments->comments[$commentId]->editedAt())->toBeNull();
$this->clock->today = new DateTimeImmutable('2026-11-26');
$this->edit->execute($this->jobId, $this->taskId, $commentId, 10, 'Valot ja mikit ok.');
expect($this->comments->comments[$commentId]->body()->value)->toBe('Valot ja mikit ok.')
->and($this->comments->comments[$commentId]->editedAt()?->format('Y-m-d'))->toBe('2026-11-26')
->and(fn () => $this->edit->execute($this->jobId, $this->taskId, $commentId, 12, 'Muutettu'))->toThrow(NotTaskCommentAuthor::class);
});
it('lets the author remove a comment, and others only when they may remove anyone\'s', function () {
$own = $this->add->execute($this->jobId, $this->taskId, 10, 'Oma');
$other = $this->add->execute($this->jobId, $this->taskId, 10, 'Toisen');
expect(fn () => $this->remove->execute($this->jobId, $this->taskId, $other, 12, false))->toThrow(NotTaskCommentAuthor::class);
$this->remove->execute($this->jobId, $this->taskId, $own, 10, false);
$this->remove->execute($this->jobId, $this->taskId, $other, 12, true);
expect($this->comments->comments)->toBe([]);
});
it('does not touch comments of a task through another job', function () {
$commentId = $this->add->execute($this->jobId, $this->taskId, 10, 'Valot ok.');
expect(fn () => $this->add->execute(99, $this->taskId, 10, 'Vieras'))->toThrow(TaskNotFound::class)
->and(fn () => $this->edit->execute(99, $this->taskId, $commentId, 10, 'Vieras'))->toThrow(TaskNotFound::class)
->and(fn () => $this->remove->execute($this->jobId, $this->taskId, 404, 10, true))->toThrow(TaskCommentNotFound::class);
});