52 lines
2.1 KiB
PHP
52 lines
2.1 KiB
PHP
<?php
|
|
|
|
namespace App\Actions\Notifications;
|
|
|
|
use App\Enums\NotificationChannel;
|
|
use App\Enums\NotificationDeliveryStatus;
|
|
use App\Models\NotificationAction;
|
|
use App\Models\NotificationDeliveryLog;
|
|
|
|
class QueueTicketStatusChangedNotificationDeliveries
|
|
{
|
|
public function __construct(
|
|
private readonly MatchNotificationFilters $matchNotificationFilters,
|
|
) {}
|
|
|
|
public function handle(TicketStatusChanged|TicketCreated $event): void
|
|
{
|
|
NotificationAction::query()
|
|
->enabled()
|
|
->where('project_id', $event->payload->projectId)
|
|
->where('event_type', $event->payload->eventType->value)
|
|
->get(['channel', 'filters', 'id'])
|
|
->each(function (NotificationAction $notificationAction) use ($event): void {
|
|
if (! $this->matchNotificationFilters->handle($notificationAction->filters ?? [], $event->payload)) {
|
|
return;
|
|
}
|
|
|
|
$notificationDeliveryLogId = null;
|
|
|
|
if (in_array($notificationAction->channel, [NotificationChannel::Discord, NotificationChannel::Webhook], true)) {
|
|
$notificationDeliveryLogId = NotificationDeliveryLog::query()->create([
|
|
'attempt_count' => 0,
|
|
'channel' => $notificationAction->channel,
|
|
'delivery_status' => NotificationDeliveryStatus::Pending,
|
|
'event_type' => $event->payload->eventType,
|
|
'notification_action_id' => $notificationAction->id,
|
|
'payload_metadata' => [
|
|
'project_key' => $event->payload->projectKey,
|
|
'ticket_key' => $event->payload->ticketKey,
|
|
],
|
|
])->id;
|
|
}
|
|
|
|
DeliverNotificationAction::dispatch(
|
|
notificationActionId: $notificationAction->id,
|
|
payload: $event->payload,
|
|
notificationDeliveryLogId: $notificationDeliveryLogId,
|
|
);
|
|
});
|
|
}
|
|
}
|