From 72cec02c78fa6f088211c6765b706ba97eacb8e7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andrei=20Ioni=C8=9B=C4=83?= Date: Tue, 23 Jan 2024 10:11:05 +0100 Subject: [PATCH] fix: allow organisations without protocols --- .../Commands/ProcessProtocolsCommand.php | 8 ++-- tests/Feature/ProtocolsTest.php | 38 ++++++++++++++++--- 2 files changed, 37 insertions(+), 9 deletions(-) diff --git a/app/Console/Commands/ProcessProtocolsCommand.php b/app/Console/Commands/ProcessProtocolsCommand.php index ca26046..e6a923d 100644 --- a/app/Console/Commands/ProcessProtocolsCommand.php +++ b/app/Console/Commands/ProcessProtocolsCommand.php @@ -90,9 +90,9 @@ private function handleExpiringProtocols(): void $this->organisations ->filter( - fn (Organisation $organisation) => $organisation + fn (Organisation $organisation) => (bool) $organisation ->last_protocol_expires_at - ?->isSameDay($checkDate) ?? true + ?->isSameDay($checkDate) ) ->each(function (Organisation $organisation) { $this->sendNotification(ExpiringProtocol::class, $organisation); @@ -119,9 +119,9 @@ private function handleExpiredProtocols(): void $this->organisations ->filter( - fn (Organisation $organisation) => $organisation + fn (Organisation $organisation) => (bool) $organisation ->last_protocol_expires_at - ?->lte($checkDate) ?? true + ?->lte($checkDate) ) ->each(function (Organisation $organisation) { $organisation->setInactive(); diff --git a/tests/Feature/ProtocolsTest.php b/tests/Feature/ProtocolsTest.php index 650f7cd..d468c1c 100644 --- a/tests/Feature/ProtocolsTest.php +++ b/tests/Feature/ProtocolsTest.php @@ -7,6 +7,7 @@ use App\Console\Commands\ProcessProtocolsCommand; use App\Enum\UserRole; use App\Models\Document; +use App\Models\Organisation; use App\Models\User; use App\Notifications\ExpiredProtocol; use App\Notifications\ExpiringProtocol; @@ -35,6 +36,14 @@ protected function setUp(): void ->create(); } + protected function getPlatformAdmins(): Collection + { + return User::query() + ->withoutGlobalScopes() + ->role(UserRole::PLATFORM_ADMIN) + ->get(); + } + /** @test */ public function it_does_not_send_notifications_for_protocols_expiring_in_less_than_30_days(): void { @@ -190,11 +199,30 @@ public function it_sends_notifications_for_protocols_that_have_expired_in_the_pa ); } - private function getPlatformAdmins(): Collection + /** @test */ + public function it_does_not_send_notifications_for_organisations_without_protocols(): void { - return User::query() - ->withoutGlobalScopes() - ->role(UserRole::PLATFORM_ADMIN) - ->get(); + $organisation = Organisation::factory() + ->create(); + + $this->artisan(ProcessProtocolsCommand::class) + ->assertSuccessful(); + + Notification::assertNotSentTo( + $organisation, + ExpiringProtocol::class + ); + + Notification::assertNotSentTo( + $organisation + ->users + ->where('role', UserRole::ORG_ADMIN), + ExpiringProtocol::class + ); + + Notification::assertNotSentTo( + $this->getPlatformAdmins(), + SummaryExpiringProtocols::class + ); } }