Add ticket status changed notification dispatch
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;
|
namespace App\Actions\Tickets;
|
||||||
|
|
||||||
|
use App\Actions\Notifications\BuildTicketStatusChangedPayload;
|
||||||
|
use App\Actions\Notifications\TicketStatusChanged;
|
||||||
use App\Enums\TicketWorklogEntryType;
|
use App\Enums\TicketWorklogEntryType;
|
||||||
use App\Models\Project;
|
use App\Models\Project;
|
||||||
use App\Models\Ticket;
|
use App\Models\Ticket;
|
||||||
use App\Models\TicketStatus;
|
use App\Models\TicketStatus;
|
||||||
use App\Models\User;
|
use App\Models\User;
|
||||||
|
use Carbon\CarbonImmutable;
|
||||||
use Illuminate\Support\Arr;
|
use Illuminate\Support\Arr;
|
||||||
|
|
||||||
class MoveProjectTicket
|
class MoveProjectTicket
|
||||||
{
|
{
|
||||||
public function __construct(
|
public function __construct(
|
||||||
|
private readonly BuildTicketStatusChangedPayload $buildTicketStatusChangedPayload,
|
||||||
private readonly CreateTicketWorklogEntry $createTicketWorklogEntry,
|
private readonly CreateTicketWorklogEntry $createTicketWorklogEntry,
|
||||||
) {}
|
) {}
|
||||||
|
|
||||||
@@ -44,6 +48,8 @@ class MoveProjectTicket
|
|||||||
'ticket_status_id' => $destinationStatus->id,
|
'ticket_status_id' => $destinationStatus->id,
|
||||||
]);
|
]);
|
||||||
|
|
||||||
|
$occurredAt = CarbonImmutable::now();
|
||||||
|
|
||||||
$this->createTicketWorklogEntry->handle(
|
$this->createTicketWorklogEntry->handle(
|
||||||
$ticket->fresh(),
|
$ticket->fresh(),
|
||||||
$user,
|
$user,
|
||||||
@@ -61,6 +67,19 @@ class MoveProjectTicket
|
|||||||
],
|
],
|
||||||
);
|
);
|
||||||
|
|
||||||
|
TicketStatusChanged::dispatch(
|
||||||
|
$this->buildTicketStatusChangedPayload->handle(
|
||||||
|
$project,
|
||||||
|
$ticket,
|
||||||
|
$user,
|
||||||
|
$originStatusId,
|
||||||
|
$originLabel,
|
||||||
|
$destinationStatus,
|
||||||
|
$destinationLabel,
|
||||||
|
$occurredAt,
|
||||||
|
),
|
||||||
|
);
|
||||||
|
|
||||||
return [
|
return [
|
||||||
'destination_label' => $destinationLabel,
|
'destination_label' => $destinationLabel,
|
||||||
'moved' => true,
|
'moved' => true,
|
||||||
|
|||||||
@@ -2,6 +2,8 @@
|
|||||||
|
|
||||||
namespace App\Providers;
|
namespace App\Providers;
|
||||||
|
|
||||||
|
use App\Actions\Notifications\QueueTicketStatusChangedNotificationDeliveries;
|
||||||
|
use App\Actions\Notifications\TicketStatusChanged;
|
||||||
use App\Actions\Permissions\PermissionService;
|
use App\Actions\Permissions\PermissionService;
|
||||||
use App\Enums\Permission;
|
use App\Enums\Permission;
|
||||||
use App\Models\Project;
|
use App\Models\Project;
|
||||||
@@ -11,6 +13,7 @@ use Carbon\CarbonImmutable;
|
|||||||
use Illuminate\Database\Eloquent\Relations\Relation;
|
use Illuminate\Database\Eloquent\Relations\Relation;
|
||||||
use Illuminate\Support\Facades\Date;
|
use Illuminate\Support\Facades\Date;
|
||||||
use Illuminate\Support\Facades\DB;
|
use Illuminate\Support\Facades\DB;
|
||||||
|
use Illuminate\Support\Facades\Event;
|
||||||
use Illuminate\Support\Facades\Gate;
|
use Illuminate\Support\Facades\Gate;
|
||||||
use Illuminate\Support\Facades\URL;
|
use Illuminate\Support\Facades\URL;
|
||||||
use Illuminate\Support\ServiceProvider;
|
use Illuminate\Support\ServiceProvider;
|
||||||
@@ -37,6 +40,7 @@ class AppServiceProvider extends ServiceProvider
|
|||||||
}
|
}
|
||||||
|
|
||||||
$this->configureAuthorization();
|
$this->configureAuthorization();
|
||||||
|
$this->configureEventListeners();
|
||||||
$this->configureDefaults();
|
$this->configureDefaults();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -85,4 +89,15 @@ class AppServiceProvider extends ServiceProvider
|
|||||||
: null,
|
: null,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Configure application event listeners.
|
||||||
|
*/
|
||||||
|
protected function configureEventListeners(): void
|
||||||
|
{
|
||||||
|
Event::listen(
|
||||||
|
TicketStatusChanged::class,
|
||||||
|
QueueTicketStatusChangedNotificationDeliveries::class,
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,209 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Tests\Feature\Notifications;
|
||||||
|
|
||||||
|
use App\Actions\Notifications\DeliverNotificationAction;
|
||||||
|
use App\Actions\ProjectTicketMetadata\InitializeProjectTicketMetadata;
|
||||||
|
use App\Enums\NotificationChannel;
|
||||||
|
use App\Enums\NotificationEventType;
|
||||||
|
use App\Models\BoardColumn;
|
||||||
|
use App\Models\NotificationAction;
|
||||||
|
use App\Models\Project;
|
||||||
|
use App\Models\Ticket;
|
||||||
|
use App\Models\TicketPriority;
|
||||||
|
use App\Models\TicketStatus;
|
||||||
|
use App\Models\User;
|
||||||
|
use Carbon\CarbonImmutable;
|
||||||
|
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||||
|
use Illuminate\Support\Facades\DB;
|
||||||
|
use Illuminate\Support\Facades\Queue;
|
||||||
|
use Tests\TestCase;
|
||||||
|
|
||||||
|
class TicketStatusChangedNotificationDispatchTest extends TestCase
|
||||||
|
{
|
||||||
|
use RefreshDatabase;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Ensure status changes queue deliveries only for enabled matching actions in the ticket project.
|
||||||
|
*/
|
||||||
|
public function test_status_changes_queue_delivery_jobs_for_enabled_matching_actions(): void
|
||||||
|
{
|
||||||
|
Queue::fake();
|
||||||
|
|
||||||
|
$user = User::factory()->create([
|
||||||
|
'name' => 'Erica Example',
|
||||||
|
]);
|
||||||
|
$project = Project::factory()->create([
|
||||||
|
'key' => 'CORE',
|
||||||
|
'name' => 'Core Platform',
|
||||||
|
]);
|
||||||
|
$otherProject = Project::factory()->create();
|
||||||
|
|
||||||
|
app(InitializeProjectTicketMetadata::class)->handle($project);
|
||||||
|
app(InitializeProjectTicketMetadata::class)->handle($otherProject);
|
||||||
|
|
||||||
|
$doingColumn = BoardColumn::factory()->for($project)->create([
|
||||||
|
'name' => 'Doing',
|
||||||
|
'position' => 1,
|
||||||
|
]);
|
||||||
|
$doingStatus = $this->createColumnStatus($doingColumn);
|
||||||
|
$backlogStatus = $this->backlogStatus($project);
|
||||||
|
$this->allowMove($backlogStatus, $doingStatus);
|
||||||
|
|
||||||
|
$ticket = $this->createTicket($project, $backlogStatus, 'Fix webhook dispatch');
|
||||||
|
|
||||||
|
$webhookAction = NotificationAction::factory()
|
||||||
|
->webhook()
|
||||||
|
->for($project)
|
||||||
|
->create([
|
||||||
|
'name' => 'Primary webhook',
|
||||||
|
]);
|
||||||
|
$discordAction = NotificationAction::factory()
|
||||||
|
->discord()
|
||||||
|
->for($project)
|
||||||
|
->create([
|
||||||
|
'name' => 'Discord alerts',
|
||||||
|
]);
|
||||||
|
$disabledAction = NotificationAction::factory()
|
||||||
|
->disabled()
|
||||||
|
->for($project)
|
||||||
|
->create([
|
||||||
|
'name' => 'Disabled webhook',
|
||||||
|
]);
|
||||||
|
$foreignProjectAction = NotificationAction::factory()
|
||||||
|
->for($otherProject)
|
||||||
|
->create([
|
||||||
|
'name' => 'Foreign webhook',
|
||||||
|
]);
|
||||||
|
|
||||||
|
DB::table('notification_actions')->insert([
|
||||||
|
'project_id' => $project->id,
|
||||||
|
'name' => 'Wrong event',
|
||||||
|
'enabled' => true,
|
||||||
|
'event_type' => 'ticket_created',
|
||||||
|
'channel' => NotificationChannel::Webhook->value,
|
||||||
|
'channel_configuration' => json_encode([]),
|
||||||
|
'message_template' => null,
|
||||||
|
'delivery_url' => 'https://hooks.example.test/kanbangit/wrong-event',
|
||||||
|
'created_at' => CarbonImmutable::now(),
|
||||||
|
'updated_at' => CarbonImmutable::now(),
|
||||||
|
]);
|
||||||
|
|
||||||
|
$this->syncProjectPermissions($project, $user, ['edit', 'view']);
|
||||||
|
|
||||||
|
$this->actingAs($user)
|
||||||
|
->patch(route('projects.tickets.status.update', [$project, $ticket]), [
|
||||||
|
'redirect_to' => 'backlog',
|
||||||
|
'ticket_status_id' => $doingStatus->id,
|
||||||
|
])
|
||||||
|
->assertRedirect(route('projects.backlog.index', $project))
|
||||||
|
->assertSessionHas('status', 'CORE-1 moved to Doing.');
|
||||||
|
|
||||||
|
Queue::assertPushed(DeliverNotificationAction::class, 2);
|
||||||
|
Queue::assertPushed(DeliverNotificationAction::class, function (DeliverNotificationAction $job) use ($webhookAction, $project, $ticket, $user, $backlogStatus, $doingStatus): bool {
|
||||||
|
return $job->notificationActionId === $webhookAction->id
|
||||||
|
&& $job->payload->projectId === $project->id
|
||||||
|
&& $job->payload->projectKey === 'CORE'
|
||||||
|
&& $job->payload->projectName === 'Core Platform'
|
||||||
|
&& $job->payload->ticketId === $ticket->id
|
||||||
|
&& $job->payload->ticketKey === 'CORE-1'
|
||||||
|
&& $job->payload->ticketTitle === 'Fix webhook dispatch'
|
||||||
|
&& $job->payload->ticketUrl === route('projects.tickets.show', [$project, $ticket])
|
||||||
|
&& $job->payload->actorId === $user->id
|
||||||
|
&& $job->payload->actorName === 'Erica Example'
|
||||||
|
&& $job->payload->fromStatusId === $backlogStatus->id
|
||||||
|
&& $job->payload->fromStatusLabel === 'Backlog'
|
||||||
|
&& $job->payload->toStatusId === $doingStatus->id
|
||||||
|
&& $job->payload->toStatusLabel === 'Doing'
|
||||||
|
&& $job->payload->eventType === NotificationEventType::TicketStatusChanged;
|
||||||
|
});
|
||||||
|
Queue::assertPushed(DeliverNotificationAction::class, fn (DeliverNotificationAction $job): bool => $job->notificationActionId === $discordAction->id);
|
||||||
|
Queue::assertNotPushed(DeliverNotificationAction::class, fn (DeliverNotificationAction $job): bool => $job->notificationActionId === $disabledAction->id);
|
||||||
|
Queue::assertNotPushed(DeliverNotificationAction::class, fn (DeliverNotificationAction $job): bool => $job->notificationActionId === $foreignProjectAction->id);
|
||||||
|
|
||||||
|
$this->assertDatabaseHas('ticket_worklog_entries', [
|
||||||
|
'ticket_id' => $ticket->id,
|
||||||
|
'actor_id' => $user->id,
|
||||||
|
'entry_type' => 'status_changed',
|
||||||
|
'summary' => 'Moved from Backlog to Doing.',
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Ensure no-op moves keep their response behavior and do not queue deliveries.
|
||||||
|
*/
|
||||||
|
public function test_no_op_status_moves_do_not_queue_notification_deliveries(): void
|
||||||
|
{
|
||||||
|
Queue::fake();
|
||||||
|
|
||||||
|
$user = User::factory()->create();
|
||||||
|
$project = Project::factory()->create([
|
||||||
|
'key' => 'CORE',
|
||||||
|
]);
|
||||||
|
|
||||||
|
app(InitializeProjectTicketMetadata::class)->handle($project);
|
||||||
|
|
||||||
|
$backlogStatus = $this->backlogStatus($project);
|
||||||
|
$ticket = $this->createTicket($project, $backlogStatus, 'Stay in backlog');
|
||||||
|
|
||||||
|
NotificationAction::factory()
|
||||||
|
->for($project)
|
||||||
|
->create([
|
||||||
|
'name' => 'Backlog webhook',
|
||||||
|
]);
|
||||||
|
|
||||||
|
$this->syncProjectPermissions($project, $user, ['edit', 'view']);
|
||||||
|
|
||||||
|
$this->actingAs($user)
|
||||||
|
->patch(route('projects.tickets.status.update', [$project, $ticket]), [
|
||||||
|
'redirect_to' => 'backlog',
|
||||||
|
'ticket_status_id' => $backlogStatus->id,
|
||||||
|
])
|
||||||
|
->assertRedirect(route('projects.backlog.index', $project))
|
||||||
|
->assertSessionHas('status', 'CORE-1 already belongs to Backlog.');
|
||||||
|
|
||||||
|
Queue::assertNothingPushed();
|
||||||
|
$this->assertDatabaseCount('ticket_worklog_entries', 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
private function backlogStatus(Project $project): TicketStatus
|
||||||
|
{
|
||||||
|
return $project->ticketStatuses()
|
||||||
|
->whereNull('board_column_id')
|
||||||
|
->firstOrFail();
|
||||||
|
}
|
||||||
|
|
||||||
|
private function createColumnStatus(BoardColumn $boardColumn): TicketStatus
|
||||||
|
{
|
||||||
|
return TicketStatus::factory()
|
||||||
|
->for($boardColumn->project)
|
||||||
|
->for($boardColumn)
|
||||||
|
->create([
|
||||||
|
'name' => $boardColumn->name,
|
||||||
|
'position' => 1,
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
|
private function createTicket(Project $project, TicketStatus $ticketStatus, string $title): Ticket
|
||||||
|
{
|
||||||
|
$priority = TicketPriority::factory()->for($project)->create([
|
||||||
|
'name' => 'High',
|
||||||
|
'position' => 2,
|
||||||
|
]);
|
||||||
|
|
||||||
|
return Ticket::factory()->create([
|
||||||
|
'project_id' => $project->id,
|
||||||
|
'sequence_number' => 1,
|
||||||
|
'ticket_key' => 'CORE-1',
|
||||||
|
'ticket_priority_id' => $priority->id,
|
||||||
|
'ticket_status_id' => $ticketStatus->id,
|
||||||
|
'ticket_type_id' => $project->ticketTypes()->firstOrFail()->id,
|
||||||
|
'title' => $title,
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
|
private function allowMove(TicketStatus $sourceStatus, TicketStatus $targetStatus): void
|
||||||
|
{
|
||||||
|
$sourceStatus->allowedTargetStatuses()->syncWithoutDetaching([$targetStatus->id]);
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user