diff --git a/app/Domain/Notifications/VacationRequestsSummaryNotification.php b/app/Domain/Notifications/VacationRequestsSummaryNotification.php new file mode 100644 index 0000000..e51a673 --- /dev/null +++ b/app/Domain/Notifications/VacationRequestsSummaryNotification.php @@ -0,0 +1,63 @@ + "waiting_for_action", + ], + ); + + return $this->buildMailMessage($notifiable, $url); + } + + protected function buildMailMessage(User $user, string $url): MailMessage + { + $user = $user->profile->first_name; + + $message = (new MailMessage()) + ->greeting( + __("Hi :user!", [ + "user" => $user, + ]) + ) + ->line("Lista wniosków oczekujących na Twoją akcję - stan na dzień {$this->day->toDisplayString()}:") + ->subject("Wnioski oczekujące na akcje - stan na dzień {$this->day->toDisplayString()}"); + + foreach ($this->vacationRequests as $request) { + $message->line( + "Wniosek nr {$request->name} użytkownika {$request->user->profile->full_name} ({$request->from->toDisplayString()} - {$request->to->toDisplayString()})" + ); + } + + return $message + ->action("Przejdź do wniosków", $url); + } +} diff --git a/app/Infrastructure/Console/Commands/SendVacationRequestRemindersToApprovers.php b/app/Infrastructure/Console/Commands/SendVacationRequestRemindersToApprovers.php deleted file mode 100644 index 99c0805..0000000 --- a/app/Infrastructure/Console/Commands/SendVacationRequestRemindersToApprovers.php +++ /dev/null @@ -1,78 +0,0 @@ -type(VacationType::all()->filter(fn(VacationType $type) => $configRetriever->isVacation($type))->all()) - ->get(); - - /** @var VacationRequest $vacationRequest */ - foreach ($vacationRequests as $vacationRequest) { - if (!$this->shouldNotify($vacationRequest, $daysCalculator)) { - continue; - } - - if ($vacationRequest->state->equals(WaitingForTechnical::class)) { - $this->notifyTechnicalApprovers($vacationRequest); - } - - if ($vacationRequest->state->equals(WaitingForAdministrative::class)) { - $this->notifyAdminApprovers($vacationRequest); - } - } - } - - protected function shouldNotify(VacationRequest $vacationRequest, WorkDaysCalculator $daysCalculator): bool - { - $days = $daysCalculator - ->calculateDays($vacationRequest->updated_at->addDay(), Carbon::today()) - ->count(); - - return $days >= static::REMINDER_INTERVAL && ($days % static::REMINDER_INTERVAL === 0); - } - - protected function notifyAdminApprovers(VacationRequest $vacationRequest): void - { - $users = User::query() - ->whereIn("role", [Role::AdministrativeApprover, Role::Administrator]) - ->get(); - - foreach ($users as $user) { - $user->notify(new VacationRequestWaitsForApprovalNotification($vacationRequest, $user)); - } - } - - protected function notifyTechnicalApprovers(VacationRequest $vacationRequest): void - { - $users = User::query() - ->whereIn("role", [Role::TechnicalApprover, Role::Administrator]) - ->get(); - - foreach ($users as $user) { - $user->notify(new VacationRequestWaitsForApprovalNotification($vacationRequest, $user)); - } - } -} diff --git a/app/Infrastructure/Console/Commands/SendVacationRequestSummariesToApprovers.php b/app/Infrastructure/Console/Commands/SendVacationRequestSummariesToApprovers.php new file mode 100644 index 0000000..9437021 --- /dev/null +++ b/app/Infrastructure/Console/Commands/SendVacationRequestSummariesToApprovers.php @@ -0,0 +1,36 @@ +whereIn("role", [Role::AdministrativeApprover, Role::TechnicalApprover, Role::Administrator]) + ->get(); + + foreach ($users as $user) { + $vacationRequests = VacationRequest::query() + ->states(VacationRequestStatesRetriever::waitingForUserActionStates($user)) + ->get(); + + if ($vacationRequests->isNotEmpty()) { + $user->notify(new VacationRequestsSummaryNotification(Carbon::today(), $vacationRequests)); + } + } + } +} diff --git a/tests/Unit/SendVacationRequestRemindersTest.php b/tests/Unit/SendVacationRequestRemindersTest.php deleted file mode 100644 index 6255744..0000000 --- a/tests/Unit/SendVacationRequestRemindersTest.php +++ /dev/null @@ -1,138 +0,0 @@ -createCurrentYearPeriod(); - - Notification::fake(); - } - - public function testReminderIsSentIfItsBeenThreeWorkDaysSinceTheUpdate(): void - { - $currentYearPeriod = YearPeriod::current(); - $this->travelTo(Carbon::create(2022, 4, 20)); - - $user = User::factory()->create(); - $technicalApprover = User::factory() - ->technicalApprover() - ->create(); - - VacationRequest::factory([ - "type" => VacationType::Vacation->value, - "state" => WaitingForTechnical::class, - ]) - ->for($user) - ->for($currentYearPeriod) - ->create(); - - $this->travelTo(Carbon::create(2022, 4, 25)); - - $this->artisan(SendVacationRequestRemindersToApprovers::class); - - Notification::assertSentTo([$technicalApprover], VacationRequestWaitsForApprovalNotification::class); - } - - public function testReminderIsSentIfItsBeenAnotherThreeWorkDaysSinceTheUpdate(): void - { - $currentYearPeriod = YearPeriod::current(); - $this->travelTo(Carbon::create(2022, 4, 20)); - - $user = User::factory()->create(); - $technicalApprover = User::factory() - ->technicalApprover() - ->create(); - - VacationRequest::factory([ - "type" => VacationType::Vacation->value, - "state" => WaitingForTechnical::class, - ]) - ->for($user) - ->for($currentYearPeriod) - ->create(); - - $this->travelTo(Carbon::create(2022, 4, 28)); - - $this->artisan(SendVacationRequestRemindersToApprovers::class); - - Notification::assertSentTo([$technicalApprover], VacationRequestWaitsForApprovalNotification::class); - } - - public function testReminderIsNotSentIfItHasntBeenThreeWorkDays(): void - { - $currentYearPeriod = YearPeriod::current(); - $this->travelTo(Carbon::create(2022, 4, 20)); - - $user = User::factory()->create(); - $technicalApprover = User::factory() - ->technicalApprover() - ->create(); - - VacationRequest::factory([ - "type" => VacationType::Vacation->value, - "state" => WaitingForTechnical::class, - ]) - ->for($user) - ->for($currentYearPeriod) - ->create(); - - $this->travelTo(Carbon::create(2022, 4, 24)); - - $this->artisan(SendVacationRequestRemindersToApprovers::class); - - Notification::assertNotSentTo([$technicalApprover], VacationRequestWaitsForApprovalNotification::class); - } - - public function testReminderIsSentToProperApprover(): void - { - $currentYearPeriod = YearPeriod::current(); - $this->travelTo(Carbon::create(2022, 4, 20)); - - $user = User::factory()->create(); - $adminApprover = User::factory() - ->administrativeApprover() - ->create(); - $technicalApprover = User::factory() - ->technicalApprover() - ->create(); - - VacationRequest::factory([ - "type" => VacationType::Vacation->value, - "state" => WaitingForAdministrative::class, - ]) - ->for($user) - ->for($currentYearPeriod) - ->create(); - - $this->travelTo(Carbon::create(2022, 4, 25)); - - $this->artisan(SendVacationRequestRemindersToApprovers::class); - - Notification::assertSentTo([$adminApprover], VacationRequestWaitsForApprovalNotification::class); - Notification::assertNotSentTo([$technicalApprover, $user], VacationRequestWaitsForApprovalNotification::class); - } -}