Give tasks one assignee and members, and schedule template tasks to due dates

- A task now has one optional assignee (tasks.assignee_user_id), whose
  calendar row it is on and whose days its estimate books, and members
  (task_members) who see it among their own jobs and in the weekly sheet
  but get no bookings. A migration moves existing assignees over: the one
  with most booked time becomes the assignee, the others members.
- Adding tasks from a workflow template asks for each step's assignee and,
  by default, schedules tasks with a due date to end on it, booking the
  estimate backwards from the due date (not before today); what does not
  fit runs past the due date and is reported as late.
- The task calendar leaves quick sale tasks out of the unscheduled list,
  shows a bar's job details in a popover on hover and focus, and shows the
  day load on its own grid line.
- The template table stacks its cells on narrow screens to reflow at 320 px.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
2026-11-24 19:01:11 +02:00
co-authored by Claude Opus 5.5
parent b4f2f1cfaa
commit 18598cc08e
45 changed files with 1133 additions and 248 deletions
+2
View File
@@ -250,6 +250,8 @@ it('adds tasks from a workflow template with the keyboard, without accessibility
->keys('[value="'.$templateId.'"]', 'Space')
->assertSee('Pohjan Lyhyt video tehtävät')
->assertNoAccessibilityIssues(level: 2)
->resize(320, 900)
->assertScript('document.documentElement.scrollWidth <= window.innerWidth', true)
->type('#row-1-hours', '2,5')
->keys('main form button[type="submit"]', 'Enter')
->assertSee('Lisättiin 2 tehtävää pohjasta Lyhyt video (videokuvaus).')
@@ -5,7 +5,6 @@ use App\Modules\Catalog\Infrastructure\Persistence\Eloquent\WorkTypeModel;
use App\Modules\Jobs\Infrastructure\Persistence\Eloquent\CustomerJobModel;
use App\Modules\Jobs\Infrastructure\Persistence\Eloquent\TaskModel;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\DB;
uses(RefreshDatabase::class);
@@ -61,6 +60,28 @@ it('reschedules a task with the keyboard and returns focus to its bar', function
expect($this->task->refresh()->planned_from?->format('Y-m-d'))->toBe(now()->addDay()->toDateString());
});
it('shows the job details of a bar on hover and focus, and closes them with Escape', function (string $colorScheme) {
$key = $this->task->id.'-'.$this->user->id;
$open = "document.getElementById('bar-info-{$key}').matches(':popover-open')";
$visit = visit(route('jobs.calendar'))->on()->desktop();
$visit = $colorScheme === 'dark' ? $visit->inDarkMode() : $visit->inLightMode();
$visit->hover('#bar-'.$key)
->assertScript($open, true)
->assertSee('Messuvideo')
->assertSee('Johanna Johto')
->assertNoAccessibilityIssues(level: 2)
->hover('#bar-info-'.$key)
->assertScript($open, true)
->keys('#bar-'.$key, 'Escape')
->assertScript($open, false)
->keys('#bar-'.$key, 'Tab')
->keys('#bar-'.$key, 'Shift')
->assertScript($open, true)
->assertNoJavaScriptErrors();
})->with(['light', 'dark']);
it('moves a task to another person by dragging its bar', function () {
// The bar (today – today + 2) is grabbed at its middle day, so dropping it
// on today + 4 moves it three days later.
@@ -72,7 +93,7 @@ it('moves a task to another person by dragging its bar', function () {
->assertSee('Tehtävä Leikkaus: Aino Ahkera');
expect($this->task->refresh()->planned_from?->format('Y-m-d'))->toBe(now()->addDays(3)->toDateString())
->and(DB::table('task_assignees')->where('task_id', $this->task->id)->pluck('user_id')->all())->toBe([$this->other->id]);
->and($this->task->assignee_user_id)->toBe($this->other->id);
});
it('has no accessibility issues on the holidays page', function (string $colorScheme) {
+2 -1
View File
@@ -42,7 +42,8 @@ class InMemoryTaskRepository implements TaskRepository
$task->status(),
$task->dueOn(),
$task->estimatedHours(),
$task->assigneeUserIds(),
$task->assigneeUserId(),
$task->memberUserIds(),
$task->createdBy(),
$task->plannedFrom(),
$task->plannedTo(),
@@ -0,0 +1,35 @@
<?php
use App\Models\User;
use App\Modules\Jobs\Infrastructure\Persistence\Eloquent\TaskModel;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\DB;
uses(RefreshDatabase::class);
it('makes the earlier assignee with most booked time the assignee and the others members', function () {
[$eero, $aino, $ville] = User::factory()->count(3)->create()->all();
$booked = TaskModel::factory()->create();
$unbooked = TaskModel::factory()->create();
$this->artisan('migrate:rollback', ['--step' => 1])->assertSuccessful();
DB::table('task_assignees')->insert([
['task_id' => $booked->id, 'user_id' => $eero->id],
['task_id' => $booked->id, 'user_id' => $aino->id],
['task_id' => $unbooked->id, 'user_id' => $ville->id],
['task_id' => $unbooked->id, 'user_id' => $aino->id],
]);
DB::table('task_bookings')->insert([
['task_id' => $booked->id, 'user_id' => $eero->id, 'day' => '2026-12-07', 'minutes' => 60],
['task_id' => $booked->id, 'user_id' => $aino->id, 'day' => '2026-12-07', 'minutes' => 240],
]);
$this->artisan('migrate')->assertSuccessful();
expect($booked->refresh()->assignee_user_id)->toBe($aino->id)
->and($unbooked->refresh()->assignee_user_id)->toBe(min($aino->id, $ville->id))
->and(DB::table('task_members')->where('task_id', $booked->id)->pluck('user_id')->all())->toBe([$eero->id])
->and(DB::table('task_members')->where('task_id', $unbooked->id)->pluck('user_id')->all())->toBe([max($aino->id, $ville->id)])
->and(DB::table('task_bookings')->pluck('user_id')->all())->toBe([$aino->id]);
});
@@ -3,6 +3,7 @@
use App\Models\User;
use App\Modules\Catalog\Infrastructure\Persistence\Eloquent\ServiceUnitModel;
use App\Modules\Catalog\Infrastructure\Persistence\Eloquent\WorkTypeModel;
use App\Modules\Customers\Infrastructure\Persistence\Eloquent\CustomerModel;
use App\Modules\Jobs\Infrastructure\Persistence\Eloquent\CustomerJobModel;
use App\Modules\Jobs\Infrastructure\Persistence\Eloquent\TaskModel;
use Illuminate\Foundation\Testing\RefreshDatabase;
@@ -53,7 +54,7 @@ it('moves a task to another person by dropping it and books their days', functio
expect($task->refresh()->planned_from?->format('Y-m-d'))->toBe('2026-12-28')
->and($task->planned_to?->format('Y-m-d'))->toBe('2026-12-29')
->and(DB::table('task_assignees')->where('task_id', $task->id)->pluck('user_id')->all())->toBe([$this->other->id])
->and($task->assignee_user_id)->toBe($this->other->id)
->and(DB::table('task_bookings')->where('task_id', $task->id)->orderBy('day')->pluck('minutes', 'day')->all())->toBe(['2026-12-28' => 360, '2026-12-29' => 360]);
});
@@ -106,7 +107,7 @@ it('shows a subcontractor only their own row and lets them change only the statu
->assertOk()
->assertSee('Oma tehtävä')
->assertDontSee('Toisen tehtävä')
->assertDontSee('Eero Työntekijä')
->assertDontSee('id="row-'.$this->employee->id.'-name"', escape: false)
->assertDontSee('Ei tekijää');
Livewire::test('jobs::calendar')
@@ -137,3 +138,43 @@ it('adds a schedule on the task form', function () {
expect($task->planned_to?->format('Y-m-d'))->toBe('2026-12-28')
->and(DB::table('task_bookings')->where('task_id', $task->id)->sum('minutes'))->toBe(240);
});
it('shows a task only on its assignee row and books nothing for its members', function () {
$task = TaskModel::factory()->assignedTo([$this->other->id, $this->employee->id])->create(['customer_job_id' => $this->job->id, 'work_type_id' => $this->graphic->id, 'title' => 'Mainoskuvat', 'estimated_hours' => 4]);
Livewire::test('jobs::calendar')->call('reschedule', $task->id, $this->job->id, $this->other->id, $this->other->id, '2026-12-22', '2026-12-22');
expect(DB::table('task_bookings')->where('task_id', $task->id)->pluck('user_id')->unique()->values()->all())->toBe([$this->other->id]);
Livewire::test('jobs::calendar')
->set('mine', true)
->assertDontSee('Mainoskuvat');
});
it('leaves the tasks of quick sale jobs out of the unscheduled tasks', function () {
$quickSales = CustomerJobModel::factory()->create(['name' => 'Pikamyynnit 2026', 'status' => 'in_production', 'quick_sale_year' => 2026]);
calendarTaskModel(['customer_job_id' => $quickSales->id, 'work_type_id' => $this->graphic->id, 'title' => 'Passikuvat'], []);
calendarTaskModel(['customer_job_id' => $this->job->id, 'work_type_id' => $this->graphic->id, 'title' => 'Mainoskuvat'], [$this->employee->id]);
Livewire::test('jobs::calendar')
->assertSee('Mainoskuvat')
->assertDontSee('Passikuvat');
});
it('shows the job details in the info popover of a bar', function () {
$this->job->update(['due_on' => '2026-12-31']);
calendarTaskModel(['customer_job_id' => $this->job->id, 'work_type_id' => $this->graphic->id, 'title' => 'Mainoskuvat', 'description' => 'Kolme kuvaa somekanaviin', 'planned_from' => '2026-12-22', 'planned_to' => '2026-12-23'], [$this->other->id]);
$task = TaskModel::query()->sole();
Livewire::test('jobs::calendar')
->assertSee('aria-describedby="bar-info-'.$task->id.'-'.$this->other->id.'"', escape: false)
->assertSeeInOrder([
'Työ', $this->job->number, 'Kampanja',
'Asiakas', CustomerModel::query()->findOrFail($this->job->customer_id)->name,
'Työn tila', 'Tilaus',
'Työn määräpäivä', '31.12.2026',
'Vastuuhenkilö', 'Eero Työntekijä',
'Työtehtävä', 'graafinen työ',
'Kolme kuvaa somekanaviin',
]);
});
@@ -55,7 +55,7 @@ it('adds the steps of a template as tasks of the job', function () {
['Kuvat', $this->graphic->id, 'waiting', null],
['Julkaisu', $this->graphic->id, 'waiting', null],
])
->and(DB::table('task_assignees')->pluck('user_id')->unique()->values()->all())->toBe([$this->employee->id]);
->and($tasks->pluck('assignee_user_id')->unique()->values()->all())->toBe([$this->employee->id]);
$this->get(route('jobs.show', $this->job->id))->assertSee('Lisättiin 3 tehtävää pohjasta Somekampanja (graafinen työ).');
});
@@ -103,13 +103,16 @@ it('shows a table of the chosen template tasks and saves their due dates and est
Livewire::test('jobs::tasks-from-template', ['job' => $this->job->id])
->assertDontSee('Pohjan Somekampanja tehtävät')
->set('templateId', (string) $this->campaign)
->assertSet('rows', array_fill(0, 3, ['due_on' => '', 'estimated_hours' => '']))
->assertSet('rows', array_fill(0, 3, ['due_on' => '', 'estimated_hours' => '', 'assignee_user_id' => (string) $this->employee->id]))
->assertSet('scheduleToDueDates', true)
->assertSee('Tekijä: Kuvat')
->assertSee('Pohjan Somekampanja tehtävät')
->assertSee('Määräpäivä: Kuvat')
->assertSee('Arvioitu aika (h): Julkaisu')
->set('rows.0.due_on', '2026-06-01')
->set('rows.0.estimated_hours', '1,5')
->set('rows.2.estimated_hours', '2')
->set('scheduleToDueDates', false)
->call('add')
->assertHasNoErrors();
@@ -143,3 +146,31 @@ it('empties the rows when another template is chosen', function () {
->set('templateId', (string) $this->campaign)
->assertSet('rows.0.estimated_hours', '');
});
it('schedules the tasks to their due dates for the chosen assignees, backwards from the due date', function () {
$this->travelTo(new DateTimeImmutable('2026-06-01 09:00'));
$colleague = User::factory()->create(['name' => 'Aino Ahkera']);
Livewire::test('jobs::tasks-from-template', ['job' => $this->job->id])
->set('templateId', (string) $this->campaign)
->set('rows.0.due_on', '2026-06-10')
->set('rows.0.estimated_hours', '12')
->set('rows.0.assignee_user_id', (string) $colleague->id)
->set('rows.1.due_on', '2026-06-10')
->set('rows.1.estimated_hours', '4')
->set('rows.1.assignee_user_id', '')
->call('add')
->assertHasNoErrors();
$tasks = TaskModel::query()->orderBy('id')->get();
expect($tasks->map(fn ($task) => [$task->assignee_user_id, $task->planned_from?->format('Y-m-d'), $task->planned_to?->format('Y-m-d')])->all())->toBe([
[$colleague->id, '2026-06-09', '2026-06-10'],
[null, '2026-06-10', '2026-06-10'],
[$this->employee->id, null, null],
])
->and(DB::table('task_bookings')->where('task_id', $tasks[0]->id)->orderBy('day')->pluck('minutes', 'day')->all())->toBe(['2026-06-09' => 240, '2026-06-10' => 480])
->and(DB::table('task_bookings')->where('task_id', '!=', $tasks[0]->id)->count())->toBe(0);
$this->get(route('jobs.show', $this->job->id))->assertSee('2 tehtävää aikataulutettiin määräpäivilleen.');
});
+36 -2
View File
@@ -3,6 +3,7 @@
use App\Models\User;
use App\Modules\Catalog\Infrastructure\Persistence\Eloquent\ServiceUnitModel;
use App\Modules\Catalog\Infrastructure\Persistence\Eloquent\WorkTypeModel;
use App\Modules\Jobs\Application\Contracts\TaskQueries;
use App\Modules\Jobs\Infrastructure\Persistence\Eloquent\CustomerJobModel;
use App\Modules\Jobs\Infrastructure\Persistence\Eloquent\TaskModel;
use Illuminate\Foundation\Testing\RefreshDatabase;
@@ -29,7 +30,8 @@ it('adds a task with work types of the job and the shared ones on offer', functi
->assertDontSee('painotyö');
Livewire::test('jobs::task-form', ['job' => $this->job->id])
->assertSet('form.assignee_user_ids', [(string) $this->employee->id])
->assertSet('form.assignee_user_id', (string) $this->employee->id)
->assertSet('form.member_user_ids', [])
->set('form.title', 'Mainoskuvat')
->set('form.work_type_id', (string) $this->graphic->id)
->set('form.due_on', '2026-05-20')
@@ -39,7 +41,8 @@ it('adds a task with work types of the job and the shared ones on offer', functi
$task = TaskModel::query()->sole();
expect($task->work_type_id)->toBe($this->graphic->id)
->and(DB::table('task_assignees')->where('task_id', $task->id)->pluck('user_id')->all())->toBe([$this->employee->id]);
->and($task->assignee_user_id)->toBe($this->employee->id)
->and(DB::table('task_members')->where('task_id', $task->id)->count())->toBe(0);
$this->get(route('jobs.show', $this->job->id))
->assertSee('Mainoskuvat')
@@ -117,3 +120,34 @@ it('returns 404 for a task of another job', function () {
$this->get(route('jobs.tasks.edit', [$this->job->id, $other->id]))->assertNotFound();
});
it('saves a task assignee and members apart, and the members find the job among their own', function () {
$colleague = User::factory()->create(['name' => 'Aino Ahkera']);
$intern = User::factory()->create(['name' => 'Ville Harjoittelija']);
Livewire::test('jobs::task-form', ['job' => $this->job->id])
->set('form.title', 'Mainoskuvat')
->set('form.work_type_id', (string) $this->graphic->id)
->set('form.assignee_user_id', (string) $colleague->id)
->set('form.member_user_ids', [(string) $intern->id, (string) $colleague->id])
->call('save')
->assertHasNoErrors();
$task = TaskModel::query()->sole();
expect($task->assignee_user_id)->toBe($colleague->id)
->and(DB::table('task_members')->where('task_id', $task->id)->pluck('user_id')->all())->toBe([$intern->id]);
$this->get(route('jobs.show', $this->job->id))
->assertSeeInOrder(['Tekijä', 'Aino Ahkera', 'Jäsenet', 'Ville Harjoittelija']);
Livewire::test('jobs::task-form', ['job' => $this->job->id, 'task' => $task->id])
->assertSet('form.assignee_user_id', (string) $colleague->id)
->assertSet('form.member_user_ids', [(string) $intern->id])
->set('form.assignee_user_id', '')
->call('save')
->assertHasNoErrors();
expect($task->refresh()->assignee_user_id)->toBeNull()
->and(app(TaskQueries::class)->assignedOpenTaskIds($intern->id))->toBe([$task->id]);
});
@@ -26,7 +26,7 @@ beforeEach(function () {
$finder = new ChecklistItemFinder($tasks, $this->items);
$this->jobId = 5;
$this->taskId = (int) $tasks->save(Task::reconstitute(1, $this->jobId, 11, new TaskTitle('Kuvaus'), null, TaskStatus::Waiting, null, null, [], 10))->id();
$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 AddChecklistItem($finder, $this->items, $transactions);
$this->rename = new RenameChecklistItem($finder, $this->items, $transactions);
@@ -5,21 +5,19 @@ use App\Modules\Jobs\Application\DTOs\CalendarTask;
use App\Modules\Jobs\Application\Services\TaskCalendarLayout;
use App\Modules\Jobs\Domain\Enums\TaskStatus;
/**
* @param list<CalendarAssignment> $assignments
*/
function calendarTask(int $id, string $from, string $to, array $assignments, ?string $dueOn = null): CalendarTask
function calendarTask(int $id, string $from, string $to, ?CalendarAssignment $assignment, ?string $dueOn = null): CalendarTask
{
return new CalendarTask($id, 1, '2026-001', 'Kampanja', 'Tehtävä '.$id, TaskStatus::Waiting, $dueOn, null, $from, $to, $assignments);
return new CalendarTask($id, 1, '2026-001', 'Kampanja', 'Tehtävä '.$id, TaskStatus::Waiting, $dueOn, null, $from, $to, $assignment);
}
it('puts each task on its assignees rows and overlapping tasks on their own lanes', function () {
it('puts each task on its assignee row and overlapping tasks on their own lanes', function () {
$days = ['2026-12-07', '2026-12-08', '2026-12-09', '2026-12-10'];
$tasks = [
calendarTask(1, '2026-12-07', '2026-12-08', [new CalendarAssignment(10, '2026-12-07', '2026-12-08', 480), new CalendarAssignment(11, '2026-12-07', '2026-12-09', 480)]),
calendarTask(2, '2026-12-08', '2026-12-08', [new CalendarAssignment(10, '2026-12-08', '2026-12-08', 120)]),
calendarTask(3, '2026-12-09', '2026-12-20', [new CalendarAssignment(10, null, null, 0)], dueOn: '2026-12-15'),
calendarTask(4, '2026-12-01', '2026-12-07', []),
calendarTask(1, '2026-12-07', '2026-12-08', new CalendarAssignment(10, '2026-12-07', '2026-12-08', 480)),
calendarTask(2, '2026-12-08', '2026-12-08', new CalendarAssignment(10, '2026-12-08', '2026-12-08', 120)),
calendarTask(3, '2026-12-09', '2026-12-20', new CalendarAssignment(10, null, null, 0), dueOn: '2026-12-15'),
calendarTask(4, '2026-12-01', '2026-12-07', null),
calendarTask(5, '2026-12-07', '2026-12-08', new CalendarAssignment(11, '2026-12-07', '2026-12-09', 480)),
];
$people = [['id' => 10, 'name' => 'Eero'], ['id' => 11, 'name' => 'Aino'], ['id' => null, 'name' => 'Ei tekijää']];
@@ -29,7 +27,7 @@ it('puts each task on its assignees rows and overlapping tasks on their own lane
expect($bars(0))->toBe([[1, 0, 2, 0, false], [2, 1, 1, 1, false], [3, 2, 2, 0, true]])
->and($rows[0]->lanes)->toBe(2)
->and($rows[0]->minutesByDay)->toBe(['2026-12-07' => 0, '2026-12-08' => 360, '2026-12-09' => 0, '2026-12-10' => 0])
->and($bars(1))->toBe([[1, 0, 3, 0, false]])
->and($bars(1))->toBe([[5, 0, 3, 0, false]])
->and($bars(2))->toBe([[4, 0, 1, 0, false]])
->and(array_map(fn ($bar) => $bar->task->taskId, $rows[0]->barsOn(1)))->toBe([1, 2]);
});
@@ -83,27 +83,28 @@ it('allows tasks from a quote request on, not before and not when archived', fun
});
it('adds a task with a work type of the job or a shared one', function () {
$taskId = $this->addTask->execute($this->jobId, new TaskData('Mainoskuvat', 11, 'waiting', dueOn: '2026-05-20', estimatedHours: '3,5', assigneeUserIds: [10]), 10);
$taskId = $this->addTask->execute($this->jobId, new TaskData('Mainoskuvat', 11, 'waiting', dueOn: '2026-05-20', estimatedHours: '3,5', assigneeUserId: 10), 10);
$this->addTask->execute($this->jobId, new TaskData('Suunnittelupalaveri', 91, 'waiting'), 10);
$task = $this->tasks->findForJob($this->jobId, $taskId);
expect($task?->dueOn()?->format('Y-m-d'))->toBe('2026-05-20')
->and($task?->estimatedHours()?->format())->toBe('3.5')
->and($task?->assigneeUserIds())->toBe([10])
->and($task?->assigneeUserId())->toBe(10)
->and($this->tasks->tasks)->toHaveCount(2);
});
it('rejects a work type of another unit and an inactive assignee', function (int $workTypeId, array $assignees, string $field) {
it('rejects a work type of another unit and an inactive assignee or member', function (int $workTypeId, ?int $assignee, array $members, string $field) {
try {
$this->addTask->execute($this->jobId, new TaskData('Työ', $workTypeId, 'waiting', assigneeUserIds: $assignees), 10);
$this->addTask->execute($this->jobId, new TaskData('Työ', $workTypeId, 'waiting', assigneeUserId: $assignee, memberUserIds: $members), 10);
$this->fail('No exception');
} catch (InvalidTaskReference $exception) {
expect($exception->field)->toBe($field);
}
})->with([
'work type of another unit' => [21, [], 'work_type'],
'inactive assignee' => [11, [12], 'assignees'],
'work type of another unit' => [21, null, [], 'work_type'],
'inactive assignee' => [11, 12, [], 'assignee'],
'inactive member' => [11, 10, [12], 'members'],
]);
it('changes, moves through statuses and removes a task', function () {
@@ -150,8 +151,8 @@ it('does not remove a task with time entries', function () {
});
it('books a scheduled task evenly from the working days of its period', function () {
$taskId = $this->addTask->execute($this->jobId, new TaskData('Taitto', 11, 'waiting', estimatedHours: '12', assigneeUserIds: [10], plannedFrom: '2026-12-04', plannedTo: '2026-12-07'), 10);
$oneDayId = $this->addTask->execute($this->jobId, new TaskData('Palaveri', 91, 'waiting', assigneeUserIds: [10], plannedFrom: '2026-12-08'), 10);
$taskId = $this->addTask->execute($this->jobId, new TaskData('Taitto', 11, 'waiting', estimatedHours: '12', assigneeUserId: 10, plannedFrom: '2026-12-04', plannedTo: '2026-12-07'), 10);
$oneDayId = $this->addTask->execute($this->jobId, new TaskData('Palaveri', 91, 'waiting', assigneeUserId: 10, plannedFrom: '2026-12-08'), 10);
expect($this->bookings->of($taskId, 10))->toBe(['2026-12-04' => 360, '2026-12-07' => 360])
->and($this->tasks->findForJob($this->jobId, $oneDayId)?->plannedTo()?->format('Y-m-d'))->toBe('2026-12-08')
@@ -159,8 +160,8 @@ it('books a scheduled task evenly from the working days of its period', function
});
it('moves what does not fit in a full day to the next working day', function () {
$this->addTask->execute($this->jobId, new TaskData('Kuvaus', 11, 'waiting', estimatedHours: '6', assigneeUserIds: [10], plannedFrom: '2026-12-04'), 10);
$taskId = $this->addTask->execute($this->jobId, new TaskData('Taitto', 11, 'waiting', estimatedHours: '4', assigneeUserIds: [10]), 10);
$this->addTask->execute($this->jobId, new TaskData('Kuvaus', 11, 'waiting', estimatedHours: '6', assigneeUserId: 10, plannedFrom: '2026-12-04'), 10);
$taskId = $this->addTask->execute($this->jobId, new TaskData('Taitto', 11, 'waiting', estimatedHours: '4', assigneeUserId: 10), 10);
$result = $this->scheduleTask->execute($this->jobId, $taskId, 10, 10, '2026-12-04', '2026-12-04');
@@ -170,20 +171,20 @@ it('moves what does not fit in a full day to the next working day', function ()
});
it('does not count done tasks in the day load', function () {
$doneId = $this->addTask->execute($this->jobId, new TaskData('Kuvaus', 11, 'waiting', estimatedHours: '8', assigneeUserIds: [10], plannedFrom: '2026-12-04'), 10);
$doneId = $this->addTask->execute($this->jobId, new TaskData('Kuvaus', 11, 'waiting', estimatedHours: '8', assigneeUserId: 10, plannedFrom: '2026-12-04'), 10);
$this->changeTaskStatus->execute($this->jobId, $doneId, TaskStatus::Done);
$taskId = $this->addTask->execute($this->jobId, new TaskData('Taitto', 11, 'waiting', estimatedHours: '8', assigneeUserIds: [10], plannedFrom: '2026-12-04'), 10);
$taskId = $this->addTask->execute($this->jobId, new TaskData('Taitto', 11, 'waiting', estimatedHours: '8', assigneeUserId: 10, plannedFrom: '2026-12-04'), 10);
expect($this->bookings->of($taskId, 10))->toBe(['2026-12-04' => 480]);
});
it('moves a task to another person and gives it the days its estimate needs', function () {
$this->users->addUser(11, 'Aino');
$taskId = $this->addTask->execute($this->jobId, new TaskData('Taitto', 11, 'waiting', estimatedHours: '20', assigneeUserIds: [10], plannedFrom: '2026-12-01'), 10);
$taskId = $this->addTask->execute($this->jobId, new TaskData('Taitto', 11, 'waiting', estimatedHours: '20', assigneeUserId: 10, plannedFrom: '2026-12-01'), 10);
$result = $this->scheduleTask->execute($this->jobId, $taskId, 10, 11, '2026-12-11', null);
expect($this->tasks->findForJob($this->jobId, $taskId)?->assigneeUserIds())->toBe([11])
expect($this->tasks->findForJob($this->jobId, $taskId)?->assigneeUserId())->toBe(11)
->and($result->plannedFrom)->toBe('2026-12-11')
->and($result->plannedTo)->toBe('2026-12-15')
->and($result->extended)->toBeFalse()
@@ -192,17 +193,48 @@ it('moves a task to another person and gives it the days its estimate needs', fu
});
it('rejects moving a task to an inactive person or ending it before it starts', function () {
$taskId = $this->addTask->execute($this->jobId, new TaskData('Taitto', 11, 'waiting', assigneeUserIds: [10]), 10);
$taskId = $this->addTask->execute($this->jobId, new TaskData('Taitto', 11, 'waiting', assigneeUserId: 10), 10);
expect(fn () => $this->scheduleTask->execute($this->jobId, $taskId, 10, 12, '2026-12-07', '2026-12-07'))->toThrow(InvalidTaskReference::class)
->and(fn () => $this->scheduleTask->execute($this->jobId, $taskId, 10, 10, '2026-12-07', '2026-12-04'))->toThrow(PlanEndsBeforeStart::class);
});
it('takes a task out of the calendar', function () {
$taskId = $this->addTask->execute($this->jobId, new TaskData('Taitto', 11, 'waiting', estimatedHours: '4', assigneeUserIds: [10], plannedFrom: '2026-12-07'), 10);
$taskId = $this->addTask->execute($this->jobId, new TaskData('Taitto', 11, 'waiting', estimatedHours: '4', assigneeUserId: 10, plannedFrom: '2026-12-07'), 10);
$this->clearSchedule->execute($this->jobId, $taskId);
expect($this->tasks->findForJob($this->jobId, $taskId)?->isScheduled())->toBeFalse()
->and($this->bookings->of($taskId, 10))->toBe([]);
});
it('books only the assignee, not the members, and keeps the assignee out of the members', function () {
$this->users->addUser(11, 'Aino');
$taskId = $this->addTask->execute($this->jobId, new TaskData('Taitto', 11, 'waiting', estimatedHours: '4', assigneeUserId: 10, memberUserIds: [11, 10], plannedFrom: '2026-12-07'), 10);
$task = $this->tasks->findForJob($this->jobId, $taskId);
expect($task?->memberUserIds())->toBe([11])
->and($this->bookings->of($taskId, 10))->toBe(['2026-12-07' => 240])
->and($this->bookings->of($taskId, 11))->toBe([]);
});
it('moves a task to a member, who then is the assignee and no longer a member', function () {
$this->users->addUser(11, 'Aino');
$this->users->addUser(13, 'Ville');
$taskId = $this->addTask->execute($this->jobId, new TaskData('Taitto', 11, 'waiting', estimatedHours: '4', assigneeUserId: 10, memberUserIds: [11, 13], plannedFrom: '2026-12-07'), 10);
$this->scheduleTask->execute($this->jobId, $taskId, 10, 11, '2026-12-07', '2026-12-07');
$task = $this->tasks->findForJob($this->jobId, $taskId);
expect($task?->assigneeUserId())->toBe(11)
->and($task?->memberUserIds())->toBe([13])
->and($this->bookings->of($taskId, 10))->toBe([])
->and($this->bookings->of($taskId, 11))->toBe(['2026-12-07' => 240]);
});
it('books nothing for a task without an assignee', function () {
$taskId = $this->addTask->execute($this->jobId, new TaskData('Taitto', 11, 'waiting', estimatedHours: '4', memberUserIds: [10], plannedFrom: '2026-12-07'), 10);
expect($this->bookings->of($taskId, 10))->toBe([])
->and($this->tasks->findForJob($this->jobId, $taskId)?->plannedTo()?->format('Y-m-d'))->toBe('2026-12-07');
});
@@ -3,6 +3,7 @@
use App\Modules\Jobs\Application\DTOs\CustomerJobData;
use App\Modules\Jobs\Application\DTOs\TemplateTaskSchedule;
use App\Modules\Jobs\Application\Services\JobDetailsFactory;
use App\Modules\Jobs\Application\Services\TaskPlanner;
use App\Modules\Jobs\Application\Services\TaskReferenceChecker;
use App\Modules\Jobs\Application\UseCases\AddTasksFromWorkflowTemplate;
use App\Modules\Jobs\Application\UseCases\CreateCustomerJob;
@@ -12,13 +13,17 @@ use App\Modules\Jobs\Domain\Exceptions\InvalidEstimatedHours;
use App\Modules\Jobs\Domain\Exceptions\InvalidTaskReference;
use App\Modules\Jobs\Domain\Exceptions\TasksNotAllowedYet;
use App\Modules\Jobs\Domain\Exceptions\WorkflowTemplateNotAvailable;
use App\Modules\Shared\Application\UseCases\HolidayCalendar;
use Tests\Fakes\FakeCatalogQueries;
use Tests\Fakes\FakeClock;
use Tests\Fakes\FakeCustomerQueries;
use Tests\Fakes\FakeSettings;
use Tests\Fakes\FakeTransactionManager;
use Tests\Fakes\FakeUserAccountQueries;
use Tests\Fakes\FakeWorkflowTemplateQueries;
use Tests\Fakes\InMemoryCustomerJobRepository;
use Tests\Fakes\InMemoryHolidayOverrides;
use Tests\Fakes\InMemoryTaskBookingRepository;
use Tests\Fakes\InMemoryTaskRepository;
beforeEach(function () {
@@ -39,6 +44,8 @@ beforeEach(function () {
$catalog->addWorkType(21, 2, 'painotyö');
$catalog->addWorkType(91, 9, 'suunnittelu');
$users->addUser(10, 'Eero');
$users->addUser(11, 'Aino');
$users->addUser(12, 'Pois', active: false);
$createJob = new CreateCustomerJob($this->jobs, $this->jobs, new JobDetailsFactory($customers, $catalog, $users), $transactions, new FakeClock(new DateTimeImmutable('2026-05-01')));
$this->jobId = $createJob->execute(new CustomerJobData('Video', 1, [1], 10, 'one_off', 'quote_request', 'normal'), 10);
@@ -49,18 +56,20 @@ beforeEach(function () {
$this->templates->addTemplate(3, 'Vanha', 11, 1, [['Kuvaus']], active: false);
$this->templates->addTemplate(4, 'Rikki', 11, 1, [['Painatus', 21]]);
$this->addTasks = new AddTasksFromWorkflowTemplate($this->jobs, $this->tasks, $this->templates, new TaskReferenceChecker($catalog, $users), $transactions);
$this->bookings = new InMemoryTaskBookingRepository($this->tasks);
$planner = new TaskPlanner($this->bookings, new HolidayCalendar(new InMemoryHolidayOverrides), new FakeSettings);
$this->addTasks = new AddTasksFromWorkflowTemplate($this->jobs, $this->tasks, $this->templates, new TaskReferenceChecker($catalog, $users), $planner, new FakeClock(new DateTimeImmutable('2026-05-01')), $transactions);
});
it('adds a waiting task per step in order, with the step work type and the adder as assignee', function () {
expect($this->addTasks->execute($this->jobId, 1, 10))->toBe(3);
expect($this->addTasks->execute($this->jobId, 1, 10)->added)->toBe(3);
$tasks = array_values($this->tasks->tasks);
expect(array_map(fn (Task $task): array => [$task->title()->value, $task->workTypeId(), $task->status(), $task->assigneeUserIds()], $tasks))->toBe([
['Suunnittelu', 91, TaskStatus::Waiting, [10]],
['Kuvaus', 11, TaskStatus::Waiting, [10]],
['Editointi', 12, TaskStatus::Waiting, [10]],
expect(array_map(fn (Task $task): array => [$task->title()->value, $task->workTypeId(), $task->status(), $task->assigneeUserId(), $task->isScheduled()], $tasks))->toBe([
['Suunnittelu', 91, TaskStatus::Waiting, 10, false],
['Kuvaus', 11, TaskStatus::Waiting, 10, false],
['Editointi', 12, TaskStatus::Waiting, 10, false],
]);
});
@@ -94,3 +103,71 @@ it('gives each task the due date and estimate entered for its step', function ()
it('rejects an estimate that is not a number', function () {
$this->addTasks->execute($this->jobId, 1, 10, [new TemplateTaskSchedule(null, 'paljon')]);
})->throws(InvalidEstimatedHours::class);
/**
* @param array<int, Task> $tasks
* @return list<array{0: ?string, 1: ?string}>
*/
function plannedPeriods(array $tasks): array
{
return array_map(fn (Task $task): array => [$task->plannedFrom()?->format('Y-m-d'), $task->plannedTo()?->format('Y-m-d')], array_values($tasks));
}
it('gives each task the assignee chosen for its step', function () {
$this->addTasks->execute($this->jobId, 1, 10, [
new TemplateTaskSchedule(null, null, 11),
new TemplateTaskSchedule(null, null, null),
new TemplateTaskSchedule(null, null, 10),
]);
expect(array_map(fn (Task $task): ?int => $task->assigneeUserId(), array_values($this->tasks->tasks)))->toBe([11, null, 10]);
});
it('rejects an inactive assignee', function () {
try {
$this->addTasks->execute($this->jobId, 1, 10, [new TemplateTaskSchedule(null, null, 12)]);
$this->fail('No exception');
} catch (InvalidTaskReference $exception) {
expect($exception->field)->toBe('assignee');
}
});
it('schedules tasks to end on their due dates, booking backwards from the due date past full days', function () {
$result = $this->addTasks->execute($this->jobId, 1, 10, [
new TemplateTaskSchedule('2026-05-12', '12', 10),
new TemplateTaskSchedule('2026-05-12', '6', 10),
new TemplateTaskSchedule(null, '2', 10),
], scheduleToDueDates: true);
[$first, $second] = array_keys($this->tasks->tasks);
expect([$result->added, $result->scheduled, $result->late])->toBe([3, 2, 0])
->and(plannedPeriods($this->tasks->tasks))->toBe([['2026-05-11', '2026-05-12'], ['2026-05-08', '2026-05-12'], [null, null]])
->and($this->bookings->of($first, 10))->toBe(['2026-05-11' => 240, '2026-05-12' => 480])
->and($this->bookings->of($second, 10))->toBe(['2026-05-08' => 120, '2026-05-11' => 240]);
});
it('does not schedule before today and lets the rest run past the due date', function () {
$result = $this->addTasks->execute($this->jobId, 1, 10, [new TemplateTaskSchedule('2026-05-04', '20', 10)], scheduleToDueDates: true);
$taskId = array_key_first($this->tasks->tasks);
// Today, 1.5., is May Day, so only Monday 4.5. is free before the due date.
expect([$result->scheduled, $result->late])->toBe([1, 1])
->and(plannedPeriods([$this->tasks->tasks[$taskId]]))->toBe([['2026-05-04', '2026-05-06']])
->and($this->bookings->of($taskId, 10))->toBe(['2026-05-04' => 480, '2026-05-05' => 480, '2026-05-06' => 240]);
});
it('gives a task without an assignee the working days its estimate needs before the due date, without bookings', function () {
$this->addTasks->execute($this->jobId, 1, 10, [new TemplateTaskSchedule('2026-05-12', '12', null)], scheduleToDueDates: true);
$taskId = array_key_first($this->tasks->tasks);
expect(plannedPeriods([$this->tasks->tasks[$taskId]]))->toBe([['2026-05-11', '2026-05-12']])
->and($this->bookings->bookings[$taskId] ?? [])->toBe([]);
});
it('leaves the tasks unscheduled when scheduling to due dates is off', function () {
$result = $this->addTasks->execute($this->jobId, 1, 10, [new TemplateTaskSchedule('2026-05-12', '12', 10)]);
expect($result->scheduled)->toBe(0)
->and(plannedPeriods($this->tasks->tasks)[0])->toBe([null, null]);
});
@@ -45,3 +45,18 @@ it('gives a placed task the working days its estimate needs', function () {
->and(TaskScheduler::defaultEnd(scheduleDay('2026-12-23'), 960, 480, ['2026-12-24', '2026-12-25'])->format('Y-m-d'))->toBe('2026-12-28')
->and(TaskScheduler::defaultEnd(scheduleDay('2026-12-05'), 0, 480, [])->format('Y-m-d'))->toBe('2026-12-05');
});
it('books backwards from a due date on a weekend, past holidays and other bookings', function () {
// Due Sat 26.12.2026; 24.–25.12. are holidays and Wed 23.12. is half booked.
$schedule = TaskScheduler::scheduleBackwards(scheduleDay('2026-12-26'), 720, ['2026-12-23' => 240], 480, ['2026-12-24', '2026-12-25'], scheduleDay('2026-12-01'));
expect($schedule->minutesByDay)->toBe(['2026-12-22' => 480, '2026-12-23' => 240])
->and($schedule->end->format('Y-m-d'))->toBe('2026-12-26')
->and($schedule->extended)->toBeFalse();
});
it('starts a task without an assignee as many working days before its end as the estimate needs, not before the earliest day', function () {
expect(TaskScheduler::defaultStart(scheduleDay('2026-12-08'), 1200, 480, [], scheduleDay('2026-12-01'))->format('Y-m-d'))->toBe('2026-12-04')
->and(TaskScheduler::defaultStart(scheduleDay('2026-12-08'), 1200, 480, [], scheduleDay('2026-12-07'))->format('Y-m-d'))->toBe('2026-12-07')
->and(TaskScheduler::defaultStart(scheduleDay('2026-12-08'), 0, 480, [], scheduleDay('2026-12-01'))->format('Y-m-d'))->toBe('2026-12-08');
});