Add ticket create notification event support
linter / quality (pull_request) Successful in 1m25s
tests / ci (8.4) (pull_request) Successful in 1m42s

This commit is contained in:
2026-04-24 19:35:13 +00:00
parent 083328f9de
commit 5f74ddd964
12 changed files with 262 additions and 18 deletions
@@ -38,7 +38,7 @@ class BuildProjectNotificationSettingsPayload
{
return [
[
'description' => 'Absolute ticket URL for the status change event.',
'description' => 'Absolute ticket URL for the notification event.',
'value' => '$url',
],
[
@@ -46,7 +46,7 @@ class BuildProjectNotificationSettingsPayload
'value' => '$ticket_key',
],
[
'description' => 'Ticket title from the changed ticket.',
'description' => 'Ticket title for the notification event.',
'value' => '$ticket_title',
],
[
@@ -54,15 +54,15 @@ class BuildProjectNotificationSettingsPayload
'value' => '$project_name',
],
[
'description' => 'Origin status label before the move.',
'description' => 'Origin status label for status moves; blank for ticket create.',
'value' => '$status_from',
],
[
'description' => 'Destination status label after the move.',
'description' => 'Destination or initial status label for the ticket event.',
'value' => '$status_to',
],
[
'description' => 'Actor name when the move has a known user.',
'description' => 'Actor name when the event has a known user.',
'value' => '$actor_name',
],
];
@@ -0,0 +1,50 @@
<?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 BuildTicketCreatedPayload
{
public function handle(
Project $project,
Ticket $ticket,
TicketStatus $ticketStatus,
?User $actor,
CarbonImmutable $occurredAt,
): TicketStatusChangedPayload {
return new TicketStatusChangedPayload(
eventType: NotificationEventType::TicketCreate,
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: null,
fromStatusLabel: null,
toStatusId: $ticketStatus->id,
toStatusLabel: $this->statusLabel($ticketStatus),
occurredAt: $occurredAt,
);
}
private function statusLabel(TicketStatus $ticketStatus): string
{
$ticketStatus->loadMissing('boardColumn');
if ($ticketStatus->boardColumn === null || $ticketStatus->boardColumn->name === $ticketStatus->name) {
return $ticketStatus->name;
}
return "{$ticketStatus->boardColumn->name} / {$ticketStatus->name}";
}
}
@@ -9,7 +9,7 @@ use App\Models\NotificationDeliveryLog;
class QueueTicketStatusChangedNotificationDeliveries
{
public function handle(TicketStatusChanged $event): void
public function handle(TicketStatusChanged|TicketCreated $event): void
{
NotificationAction::query()
->enabled()
@@ -13,7 +13,7 @@ class RenderDiscordMessageTemplate
'$ticket_key' => $payload->ticketKey,
'$ticket_title' => $payload->ticketTitle,
'$project_name' => $payload->projectName,
'$status_from' => $payload->fromStatusLabel,
'$status_from' => $payload->fromStatusLabel ?? '',
'$status_to' => $payload->toStatusLabel,
'$actor_name' => $payload->actorName ?? '',
]);
@@ -0,0 +1,15 @@
<?php
namespace App\Actions\Notifications;
use Illuminate\Contracts\Events\ShouldDispatchAfterCommit;
use Illuminate\Foundation\Events\Dispatchable;
final readonly class TicketCreated implements ShouldDispatchAfterCommit
{
use Dispatchable;
public function __construct(
public TicketStatusChangedPayload $payload,
) {}
}
@@ -22,8 +22,8 @@ final readonly class TicketStatusChangedPayload implements Arrayable
public string $ticketUrl,
public ?int $actorId,
public ?string $actorName,
public int $fromStatusId,
public string $fromStatusLabel,
public ?int $fromStatusId,
public ?string $fromStatusLabel,
public int $toStatusId,
public string $toStatusLabel,
public CarbonImmutable $occurredAt,
@@ -35,7 +35,7 @@ final readonly class TicketStatusChangedPayload implements Arrayable
* 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},
* from: array{id: int, label: string}|null,
* to: array{id: int, label: string},
* ticket_url: string,
* occurred_at: string
@@ -61,10 +61,12 @@ final readonly class TicketStatusChangedPayload implements Arrayable
'id' => $this->actorId,
'name' => $this->actorName,
],
'from' => [
'id' => $this->fromStatusId,
'label' => $this->fromStatusLabel,
],
'from' => $this->fromStatusId === null || $this->fromStatusLabel === null
? null
: [
'id' => $this->fromStatusId,
'label' => $this->fromStatusLabel,
],
'to' => [
'id' => $this->toStatusId,
'label' => $this->toStatusLabel,
@@ -2,17 +2,21 @@
namespace App\Actions\Tickets;
use App\Actions\Notifications\BuildTicketCreatedPayload;
use App\Actions\Notifications\TicketCreated;
use App\Enums\TicketWorklogEntryType;
use App\Models\Project;
use App\Models\Ticket;
use App\Models\TicketStatus;
use App\Models\User;
use Carbon\CarbonImmutable;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\DB;
class CreateProjectTicket
{
public function __construct(
private readonly BuildTicketCreatedPayload $buildTicketCreatedPayload,
private readonly CreateTicketWorklogEntry $createTicketWorklogEntry,
) {}
@@ -53,6 +57,8 @@ class CreateProjectTicket
'story_points' => Arr::get($attributes, 'story_points'),
]);
$occurredAt = CarbonImmutable::now();
if ($actor !== null) {
$this->createTicketWorklogEntry->handle(
$ticket,
@@ -69,6 +75,16 @@ class CreateProjectTicket
);
}
TicketCreated::dispatch(
$this->buildTicketCreatedPayload->handle(
$project,
$ticket,
$ticketStatus,
$actor,
$occurredAt,
),
);
return $ticket;
});
}
+2
View File
@@ -5,11 +5,13 @@ namespace App\Enums;
enum NotificationEventType: string
{
case TicketStatusChanged = 'ticket_status_changed';
case TicketCreate = 'ticket_create';
public function label(): string
{
return match ($this) {
self::TicketStatusChanged => 'Ticket status changed',
self::TicketCreate => 'Ticket create',
};
}
+6
View File
@@ -3,6 +3,7 @@
namespace App\Providers;
use App\Actions\Notifications\QueueTicketStatusChangedNotificationDeliveries;
use App\Actions\Notifications\TicketCreated;
use App\Actions\Notifications\TicketStatusChanged;
use App\Actions\Permissions\PermissionService;
use App\Enums\Permission;
@@ -99,5 +100,10 @@ class AppServiceProvider extends ServiceProvider
TicketStatusChanged::class,
QueueTicketStatusChangedNotificationDeliveries::class,
);
Event::listen(
TicketCreated::class,
QueueTicketStatusChangedNotificationDeliveries::class,
);
}
}