From 3f551d162fba35a3b9c20b985f99e4dbed44c0b0 Mon Sep 17 00:00:00 2001 From: EwelinaSkrzypacz Date: Tue, 5 Sep 2023 13:32:52 +0200 Subject: [PATCH 1/3] #235 - wip --- .../BenefitsReportCreationNotification.php | 44 +++++++++++++++++++ ...otificationAboutBenefitsReportCreation.php | 27 ++++++++++++ app/Infrastructure/Console/Kernel.php | 5 +++ lang/pl.json | 5 ++- ...BenefitsReportCreationNotificationTest.php | 43 ++++++++++++++++++ 5 files changed, 123 insertions(+), 1 deletion(-) create mode 100644 app/Domain/Notifications/BenefitsReportCreationNotification.php create mode 100644 app/Infrastructure/Console/Commands/SendNotificationAboutBenefitsReportCreation.php create mode 100644 tests/Unit/BenefitsReportCreationNotificationTest.php diff --git a/app/Domain/Notifications/BenefitsReportCreationNotification.php b/app/Domain/Notifications/BenefitsReportCreationNotification.php new file mode 100644 index 00000000..ef29a04a --- /dev/null +++ b/app/Domain/Notifications/BenefitsReportCreationNotification.php @@ -0,0 +1,44 @@ +buildMailMessage($notifiable, $url); + } + + protected function buildMailMessage(Notifiable $notifiable, string $url): MailMessage + { + $user = $notifiable->profile->first_name; + + $message = (new MailMessage()) + ->greeting( + __("Hi :user!", [ + "user" => $user, + ]), + ) + ->subject(__("Reminder to create benefit report")) + ->line(__("This message is a reminder to create a report for benefits for this month. If a report has already been created, please ignore this message.")); + + return $message + ->action(__("Benefits reports"), $url); + } +} diff --git a/app/Infrastructure/Console/Commands/SendNotificationAboutBenefitsReportCreation.php b/app/Infrastructure/Console/Commands/SendNotificationAboutBenefitsReportCreation.php new file mode 100644 index 00000000..54c44582 --- /dev/null +++ b/app/Infrastructure/Console/Commands/SendNotificationAboutBenefitsReportCreation.php @@ -0,0 +1,27 @@ +whereIn("role", [Role::AdministrativeApprover]) + ->get(); + + foreach ($usersToNotify as $user) { + $user->notify(new BenefitsReportCreationNotification()); + } + } +} diff --git a/app/Infrastructure/Console/Kernel.php b/app/Infrastructure/Console/Kernel.php index 504f6cbc..62d2c536 100644 --- a/app/Infrastructure/Console/Kernel.php +++ b/app/Infrastructure/Console/Kernel.php @@ -11,6 +11,7 @@ use Psr\Container\NotFoundExceptionInterface; use Toby\Infrastructure\Console\Commands\Database\BackupPostgresDatabase; use Toby\Infrastructure\Console\Commands\SendDailySummaryToSlack; +use Toby\Infrastructure\Console\Commands\SendNotificationAboutBenefitsReportCreation; use Toby\Infrastructure\Console\Commands\SendNotificationAboutUpcomingAndOverdueMedicalExams; use Toby\Infrastructure\Console\Commands\SendNotificationAboutUpcomingAndOverdueOhsTraining; use Toby\Infrastructure\Console\Commands\SendVacationRequestSummariesToApprovers; @@ -48,6 +49,10 @@ protected function schedule(Schedule $schedule): void ->monthlyOn(1, "08:00") ->onOneServer(); + $schedule->command(SendNotificationAboutBenefitsReportCreation::class) + ->lastDayOfMonth("08:00") + ->onOneServer(); + $schedule->job(CheckYearPeriod::class) ->yearlyOn(1, 1, "01:00") ->onOneServer(); diff --git a/lang/pl.json b/lang/pl.json index 2029d0af..9e191e1f 100644 --- a/lang/pl.json +++ b/lang/pl.json @@ -142,5 +142,8 @@ "The deadline for OHS training for some employees is about to expire.": "Niedługo mija termin szkolenia BHP dla części pracowników.", "Below is a list of employees with upcoming OHS training:": "Poniżej znajduje się lista pracowników ze zbliżającym się terminem szkolenia BHP:", "The deadline for OHS training for some employees has passed.": "Termin szkolenia BHP dla części pracowników minął.", - "Below is a list of employees with overdue OHS training:": "Poniżej znajduje się lista pracowników z przeterminowanym szkoleniem BHP:" + "Below is a list of employees with overdue OHS training:": "Poniżej znajduje się lista pracowników z przeterminowanym szkoleniem BHP:", + "Reminder to create benefit report": "Przypomnienie o utworzeniu raportu benefitów", + "This message is a reminder to create a report for benefits for this month. If a report has already been created, please ignore this message.": "Ta wiadomość jest przypomnieniem o utworzeniu raportu dla benefitów za obecny miesiąc. Jeśli raport już został utworzony, zignoruj tę wiadomość.", + "Benefits reports": "Raporty benefitów" } diff --git a/tests/Unit/BenefitsReportCreationNotificationTest.php b/tests/Unit/BenefitsReportCreationNotificationTest.php new file mode 100644 index 00000000..18fb7aab --- /dev/null +++ b/tests/Unit/BenefitsReportCreationNotificationTest.php @@ -0,0 +1,43 @@ + Role::Employee, + ])->create(); + $technicalApprover = User::factory([ + "role" => Role::TechnicalApprover, + ])->create(); + $administrativeApprover = User::factory([ + "role" => Role::AdministrativeApprover, + ])->create(); + + $admin = User::factory([ + "role" => Role::Administrator, + ])->create(); + + $this->artisan(SendNotificationAboutBenefitsReportCreation::class) + ->execute(); + + Notification::assertSentTo($administrativeApprover, BenefitsReportCreationNotification::class); + Notification::assertNotSentTo([$user, $technicalApprover, $admin], BenefitsReportCreationNotification::class); + } +} From 35650aeb4636a1fdd10dc8cef9b6aae58deb4b54 Mon Sep 17 00:00:00 2001 From: Ewelina Skrzypacz <56546832+EwelinaSkrzypacz@users.noreply.github.com> Date: Mon, 25 Sep 2023 09:55:03 +0200 Subject: [PATCH 2/3] Update app/Domain/Notifications/BenefitsReportCreationNotification.php Co-authored-by: Krzysztof Rewak --- .../Notifications/BenefitsReportCreationNotification.php | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/app/Domain/Notifications/BenefitsReportCreationNotification.php b/app/Domain/Notifications/BenefitsReportCreationNotification.php index ef29a04a..068f7a72 100644 --- a/app/Domain/Notifications/BenefitsReportCreationNotification.php +++ b/app/Domain/Notifications/BenefitsReportCreationNotification.php @@ -18,9 +18,7 @@ public function via(): array public function toMail(Notifiable $notifiable): MailMessage { - $url = route( - "assigned-benefits.index", - ); + $url = route("assigned-benefits.index"); return $this->buildMailMessage($notifiable, $url); } From 481bb2e3e381b7c888376926081030a45982ca3b Mon Sep 17 00:00:00 2001 From: EwelinaSkrzypacz Date: Tue, 26 Sep 2023 07:34:10 +0200 Subject: [PATCH 3/3] #235 - fix --- ...NotificationAboutBenefitsReportCreation.php | 7 ++----- config/permission.php | 2 ++ database/seeders/PermissionsSeeder.php | 18 ++++++++++++------ resources/js/Composables/permissionInfo.js | 5 +++++ tests/Feature/PermissionTest.php | 3 +++ .../BenefitsReportCreationNotificationTest.php | 18 ++++-------------- 6 files changed, 28 insertions(+), 25 deletions(-) diff --git a/app/Infrastructure/Console/Commands/SendNotificationAboutBenefitsReportCreation.php b/app/Infrastructure/Console/Commands/SendNotificationAboutBenefitsReportCreation.php index 54c44582..b3c95291 100644 --- a/app/Infrastructure/Console/Commands/SendNotificationAboutBenefitsReportCreation.php +++ b/app/Infrastructure/Console/Commands/SendNotificationAboutBenefitsReportCreation.php @@ -5,9 +5,8 @@ namespace Toby\Infrastructure\Console\Commands; use Illuminate\Console\Command; -use Toby\Domain\Enums\Role; +use Spatie\Permission\Models\Permission; use Toby\Domain\Notifications\BenefitsReportCreationNotification; -use Toby\Eloquent\Models\User; class SendNotificationAboutBenefitsReportCreation extends Command { @@ -16,9 +15,7 @@ class SendNotificationAboutBenefitsReportCreation extends Command public function handle(): void { - $usersToNotify = User::query() - ->whereIn("role", [Role::AdministrativeApprover]) - ->get(); + $usersToNotify = Permission::findByName("receiveBenefitsReportCreationNotification")->users()->get(); foreach ($usersToNotify as $user) { $user->notify(new BenefitsReportCreationNotification()); diff --git a/config/permission.php b/config/permission.php index 5bcbf041..0420acb2 100644 --- a/config/permission.php +++ b/config/permission.php @@ -52,6 +52,7 @@ "receiveVacationRequestsSummaryNotification", "receiveVacationRequestWaitsForApprovalNotification", "receiveVacationRequestStatusChangedNotification", + "receiveBenefitsReportCreationNotification", ], "permission_roles" => [ Role::Administrator->value => [ @@ -92,6 +93,7 @@ "receiveVacationRequestsSummaryNotification", "receiveVacationRequestWaitsForApprovalNotification", "receiveVacationRequestStatusChangedNotification", + "receiveBenefitsReportCreationNotification", ], Role::TechnicalApprover->value => [ "manageTechnologies", diff --git a/database/seeders/PermissionsSeeder.php b/database/seeders/PermissionsSeeder.php index 6c7d27b5..2b523570 100644 --- a/database/seeders/PermissionsSeeder.php +++ b/database/seeders/PermissionsSeeder.php @@ -11,12 +11,18 @@ class PermissionsSeeder extends Seeder { public function run(): void { - foreach (config("permission.permissions") as $permission) { - Permission::create( - [ - "name" => $permission, - ], - ); + $configPermissions = config("permission.permissions"); + + foreach ($configPermissions as $permission) { + Permission::findOrCreate($permission, "web"); + } + + $permissions = Permission::all(); + + foreach ($permissions as $permission) { + if (!in_array($permission->name, $configPermissions, true)) { + $permission->delete(); + } } } } diff --git a/resources/js/Composables/permissionInfo.js b/resources/js/Composables/permissionInfo.js index 0c1e81fe..a02c0921 100644 --- a/resources/js/Composables/permissionInfo.js +++ b/resources/js/Composables/permissionInfo.js @@ -79,6 +79,11 @@ const permissionsInfo = [ 'value': 'receiveUpcomingAndOverdueOhsTrainingNotification', 'section': 'Powiadomienia', }, + { + 'name': 'Tworzenie raportu benefitów', + 'value': 'receiveBenefitsReportCreationNotification', + 'section': 'Powiadomienia', + }, { 'name': 'Podsumowania wniosków urlopowych', 'value': 'receiveVacationRequestsSummaryNotification', diff --git a/tests/Feature/PermissionTest.php b/tests/Feature/PermissionTest.php index 7f578a4a..9d152a3c 100644 --- a/tests/Feature/PermissionTest.php +++ b/tests/Feature/PermissionTest.php @@ -42,6 +42,7 @@ public function testAdminCanSeeEditEmployeePermissionsForm(): void ->where("skipRequestFlow", false) ->where("receiveUpcomingAndOverdueMedicalExamsNotification", false) ->where("receiveUpcomingAndOverdueOhsTrainingNotification", false) + ->where("receiveBenefitsReportCreationNotification", false) ->where("receiveVacationRequestsSummaryNotification", false) ->where("receiveVacationRequestWaitsForApprovalNotification", false) ->where("receiveVacationRequestStatusChangedNotification", false), @@ -78,6 +79,7 @@ public function testAdminCanSeeEditTechnicalApproverPermissionsForm(): void ->where("skipRequestFlow", false) ->where("receiveUpcomingAndOverdueMedicalExamsNotification", false) ->where("receiveUpcomingAndOverdueOhsTrainingNotification", false) + ->where("receiveBenefitsReportCreationNotification", false) ->where("receiveVacationRequestsSummaryNotification", true) ->where("receiveVacationRequestWaitsForApprovalNotification", true) ->where("receiveVacationRequestStatusChangedNotification", true), @@ -114,6 +116,7 @@ public function testAdminCanSeeEditAdministrativeApproverPermissionsForm(): void ->where("skipRequestFlow", true) ->where("receiveUpcomingAndOverdueMedicalExamsNotification", true) ->where("receiveUpcomingAndOverdueOhsTrainingNotification", true) + ->where("receiveBenefitsReportCreationNotification", true) ->where("receiveVacationRequestsSummaryNotification", true) ->where("receiveVacationRequestWaitsForApprovalNotification", true) ->where("receiveVacationRequestStatusChangedNotification", true), diff --git a/tests/Unit/BenefitsReportCreationNotificationTest.php b/tests/Unit/BenefitsReportCreationNotificationTest.php index 18fb7aab..4c29ed11 100644 --- a/tests/Unit/BenefitsReportCreationNotificationTest.php +++ b/tests/Unit/BenefitsReportCreationNotificationTest.php @@ -7,7 +7,6 @@ use Illuminate\Foundation\Testing\DatabaseMigrations; use Illuminate\Support\Facades\Notification; use Tests\TestCase; -use Toby\Domain\Enums\Role; use Toby\Domain\Notifications\BenefitsReportCreationNotification; use Toby\Eloquent\Models\User; use Toby\Infrastructure\Console\Commands\SendNotificationAboutBenefitsReportCreation; @@ -20,19 +19,10 @@ public function testSendingNotificationAboutBenefitsReportCreation(): void { Notification::fake(); - $user = User::factory([ - "role" => Role::Employee, - ])->create(); - $technicalApprover = User::factory([ - "role" => Role::TechnicalApprover, - ])->create(); - $administrativeApprover = User::factory([ - "role" => Role::AdministrativeApprover, - ])->create(); - - $admin = User::factory([ - "role" => Role::Administrator, - ])->create(); + $user = User::factory()->employee()->create(); + $technicalApprover = User::factory()->technicalApprover()->create(); + $administrativeApprover = User::factory()->administrativeApprover()->create(); + $admin = User::factory()->admin()->create(); $this->artisan(SendNotificationAboutBenefitsReportCreation::class) ->execute();