91 lines
2.5 KiB
PHP
91 lines
2.5 KiB
PHP
<?php
|
|
|
|
namespace App\Services;
|
|
|
|
use App\Models\ServiceRequest;
|
|
use App\Models\User;
|
|
use App\Notifications\RequestAssignedNotification;
|
|
use App\Notifications\RequestCommentAddedNotification;
|
|
use App\Notifications\RequestOverdueNotification;
|
|
use App\Notifications\RequestStatusChangedNotification;
|
|
use App\Notifications\SlaWarningNotification;
|
|
use Illuminate\Support\Collection;
|
|
|
|
class NotificationService
|
|
{
|
|
public function __construct(private readonly SettingsService $settingsService)
|
|
{
|
|
}
|
|
|
|
public function notifyRequestAssigned(ServiceRequest $request): void
|
|
{
|
|
$assignee = $request->assignee;
|
|
|
|
if (!$assignee || !$this->enabled('assigned')) {
|
|
return;
|
|
}
|
|
|
|
$assignee->notify(new RequestAssignedNotification($request));
|
|
}
|
|
|
|
public function notifyStatusChanged(ServiceRequest $request): void
|
|
{
|
|
if (!$this->enabled('status_changed')) {
|
|
return;
|
|
}
|
|
|
|
$request->client?->notify(new RequestStatusChangedNotification($request));
|
|
}
|
|
|
|
public function notifyCommentAdded(ServiceRequest $request, User $author): void
|
|
{
|
|
if (!$this->enabled('comment_added')) {
|
|
return;
|
|
}
|
|
|
|
$recipients = collect([$request->client, $request->assignee])
|
|
->filter()
|
|
->reject(fn (User $user) => $user->id === $author->id);
|
|
|
|
/** @var User $recipient */
|
|
foreach ($recipients as $recipient) {
|
|
$recipient->notify(new RequestCommentAddedNotification($request, $author));
|
|
}
|
|
}
|
|
|
|
public function notifyOverdue(ServiceRequest $request, Collection $managers): void
|
|
{
|
|
if (!$this->enabled('overdue')) {
|
|
return;
|
|
}
|
|
|
|
$recipients = collect([$request->assignee])->filter()->merge($managers);
|
|
|
|
/** @var User $recipient */
|
|
foreach ($recipients->unique('id') as $recipient) {
|
|
$recipient->notify(new RequestOverdueNotification($request));
|
|
}
|
|
}
|
|
|
|
public function notifySlaWarning(ServiceRequest $request, Collection $managers): void
|
|
{
|
|
if (!$this->enabled('sla_warning')) {
|
|
return;
|
|
}
|
|
|
|
$recipients = collect([$request->assignee])->filter()->merge($managers);
|
|
|
|
/** @var User $recipient */
|
|
foreach ($recipients->unique('id') as $recipient) {
|
|
$recipient->notify(new SlaWarningNotification($request));
|
|
}
|
|
}
|
|
|
|
private function enabled(string $key): bool
|
|
{
|
|
$map = $this->settingsService->emailNotifications();
|
|
|
|
return (bool) ($map[$key] ?? true);
|
|
}
|
|
}
|