diff --git a/app/Models/Ticket.php b/app/Models/Ticket.php index 0687027..67efa3a 100644 --- a/app/Models/Ticket.php +++ b/app/Models/Ticket.php @@ -4,8 +4,8 @@ namespace App\Models; -use Illuminate\Database\Eloquent\{Model, SoftDeletes}; use Illuminate\Database\Eloquent\Attributes\Fillable; +use Illuminate\Database\Eloquent\{Model, SoftDeletes}; use Illuminate\Database\Eloquent\Relations\{BelongsTo, BelongsToMany, HasMany}; use Illuminate\Support\Str; diff --git a/app/Services/TicketService.php b/app/Services/TicketService.php index 9363658..e122b6f 100644 --- a/app/Services/TicketService.php +++ b/app/Services/TicketService.php @@ -278,5 +278,16 @@ private function sendNotifications(Ticket $ticket): void } } } + + // Always notify all administrators when a ticket is raised + $admins = User::whereHas('roles', function ($q): void { + $q->whereIn('name', ['Admin', 'admin']); + })->get(); + foreach ($admins as $admin) { + if (! in_array($admin->id, $notifiedUserIds, true)) { + $admin->notify($notification); + $notifiedUserIds[] = $admin->id; + } + } } } diff --git a/tests/Feature/TicketSystemTest.php b/tests/Feature/TicketSystemTest.php index cd7f949..783f990 100644 --- a/tests/Feature/TicketSystemTest.php +++ b/tests/Feature/TicketSystemTest.php @@ -356,3 +356,32 @@ App\Notifications\TicketCommentedNotification::class ); }); + +test('raising a support ticket always triggers a notification to all administrators', function (): void { + Notification::fake(); + + $user = User::factory()->create(); + $user->assignRole('user'); + $this->actingAs($user); + + $admin = User::where('email', 'admin@example.com')->firstOrFail(); + + $dto = new CreateTicketRequest( + userId: $user->id, + connectedAppId: null, + issueCategory: 'Technical Issue', + description: 'SSO log-in error.', + notifiedRoleIds: [], + notifiedUserIds: [], + attachment: null, + ); + + $service = app(TicketService::class); + $service->create($dto); + + // Administrator should be notified of the new ticket + Notification::assertSentTo( + $admin, + TicketRaisedNotification::class + ); +});