Refactor notification event payloads and listener naming
This commit is contained in:
@@ -0,0 +1,113 @@
|
||||
<?php
|
||||
|
||||
namespace App\Actions\Notifications;
|
||||
|
||||
use App\Enums\NotificationEventType;
|
||||
use Carbon\CarbonImmutable;
|
||||
|
||||
final readonly class TicketCreatedPayload implements NotificationEventPayload
|
||||
{
|
||||
public NotificationEventType $eventType;
|
||||
|
||||
public ?int $fromStatusId;
|
||||
|
||||
public ?string $fromStatusLabel;
|
||||
|
||||
public function __construct(
|
||||
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 $toStatusId,
|
||||
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;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<string, scalar|null>
|
||||
*/
|
||||
public function deliveryMetadata(): array
|
||||
{
|
||||
return [
|
||||
'project_key' => $this->projectKey,
|
||||
'ticket_key' => $this->ticketKey,
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<string, string>
|
||||
*/
|
||||
public function discordTemplateVariables(): array
|
||||
{
|
||||
return [
|
||||
'$url' => $this->ticketUrl,
|
||||
'$ticket_key' => $this->ticketKey,
|
||||
'$ticket_title' => $this->ticketTitle,
|
||||
'$project_name' => $this->projectName,
|
||||
'$status_from' => '',
|
||||
'$status_to' => $this->toStatusLabel,
|
||||
'$actor_name' => $this->actorName ?? '',
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @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: null,
|
||||
* 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' => null,
|
||||
'to' => [
|
||||
'id' => $this->toStatusId,
|
||||
'label' => $this->toStatusLabel,
|
||||
],
|
||||
'ticket_url' => $this->ticketUrl,
|
||||
'occurred_at' => $this->occurredAt->toISOString(),
|
||||
];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user