Merge pull request 'codex/v0.5-webhook-notification/03-ticket-status-changed-trigger' (#49) from codex/v0.5-webhook-notification/03-ticket-status-changed-trigger into main
Reviewed-on: OpenSource/KanbanGit#49 Reviewed-by: Koda <koda@twyco.de>
This commit was merged in pull request #49.
This commit is contained in:
@@ -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(),
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -2,16 +2,20 @@
|
||||
|
||||
namespace App\Actions\Tickets;
|
||||
|
||||
use App\Actions\Notifications\BuildTicketStatusChangedPayload;
|
||||
use App\Actions\Notifications\TicketStatusChanged;
|
||||
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;
|
||||
|
||||
class MoveProjectTicket
|
||||
{
|
||||
public function __construct(
|
||||
private readonly BuildTicketStatusChangedPayload $buildTicketStatusChangedPayload,
|
||||
private readonly CreateTicketWorklogEntry $createTicketWorklogEntry,
|
||||
) {}
|
||||
|
||||
@@ -44,6 +48,8 @@ class MoveProjectTicket
|
||||
'ticket_status_id' => $destinationStatus->id,
|
||||
]);
|
||||
|
||||
$occurredAt = CarbonImmutable::now();
|
||||
|
||||
$this->createTicketWorklogEntry->handle(
|
||||
$ticket->fresh(),
|
||||
$user,
|
||||
@@ -61,6 +67,19 @@ class MoveProjectTicket
|
||||
],
|
||||
);
|
||||
|
||||
TicketStatusChanged::dispatch(
|
||||
$this->buildTicketStatusChangedPayload->handle(
|
||||
$project,
|
||||
$ticket,
|
||||
$user,
|
||||
$originStatusId,
|
||||
$originLabel,
|
||||
$destinationStatus,
|
||||
$destinationLabel,
|
||||
$occurredAt,
|
||||
),
|
||||
);
|
||||
|
||||
return [
|
||||
'destination_label' => $destinationLabel,
|
||||
'moved' => true,
|
||||
|
||||
Reference in New Issue
Block a user