Add the task calendar, holidays, workflow templates and customer removal
Task calendar (/jobs/calendar) replaces per-unit task Kanban columns (K34): a two-week Gantt of tasks per person. Bars move, resize and change person by dragging or through a keyboard dialog. Each task's estimate is booked evenly over working days into task_bookings, and what exceeds the daily maximum (jobs.daily_capacity_hours, 8 h) moves to the next working day. Tasks get planned_from/planned_to on the form. Holidays (/settings/holidays): Finnish public holidays calculated per year, which the admin can take out of use, plus company days off. Also includes the earlier uncommitted work: workflow templates with adding tasks from a template, and removing customers no module refers to. Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
@@ -0,0 +1,136 @@
|
||||
<?php
|
||||
|
||||
use App\Modules\Catalog\Application\DTOs\WorkflowStepData;
|
||||
use App\Modules\Catalog\Application\DTOs\WorkflowTemplateData;
|
||||
use App\Modules\Catalog\Application\Services\WorkflowStepBuilder;
|
||||
use App\Modules\Catalog\Application\UseCases\AddServiceUnit;
|
||||
use App\Modules\Catalog\Application\UseCases\AddWorkflowTemplate;
|
||||
use App\Modules\Catalog\Application\UseCases\AddWorkType;
|
||||
use App\Modules\Catalog\Application\UseCases\SetCatalogItemActive;
|
||||
use App\Modules\Catalog\Application\UseCases\SetWorkflowTemplateActive;
|
||||
use App\Modules\Catalog\Application\UseCases\UpdateWorkflowTemplate;
|
||||
use App\Modules\Catalog\Domain\Entities\ServiceUnit;
|
||||
use App\Modules\Catalog\Domain\Enums\CatalogLevel;
|
||||
use App\Modules\Catalog\Domain\Exceptions\CatalogItemNotFound;
|
||||
use App\Modules\Catalog\Domain\Exceptions\InvalidWorkflowStepTitle;
|
||||
use App\Modules\Catalog\Domain\Exceptions\WorkflowStepWorkTypeNotAllowed;
|
||||
use App\Modules\Catalog\Domain\Exceptions\WorkflowTemplateNameTaken;
|
||||
use App\Modules\Catalog\Domain\Exceptions\WorkflowTemplateNeedsSteps;
|
||||
use App\Modules\Catalog\Domain\Exceptions\WorkflowTemplateNotFound;
|
||||
use App\Modules\Catalog\Domain\ValueObjects\CatalogName;
|
||||
use Tests\Fakes\FakeTransactionManager;
|
||||
use Tests\Fakes\InMemoryServiceRepository;
|
||||
use Tests\Fakes\InMemoryServiceUnitRepository;
|
||||
use Tests\Fakes\InMemoryWorkflowTemplateRepository;
|
||||
use Tests\Fakes\InMemoryWorkTypeRepository;
|
||||
|
||||
beforeEach(function () {
|
||||
$units = new InMemoryServiceUnitRepository;
|
||||
$services = new InMemoryServiceRepository;
|
||||
$this->workTypes = new InMemoryWorkTypeRepository;
|
||||
$this->templates = new InMemoryWorkflowTemplateRepository;
|
||||
$transactions = new FakeTransactionManager;
|
||||
$builder = new WorkflowStepBuilder($this->workTypes, $units);
|
||||
|
||||
$this->add = new AddWorkflowTemplate($this->templates, $this->workTypes, $builder, $transactions);
|
||||
$this->update = new UpdateWorkflowTemplate($this->templates, $this->workTypes, $builder, $transactions);
|
||||
$this->setActive = new SetWorkflowTemplateActive($this->templates, $transactions);
|
||||
$this->setWorkTypeActive = new SetCatalogItemActive($units, $services, $this->workTypes, $transactions);
|
||||
|
||||
$addUnit = new AddServiceUnit($units, $transactions);
|
||||
$addWorkType = new AddWorkType($units, $services, $this->workTypes, $transactions);
|
||||
$media = $addUnit->execute('Mediatuotanto');
|
||||
$marketing = $addUnit->execute('Markkinointi');
|
||||
$shared = (int) $units->save(ServiceUnit::create(new CatalogName('Kaikille yhteiset'), 3, shared: true))->id();
|
||||
|
||||
$this->videography = $addWorkType->execute($media, null, 'videokuvaus');
|
||||
$this->editing = $addWorkType->execute($media, null, 'editointi');
|
||||
$this->graphics = $addWorkType->execute($marketing, null, 'graafinen työ');
|
||||
$this->planning = $addWorkType->execute($shared, null, 'suunnittelu');
|
||||
|
||||
$this->data = fn (string $name, array $steps, ?int $workTypeId = null): WorkflowTemplateData => new WorkflowTemplateData(
|
||||
$name,
|
||||
$workTypeId ?? $this->videography,
|
||||
array_map(fn (array $step): WorkflowStepData => new WorkflowStepData(...$step), $steps),
|
||||
);
|
||||
});
|
||||
|
||||
it('adds a template with steps in order and their own work types', function () {
|
||||
$id = $this->add->execute(($this->data)('Lyhyt video', [
|
||||
['Suunnittelu', null, $this->planning],
|
||||
[' Kuvaus ', 'Kuvauspäivä asiakkaalla'],
|
||||
['Editointi', '', $this->editing],
|
||||
]));
|
||||
|
||||
$template = $this->templates->findById($id);
|
||||
|
||||
expect($template?->name()->value)->toBe('Lyhyt video')
|
||||
->and($template?->isActive())->toBeTrue()
|
||||
->and(array_map(fn ($step) => [$step->title->value, $step->description, $step->workTypeId], $template?->steps() ?? []))->toBe([
|
||||
['Suunnittelu', null, $this->planning],
|
||||
['Kuvaus', 'Kuvauspäivä asiakkaalla', null],
|
||||
['Editointi', null, $this->editing],
|
||||
]);
|
||||
});
|
||||
|
||||
it('needs at least one step with a title, and tells which step is wrong', function () {
|
||||
expect(fn () => $this->add->execute(($this->data)('Tyhjä', [])))->toThrow(WorkflowTemplateNeedsSteps::class);
|
||||
|
||||
try {
|
||||
$this->add->execute(($this->data)('Lyhyt video', [['Kuvaus'], [' ']]));
|
||||
$this->fail('The empty title was accepted.');
|
||||
} catch (InvalidWorkflowStepTitle $exception) {
|
||||
expect($exception->stepIndex)->toBe(1);
|
||||
}
|
||||
});
|
||||
|
||||
it('rejects a name the work type already has, but allows it for another work type', function () {
|
||||
$this->add->execute(($this->data)('Lyhyt video', [['Kuvaus']]));
|
||||
$this->add->execute(($this->data)('lyhyt video', [['Kuvaus']], $this->editing));
|
||||
|
||||
expect(fn () => $this->add->execute(($this->data)('LYHYT VIDEO', [['Kuvaus']])))->toThrow(WorkflowTemplateNameTaken::class);
|
||||
});
|
||||
|
||||
it('allows a step work type only from the same unit or the shared unit, and in use', function () {
|
||||
try {
|
||||
$this->add->execute(($this->data)('Lyhyt video', [['Kuvaus'], ['Grafiikka', null, $this->graphics]]));
|
||||
$this->fail('A work type of another unit was accepted.');
|
||||
} catch (WorkflowStepWorkTypeNotAllowed $exception) {
|
||||
expect($exception->stepIndex)->toBe(1);
|
||||
}
|
||||
|
||||
$this->setWorkTypeActive->execute(CatalogLevel::WorkType, $this->editing, false);
|
||||
|
||||
expect(fn () => $this->add->execute(($this->data)('Lyhyt video', [['Editointi', null, $this->editing]])))
|
||||
->toThrow(WorkflowStepWorkTypeNotAllowed::class);
|
||||
});
|
||||
|
||||
it('needs a template work type in use', function () {
|
||||
$this->setWorkTypeActive->execute(CatalogLevel::WorkType, $this->videography, false);
|
||||
|
||||
$this->add->execute(($this->data)('Lyhyt video', [['Kuvaus']]));
|
||||
})->throws(CatalogItemNotFound::class);
|
||||
|
||||
it('updates name and steps, keeping step work types taken out of use since', function () {
|
||||
$id = $this->add->execute(($this->data)('Lyhyt video', [['Kuvaus'], ['Editointi', null, $this->editing]]));
|
||||
$this->setWorkTypeActive->execute(CatalogLevel::WorkType, $this->editing, false);
|
||||
|
||||
$this->update->execute($id, ($this->data)('Lyhyt video 2', [['Editointi', null, $this->editing], ['Toimitus']]));
|
||||
|
||||
$template = $this->templates->findById($id);
|
||||
|
||||
expect($template?->name()->value)->toBe('Lyhyt video 2')
|
||||
->and(array_map(fn ($step) => $step->title->value, $template?->steps() ?? []))->toBe(['Editointi', 'Toimitus']);
|
||||
|
||||
expect(fn () => $this->update->execute(999, ($this->data)('X', [['Kuvaus']])))->toThrow(WorkflowTemplateNotFound::class);
|
||||
});
|
||||
|
||||
it('takes a template out of use and back', function () {
|
||||
$id = $this->add->execute(($this->data)('Lyhyt video', [['Kuvaus']]));
|
||||
|
||||
$this->setActive->execute($id, false);
|
||||
expect($this->templates->findById($id)?->isActive())->toBeFalse();
|
||||
|
||||
$this->setActive->execute($id, true);
|
||||
expect($this->templates->findById($id)?->isActive())->toBeTrue();
|
||||
});
|
||||
@@ -0,0 +1,70 @@
|
||||
<?php
|
||||
|
||||
use App\Modules\Customers\Application\Services\CustomerRemoval;
|
||||
use App\Modules\Customers\Application\UseCases\RemoveCustomer;
|
||||
use App\Modules\Customers\Domain\Entities\BillingUnit;
|
||||
use App\Modules\Customers\Domain\Entities\Customer;
|
||||
use App\Modules\Customers\Domain\Entities\CustomerContact;
|
||||
use App\Modules\Customers\Domain\Enums\CustomerType;
|
||||
use App\Modules\Customers\Domain\Exceptions\CustomerInUse;
|
||||
use App\Modules\Customers\Domain\Exceptions\CustomerNotFound;
|
||||
use App\Modules\Customers\Domain\Exceptions\InternalCustomerCannotBeRemoved;
|
||||
use App\Modules\Customers\Domain\ValueObjects\BillingUnitName;
|
||||
use App\Modules\Customers\Domain\ValueObjects\ContactName;
|
||||
use App\Modules\Customers\Domain\ValueObjects\CustomerName;
|
||||
use Tests\Fakes\FakeCustomerReferences;
|
||||
use Tests\Fakes\FakeTransactionManager;
|
||||
use Tests\Fakes\InMemoryBillingUnitRepository;
|
||||
use Tests\Fakes\InMemoryCustomerContactRepository;
|
||||
use Tests\Fakes\InMemoryCustomerRepository;
|
||||
|
||||
beforeEach(function () {
|
||||
$this->customers = new InMemoryCustomerRepository;
|
||||
$this->contacts = new InMemoryCustomerContactRepository;
|
||||
$this->billingUnits = new InMemoryBillingUnitRepository;
|
||||
$this->references = new FakeCustomerReferences;
|
||||
$this->removal = new CustomerRemoval($this->customers, $this->contacts, $this->billingUnits, $this->references);
|
||||
$this->removeCustomer = new RemoveCustomer($this->customers, $this->removal, new FakeTransactionManager);
|
||||
|
||||
$this->customerId = $this->customers->save(Customer::register(new CustomerName('Mainos Oy'), CustomerType::Company))->id();
|
||||
});
|
||||
|
||||
it('removes a customer that has nothing but its name', function () {
|
||||
expect($this->removal->canRemove($this->customerId))->toBeTrue();
|
||||
|
||||
$this->removeCustomer->execute($this->customerId);
|
||||
|
||||
expect($this->customers->findById($this->customerId))->toBeNull()
|
||||
->and($this->removal->canRemove($this->customerId))->toBeFalse();
|
||||
});
|
||||
|
||||
it('does not remove an unknown customer', function () {
|
||||
$this->removeCustomer->execute(999);
|
||||
})->throws(CustomerNotFound::class);
|
||||
|
||||
it('does not remove the internal customer', function () {
|
||||
$internalId = $this->customers->save(Customer::reconstitute(50, new CustomerName('Oma Oy'), CustomerType::Company, null, null, true))->id();
|
||||
|
||||
expect($this->removal->canRemove($internalId))->toBeFalse()
|
||||
->and(fn () => $this->removeCustomer->execute($internalId))->toThrow(InternalCustomerCannotBeRemoved::class)
|
||||
->and($this->customers->findById($internalId))->not->toBeNull();
|
||||
});
|
||||
|
||||
it('does not remove a customer that has a contact or a billing unit', function (string $row) {
|
||||
match ($row) {
|
||||
'contact' => $this->contacts->save(CustomerContact::create($this->customerId, new ContactName('Maija'))),
|
||||
'billing unit' => $this->billingUnits->save(BillingUnit::create($this->customerId, new BillingUnitName('Mainos Oy'))),
|
||||
};
|
||||
|
||||
expect($this->removal->canRemove($this->customerId))->toBeFalse()
|
||||
->and(fn () => $this->removeCustomer->execute($this->customerId))->toThrow(CustomerInUse::class)
|
||||
->and($this->customers->findById($this->customerId))->not->toBeNull();
|
||||
})->with(['contact', 'billing unit']);
|
||||
|
||||
it('does not remove a customer that other modules refer to', function () {
|
||||
$this->references->referencedIds = [$this->customerId];
|
||||
|
||||
expect($this->removal->canRemove($this->customerId))->toBeFalse()
|
||||
->and(fn () => $this->removeCustomer->execute($this->customerId))->toThrow(CustomerInUse::class)
|
||||
->and($this->customers->findById($this->customerId))->not->toBeNull();
|
||||
});
|
||||
@@ -0,0 +1,35 @@
|
||||
<?php
|
||||
|
||||
use App\Modules\Jobs\Application\DTOs\CalendarAssignment;
|
||||
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
|
||||
{
|
||||
return new CalendarTask($id, 1, '2026-001', 'Kampanja', 'Tehtävä '.$id, TaskStatus::Waiting, $dueOn, null, $from, $to, $assignments);
|
||||
}
|
||||
|
||||
it('puts each task on its assignees rows 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', []),
|
||||
];
|
||||
$people = [['id' => 10, 'name' => 'Eero'], ['id' => 11, 'name' => 'Aino'], ['id' => null, 'name' => 'Ei tekijää']];
|
||||
|
||||
$rows = TaskCalendarLayout::build($days, $people, $tasks, [10 => ['2026-12-08' => 360]]);
|
||||
$bars = fn (int $row): array => array_map(fn ($bar) => [$bar->task->taskId, $bar->start, $bar->span, $bar->lane, $bar->late], $rows[$row]->bars);
|
||||
|
||||
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(2))->toBe([[4, 0, 1, 0, false]])
|
||||
->and(array_map(fn ($bar) => $bar->task->taskId, $rows[0]->barsOn(1)))->toBe([1, 2]);
|
||||
});
|
||||
@@ -3,28 +3,36 @@
|
||||
use App\Modules\Jobs\Application\DTOs\CustomerJobData;
|
||||
use App\Modules\Jobs\Application\DTOs\TaskData;
|
||||
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\AddTask;
|
||||
use App\Modules\Jobs\Application\UseCases\ChangeJobStatus;
|
||||
use App\Modules\Jobs\Application\UseCases\ChangeTaskStatus;
|
||||
use App\Modules\Jobs\Application\UseCases\ClearTaskSchedule;
|
||||
use App\Modules\Jobs\Application\UseCases\CreateCustomerJob;
|
||||
use App\Modules\Jobs\Application\UseCases\RemoveTask;
|
||||
use App\Modules\Jobs\Application\UseCases\ScheduleTask;
|
||||
use App\Modules\Jobs\Application\UseCases\UpdateTask;
|
||||
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\PlanEndsBeforeStart;
|
||||
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 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\FakeTaskUsage;
|
||||
use Tests\Fakes\FakeTransactionManager;
|
||||
use Tests\Fakes\FakeUserAccountQueries;
|
||||
use Tests\Fakes\InMemoryCustomerJobRepository;
|
||||
use Tests\Fakes\InMemoryHolidayOverrides;
|
||||
use Tests\Fakes\InMemoryTaskBookingRepository;
|
||||
use Tests\Fakes\InMemoryTaskRepository;
|
||||
|
||||
beforeEach(function () {
|
||||
@@ -50,8 +58,12 @@ beforeEach(function () {
|
||||
$this->tentativeJobId = $createJob->execute(new CustomerJobData('Alustava', 1, [1], 10, 'one_off', 'tentative', 'normal'), 10);
|
||||
|
||||
$checker = new TaskReferenceChecker($this->catalog, $this->users);
|
||||
$this->addTask = new AddTask($this->jobs, $this->tasks, $checker, $transactions);
|
||||
$this->updateTask = new UpdateTask($this->jobs, $this->tasks, $checker, $transactions);
|
||||
$this->bookings = new InMemoryTaskBookingRepository($this->tasks);
|
||||
$this->planner = new TaskPlanner($this->bookings, new HolidayCalendar(new InMemoryHolidayOverrides), new FakeSettings);
|
||||
$this->addTask = new AddTask($this->jobs, $this->tasks, $checker, $this->planner, $transactions);
|
||||
$this->updateTask = new UpdateTask($this->jobs, $this->tasks, $checker, $this->planner, $transactions);
|
||||
$this->scheduleTask = new ScheduleTask($this->jobs, $this->tasks, $checker, $this->planner, $transactions);
|
||||
$this->clearSchedule = new ClearTaskSchedule($this->tasks, $this->planner, $transactions);
|
||||
$this->changeTaskStatus = new ChangeTaskStatus($this->tasks, $transactions);
|
||||
$this->usage = new FakeTaskUsage;
|
||||
$this->removeTask = new RemoveTask($this->tasks, $this->usage, $transactions);
|
||||
@@ -136,3 +148,61 @@ it('does not remove a task with time entries', function () {
|
||||
->and(JobStatus::Done->locksTime())->toBeTrue()
|
||||
->and(JobStatus::OnHold->locksTime())->toBeFalse();
|
||||
});
|
||||
|
||||
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);
|
||||
|
||||
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')
|
||||
->and($this->bookings->of($oneDayId, 10))->toBe([]);
|
||||
});
|
||||
|
||||
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);
|
||||
|
||||
$result = $this->scheduleTask->execute($this->jobId, $taskId, 10, 10, '2026-12-04', '2026-12-04');
|
||||
|
||||
expect($result->extended)->toBeTrue()
|
||||
->and($result->plannedTo)->toBe('2026-12-07')
|
||||
->and($this->bookings->of($taskId, 10))->toBe(['2026-12-04' => 120, '2026-12-07' => 120]);
|
||||
});
|
||||
|
||||
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);
|
||||
$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);
|
||||
|
||||
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);
|
||||
|
||||
$result = $this->scheduleTask->execute($this->jobId, $taskId, 10, 11, '2026-12-11', null);
|
||||
|
||||
expect($this->tasks->findForJob($this->jobId, $taskId)?->assigneeUserIds())->toBe([11])
|
||||
->and($result->plannedFrom)->toBe('2026-12-11')
|
||||
->and($result->plannedTo)->toBe('2026-12-15')
|
||||
->and($result->extended)->toBeFalse()
|
||||
->and($this->bookings->of($taskId, 10))->toBe([])
|
||||
->and(array_sum($this->bookings->of($taskId, 11)))->toBe(1200);
|
||||
});
|
||||
|
||||
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);
|
||||
|
||||
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);
|
||||
|
||||
$this->clearSchedule->execute($this->jobId, $taskId);
|
||||
|
||||
expect($this->tasks->findForJob($this->jobId, $taskId)?->isScheduled())->toBeFalse()
|
||||
->and($this->bookings->of($taskId, 10))->toBe([]);
|
||||
});
|
||||
|
||||
@@ -0,0 +1,96 @@
|
||||
<?php
|
||||
|
||||
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\TaskReferenceChecker;
|
||||
use App\Modules\Jobs\Application\UseCases\AddTasksFromWorkflowTemplate;
|
||||
use App\Modules\Jobs\Application\UseCases\CreateCustomerJob;
|
||||
use App\Modules\Jobs\Domain\Entities\Task;
|
||||
use App\Modules\Jobs\Domain\Enums\TaskStatus;
|
||||
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 Tests\Fakes\FakeCatalogQueries;
|
||||
use Tests\Fakes\FakeClock;
|
||||
use Tests\Fakes\FakeCustomerQueries;
|
||||
use Tests\Fakes\FakeTransactionManager;
|
||||
use Tests\Fakes\FakeUserAccountQueries;
|
||||
use Tests\Fakes\FakeWorkflowTemplateQueries;
|
||||
use Tests\Fakes\InMemoryCustomerJobRepository;
|
||||
use Tests\Fakes\InMemoryTaskRepository;
|
||||
|
||||
beforeEach(function () {
|
||||
$this->jobs = new InMemoryCustomerJobRepository;
|
||||
$this->tasks = new InMemoryTaskRepository;
|
||||
$customers = new FakeCustomerQueries;
|
||||
$catalog = new FakeCatalogQueries;
|
||||
$users = new FakeUserAccountQueries;
|
||||
$this->templates = new FakeWorkflowTemplateQueries;
|
||||
$transactions = new FakeTransactionManager;
|
||||
|
||||
$customers->customers = [1 => 'Mainos Oy'];
|
||||
$catalog->addServiceUnit(1, 'Mediatuotanto');
|
||||
$catalog->addServiceUnit(2, 'Painotuotanto');
|
||||
$catalog->addServiceUnit(9, 'Kaikille yhteiset', shared: true);
|
||||
$catalog->addWorkType(11, 1, 'videokuvaus');
|
||||
$catalog->addWorkType(12, 1, 'editointi');
|
||||
$catalog->addWorkType(21, 2, 'painotyö');
|
||||
$catalog->addWorkType(91, 9, 'suunnittelu');
|
||||
$users->addUser(10, 'Eero');
|
||||
|
||||
$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);
|
||||
$this->tentativeJobId = $createJob->execute(new CustomerJobData('Alustava', 1, [1], 10, 'one_off', 'tentative', 'normal'), 10);
|
||||
|
||||
$this->templates->addTemplate(1, 'Lyhyt video', 11, 1, [['Suunnittelu', 91], ['Kuvaus'], ['Editointi', 12]]);
|
||||
$this->templates->addTemplate(2, 'Painos', 21, 2, [['Painatus']]);
|
||||
$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);
|
||||
});
|
||||
|
||||
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);
|
||||
|
||||
$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]],
|
||||
]);
|
||||
});
|
||||
|
||||
it('rejects a template out of use or for other service units', function (int $templateId) {
|
||||
$this->addTasks->execute($this->jobId, $templateId, 10);
|
||||
})->with([3, 2, 999])->throws(WorkflowTemplateNotAvailable::class);
|
||||
|
||||
it('rejects a step whose work type is not available for the job', function () {
|
||||
expect(fn () => $this->addTasks->execute($this->jobId, 4, 10))->toThrow(InvalidTaskReference::class);
|
||||
});
|
||||
|
||||
it('adds no tasks before the job is a quote request', function () {
|
||||
$this->addTasks->execute($this->tentativeJobId, 1, 10);
|
||||
})->throws(TasksNotAllowedYet::class);
|
||||
|
||||
it('gives each task the due date and estimate entered for its step', function () {
|
||||
$this->addTasks->execute($this->jobId, 1, 10, [
|
||||
new TemplateTaskSchedule('2026-05-10', '1,5'),
|
||||
new TemplateTaskSchedule(null, '8'),
|
||||
]);
|
||||
|
||||
$tasks = array_values($this->tasks->tasks);
|
||||
|
||||
expect(array_map(fn (Task $task): array => [$task->dueOn()?->format('Y-m-d'), $task->estimatedHours()?->format()], $tasks))->toBe([
|
||||
['2026-05-10', '1.5'],
|
||||
[null, '8'],
|
||||
[null, null],
|
||||
]);
|
||||
});
|
||||
|
||||
it('rejects an estimate that is not a number', function () {
|
||||
$this->addTasks->execute($this->jobId, 1, 10, [new TemplateTaskSchedule(null, 'paljon')]);
|
||||
})->throws(InvalidEstimatedHours::class);
|
||||
@@ -0,0 +1,47 @@
|
||||
<?php
|
||||
|
||||
use App\Modules\Jobs\Domain\Services\TaskScheduler;
|
||||
|
||||
function scheduleDay(string $date): DateTimeImmutable
|
||||
{
|
||||
return new DateTimeImmutable($date);
|
||||
}
|
||||
|
||||
it('splits the estimate evenly over the working days of the period', function () {
|
||||
// Fri 4.12.2026 – Tue 8.12.2026; the weekend in between gets nothing.
|
||||
$schedule = TaskScheduler::schedule(scheduleDay('2026-12-04'), scheduleDay('2026-12-08'), 600, [], 480, []);
|
||||
|
||||
expect($schedule->minutesByDay)->toBe(['2026-12-04' => 210, '2026-12-07' => 195, '2026-12-08' => 195])
|
||||
->and($schedule->end->format('Y-m-d'))->toBe('2026-12-08')
|
||||
->and($schedule->extended)->toBeFalse();
|
||||
});
|
||||
|
||||
it('skips holidays and uses weekend days when the period has nothing else', function () {
|
||||
$overHolidays = TaskScheduler::schedule(scheduleDay('2026-12-24'), scheduleDay('2026-12-28'), 240, [], 480, ['2026-12-24', '2026-12-25']);
|
||||
$weekend = TaskScheduler::schedule(scheduleDay('2026-12-05'), scheduleDay('2026-12-06'), 300, [], 480, []);
|
||||
|
||||
expect($overHolidays->minutesByDay)->toBe(['2026-12-28' => 240])
|
||||
->and($weekend->minutesByDay)->toBe(['2026-12-05' => 150, '2026-12-06' => 150]);
|
||||
});
|
||||
|
||||
it('moves what does not fit to the next working days and ends later', function () {
|
||||
$schedule = TaskScheduler::schedule(scheduleDay('2026-12-04'), scheduleDay('2026-12-04'), 600, ['2026-12-04' => 300, '2026-12-07' => 480], 480, []);
|
||||
|
||||
expect($schedule->minutesByDay)->toBe(['2026-12-04' => 180, '2026-12-08' => 420])
|
||||
->and($schedule->end->format('Y-m-d'))->toBe('2026-12-08')
|
||||
->and($schedule->extended)->toBeTrue();
|
||||
});
|
||||
|
||||
it('books nothing without an estimate', function () {
|
||||
$schedule = TaskScheduler::schedule(scheduleDay('2026-12-07'), scheduleDay('2026-12-09'), 0, [], 480, []);
|
||||
|
||||
expect($schedule->minutesByDay)->toBe([])
|
||||
->and($schedule->end->format('Y-m-d'))->toBe('2026-12-09');
|
||||
});
|
||||
|
||||
it('gives a placed task the working days its estimate needs', function () {
|
||||
expect(TaskScheduler::defaultEnd(scheduleDay('2026-12-07'), 240, 480, [])->format('Y-m-d'))->toBe('2026-12-07')
|
||||
->and(TaskScheduler::defaultEnd(scheduleDay('2026-12-04'), 1200, 480, [])->format('Y-m-d'))->toBe('2026-12-08')
|
||||
->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');
|
||||
});
|
||||
@@ -0,0 +1,65 @@
|
||||
<?php
|
||||
|
||||
use App\Modules\Shared\Application\UseCases\HolidayCalendar;
|
||||
use App\Modules\Shared\Domain\Exceptions\HolidayAlreadyExists;
|
||||
use App\Modules\Shared\Domain\Exceptions\InvalidHolidayName;
|
||||
use App\Modules\Shared\Domain\Exceptions\NotAHoliday;
|
||||
use App\Modules\Shared\Domain\Services\FinnishHolidays;
|
||||
use Tests\Fakes\InMemoryHolidayOverrides;
|
||||
|
||||
it('calculates Easter and Midsummer Eve', function (int $year, string $easter, string $midsummerEve) {
|
||||
$holidays = FinnishHolidays::forYear($year);
|
||||
|
||||
expect(FinnishHolidays::easterSunday($year)->format('Y-m-d'))->toBe($easter)
|
||||
->and(array_search('midsummer_eve', $holidays, true))->toBe($midsummerEve);
|
||||
})->with([
|
||||
[2025, '2025-04-20', '2025-06-20'],
|
||||
[2026, '2026-04-05', '2026-06-19'],
|
||||
[2027, '2027-03-28', '2027-06-25'],
|
||||
]);
|
||||
|
||||
it('lists the holidays of a year in date order', function () {
|
||||
expect(FinnishHolidays::forYear(2026))->toBe([
|
||||
'2026-01-01' => 'new_years_day',
|
||||
'2026-01-06' => 'epiphany',
|
||||
'2026-04-03' => 'good_friday',
|
||||
'2026-04-06' => 'easter_monday',
|
||||
'2026-05-01' => 'may_day',
|
||||
'2026-05-14' => 'ascension_day',
|
||||
'2026-06-19' => 'midsummer_eve',
|
||||
'2026-12-06' => 'independence_day',
|
||||
'2026-12-24' => 'christmas_eve',
|
||||
'2026-12-25' => 'christmas_day',
|
||||
'2026-12-26' => 'st_stephens_day',
|
||||
]);
|
||||
});
|
||||
|
||||
it('takes holidays out of use and adds company days off', function () {
|
||||
$calendar = new HolidayCalendar(new InMemoryHolidayOverrides);
|
||||
|
||||
$calendar->disable('2026-12-24');
|
||||
$calendar->addCompanyDay('2026-12-23', ' Pikkujoulu ');
|
||||
|
||||
$december = $calendar->between('2026-12-20', '2026-12-31');
|
||||
|
||||
expect(array_map(fn ($holiday) => [$holiday->date, $holiday->key, $holiday->name, $holiday->active], $december))->toBe([
|
||||
['2026-12-23', null, 'Pikkujoulu', true],
|
||||
['2026-12-24', 'christmas_eve', null, false],
|
||||
['2026-12-25', 'christmas_day', null, true],
|
||||
['2026-12-26', 'st_stephens_day', null, true],
|
||||
])->and($calendar->activeDatesBetween('2026-12-20', '2026-12-31'))->toBe(['2026-12-23', '2026-12-25', '2026-12-26']);
|
||||
|
||||
$calendar->enable('2026-12-24');
|
||||
$calendar->removeCompanyDay('2026-12-23');
|
||||
|
||||
expect($calendar->activeDatesBetween('2026-12-20', '2026-12-31'))->toBe(['2026-12-24', '2026-12-25', '2026-12-26']);
|
||||
});
|
||||
|
||||
it('rejects invalid holiday changes', function () {
|
||||
$calendar = new HolidayCalendar(new InMemoryHolidayOverrides);
|
||||
|
||||
expect(fn () => $calendar->disable('2026-12-23'))->toThrow(NotAHoliday::class)
|
||||
->and(fn () => $calendar->removeCompanyDay('2026-12-25'))->toThrow(NotAHoliday::class)
|
||||
->and(fn () => $calendar->addCompanyDay('2026-12-25', 'Joulu'))->toThrow(HolidayAlreadyExists::class)
|
||||
->and(fn () => $calendar->addCompanyDay('2026-12-23', ' '))->toThrow(InvalidHolidayName::class);
|
||||
});
|
||||
Reference in New Issue
Block a user