Implements spec `099-alerts-v1-teams-email`. - Monitoring navigation: Alerts as a cluster under Monitoring; default landing is Alert deliveries. - Tenant panel: Alerts points to `/admin/alerts` and the cluster navigation is hidden in tenant panel. - Guard compliance: removes direct `Gate::` usage from Alert resources so `NoAdHocFilamentAuthPatternsTest` passes. Verification: - Full suite: `1348 passed, 7 skipped` (EXIT=0). Co-authored-by: Ahmed Darrazi <ahmed.darrazi@live.de> Reviewed-on: #121
97 lines
3.0 KiB
PHP
97 lines
3.0 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Services\Alerts;
|
|
|
|
use App\Models\AlertDelivery;
|
|
use App\Models\AlertDestination;
|
|
use App\Notifications\Alerts\EmailAlertNotification;
|
|
use Illuminate\Support\Arr;
|
|
use Illuminate\Support\Facades\Notification;
|
|
use RuntimeException;
|
|
use Throwable;
|
|
|
|
class AlertSender
|
|
{
|
|
public function __construct(
|
|
private readonly TeamsWebhookSender $teamsWebhookSender,
|
|
) {}
|
|
|
|
public function send(AlertDelivery $delivery): void
|
|
{
|
|
$destination = $delivery->destination;
|
|
|
|
if (! $destination instanceof AlertDestination) {
|
|
throw new RuntimeException('Alert destination is missing.');
|
|
}
|
|
|
|
if (! (bool) $destination->is_enabled) {
|
|
throw new RuntimeException('Alert destination is disabled.');
|
|
}
|
|
|
|
$payload = is_array($delivery->payload) ? $delivery->payload : [];
|
|
|
|
try {
|
|
if ($destination->type === AlertDestination::TYPE_TEAMS_WEBHOOK) {
|
|
$this->deliverTeams($destination, $payload);
|
|
|
|
return;
|
|
}
|
|
|
|
if ($destination->type === AlertDestination::TYPE_EMAIL) {
|
|
$this->deliverEmail($delivery, $destination, $payload);
|
|
|
|
return;
|
|
}
|
|
} catch (Throwable $exception) {
|
|
throw new RuntimeException($this->channelFailureMessage((string) $destination->type), previous: $exception);
|
|
}
|
|
|
|
throw new RuntimeException('Alert destination type is not supported.');
|
|
}
|
|
|
|
/**
|
|
* @param array<string, mixed> $payload
|
|
*/
|
|
private function deliverTeams(AlertDestination $destination, array $payload): void
|
|
{
|
|
$config = is_array($destination->config) ? $destination->config : [];
|
|
$webhookUrl = trim((string) Arr::get($config, 'webhook_url', ''));
|
|
|
|
if ($webhookUrl === '') {
|
|
throw new RuntimeException('Teams webhook destination is not configured.');
|
|
}
|
|
|
|
$this->teamsWebhookSender->send($webhookUrl, $payload);
|
|
}
|
|
|
|
/**
|
|
* @param array<string, mixed> $payload
|
|
*/
|
|
private function deliverEmail(AlertDelivery $delivery, AlertDestination $destination, array $payload): void
|
|
{
|
|
$config = is_array($destination->config) ? $destination->config : [];
|
|
$recipients = Arr::get($config, 'recipients', []);
|
|
$recipients = is_array($recipients)
|
|
? array_values(array_unique(array_filter(array_map(static fn (mixed $value): string => trim((string) $value), $recipients))))
|
|
: [];
|
|
|
|
if ($recipients === []) {
|
|
throw new RuntimeException('Email destination has no recipients.');
|
|
}
|
|
|
|
Notification::route('mail', $recipients)
|
|
->notify(new EmailAlertNotification($delivery, $payload));
|
|
}
|
|
|
|
private function channelFailureMessage(string $type): string
|
|
{
|
|
return match ($type) {
|
|
AlertDestination::TYPE_TEAMS_WEBHOOK => 'Teams delivery failed.',
|
|
AlertDestination::TYPE_EMAIL => 'Email delivery failed.',
|
|
default => 'Alert delivery failed.',
|
|
};
|
|
}
|
|
}
|