From ff5e54cad709ffde7ab49969b9ee3733c2d46ebd Mon Sep 17 00:00:00 2001 From: Koda Date: Wed, 29 Apr 2026 09:35:31 +0000 Subject: [PATCH] Move notification event type onto events --- .../Notifications/DeliverNotificationAction.php | 2 +- app/Actions/Notifications/NotificationEvent.php | 4 ++++ .../Notifications/NotificationEventPayload.php | 3 --- .../QueueNotificationEventDeliveries.php | 5 +++-- app/Actions/Notifications/TicketCreated.php | 6 ++++++ app/Actions/Notifications/TicketCreatedPayload.php | 10 +--------- app/Actions/Notifications/TicketStatusChanged.php | 6 ++++++ .../Notifications/TicketStatusChangedPayload.php | 13 ++----------- .../TicketCreatedNotificationDispatchTest.php | 4 ++-- .../TicketStatusChangedNotificationDispatchTest.php | 2 +- 10 files changed, 26 insertions(+), 29 deletions(-) diff --git a/app/Actions/Notifications/DeliverNotificationAction.php b/app/Actions/Notifications/DeliverNotificationAction.php index 20c7990..d614767 100644 --- a/app/Actions/Notifications/DeliverNotificationAction.php +++ b/app/Actions/Notifications/DeliverNotificationAction.php @@ -245,7 +245,7 @@ class DeliverNotificationAction implements ShouldQueue 'attempt_count' => 0, 'channel' => $notificationAction->channel, 'delivery_status' => NotificationDeliveryStatus::Pending, - 'event_type' => $this->event->payload()->eventType(), + 'event_type' => $this->event->eventType(), 'notification_action_id' => $notificationAction->id, 'payload_metadata' => $this->payloadMetadata(), ]); diff --git a/app/Actions/Notifications/NotificationEvent.php b/app/Actions/Notifications/NotificationEvent.php index 16dad08..0af8390 100644 --- a/app/Actions/Notifications/NotificationEvent.php +++ b/app/Actions/Notifications/NotificationEvent.php @@ -2,7 +2,11 @@ namespace App\Actions\Notifications; +use App\Enums\NotificationEventType; + interface NotificationEvent { + public function eventType(): NotificationEventType; + public function payload(): NotificationEventPayload; } diff --git a/app/Actions/Notifications/NotificationEventPayload.php b/app/Actions/Notifications/NotificationEventPayload.php index 274b8b6..a673434 100644 --- a/app/Actions/Notifications/NotificationEventPayload.php +++ b/app/Actions/Notifications/NotificationEventPayload.php @@ -2,7 +2,6 @@ namespace App\Actions\Notifications; -use App\Enums\NotificationEventType; use Illuminate\Contracts\Support\Arrayable; /** @@ -10,8 +9,6 @@ use Illuminate\Contracts\Support\Arrayable; */ interface NotificationEventPayload extends Arrayable { - public function eventType(): NotificationEventType; - public function projectId(): int; /** diff --git a/app/Actions/Notifications/QueueNotificationEventDeliveries.php b/app/Actions/Notifications/QueueNotificationEventDeliveries.php index 68ab3c8..513a234 100644 --- a/app/Actions/Notifications/QueueNotificationEventDeliveries.php +++ b/app/Actions/Notifications/QueueNotificationEventDeliveries.php @@ -12,11 +12,12 @@ class QueueNotificationEventDeliveries public function handle(NotificationEvent $event): void { $payload = $event->payload(); + $eventType = $event->eventType(); NotificationAction::query() ->enabled() ->where('project_id', $payload->projectId()) - ->where('event_type', $payload->eventType()->value) + ->where('event_type', $eventType->value) ->get(['channel', 'id']) ->each(function (NotificationAction $notificationAction) use ($event, $payload): void { $notificationDeliveryLogId = null; @@ -26,7 +27,7 @@ class QueueNotificationEventDeliveries 'attempt_count' => 0, 'channel' => $notificationAction->channel, 'delivery_status' => NotificationDeliveryStatus::Pending, - 'event_type' => $payload->eventType(), + 'event_type' => $event->eventType(), 'notification_action_id' => $notificationAction->id, 'payload_metadata' => $payload->deliveryMetadata(), ])->id; diff --git a/app/Actions/Notifications/TicketCreated.php b/app/Actions/Notifications/TicketCreated.php index 7d5b192..62bf62c 100644 --- a/app/Actions/Notifications/TicketCreated.php +++ b/app/Actions/Notifications/TicketCreated.php @@ -2,6 +2,7 @@ namespace App\Actions\Notifications; +use App\Enums\NotificationEventType; use Illuminate\Contracts\Events\ShouldDispatchAfterCommit; use Illuminate\Foundation\Events\Dispatchable; @@ -13,6 +14,11 @@ final readonly class TicketCreated implements NotificationEvent, ShouldDispatchA public TicketCreatedPayload $payload, ) {} + public function eventType(): NotificationEventType + { + return NotificationEventType::TicketCreate; + } + public function payload(): NotificationEventPayload { return $this->payload; diff --git a/app/Actions/Notifications/TicketCreatedPayload.php b/app/Actions/Notifications/TicketCreatedPayload.php index a741cbc..f9373f0 100644 --- a/app/Actions/Notifications/TicketCreatedPayload.php +++ b/app/Actions/Notifications/TicketCreatedPayload.php @@ -7,8 +7,6 @@ use Carbon\CarbonImmutable; final readonly class TicketCreatedPayload implements NotificationEventPayload { - public NotificationEventType $eventType; - public ?int $fromStatusId; public ?string $fromStatusLabel; @@ -27,16 +25,10 @@ final readonly class TicketCreatedPayload implements NotificationEventPayload public string $toStatusLabel, public CarbonImmutable $occurredAt, ) { - $this->eventType = NotificationEventType::TicketCreate; $this->fromStatusId = null; $this->fromStatusLabel = null; } - public function eventType(): NotificationEventType - { - return $this->eventType; - } - public function projectId(): int { return $this->projectId; @@ -84,7 +76,7 @@ final readonly class TicketCreatedPayload implements NotificationEventPayload public function toArray(): array { return [ - 'event_type' => $this->eventType->value, + 'event_type' => NotificationEventType::TicketCreate->value, 'project' => [ 'id' => $this->projectId, 'key' => $this->projectKey, diff --git a/app/Actions/Notifications/TicketStatusChanged.php b/app/Actions/Notifications/TicketStatusChanged.php index b31bb55..41e2aa5 100644 --- a/app/Actions/Notifications/TicketStatusChanged.php +++ b/app/Actions/Notifications/TicketStatusChanged.php @@ -2,6 +2,7 @@ namespace App\Actions\Notifications; +use App\Enums\NotificationEventType; use Illuminate\Contracts\Events\ShouldDispatchAfterCommit; use Illuminate\Foundation\Events\Dispatchable; @@ -13,6 +14,11 @@ final readonly class TicketStatusChanged implements NotificationEvent, ShouldDis public TicketStatusChangedPayload $payload, ) {} + public function eventType(): NotificationEventType + { + return NotificationEventType::TicketStatusChanged; + } + public function payload(): NotificationEventPayload { return $this->payload; diff --git a/app/Actions/Notifications/TicketStatusChangedPayload.php b/app/Actions/Notifications/TicketStatusChangedPayload.php index fab97e5..cc6997b 100644 --- a/app/Actions/Notifications/TicketStatusChangedPayload.php +++ b/app/Actions/Notifications/TicketStatusChangedPayload.php @@ -7,8 +7,6 @@ use Carbon\CarbonImmutable; final readonly class TicketStatusChangedPayload implements NotificationEventPayload { - public NotificationEventType $eventType; - public function __construct( public int $projectId, public string $projectKey, @@ -24,14 +22,7 @@ final readonly class TicketStatusChangedPayload implements NotificationEventPayl public int $toStatusId, public string $toStatusLabel, public CarbonImmutable $occurredAt, - ) { - $this->eventType = NotificationEventType::TicketStatusChanged; - } - - public function eventType(): NotificationEventType - { - return $this->eventType; - } + ) {} public function projectId(): int { @@ -80,7 +71,7 @@ final readonly class TicketStatusChangedPayload implements NotificationEventPayl public function toArray(): array { return [ - 'event_type' => $this->eventType->value, + 'event_type' => NotificationEventType::TicketStatusChanged->value, 'project' => [ 'id' => $this->projectId, 'key' => $this->projectKey, diff --git a/tests/Feature/Notifications/TicketCreatedNotificationDispatchTest.php b/tests/Feature/Notifications/TicketCreatedNotificationDispatchTest.php index 41690c0..b8bec34 100644 --- a/tests/Feature/Notifications/TicketCreatedNotificationDispatchTest.php +++ b/tests/Feature/Notifications/TicketCreatedNotificationDispatchTest.php @@ -116,11 +116,11 @@ class TicketCreatedNotificationDispatchTest extends TestCase && $job->event->payload()->fromStatusLabel === null && $job->event->payload()->toStatusId === $backlogStatus->id && $job->event->payload()->toStatusLabel === 'Backlog' - && $job->event->payload()->eventType() === NotificationEventType::TicketCreate + && $job->event->eventType() === NotificationEventType::TicketCreate && $job->notificationDeliveryLogId !== null; }); Queue::assertPushed(DeliverNotificationAction::class, fn (DeliverNotificationAction $job): bool => $job->notificationActionId === $discordAction->id - && $job->event->payload()->eventType() === NotificationEventType::TicketCreate + && $job->event->eventType() === NotificationEventType::TicketCreate && $job->notificationDeliveryLogId !== null); Queue::assertNotPushed(DeliverNotificationAction::class, fn (DeliverNotificationAction $job): bool => $job->notificationActionId === $disabledAction->id); Queue::assertNotPushed(DeliverNotificationAction::class, fn (DeliverNotificationAction $job): bool => $job->notificationActionId === $statusChangedAction->id); diff --git a/tests/Feature/Notifications/TicketStatusChangedNotificationDispatchTest.php b/tests/Feature/Notifications/TicketStatusChangedNotificationDispatchTest.php index 32893e7..0aedc19 100644 --- a/tests/Feature/Notifications/TicketStatusChangedNotificationDispatchTest.php +++ b/tests/Feature/Notifications/TicketStatusChangedNotificationDispatchTest.php @@ -117,7 +117,7 @@ class TicketStatusChangedNotificationDispatchTest extends TestCase && $job->event->payload()->fromStatusLabel === 'Backlog' && $job->event->payload()->toStatusId === $doingStatus->id && $job->event->payload()->toStatusLabel === 'Doing' - && $job->event->payload()->eventType() === NotificationEventType::TicketStatusChanged + && $job->event->eventType() === NotificationEventType::TicketStatusChanged && $job->notificationDeliveryLogId !== null; }); Queue::assertPushed(DeliverNotificationAction::class, fn (DeliverNotificationAction $job): bool => $job->notificationActionId === $discordAction->id