Add notifications: header bell, task start run and assignment notices
A new Notifications module shows a bell beside the profile menu with the number of unseen notifications and a panel of the last 7 days; opening it marks them seen, and each notification links to its job or task. - A daily run at 14.00 (notifications:task-starts) tells the assignee and members of tasks that start by the next working day, skipping holidays and days off, done tasks and archived jobs. - Jobs publishes PeopleAddedToTask and PeopleAddedToJob when someone else makes a user a task's assignee or member or gives them a role on a job; those users are notified at once. UpdateTask and ScheduleTask now take the acting user. - A notification link to #task-<id> opens that task card on the job page. Also: simplifying job fields per service unit (K31) moves to the end of phase 4, and the workflow template browser test waits for Livewire's progress bar before its last accessibility check. Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
This commit is contained in:
@@ -394,6 +394,8 @@ it('adds tasks from a workflow template with the keyboard, without accessibility
|
||||
->keys('main form button[type="submit"]', 'Enter')
|
||||
->assertSee('Lisättiin 2 tehtävää pohjasta Lyhyt video (videokuvaus).')
|
||||
->assertSee('Suunnittelu')
|
||||
// The page was reached with wire:navigate: wait until Livewire's progress bar (role="bar", not an ARIA role) is gone.
|
||||
->assertMissing('#nprogress .bar')
|
||||
->assertNoAccessibilityIssues(level: 2);
|
||||
|
||||
expect(TaskModel::query()->where('customer_job_id', $this->job->id)->orderBy('id')->pluck('estimated_hours')->map(fn ($hours) => $hours === null ? null : (float) $hours)->all())->toBe([null, 2.5]);
|
||||
|
||||
@@ -0,0 +1,71 @@
|
||||
<?php
|
||||
|
||||
use App\Models\User;
|
||||
use App\Modules\Jobs\Infrastructure\Persistence\Eloquent\CustomerJobModel;
|
||||
use App\Modules\Jobs\Infrastructure\Persistence\Eloquent\TaskModel;
|
||||
use App\Modules\Notifications\Infrastructure\Persistence\Eloquent\NotificationModel;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
|
||||
uses(RefreshDatabase::class);
|
||||
|
||||
beforeEach(function () {
|
||||
$this->user = User::factory()->create(['name' => 'Eetu Esimerkki']);
|
||||
$this->actingAs($this->user);
|
||||
$this->job = CustomerJobModel::factory()->create(['status' => 'order', 'number' => '2026-0042', 'name' => 'Messuvideo']);
|
||||
$this->task = TaskModel::factory()->assignedTo([$this->user->id])->create(['customer_job_id' => $this->job->id, 'title' => 'Kuvauspäivä']);
|
||||
|
||||
foreach ([1, 2] as $number) {
|
||||
NotificationModel::query()->create([
|
||||
'user_id' => $this->user->id,
|
||||
'type' => 'task_starts_soon',
|
||||
'params' => ['task_id' => $this->task->id, 'task_title' => 'Kuvauspäivä', 'job_id' => $this->job->id, 'job_number' => '2026-0042', 'job_name' => 'Messuvideo', 'starts_on' => '2026-11-30'],
|
||||
'dedupe_key' => 'browser:'.$number,
|
||||
'created_at' => now()->subHours($number),
|
||||
]);
|
||||
}
|
||||
});
|
||||
|
||||
it('has no accessibility issues with the notifications closed and open', function (string $device, string $colorScheme) {
|
||||
$visit = visit(route('jobs.index'));
|
||||
$visit = $device === 'mobile' ? $visit->on()->mobile() : $visit->on()->desktop();
|
||||
$visit = $colorScheme === 'dark' ? $visit->inDarkMode() : $visit->inLightMode();
|
||||
|
||||
$visit->assertNoJavaScriptErrors()
|
||||
->assertSeeIn('[data-notifications-bell]', '2')
|
||||
->assertNoAccessibilityIssues(level: 2)
|
||||
->click('[data-notifications-bell]')
|
||||
->assertVisible('#notifications-panel')
|
||||
->assertSee('Uusi')
|
||||
->assertNoAccessibilityIssues(level: 2);
|
||||
})->with([
|
||||
'desktop, light' => ['desktop', 'light'],
|
||||
'desktop, dark' => ['desktop', 'dark'],
|
||||
'mobile, light' => ['mobile', 'light'],
|
||||
'mobile, dark' => ['mobile', 'dark'],
|
||||
]);
|
||||
|
||||
it('opens with the keyboard, marks the notifications seen and closes with Escape', function () {
|
||||
visit(route('jobs.index'))
|
||||
->assertScript("document.querySelector('[data-notifications-bell]').getAttribute('aria-describedby')", 'notifications-count')
|
||||
->assertSeeIn('#notifications-count', '2 lukematonta ilmoitusta')
|
||||
->keys('[data-notifications-bell]', 'Enter')
|
||||
->assertVisible('#notifications-panel')
|
||||
->assertSeeIn('#notifications-count', 'Ei lukemattomia ilmoituksia')
|
||||
->assertScript("document.querySelector('[data-notifications-bell]').getAttribute('aria-expanded')", 'true')
|
||||
->keys('[data-notifications-bell]', 'Escape')
|
||||
->assertMissing('#notifications-panel')
|
||||
->assertScript("document.activeElement.hasAttribute('data-notifications-bell')", true)
|
||||
->resize(320, 900)
|
||||
->click('[data-notifications-bell]')
|
||||
->assertScript('document.documentElement.scrollWidth <= window.innerWidth', true);
|
||||
|
||||
expect(NotificationModel::query()->whereNull('seen_at')->count())->toBe(0);
|
||||
});
|
||||
|
||||
it('opens the task from a notification', function () {
|
||||
visit(route('jobs.index'))
|
||||
->click('[data-notifications-bell]')
|
||||
->click('#notifications-panel li:first-child a')
|
||||
->assertPathIs('/jobs/'.$this->job->id)
|
||||
->assertVisible('#task-'.$this->task->id.'-details');
|
||||
});
|
||||
@@ -0,0 +1,19 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Fakes;
|
||||
|
||||
use App\Modules\Jobs\Application\Contracts\UpcomingTasks;
|
||||
use App\Modules\Jobs\Application\DTOs\UpcomingTask;
|
||||
|
||||
class FakeUpcomingTasks implements UpcomingTasks
|
||||
{
|
||||
/**
|
||||
* @param list<UpcomingTask> $tasks
|
||||
*/
|
||||
public function __construct(public array $tasks = []) {}
|
||||
|
||||
public function startingBetween(string $after, string $to): array
|
||||
{
|
||||
return array_values(array_filter($this->tasks, fn (UpcomingTask $task): bool => $task->plannedFrom > $after && $task->plannedFrom <= $to));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Fakes;
|
||||
|
||||
use App\Modules\Notifications\Domain\Contracts\NotificationRepository;
|
||||
use App\Modules\Notifications\Domain\ValueObjects\NewNotification;
|
||||
use DateTimeImmutable;
|
||||
|
||||
class InMemoryNotificationRepository implements NotificationRepository
|
||||
{
|
||||
/** @var array<string, NewNotification> By dedupe key. */
|
||||
public array $notifications = [];
|
||||
|
||||
/** @var array<string, DateTimeImmutable> */
|
||||
public array $seenAt = [];
|
||||
|
||||
public function addIfNew(NewNotification $notification): bool
|
||||
{
|
||||
if (isset($this->notifications[$notification->dedupeKey])) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$this->notifications[$notification->dedupeKey] = $notification;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public function markSeen(int $userId, DateTimeImmutable $at): void
|
||||
{
|
||||
foreach ($this->notifications as $key => $notification) {
|
||||
if ($notification->userId === $userId && ! isset($this->seenAt[$key])) {
|
||||
$this->seenAt[$key] = $at;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public function deleteOlderThan(DateTimeImmutable $moment): void
|
||||
{
|
||||
$this->notifications = array_filter($this->notifications, fn (NewNotification $notification): bool => $notification->createdAt >= $moment);
|
||||
}
|
||||
|
||||
/**
|
||||
* Who was told about which task: "userId:taskId", in order of adding.
|
||||
*
|
||||
* @return list<string>
|
||||
*/
|
||||
public function recipients(): array
|
||||
{
|
||||
return array_values(array_map(fn (NewNotification $notification): string => $notification->userId.':'.$notification->params['task_id'], $this->notifications));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,165 @@
|
||||
<?php
|
||||
|
||||
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\Application\Contracts\UpcomingTasks;
|
||||
use App\Modules\Jobs\Application\DTOs\CustomerJobData;
|
||||
use App\Modules\Jobs\Application\UseCases\CreateCustomerJob;
|
||||
use App\Modules\Jobs\Infrastructure\Persistence\Eloquent\CustomerJobModel;
|
||||
use App\Modules\Jobs\Infrastructure\Persistence\Eloquent\TaskModel;
|
||||
use App\Modules\Notifications\Infrastructure\Persistence\Eloquent\NotificationModel;
|
||||
use Carbon\CarbonImmutable;
|
||||
use Illuminate\Console\Scheduling\Event;
|
||||
use Illuminate\Console\Scheduling\Schedule;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Livewire\Livewire;
|
||||
|
||||
uses(RefreshDatabase::class);
|
||||
|
||||
beforeEach(function () {
|
||||
// Thursday afternoon in Finland: tasks starting on Friday are notified.
|
||||
$this->travelTo(CarbonImmutable::parse('2026-11-26 14:00', 'Europe/Helsinki'));
|
||||
$this->user = User::factory()->create();
|
||||
$this->member = User::factory()->create();
|
||||
$this->job = CustomerJobModel::factory()->create(['status' => 'order', 'number' => '2026-0042', 'name' => 'Messuvideo']);
|
||||
});
|
||||
|
||||
function notificationFor(User $user, array $overrides = []): NotificationModel
|
||||
{
|
||||
static $sequence = 0;
|
||||
$sequence++;
|
||||
|
||||
return NotificationModel::query()->create([
|
||||
'user_id' => $user->id,
|
||||
'type' => 'task_starts_soon',
|
||||
'params' => ['task_id' => 7, 'task_title' => 'Kuvauspäivä', 'job_id' => 3, 'job_number' => '2026-0042', 'job_name' => 'Messuvideo', 'starts_on' => '2026-11-30'],
|
||||
'dedupe_key' => 'test:'.$sequence,
|
||||
'created_at' => now(),
|
||||
...$overrides,
|
||||
]);
|
||||
}
|
||||
|
||||
it('finds tasks starting in the period with their assignee and members, without done tasks and archived jobs', function () {
|
||||
$task = TaskModel::factory()->assignedTo([$this->user->id, $this->member->id])->create(['customer_job_id' => $this->job->id, 'title' => 'Kuvauspäivä', 'planned_from' => '2026-11-27', 'planned_to' => '2026-11-27']);
|
||||
TaskModel::factory()->assignedTo([$this->user->id])->create(['customer_job_id' => $this->job->id, 'status' => 'done', 'planned_from' => '2026-11-27']);
|
||||
TaskModel::factory()->assignedTo([$this->user->id])->create(['customer_job_id' => CustomerJobModel::factory()->create(['status' => 'archived'])->id, 'planned_from' => '2026-11-27']);
|
||||
TaskModel::factory()->assignedTo([$this->user->id])->create(['customer_job_id' => $this->job->id, 'planned_from' => '2026-11-26']);
|
||||
|
||||
$tasks = app(UpcomingTasks::class)->startingBetween('2026-11-26', '2026-11-27');
|
||||
|
||||
expect($tasks)->toHaveCount(1)
|
||||
->and($tasks[0]->taskId)->toBe($task->id)
|
||||
->and($tasks[0]->jobNumber)->toBe('2026-0042')
|
||||
->and($tasks[0]->plannedFrom)->toBe('2026-11-27')
|
||||
->and($tasks[0]->recipientIds)->toBe([$this->user->id, $this->member->id]);
|
||||
});
|
||||
|
||||
it('notifies with the daily command, scheduled at 14.00 Finnish time', function () {
|
||||
$task = TaskModel::factory()->assignedTo([$this->user->id, $this->member->id])->create(['customer_job_id' => $this->job->id, 'title' => 'Kuvauspäivä', 'planned_from' => '2026-11-27']);
|
||||
|
||||
$this->artisan('notifications:task-starts')->expectsOutputToContain('2 notifications added.')->assertSuccessful();
|
||||
$this->artisan('notifications:task-starts')->expectsOutputToContain('0 notifications added.');
|
||||
|
||||
expect(NotificationModel::query()->where('user_id', $this->member->id)->sole()->params['task_id'])->toBe($task->id);
|
||||
|
||||
$event = collect(app(Schedule::class)->events())->first(fn (Event $event): bool => str_contains((string) $event->command, 'notifications:task-starts'));
|
||||
|
||||
expect($event)->not->toBeNull()
|
||||
->and($event->expression)->toBe('0 14 * * *')
|
||||
->and($event->timezone)->toBe('Europe/Helsinki');
|
||||
});
|
||||
|
||||
it('shows the unseen count of the user\'s own notifications of the last 7 days', function () {
|
||||
notificationFor($this->user);
|
||||
notificationFor($this->user, ['created_at' => now()->subDays(8)]);
|
||||
notificationFor($this->member);
|
||||
|
||||
$this->actingAs($this->user);
|
||||
|
||||
Livewire::test('notifications::bell')
|
||||
->assertSee('1 lukematon ilmoitus')
|
||||
->assertSee('Tehtävä Kuvauspäivä (työ 2026-0042 Messuvideo) alkaa ma 30.11.2026.')
|
||||
->assertSee(route('jobs.show', 3).'#task-7', false)
|
||||
->assertSee('26.11.2026 klo 14.00');
|
||||
});
|
||||
|
||||
it('marks the notifications seen when the panel is opened, keeping them marked new for now', function () {
|
||||
$notification = notificationFor($this->user);
|
||||
$this->actingAs($this->user);
|
||||
|
||||
Livewire::test('notifications::bell')
|
||||
->call('open')
|
||||
->assertSee('Ei lukemattomia ilmoituksia')
|
||||
->assertSet('newIds', [$notification->id])
|
||||
->assertSee('Uusi');
|
||||
|
||||
expect($notification->fresh()->seen_at)->not->toBeNull();
|
||||
|
||||
Livewire::test('notifications::bell')->assertDontSee('Uusi');
|
||||
});
|
||||
|
||||
it('tells when there are no notifications, and in the user\'s language', function () {
|
||||
$this->actingAs($this->user);
|
||||
Livewire::test('notifications::bell')->assertSee('Ei ilmoituksia viimeiseltä 7 päivältä.');
|
||||
|
||||
notificationFor($this->member);
|
||||
$this->member->forceFill(['locale' => 'en'])->save();
|
||||
$this->actingAs($this->member);
|
||||
app()->setLocale('en');
|
||||
|
||||
Livewire::test('notifications::bell')
|
||||
->assertSee('Task Kuvauspäivä (job 2026-0042 Messuvideo) starts on Mon 30.11.2026.')
|
||||
->assertSee('1 unread notification');
|
||||
});
|
||||
|
||||
it('shows the bell in the header of signed-in pages', function () {
|
||||
$this->actingAs($this->user);
|
||||
|
||||
$this->get(route('jobs.index'))
|
||||
->assertOk()
|
||||
->assertSee('data-notifications-bell', false);
|
||||
});
|
||||
|
||||
it('notifies people at once when someone else adds them to a task, not the one who added', function () {
|
||||
$marketing = ServiceUnitModel::query()->where('name', 'Markkinointi')->sole();
|
||||
DB::table('customer_job_service_unit')->where('customer_job_id', $this->job->id)->update(['service_unit_id' => $marketing->id]);
|
||||
$this->actingAs($this->user);
|
||||
|
||||
Livewire::test('jobs::task-form', ['job' => $this->job->id])
|
||||
->set('form.title', 'Mainoskuvat')
|
||||
->set('form.work_type_id', (string) WorkTypeModel::query()->where('name', 'graafinen työ')->value('id'))
|
||||
->set('form.member_user_ids', [(string) $this->member->id])
|
||||
->call('save')
|
||||
->assertHasNoErrors();
|
||||
|
||||
expect(NotificationModel::query()->where('user_id', $this->user->id)->count())->toBe(0)
|
||||
->and(NotificationModel::query()->where('user_id', $this->member->id)->sole()->type->value)->toBe('task_assigned');
|
||||
|
||||
$this->actingAs($this->member);
|
||||
Livewire::test('notifications::bell')
|
||||
->assertSee('Sinut lisättiin tehtävän Mainoskuvat jäseneksi (työ 2026-0042 Messuvideo).')
|
||||
->assertSee(route('jobs.show', $this->job->id).'#task-'.TaskModel::query()->sole()->id, false);
|
||||
});
|
||||
|
||||
it('notifies people at once about their new roles on a job', function () {
|
||||
$jobId = app(CreateCustomerJob::class)->execute(new CustomerJobData(
|
||||
name: 'Esite',
|
||||
customerId: CustomerModel::factory()->create()->id,
|
||||
serviceUnitIds: [ServiceUnitModel::query()->value('id')],
|
||||
responsibleUserId: $this->member->id,
|
||||
kind: 'one_off',
|
||||
status: 'order',
|
||||
priority: 'normal',
|
||||
memberUserIds: [$this->member->id, $this->user->id],
|
||||
), $this->user->id);
|
||||
|
||||
$this->actingAs($this->member);
|
||||
Livewire::test('notifications::bell')
|
||||
->assertSee('Sinut lisättiin työn '.CustomerJobModel::query()->find($jobId)->number.' Esite vastuuhenkilöksi ja jäseneksi.')
|
||||
->assertSee(route('jobs.show', $jobId), false);
|
||||
|
||||
expect(NotificationModel::query()->where('user_id', $this->user->id)->count())->toBe(0);
|
||||
});
|
||||
@@ -6,16 +6,19 @@ use App\Modules\Jobs\Application\UseCases\ChangeJobStatus;
|
||||
use App\Modules\Jobs\Application\UseCases\CreateCustomerJob;
|
||||
use App\Modules\Jobs\Application\UseCases\UpdateCustomerJob;
|
||||
use App\Modules\Jobs\Domain\Enums\JobStatus;
|
||||
use App\Modules\Jobs\Domain\Events\PeopleAddedToJob;
|
||||
use App\Modules\Jobs\Domain\Exceptions\InvalidJobReference;
|
||||
use App\Modules\Users\Domain\Enums\Role;
|
||||
use Tests\Fakes\FakeCatalogQueries;
|
||||
use Tests\Fakes\FakeClock;
|
||||
use Tests\Fakes\FakeCustomerQueries;
|
||||
use Tests\Fakes\FakeEventDispatcher;
|
||||
use Tests\Fakes\FakeTransactionManager;
|
||||
use Tests\Fakes\FakeUserAccountQueries;
|
||||
use Tests\Fakes\InMemoryCustomerJobRepository;
|
||||
|
||||
beforeEach(function () {
|
||||
$this->events = new FakeEventDispatcher;
|
||||
$this->jobs = new InMemoryCustomerJobRepository;
|
||||
$this->customers = new FakeCustomerQueries;
|
||||
$this->catalog = new FakeCatalogQueries;
|
||||
@@ -33,8 +36,8 @@ beforeEach(function () {
|
||||
$this->users->addUser(12, 'Pois', active: false);
|
||||
|
||||
$factory = new JobDetailsFactory($this->customers, $this->catalog, $this->users);
|
||||
$this->create = new CreateCustomerJob($this->jobs, $this->jobs, $factory, $transactions, $this->clock);
|
||||
$this->update = new UpdateCustomerJob($this->jobs, $factory, $transactions, $this->clock);
|
||||
$this->create = new CreateCustomerJob($this->jobs, $this->jobs, $factory, $transactions, $this->clock, $this->events);
|
||||
$this->update = new UpdateCustomerJob($this->jobs, $factory, $transactions, $this->clock, $this->events);
|
||||
$this->changeStatus = new ChangeJobStatus($this->jobs, $transactions, new FakeClock(new DateTimeImmutable('2026-05-01')));
|
||||
});
|
||||
|
||||
@@ -104,3 +107,21 @@ it('records each changed field, and nothing when nothing changed', function () {
|
||||
|
||||
expect($this->jobs->changedFields($jobId))->toBe(['created', 'priority', 'customer_contact', 'tags', 'status']);
|
||||
});
|
||||
|
||||
it('publishes the new roles people got on a job', function () {
|
||||
$jobId = $this->create->execute(jobData(['memberUserIds' => [11]]), 10);
|
||||
$added = end($this->events->dispatched);
|
||||
|
||||
expect($added)->toBeInstanceOf(PeopleAddedToJob::class)
|
||||
->and($added->jobId)->toBe($jobId)
|
||||
->and($added->jobName)->toBe('Messuvideo')
|
||||
->and($added->newRoles)->toBe([10 => ['responsible'], 11 => ['member']])
|
||||
->and($added->actorUserId)->toBe(10);
|
||||
|
||||
$count = count($this->events->dispatched);
|
||||
$this->update->execute($jobId, jobData(['memberUserIds' => [11], 'name' => 'Messuvideo 2']), 10);
|
||||
expect($this->events->dispatched)->toHaveCount($count);
|
||||
|
||||
$this->update->execute($jobId, jobData(['memberUserIds' => [11], 'contactUserId' => 11]), 10);
|
||||
expect(end($this->events->dispatched)->newRoles)->toBe([11 => ['contact']]);
|
||||
});
|
||||
|
||||
@@ -15,6 +15,7 @@ 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\Events\PeopleAddedToTask;
|
||||
use App\Modules\Jobs\Domain\Exceptions\InvalidTaskReference;
|
||||
use App\Modules\Jobs\Domain\Exceptions\InvalidTaskTitle;
|
||||
use App\Modules\Jobs\Domain\Exceptions\PlanEndsBeforeStart;
|
||||
@@ -26,6 +27,7 @@ use App\Modules\Shared\Application\UseCases\HolidayCalendar;
|
||||
use Tests\Fakes\FakeCatalogQueries;
|
||||
use Tests\Fakes\FakeClock;
|
||||
use Tests\Fakes\FakeCustomerQueries;
|
||||
use Tests\Fakes\FakeEventDispatcher;
|
||||
use Tests\Fakes\FakeSettings;
|
||||
use Tests\Fakes\FakeTaskImageFiles;
|
||||
use Tests\Fakes\FakeTaskUsage;
|
||||
@@ -38,6 +40,7 @@ use Tests\Fakes\InMemoryTaskImageRepository;
|
||||
use Tests\Fakes\InMemoryTaskRepository;
|
||||
|
||||
beforeEach(function () {
|
||||
$this->events = new FakeEventDispatcher;
|
||||
$this->jobs = new InMemoryCustomerJobRepository;
|
||||
$this->tasks = new InMemoryTaskRepository;
|
||||
$customers = new FakeCustomerQueries;
|
||||
@@ -55,16 +58,16 @@ beforeEach(function () {
|
||||
$this->users->addUser(10, 'Eero');
|
||||
$this->users->addUser(12, 'Pois', active: false);
|
||||
|
||||
$createJob = new CreateCustomerJob($this->jobs, $this->jobs, new JobDetailsFactory($customers, $this->catalog, $this->users), $transactions, new FakeClock(new DateTimeImmutable('2026-05-01')));
|
||||
$createJob = new CreateCustomerJob($this->jobs, $this->jobs, new JobDetailsFactory($customers, $this->catalog, $this->users), $transactions, new FakeClock(new DateTimeImmutable('2026-05-01')), $this->events);
|
||||
$this->jobId = $createJob->execute(new CustomerJobData('Kampanja', 1, [1], 10, 'one_off', 'quote_request', 'normal'), 10);
|
||||
$this->tentativeJobId = $createJob->execute(new CustomerJobData('Alustava', 1, [1], 10, 'one_off', 'tentative', 'normal'), 10);
|
||||
|
||||
$checker = new TaskReferenceChecker($this->catalog, $this->users);
|
||||
$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->addTask = new AddTask($this->jobs, $this->tasks, $checker, $this->planner, $transactions, $this->events);
|
||||
$this->updateTask = new UpdateTask($this->jobs, $this->tasks, $checker, $this->planner, $transactions, $this->events);
|
||||
$this->scheduleTask = new ScheduleTask($this->jobs, $this->tasks, $checker, $this->planner, $transactions, $this->events);
|
||||
$this->clearSchedule = new ClearTaskSchedule($this->tasks, $this->planner, $transactions);
|
||||
$this->changeTaskStatus = new ChangeTaskStatus($this->tasks, $transactions);
|
||||
$this->usage = new FakeTaskUsage;
|
||||
@@ -112,7 +115,7 @@ it('rejects a work type of another unit and an inactive assignee or member', fun
|
||||
it('changes, moves through statuses and removes a task', function () {
|
||||
$taskId = $this->addTask->execute($this->jobId, new TaskData('Mainoskuvat', 11, 'waiting'), 10);
|
||||
|
||||
$this->updateTask->execute($this->jobId, $taskId, new TaskData('Mainoskuvat ja banneri', 11, 'in_progress'));
|
||||
$this->updateTask->execute($this->jobId, $taskId, new TaskData('Mainoskuvat ja banneri', 11, 'in_progress'), 10);
|
||||
$this->changeTaskStatus->execute($this->jobId, $taskId, TaskStatus::Done);
|
||||
|
||||
$task = $this->tasks->findForJob($this->jobId, $taskId);
|
||||
@@ -127,7 +130,7 @@ it('can still change a task after the job moved back to an earlier status', func
|
||||
$taskId = $this->addTask->execute($this->jobId, new TaskData('Mainoskuvat', 11, 'waiting'), 10);
|
||||
$this->changeJobStatus->execute($this->jobId, JobStatus::Tentative, 10);
|
||||
|
||||
$this->updateTask->execute($this->jobId, $taskId, new TaskData('Mainoskuvat', 11, 'in_review'));
|
||||
$this->updateTask->execute($this->jobId, $taskId, new TaskData('Mainoskuvat', 11, 'in_review'), 10);
|
||||
|
||||
expect($this->tasks->findForJob($this->jobId, $taskId)?->status())->toBe(TaskStatus::InReview);
|
||||
});
|
||||
@@ -136,7 +139,7 @@ it('does not touch a task of another job', function (string $action) {
|
||||
$taskId = $this->addTask->execute($this->jobId, new TaskData('Mainoskuvat', 11, 'waiting'), 10);
|
||||
|
||||
match ($action) {
|
||||
'update' => $this->updateTask->execute($this->tentativeJobId, $taskId, new TaskData('X', 11, 'done')),
|
||||
'update' => $this->updateTask->execute($this->tentativeJobId, $taskId, new TaskData('X', 11, 'done'), 10),
|
||||
'status' => $this->changeTaskStatus->execute($this->tentativeJobId, $taskId, TaskStatus::Done),
|
||||
'remove' => $this->removeTask->execute($this->tentativeJobId, $taskId),
|
||||
};
|
||||
@@ -165,7 +168,7 @@ 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', 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');
|
||||
$result = $this->scheduleTask->execute($this->jobId, $taskId, 10, 10, '2026-12-04', '2026-12-04', 10);
|
||||
|
||||
expect($result->extended)->toBeTrue()
|
||||
->and($result->plannedTo)->toBe('2026-12-07')
|
||||
@@ -184,7 +187,7 @@ it('moves a task to another person and gives it the days its estimate needs', fu
|
||||
$this->users->addUser(11, 'Aino');
|
||||
$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);
|
||||
$result = $this->scheduleTask->execute($this->jobId, $taskId, 10, 11, '2026-12-11', null, 10);
|
||||
|
||||
expect($this->tasks->findForJob($this->jobId, $taskId)?->assigneeUserId())->toBe(11)
|
||||
->and($result->plannedFrom)->toBe('2026-12-11')
|
||||
@@ -197,8 +200,8 @@ 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', 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);
|
||||
expect(fn () => $this->scheduleTask->execute($this->jobId, $taskId, 10, 12, '2026-12-07', '2026-12-07', 10))->toThrow(InvalidTaskReference::class)
|
||||
->and(fn () => $this->scheduleTask->execute($this->jobId, $taskId, 10, 10, '2026-12-07', '2026-12-04', 10))->toThrow(PlanEndsBeforeStart::class);
|
||||
});
|
||||
|
||||
it('takes a task out of the calendar', function () {
|
||||
@@ -225,7 +228,7 @@ it('moves a task to a member, who then is the assignee and no longer a member',
|
||||
$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');
|
||||
$this->scheduleTask->execute($this->jobId, $taskId, 10, 11, '2026-12-07', '2026-12-07', 10);
|
||||
$task = $this->tasks->findForJob($this->jobId, $taskId);
|
||||
|
||||
expect($task?->assigneeUserId())->toBe(11)
|
||||
@@ -240,3 +243,32 @@ it('books nothing for a task without an assignee', function () {
|
||||
expect($this->bookings->of($taskId, 10))->toBe([])
|
||||
->and($this->tasks->findForJob($this->jobId, $taskId)?->plannedTo()?->format('Y-m-d'))->toBe('2026-12-07');
|
||||
});
|
||||
|
||||
it('publishes who was added to a task: the new assignee and new members, not those who already were', function () {
|
||||
$this->users->addUser(11, 'Aino');
|
||||
$this->users->addUser(13, 'Ville');
|
||||
|
||||
$taskId = $this->addTask->execute($this->jobId, new TaskData('Mainoskuvat', 11, 'waiting', assigneeUserId: 10, memberUserIds: [11]), 10);
|
||||
$added = end($this->events->dispatched);
|
||||
|
||||
expect($added)->toBeInstanceOf(PeopleAddedToTask::class)
|
||||
->and($added->taskId)->toBe($taskId)
|
||||
->and($added->taskTitle)->toBe('Mainoskuvat')
|
||||
->and($added->jobName)->toBe('Kampanja')
|
||||
->and($added->newAssigneeId)->toBe(10)
|
||||
->and($added->newMemberIds)->toBe([11])
|
||||
->and($added->actorUserId)->toBe(10);
|
||||
|
||||
$count = count($this->events->dispatched);
|
||||
$this->updateTask->execute($this->jobId, $taskId, new TaskData('Mainoskuvat', 11, 'waiting', assigneeUserId: 10, memberUserIds: [11]), 10);
|
||||
expect($this->events->dispatched)->toHaveCount($count);
|
||||
|
||||
$this->updateTask->execute($this->jobId, $taskId, new TaskData('Mainoskuvat', 11, 'waiting', assigneeUserId: 10, memberUserIds: [11, 13]), 11);
|
||||
expect(end($this->events->dispatched)->newAssigneeId)->toBeNull()
|
||||
->and(end($this->events->dispatched)->newMemberIds)->toBe([13])
|
||||
->and(end($this->events->dispatched)->actorUserId)->toBe(11);
|
||||
|
||||
$this->scheduleTask->execute($this->jobId, $taskId, 10, 13, '2026-12-07', '2026-12-07', 10);
|
||||
expect(end($this->events->dispatched)->newAssigneeId)->toBe(13)
|
||||
->and(end($this->events->dispatched)->newMemberIds)->toBe([]);
|
||||
});
|
||||
|
||||
@@ -17,6 +17,7 @@ use App\Modules\Shared\Application\UseCases\HolidayCalendar;
|
||||
use Tests\Fakes\FakeCatalogQueries;
|
||||
use Tests\Fakes\FakeClock;
|
||||
use Tests\Fakes\FakeCustomerQueries;
|
||||
use Tests\Fakes\FakeEventDispatcher;
|
||||
use Tests\Fakes\FakeSettings;
|
||||
use Tests\Fakes\FakeTransactionManager;
|
||||
use Tests\Fakes\FakeUserAccountQueries;
|
||||
@@ -27,6 +28,7 @@ use Tests\Fakes\InMemoryTaskBookingRepository;
|
||||
use Tests\Fakes\InMemoryTaskRepository;
|
||||
|
||||
beforeEach(function () {
|
||||
$this->events = new FakeEventDispatcher;
|
||||
$this->jobs = new InMemoryCustomerJobRepository;
|
||||
$this->tasks = new InMemoryTaskRepository;
|
||||
$customers = new FakeCustomerQueries;
|
||||
@@ -47,7 +49,7 @@ beforeEach(function () {
|
||||
$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')));
|
||||
$createJob = new CreateCustomerJob($this->jobs, $this->jobs, new JobDetailsFactory($customers, $catalog, $users), $transactions, new FakeClock(new DateTimeImmutable('2026-05-01')), $this->events);
|
||||
$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);
|
||||
|
||||
@@ -58,7 +60,7 @@ beforeEach(function () {
|
||||
|
||||
$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);
|
||||
$this->addTasks = new AddTasksFromWorkflowTemplate($this->jobs, $this->tasks, $this->templates, new TaskReferenceChecker($catalog, $users), $planner, new FakeClock(new DateTimeImmutable('2026-05-01')), $transactions, $this->events);
|
||||
});
|
||||
|
||||
it('adds a waiting task per step in order, with the step work type and the adder as assignee', function () {
|
||||
|
||||
@@ -0,0 +1,46 @@
|
||||
<?php
|
||||
|
||||
use App\Modules\Jobs\Domain\Events\PeopleAddedToJob;
|
||||
use App\Modules\Jobs\Domain\Events\PeopleAddedToTask;
|
||||
use App\Modules\Notifications\Application\UseCases\NotifyAddedPeople;
|
||||
use App\Modules\Notifications\Domain\Enums\NotificationType;
|
||||
use Tests\Fakes\FakeClock;
|
||||
use Tests\Fakes\InMemoryNotificationRepository;
|
||||
|
||||
beforeEach(function () {
|
||||
$this->notifications = new InMemoryNotificationRepository;
|
||||
$this->notify = new NotifyAddedPeople($this->notifications, new FakeClock(new DateTimeImmutable('2026-11-25')));
|
||||
});
|
||||
|
||||
it('tells the new assignee and members of a task, but not the one who added them', function () {
|
||||
$this->notify->forTask(new PeopleAddedToTask(7, 'Kuvauspäivä', 3, '2026-0042', 'Messuvideo', 10, [11, 12], 12));
|
||||
|
||||
$notifications = array_values($this->notifications->notifications);
|
||||
|
||||
expect($notifications)->toHaveCount(2)
|
||||
->and($notifications[0]->userId)->toBe(10)
|
||||
->and($notifications[0]->type)->toBe(NotificationType::TaskAssigned)
|
||||
->and($notifications[0]->params)->toBe(['task_id' => 7, 'task_title' => 'Kuvauspäivä', 'job_id' => 3, 'job_number' => '2026-0042', 'job_name' => 'Messuvideo', 'role' => 'assignee'])
|
||||
->and($notifications[1]->userId)->toBe(11)
|
||||
->and($notifications[1]->params['role'])->toBe('member');
|
||||
});
|
||||
|
||||
it('tells each person about their new roles on a job, but not the one who gave them', function () {
|
||||
$this->notify->forJob(new PeopleAddedToJob(3, '2026-0042', 'Messuvideo', [10 => ['responsible'], 11 => ['contact', 'member']], 10));
|
||||
|
||||
$notifications = array_values($this->notifications->notifications);
|
||||
|
||||
expect($notifications)->toHaveCount(1)
|
||||
->and($notifications[0]->userId)->toBe(11)
|
||||
->and($notifications[0]->type)->toBe(NotificationType::JobAssigned)
|
||||
->and($notifications[0]->params['roles'])->toBe('contact,member');
|
||||
});
|
||||
|
||||
it('tells again when the same person is added again later', function () {
|
||||
$event = new PeopleAddedToTask(7, 'Kuvauspäivä', 3, '2026-0042', 'Messuvideo', 10, [], 12);
|
||||
|
||||
$this->notify->forTask($event);
|
||||
$this->notify->forTask($event);
|
||||
|
||||
expect($this->notifications->notifications)->toHaveCount(2);
|
||||
});
|
||||
@@ -0,0 +1,90 @@
|
||||
<?php
|
||||
|
||||
use App\Modules\Jobs\Application\DTOs\UpcomingTask;
|
||||
use App\Modules\Notifications\Application\Services\WorkingDays;
|
||||
use App\Modules\Notifications\Application\UseCases\NotifyTasksStartingSoon;
|
||||
use App\Modules\Notifications\Domain\ValueObjects\NewNotification;
|
||||
use App\Modules\Shared\Application\UseCases\HolidayCalendar;
|
||||
use Tests\Fakes\FakeClock;
|
||||
use Tests\Fakes\FakeTransactionManager;
|
||||
use Tests\Fakes\FakeUpcomingTasks;
|
||||
use Tests\Fakes\InMemoryHolidayOverrides;
|
||||
use Tests\Fakes\InMemoryNotificationRepository;
|
||||
|
||||
beforeEach(function () {
|
||||
$this->tasks = new FakeUpcomingTasks;
|
||||
$this->notifications = new InMemoryNotificationRepository;
|
||||
$this->clock = new FakeClock(new DateTimeImmutable('2026-11-26'));
|
||||
$this->notify = new NotifyTasksStartingSoon(
|
||||
$this->tasks,
|
||||
$this->notifications,
|
||||
new WorkingDays(new HolidayCalendar(new InMemoryHolidayOverrides)),
|
||||
new FakeTransactionManager,
|
||||
$this->clock,
|
||||
);
|
||||
});
|
||||
|
||||
function upcomingTask(int $id, string $startsOn, array $recipients = [10]): UpcomingTask
|
||||
{
|
||||
return new UpcomingTask($id, 'Tehtävä '.$id, 5, '2026-0042', 'Messuvideo', $startsOn, $recipients);
|
||||
}
|
||||
|
||||
it('notifies on Thursday about tasks starting on Friday, not later ones or today\'s', function () {
|
||||
$this->tasks->tasks = [upcomingTask(1, '2026-11-26'), upcomingTask(2, '2026-11-27'), upcomingTask(3, '2026-11-30')];
|
||||
|
||||
expect($this->notify->execute())->toBe(1)
|
||||
->and($this->notifications->recipients())->toBe(['10:2']);
|
||||
});
|
||||
|
||||
it('notifies on Friday about tasks starting on Saturday to Monday', function () {
|
||||
$this->clock->today = new DateTimeImmutable('2026-11-27');
|
||||
$this->tasks->tasks = [upcomingTask(1, '2026-11-28'), upcomingTask(2, '2026-11-30'), upcomingTask(3, '2026-12-01')];
|
||||
|
||||
$this->notify->execute();
|
||||
|
||||
expect($this->notifications->recipients())->toBe(['10:1', '10:2']);
|
||||
});
|
||||
|
||||
it('skips holidays: before Christmas it notifies up to the next working day after them', function () {
|
||||
$this->clock->today = new DateTimeImmutable('2026-12-23');
|
||||
$this->tasks->tasks = [upcomingTask(1, '2026-12-28'), upcomingTask(2, '2026-12-29')];
|
||||
|
||||
$this->notify->execute();
|
||||
|
||||
expect($this->notifications->recipients())->toBe(['10:1']);
|
||||
});
|
||||
|
||||
it('notifies nobody on a day off', function () {
|
||||
$this->clock->today = new DateTimeImmutable('2026-11-29');
|
||||
$this->tasks->tasks = [upcomingTask(1, '2026-11-30')];
|
||||
|
||||
expect($this->notify->execute())->toBe(0)
|
||||
->and($this->notifications->notifications)->toBe([]);
|
||||
});
|
||||
|
||||
it('notifies each recipient once, also when run again', function () {
|
||||
$this->tasks->tasks = [upcomingTask(1, '2026-11-27', [10, 12])];
|
||||
|
||||
expect($this->notify->execute())->toBe(2)
|
||||
->and($this->notify->execute())->toBe(0)
|
||||
->and($this->notifications->recipients())->toBe(['10:1', '12:1']);
|
||||
|
||||
$notification = array_values($this->notifications->notifications)[0];
|
||||
expect($notification->params)->toBe([
|
||||
'task_id' => 1,
|
||||
'task_title' => 'Tehtävä 1',
|
||||
'job_id' => 5,
|
||||
'job_number' => '2026-0042',
|
||||
'job_name' => 'Messuvideo',
|
||||
'starts_on' => '2026-11-27',
|
||||
]);
|
||||
});
|
||||
|
||||
it('removes notifications older than a week', function () {
|
||||
$this->notifications->addIfNew(NewNotification::taskStartsSoon(10, 1, '2026-11-18', ['task_id' => 1], new DateTimeImmutable('2026-11-17 14:00')));
|
||||
$this->notifications->addIfNew(NewNotification::taskStartsSoon(10, 2, '2026-11-20', ['task_id' => 2], new DateTimeImmutable('2026-11-19 14:00')));
|
||||
|
||||
$this->notify->execute();
|
||||
|
||||
expect($this->notifications->recipients())->toBe(['10:2']);
|
||||
});
|
||||
Reference in New Issue
Block a user