Add time tracking on tasks with a weekly sheet
Phase 1, item 8 (last of phase 1). New TimeTracking module:
- One entry per user, task and day (0.25-24 h in quarters, with a note);
the job and work type are copied from the task. A user's day cannot
exceed 24 h, and hours on a done, invoiced, billed or archived job
cannot be added or changed.
- Weekly sheet /time: tasks as rows (own open tasks, tasks with hours
that week, tasks added by search), days as columns, row, day and week
totals, week navigation. A cell saves on change and an empty cell
removes the entry; on phones one day at a time. Saves and errors are
announced, errors are tied to their cell.
- Task booking page /time/tasks/{task}: date, hours and note, added to
the day's entry; own entries with edit and remove.
- Anyone but subcontractors may book on any unlocked task;
subcontractors only on jobs they are attached to.
Jobs module:
- JobStatus::locksTime(), BookableTask and task queries for booking.
- A task with time entries cannot be removed (TaskUsage contract,
provided by TimeTracking); the job page links tasks to booking.
- The "involved in a job" rule is shared in JobInvolvement.
Spec and CLAUDE.md updated.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
@@ -0,0 +1,78 @@
|
||||
<?php
|
||||
|
||||
use App\Models\User;
|
||||
use App\Modules\Jobs\Infrastructure\Persistence\Eloquent\TaskModel;
|
||||
use App\Modules\TimeTracking\Infrastructure\Persistence\Eloquent\TimeEntryModel;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
|
||||
uses(RefreshDatabase::class);
|
||||
|
||||
beforeEach(function () {
|
||||
$this->user = User::factory()->create();
|
||||
$this->actingAs($this->user);
|
||||
$this->task = TaskModel::factory()->assignedTo([$this->user->id])->create(['title' => 'Mainoskuvat']);
|
||||
$this->monday = now()->startOfWeek()->toDateString();
|
||||
TimeEntryModel::factory()->create(['task_id' => $this->task->id, 'user_id' => $this->user->id, 'worked_on' => $this->monday, 'hours' => 1.5, 'note' => 'Luonnokset']);
|
||||
});
|
||||
|
||||
it('has no accessibility issues on the time pages and no sideways scrolling', function (string $page, string $device, string $colorScheme) {
|
||||
$url = $page === 'week' ? route('time.week') : route('time.task', $this->task->id);
|
||||
|
||||
$visit = visit($url);
|
||||
$visit = $device === 'mobile' ? $visit->on()->mobile() : $visit->on()->desktop();
|
||||
$visit = $colorScheme === 'dark' ? $visit->inDarkMode() : $visit->inLightMode();
|
||||
|
||||
$visit->assertSee('Mainoskuvat')
|
||||
->assertScript('document.documentElement.scrollWidth <= window.innerWidth', true)
|
||||
->assertNoJavaScriptErrors()
|
||||
->assertNoAccessibilityIssues(level: 2);
|
||||
})->with(['week', 'task page'])->with([
|
||||
'desktop, light' => ['desktop', 'light'],
|
||||
'desktop, dark' => ['desktop', 'dark'],
|
||||
'mobile, light' => ['mobile', 'light'],
|
||||
'mobile, dark' => ['mobile', 'dark'],
|
||||
]);
|
||||
|
||||
it('records hours in the weekly view with the keyboard', function () {
|
||||
$tuesday = now()->startOfWeek()->addDay()->toDateString();
|
||||
$cell = '#cell-'.$this->task->id.'-'.$tuesday;
|
||||
|
||||
visit(route('time.week'))
|
||||
->on()->desktop()
|
||||
->type($cell, '2,5')
|
||||
->keys($cell, 'Tab')
|
||||
->assertSee('Tallennettu: Mainoskuvat')
|
||||
->assertSee('Viikko yhteensä: 4 h')
|
||||
->assertNoAccessibilityIssues(level: 2);
|
||||
|
||||
expect((float) TimeEntryModel::query()->whereDate('worked_on', $tuesday)->sole()->hours)->toBe(2.5);
|
||||
});
|
||||
|
||||
it('shows an invalid value as an error at the field', function (string $device) {
|
||||
$selector = $device === 'mobile'
|
||||
? '#mcell-'.$this->task->id.'-'.$this->monday
|
||||
: '#cell-'.$this->task->id.'-'.$this->monday;
|
||||
|
||||
$visit = visit(route('time.week'));
|
||||
$visit = $device === 'mobile' ? $visit->on()->mobile() : $visit->on()->desktop();
|
||||
|
||||
if ($device === 'mobile') {
|
||||
$visit->click('[aria-pressed][wire\\:click="selectDay(\''.$this->monday.'\')"]');
|
||||
}
|
||||
|
||||
$visit->clear($selector)
|
||||
->type($selector, '1,1')
|
||||
->keys($selector, 'Tab')
|
||||
->assertSee('Anna tunnit vartin tarkkuudella')
|
||||
->assertAttribute($selector, 'aria-invalid', 'true')
|
||||
->assertNoAccessibilityIssues(level: 2);
|
||||
})->with(['desktop', 'mobile']);
|
||||
|
||||
it('records hours from the task page', function () {
|
||||
visit(route('time.task', $this->task->id))
|
||||
->fill('[wire\\:model="workedOn"]', $this->monday)
|
||||
->fill('[wire\\:model="hours"]', '2')
|
||||
->press('Kirjaa tunnit')
|
||||
->assertSee('kirjaus on nyt 3,5 h.')
|
||||
->assertNoAccessibilityIssues(level: 2);
|
||||
});
|
||||
@@ -0,0 +1,53 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Fakes;
|
||||
|
||||
use App\Modules\Jobs\Application\Contracts\TaskQueries;
|
||||
use App\Modules\Jobs\Application\DTOs\BookableTask;
|
||||
use App\Modules\Jobs\Application\DTOs\TaskSummary;
|
||||
use App\Modules\Jobs\Domain\Enums\JobStatus;
|
||||
use App\Modules\Jobs\Domain\Enums\TaskStatus;
|
||||
|
||||
/**
|
||||
* Tasks for unit tests of other modules, as time is recorded on them.
|
||||
*/
|
||||
class FakeTaskQueries implements TaskQueries
|
||||
{
|
||||
/** @var array<int, BookableTask> */
|
||||
public array $tasks = [];
|
||||
|
||||
public function addTask(int $taskId, int $jobId = 1, JobStatus $jobStatus = JobStatus::Order, int $workTypeId = 7): void
|
||||
{
|
||||
$this->tasks[$taskId] = new BookableTask($taskId, $jobId, '2026-0001', 'Kampanja', 'Tehtävä '.$taskId, $workTypeId, 'graafinen työ', $jobStatus, TaskStatus::Waiting);
|
||||
}
|
||||
|
||||
public function tasksOf(int $jobId): array
|
||||
{
|
||||
return [];
|
||||
}
|
||||
|
||||
public function findTask(int $jobId, int $taskId): ?TaskSummary
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
public function bookableTask(int $taskId): ?BookableTask
|
||||
{
|
||||
return $this->tasks[$taskId] ?? null;
|
||||
}
|
||||
|
||||
public function bookableTasks(array $taskIds): array
|
||||
{
|
||||
return array_values(array_intersect_key($this->tasks, array_flip($taskIds)));
|
||||
}
|
||||
|
||||
public function assignedOpenTaskIds(int $userId): array
|
||||
{
|
||||
return [];
|
||||
}
|
||||
|
||||
public function searchBookableTasks(string $term, ?int $onlyMemberUserId = null, int $limit = 8): array
|
||||
{
|
||||
return [];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Fakes;
|
||||
|
||||
use App\Modules\Jobs\Application\Contracts\TaskUsage;
|
||||
|
||||
class FakeTaskUsage implements TaskUsage
|
||||
{
|
||||
/** @var list<int> */
|
||||
public array $tasksWithTime = [];
|
||||
|
||||
public function hasTimeEntries(int $taskId): bool
|
||||
{
|
||||
return in_array($taskId, $this->tasksWithTime, true);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Fakes;
|
||||
|
||||
use App\Modules\TimeTracking\Domain\Contracts\TimeEntryRepository;
|
||||
use App\Modules\TimeTracking\Domain\Entities\TimeEntry;
|
||||
use DateTimeImmutable;
|
||||
|
||||
class InMemoryTimeEntryRepository implements TimeEntryRepository
|
||||
{
|
||||
/** @var array<int, TimeEntry> */
|
||||
public array $entries = [];
|
||||
|
||||
private int $nextId = 1;
|
||||
|
||||
public function findForDay(int $userId, int $taskId, DateTimeImmutable $day): ?TimeEntry
|
||||
{
|
||||
foreach ($this->entries as $entry) {
|
||||
if ($entry->userId() === $userId && $entry->taskId() === $taskId && $entry->workedOn()->format('Y-m-d') === $day->format('Y-m-d')) {
|
||||
return clone $entry;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public function findForUser(int $userId, int $entryId): ?TimeEntry
|
||||
{
|
||||
$entry = $this->entries[$entryId] ?? null;
|
||||
|
||||
return $entry !== null && $entry->userId() === $userId ? clone $entry : null;
|
||||
}
|
||||
|
||||
public function save(TimeEntry $entry): TimeEntry
|
||||
{
|
||||
$id = $entry->id() ?? $this->nextId++;
|
||||
$this->entries[$id] = TimeEntry::reconstitute($id, $entry->userId(), $entry->taskId(), $entry->jobId(), $entry->workTypeId(), $entry->workedOn(), $entry->hours(), $entry->note());
|
||||
|
||||
return clone $this->entries[$id];
|
||||
}
|
||||
|
||||
public function delete(TimeEntry $entry): void
|
||||
{
|
||||
unset($this->entries[$entry->id()]);
|
||||
}
|
||||
|
||||
public function quartersOn(int $userId, DateTimeImmutable $day, ?int $exceptEntryId = null): int
|
||||
{
|
||||
$quarters = 0;
|
||||
|
||||
foreach ($this->entries as $id => $entry) {
|
||||
if ($id !== $exceptEntryId && $entry->userId() === $userId && $entry->workedOn()->format('Y-m-d') === $day->format('Y-m-d')) {
|
||||
$quarters += $entry->hours()->quarters;
|
||||
}
|
||||
}
|
||||
|
||||
return $quarters;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,61 @@
|
||||
<?php
|
||||
|
||||
use App\Models\User;
|
||||
use App\Modules\Jobs\Application\UseCases\RemoveTask;
|
||||
use App\Modules\Jobs\Domain\Exceptions\TaskHasTimeEntries;
|
||||
use App\Modules\Jobs\Infrastructure\Persistence\Eloquent\TaskModel;
|
||||
use App\Modules\TimeTracking\Infrastructure\Persistence\Eloquent\TimeEntryModel;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Livewire\Livewire;
|
||||
|
||||
uses(RefreshDatabase::class);
|
||||
|
||||
beforeEach(function () {
|
||||
$this->employee = User::factory()->create();
|
||||
$this->actingAs($this->employee);
|
||||
$this->task = TaskModel::factory()->assignedTo([$this->employee->id])->create(['title' => 'Mainoskuvat']);
|
||||
$this->monday = now()->startOfWeek()->toDateString();
|
||||
});
|
||||
|
||||
it('saves and clears a cell of the weekly view', function () {
|
||||
$this->get(route('time.week'))->assertOk()->assertSee('Mainoskuvat');
|
||||
|
||||
Livewire::test('time::week')
|
||||
->call('saveCell', $this->task->id, $this->monday, '2,5')
|
||||
->assertSee('2,5 h.')
|
||||
->call('saveCell', $this->task->id, $this->monday, '1,1')
|
||||
->assertHasErrors(["cells.{$this->task->id}.{$this->monday}"]);
|
||||
|
||||
expect((float) TimeEntryModel::query()->sole()->hours)->toBe(2.5);
|
||||
|
||||
Livewire::test('time::week')->call('saveCell', $this->task->id, $this->monday, '');
|
||||
|
||||
expect(TimeEntryModel::query()->count())->toBe(0);
|
||||
});
|
||||
|
||||
it('adds hours from the task page to the day entry and locks done jobs', function () {
|
||||
Livewire::test('time::task', ['task' => $this->task->id])
|
||||
->set('workedOn', $this->monday)
|
||||
->set('hours', '2')
|
||||
->call('save')
|
||||
->set('hours', '1,5')
|
||||
->set('note', 'Lisää kuvia')
|
||||
->call('save')
|
||||
->assertSee('kirjaus on nyt 3,5 h.');
|
||||
|
||||
expect(TimeEntryModel::query()->count())->toBe(1);
|
||||
|
||||
$this->task->customer_job_id && DB::table('customer_jobs')->where('id', $this->task->customer_job_id)->update(['status' => 'done']);
|
||||
|
||||
Livewire::test('time::task', ['task' => $this->task->id])->assertSee('tunnit on lukittu');
|
||||
});
|
||||
|
||||
it('keeps subcontractors to their own jobs and blocks removing tasks with hours', function () {
|
||||
TimeEntryModel::factory()->create(['task_id' => $this->task->id, 'user_id' => $this->employee->id]);
|
||||
|
||||
expect(fn () => app(RemoveTask::class)->execute($this->task->customer_job_id, $this->task->id))
|
||||
->toThrow(TaskHasTimeEntries::class);
|
||||
|
||||
$this->actingAs(User::factory()->subcontractor()->create());
|
||||
$this->get(route('time.task', $this->task->id))->assertForbidden();
|
||||
});
|
||||
@@ -14,12 +14,14 @@ use App\Modules\Jobs\Domain\Enums\JobStatus;
|
||||
use App\Modules\Jobs\Domain\Enums\TaskStatus;
|
||||
use App\Modules\Jobs\Domain\Exceptions\InvalidTaskReference;
|
||||
use App\Modules\Jobs\Domain\Exceptions\InvalidTaskTitle;
|
||||
use App\Modules\Jobs\Domain\Exceptions\TaskHasTimeEntries;
|
||||
use App\Modules\Jobs\Domain\Exceptions\TaskNotFound;
|
||||
use App\Modules\Jobs\Domain\Exceptions\TasksNotAllowedYet;
|
||||
use App\Modules\Jobs\Domain\ValueObjects\TaskTitle;
|
||||
use Tests\Fakes\FakeCatalogQueries;
|
||||
use Tests\Fakes\FakeClock;
|
||||
use Tests\Fakes\FakeCustomerQueries;
|
||||
use Tests\Fakes\FakeTaskUsage;
|
||||
use Tests\Fakes\FakeTransactionManager;
|
||||
use Tests\Fakes\FakeUserAccountQueries;
|
||||
use Tests\Fakes\InMemoryCustomerJobRepository;
|
||||
@@ -51,7 +53,8 @@ beforeEach(function () {
|
||||
$this->addTask = new AddTask($this->jobs, $this->tasks, $checker, $transactions);
|
||||
$this->updateTask = new UpdateTask($this->jobs, $this->tasks, $checker, $transactions);
|
||||
$this->changeTaskStatus = new ChangeTaskStatus($this->tasks, $transactions);
|
||||
$this->removeTask = new RemoveTask($this->tasks, $transactions);
|
||||
$this->usage = new FakeTaskUsage;
|
||||
$this->removeTask = new RemoveTask($this->tasks, $this->usage, $transactions);
|
||||
$this->changeJobStatus = new ChangeJobStatus($this->jobs, $transactions);
|
||||
});
|
||||
|
||||
@@ -123,3 +126,13 @@ it('does not touch a task of another job', function (string $action) {
|
||||
'remove' => $this->removeTask->execute($this->tentativeJobId, $taskId),
|
||||
};
|
||||
})->throws(TaskNotFound::class)->with(['update', 'status', 'remove']);
|
||||
|
||||
it('does not remove a task with time entries', function () {
|
||||
$taskId = $this->addTask->execute($this->jobId, new TaskData('Mainoskuvat', 11, 'waiting'), 10);
|
||||
$this->usage->tasksWithTime = [$taskId];
|
||||
|
||||
expect(fn () => $this->removeTask->execute($this->jobId, $taskId))->toThrow(TaskHasTimeEntries::class)
|
||||
->and($this->tasks->tasks)->toHaveCount(1)
|
||||
->and(JobStatus::Done->locksTime())->toBeTrue()
|
||||
->and(JobStatus::OnHold->locksTime())->toBeFalse();
|
||||
});
|
||||
|
||||
@@ -0,0 +1,121 @@
|
||||
<?php
|
||||
|
||||
use App\Modules\Jobs\Domain\Enums\JobStatus;
|
||||
use App\Modules\TimeTracking\Application\Services\TimeBooking;
|
||||
use App\Modules\TimeTracking\Application\UseCases\AddTaskHours;
|
||||
use App\Modules\TimeTracking\Application\UseCases\RemoveTimeEntry;
|
||||
use App\Modules\TimeTracking\Application\UseCases\SetDayHours;
|
||||
use App\Modules\TimeTracking\Application\UseCases\UpdateTimeEntry;
|
||||
use App\Modules\TimeTracking\Domain\Entities\TimeEntry;
|
||||
use App\Modules\TimeTracking\Domain\Exceptions\DailyHoursExceeded;
|
||||
use App\Modules\TimeTracking\Domain\Exceptions\InvalidWorkedHours;
|
||||
use App\Modules\TimeTracking\Domain\Exceptions\TaskNotBookable;
|
||||
use App\Modules\TimeTracking\Domain\Exceptions\TimeEntryNotFound;
|
||||
use App\Modules\TimeTracking\Domain\Exceptions\TimeLocked;
|
||||
use App\Modules\TimeTracking\Domain\ValueObjects\WorkedHours;
|
||||
use Tests\Fakes\FakeTaskQueries;
|
||||
use Tests\Fakes\FakeTransactionManager;
|
||||
use Tests\Fakes\InMemoryTimeEntryRepository;
|
||||
|
||||
beforeEach(function () {
|
||||
$this->tasks = new FakeTaskQueries;
|
||||
$this->entries = new InMemoryTimeEntryRepository;
|
||||
$booking = new TimeBooking($this->tasks, $this->entries);
|
||||
$transactions = new FakeTransactionManager;
|
||||
|
||||
$this->setDayHours = new SetDayHours($this->entries, $booking, $transactions);
|
||||
$this->addTaskHours = new AddTaskHours($this->entries, $booking, $transactions);
|
||||
$this->updateEntry = new UpdateTimeEntry($this->entries, $booking, $transactions);
|
||||
$this->removeEntry = new RemoveTimeEntry($this->entries, $booking, $transactions);
|
||||
|
||||
$this->tasks->addTask(1);
|
||||
$this->tasks->addTask(2);
|
||||
$this->tasks->addTask(3, jobStatus: JobStatus::Done);
|
||||
});
|
||||
|
||||
it('parses hours in quarters, with a comma or a point', function (string $input, ?string $formatted) {
|
||||
expect(WorkedHours::parse($input)?->format())->toBe($formatted);
|
||||
})->with([
|
||||
'quarter' => ['0,25', '0,25'],
|
||||
'comma' => ['7,5', '7,5'],
|
||||
'point' => ['7.50', '7,5'],
|
||||
'whole day' => ['24', '24'],
|
||||
'empty' => ['', null],
|
||||
]);
|
||||
|
||||
it('rejects hours that are not quarters or out of range', function (string $input) {
|
||||
WorkedHours::parse($input);
|
||||
})->throws(InvalidWorkedHours::class)->with(['0,1', '24,25', '-1', 'kaksi', '0']);
|
||||
|
||||
it('adds hours and continues the note of a day entry', function () {
|
||||
$entry = TimeEntry::create(10, 1, 1, 7, new DateTimeImmutable('2026-05-18'), new WorkedHours(8), 'Luonnokset');
|
||||
|
||||
$entry->addHours(new WorkedHours(2), 'Korjaukset');
|
||||
$entry->addHours(new WorkedHours(1), ' ');
|
||||
|
||||
expect($entry->hours()->format())->toBe('2,75')
|
||||
->and($entry->note())->toBe("Luonnokset\nKorjaukset");
|
||||
});
|
||||
|
||||
it('sets a cell, changes it and clears it', function () {
|
||||
$this->setDayHours->execute(10, 1, '2026-05-18', '2,5');
|
||||
$this->setDayHours->execute(10, 1, '2026-05-18', '3');
|
||||
|
||||
expect($this->entries->entries)->toHaveCount(1)
|
||||
->and(array_values($this->entries->entries)[0]->hours()->format())->toBe('3')
|
||||
->and(array_values($this->entries->entries)[0]->workTypeId())->toBe(7);
|
||||
|
||||
$this->setDayHours->execute(10, 1, '2026-05-18', '0');
|
||||
expect($this->entries->entries)->toBeEmpty();
|
||||
|
||||
$this->setDayHours->execute(10, 1, '2026-05-18', '');
|
||||
expect($this->entries->entries)->toBeEmpty();
|
||||
});
|
||||
|
||||
it('adds hours from the task page to the day entry', function () {
|
||||
$this->addTaskHours->execute(10, 1, '2026-05-18', '2', 'Aamu');
|
||||
$total = $this->addTaskHours->execute(10, 1, '2026-05-18', '1,5', 'Iltapäivä');
|
||||
|
||||
expect($total)->toBe('3,5')
|
||||
->and($this->entries->entries)->toHaveCount(1);
|
||||
});
|
||||
|
||||
it('keeps a day within 24 hours across tasks', function () {
|
||||
$this->setDayHours->execute(10, 1, '2026-05-18', '20');
|
||||
|
||||
expect(fn () => $this->setDayHours->execute(10, 2, '2026-05-18', '4,25'))->toThrow(DailyHoursExceeded::class);
|
||||
|
||||
$this->setDayHours->execute(10, 2, '2026-05-18', '4');
|
||||
$this->setDayHours->execute(10, 1, '2026-05-18', '19');
|
||||
$this->setDayHours->execute(11, 1, '2026-05-18', '8');
|
||||
|
||||
expect($this->entries->quartersOn(10, new DateTimeImmutable('2026-05-18')))->toBe(92);
|
||||
});
|
||||
|
||||
it('does not record or change hours on a locked job or an unknown task', function () {
|
||||
expect(fn () => $this->setDayHours->execute(10, 3, '2026-05-18', '1'))->toThrow(TimeLocked::class)
|
||||
->and(fn () => $this->addTaskHours->execute(10, 99, '2026-05-18', '1'))->toThrow(TaskNotBookable::class);
|
||||
|
||||
$this->setDayHours->execute(10, 1, '2026-05-18', '2');
|
||||
$entryId = array_key_first($this->entries->entries);
|
||||
$this->tasks->addTask(1, jobStatus: JobStatus::Invoiced);
|
||||
|
||||
expect(fn () => $this->updateEntry->execute(10, $entryId, '3', null))->toThrow(TimeLocked::class)
|
||||
->and(fn () => $this->removeEntry->execute(10, $entryId))->toThrow(TimeLocked::class)
|
||||
->and($this->entries->entries)->toHaveCount(1);
|
||||
});
|
||||
|
||||
it('lets users change only their own entries', function () {
|
||||
$this->setDayHours->execute(10, 1, '2026-05-18', '2');
|
||||
$entryId = array_key_first($this->entries->entries);
|
||||
|
||||
expect(fn () => $this->updateEntry->execute(11, $entryId, '3', null))->toThrow(TimeEntryNotFound::class)
|
||||
->and(fn () => $this->removeEntry->execute(11, $entryId))->toThrow(TimeEntryNotFound::class);
|
||||
|
||||
$this->updateEntry->execute(10, $entryId, '3,75', 'Muutettu');
|
||||
expect($this->entries->entries[$entryId]->hours()->format())->toBe('3,75')
|
||||
->and($this->entries->entries[$entryId]->note())->toBe('Muutettu');
|
||||
|
||||
$this->removeEntry->execute(10, $entryId);
|
||||
expect($this->entries->entries)->toBeEmpty();
|
||||
});
|
||||
Reference in New Issue
Block a user