Add ticket status changed notification dispatch

This commit is contained in:
Eric Peters
2026-04-20 22:50:22 +02:00
parent ee15efe739
commit 71f07a8f7b
8 changed files with 417 additions and 0 deletions
@@ -0,0 +1,42 @@
<?php
namespace App\Actions\Notifications;
use App\Enums\NotificationEventType;
use App\Models\Project;
use App\Models\Ticket;
use App\Models\TicketStatus;
use App\Models\User;
use Carbon\CarbonImmutable;
class BuildTicketStatusChangedPayload
{
public function handle(
Project $project,
Ticket $ticket,
?User $actor,
int $originStatusId,
string $originLabel,
TicketStatus $destinationStatus,
string $destinationLabel,
CarbonImmutable $occurredAt,
): TicketStatusChangedPayload {
return new TicketStatusChangedPayload(
eventType: NotificationEventType::TicketStatusChanged,
projectId: $project->id,
projectKey: $project->key,
projectName: $project->name,
ticketId: $ticket->id,
ticketKey: $ticket->ticket_key,
ticketTitle: $ticket->title,
ticketUrl: route('projects.tickets.show', [$project, $ticket]),
actorId: $actor?->id,
actorName: $actor?->name,
fromStatusId: $originStatusId,
fromStatusLabel: $originLabel,
toStatusId: $destinationStatus->id,
toStatusLabel: $destinationLabel,
occurredAt: $occurredAt,
);
}
}
@@ -0,0 +1,18 @@
<?php
namespace App\Actions\Notifications;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Queue\Queueable;
class DeliverNotificationAction implements ShouldQueue
{
use Queueable;
public function __construct(
public int $notificationActionId,
public TicketStatusChangedPayload $payload,
) {}
public function handle(): void {}
}
@@ -0,0 +1,23 @@
<?php
namespace App\Actions\Notifications;
use App\Models\NotificationAction;
class QueueTicketStatusChangedNotificationDeliveries
{
public function handle(TicketStatusChanged $event): void
{
NotificationAction::query()
->enabled()
->where('project_id', $event->payload->projectId)
->where('event_type', $event->payload->eventType->value)
->get(['id'])
->each(function (NotificationAction $notificationAction) use ($event): void {
DeliverNotificationAction::dispatch(
$notificationAction->id,
$event->payload,
);
});
}
}
@@ -0,0 +1,15 @@
<?php
namespace App\Actions\Notifications;
use Illuminate\Contracts\Events\ShouldDispatchAfterCommit;
use Illuminate\Foundation\Events\Dispatchable;
final readonly class TicketStatusChanged implements ShouldDispatchAfterCommit
{
use Dispatchable;
public function __construct(
public TicketStatusChangedPayload $payload,
) {}
}
@@ -0,0 +1,76 @@
<?php
namespace App\Actions\Notifications;
use App\Enums\NotificationEventType;
use Carbon\CarbonImmutable;
use Illuminate\Contracts\Support\Arrayable;
/**
* @implements Arrayable<string, mixed>
*/
final readonly class TicketStatusChangedPayload implements Arrayable
{
public function __construct(
public NotificationEventType $eventType,
public int $projectId,
public string $projectKey,
public string $projectName,
public int $ticketId,
public string $ticketKey,
public string $ticketTitle,
public string $ticketUrl,
public ?int $actorId,
public ?string $actorName,
public int $fromStatusId,
public string $fromStatusLabel,
public int $toStatusId,
public string $toStatusLabel,
public CarbonImmutable $occurredAt,
) {}
/**
* @return array{
* event_type: string,
* project: array{id: int, key: string, name: string},
* ticket: array{id: int, key: string, title: string},
* actor: array{id: int, name: string}|null,
* from: array{id: int, label: string},
* to: array{id: int, label: string},
* ticket_url: string,
* occurred_at: string
* }
*/
public function toArray(): array
{
return [
'event_type' => $this->eventType->value,
'project' => [
'id' => $this->projectId,
'key' => $this->projectKey,
'name' => $this->projectName,
],
'ticket' => [
'id' => $this->ticketId,
'key' => $this->ticketKey,
'title' => $this->ticketTitle,
],
'actor' => $this->actorId === null || $this->actorName === null
? null
: [
'id' => $this->actorId,
'name' => $this->actorName,
],
'from' => [
'id' => $this->fromStatusId,
'label' => $this->fromStatusLabel,
],
'to' => [
'id' => $this->toStatusId,
'label' => $this->toStatusLabel,
],
'ticket_url' => $this->ticketUrl,
'occurred_at' => $this->occurredAt->toISOString(),
];
}
}