Feat: notify all administrators when a ticket is raised

- Updated `TicketService` to trigger notifications to all administrators upon ticket creation.
- Added test to verify administrator notifications when a support ticket is raised.
- Minor formatting adjustments in `Ticket` model's imports for consistency.
This commit is contained in:
= 2026-05-26 07:14:31 +00:00
parent 67002d6979
commit 04c62145b5
3 changed files with 41 additions and 1 deletions

View File

@ -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;

View File

@ -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;
}
}
}
}

View File

@ -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
);
});