Add assignee changed notification event support

This commit is contained in:
2026-05-18 23:30:32 +00:00
parent 3ddfcc89f9
commit c9050886ac
13 changed files with 558 additions and 12 deletions
@@ -60,7 +60,7 @@ class BuildProjectNotificationSettingsPayload
'value' => '$project_name',
],
[
'description' => 'Origin status label for status moves; blank for ticket create.',
'description' => 'Origin status label for status moves; blank for ticket create and assignee changes.',
'value' => '$status_from',
],
[
@@ -71,6 +71,14 @@ class BuildProjectNotificationSettingsPayload
'description' => 'Actor name when the event has a known user.',
'value' => '$actor_name',
],
[
'description' => 'Previous assignee name when the event updates assignment; blank otherwise.',
'value' => '$assignee_from',
],
[
'description' => 'New assignee name when the event updates assignment; blank when unassigned.',
'value' => '$assignee_to',
],
];
}
}
@@ -0,0 +1,63 @@
<?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 BuildTicketAssigneeChangedPayload
{
public function handle(
Project $project,
Ticket $ticket,
?User $actor,
?int $fromAssigneeId,
?string $fromAssigneeName,
?int $toAssigneeId,
?string $toAssigneeName,
CarbonImmutable $occurredAt,
): TicketStatusChangedPayload {
$ticket->loadMissing('ticketStatus.boardColumn');
return new TicketStatusChangedPayload(
eventType: NotificationEventType::TicketAssigneeChanged,
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: $ticket->ticket_status_id,
toStatusLabel: $this->statusLabel($ticket->ticketStatus),
occurredAt: $occurredAt,
fromAssigneeId: $fromAssigneeId,
fromAssigneeName: $fromAssigneeName,
toAssigneeId: $toAssigneeId,
toAssigneeName: $toAssigneeName,
);
}
private function statusLabel(?TicketStatus $ticketStatus): string
{
if ($ticketStatus === null) {
return 'Unknown status';
}
$ticketStatus->loadMissing('boardColumn');
if ($ticketStatus->boardColumn === null || $ticketStatus->boardColumn->name === $ticketStatus->name) {
return $ticketStatus->name;
}
return "{$ticketStatus->boardColumn->name} / {$ticketStatus->name}";
}
}
@@ -207,6 +207,16 @@ class MatchNotificationFilters
'id' => $actorId,
'name' => $actorName,
],
'assignee' => [
'from' => [
'id' => $payload->fromAssigneeId,
'name' => $payload->fromAssigneeName,
],
'to' => [
'id' => $payload->toAssigneeId,
'name' => $payload->toAssigneeName,
],
],
'status' => [
'from' => [
'id' => $fromStatusId,
@@ -0,0 +1,26 @@
<?php
namespace App\Actions\Notifications;
use App\Enums\NotificationEventType;
use Illuminate\Contracts\Events\ShouldDispatchAfterCommit;
use Illuminate\Foundation\Events\Dispatchable;
final readonly class TicketAssigneeChanged implements NotificationEvent, ShouldDispatchAfterCommit
{
use Dispatchable;
public function __construct(
public TicketStatusChangedPayload $payload,
) {}
public function eventType(): NotificationEventType
{
return NotificationEventType::TicketAssigneeChanged;
}
public function payload(): NotificationEventPayload
{
return $this->payload;
}
}
@@ -17,11 +17,16 @@ final readonly class TicketStatusChangedPayload implements NotificationEventPayl
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,
public ?int $fromAssigneeId = null,
public ?string $fromAssigneeName = null,
public ?int $toAssigneeId = null,
public ?string $toAssigneeName = null,
public NotificationEventType $eventType = NotificationEventType::TicketStatusChanged,
) {}
public function projectId(): int
@@ -50,9 +55,11 @@ final readonly class TicketStatusChangedPayload implements NotificationEventPayl
'$ticket_key' => $this->ticketKey,
'$ticket_title' => $this->ticketTitle,
'$project_name' => $this->projectName,
'$status_from' => $this->fromStatusLabel,
'$status_from' => $this->fromStatusLabel ?? '',
'$status_to' => $this->toStatusLabel,
'$actor_name' => $this->actorName ?? '',
'$assignee_from' => $this->fromAssigneeName ?? '',
'$assignee_to' => $this->toAssigneeName ?? '',
];
}
@@ -62,7 +69,11 @@ final readonly class TicketStatusChangedPayload implements NotificationEventPayl
* 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},
* assignee: array{
* from: array{id: int, name: string}|null,
* to: array{id: int, name: string}|null
* },
* from: array{id: int, label: string}|null,
* to: array{id: int, label: string},
* ticket_url: string,
* occurred_at: string
@@ -71,7 +82,7 @@ final readonly class TicketStatusChangedPayload implements NotificationEventPayl
public function toArray(): array
{
return [
'event_type' => NotificationEventType::TicketStatusChanged->value,
'event_type' => $this->eventType->value,
'project' => [
'id' => $this->projectId,
'key' => $this->projectKey,
@@ -88,10 +99,26 @@ final readonly class TicketStatusChangedPayload implements NotificationEventPayl
'id' => $this->actorId,
'name' => $this->actorName,
],
'from' => [
'id' => $this->fromStatusId,
'label' => $this->fromStatusLabel,
'assignee' => [
'from' => $this->fromAssigneeId === null || $this->fromAssigneeName === null
? null
: [
'id' => $this->fromAssigneeId,
'name' => $this->fromAssigneeName,
],
'to' => $this->toAssigneeId === null || $this->toAssigneeName === null
? null
: [
'id' => $this->toAssigneeId,
'name' => $this->toAssigneeName,
],
],
'from' => $this->fromStatusId === null || $this->fromStatusLabel === null
? null
: [
'id' => $this->fromStatusId,
'label' => $this->fromStatusLabel,
],
'to' => [
'id' => $this->toStatusId,
'label' => $this->toStatusLabel,
+34 -3
View File
@@ -2,12 +2,15 @@
namespace App\Actions\Tickets;
use App\Actions\Notifications\BuildTicketAssigneeChangedPayload;
use App\Actions\Notifications\TicketAssigneeChanged;
use App\Actions\Tickets\Support\TicketAcceptanceCriteriaData;
use App\Enums\TicketWorklogEntryType;
use App\Models\Ticket;
use App\Models\TicketFieldDefinition;
use App\Models\TicketFieldValue;
use App\Models\User;
use Carbon\CarbonImmutable;
use Illuminate\Support\Arr;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\DB;
@@ -15,6 +18,7 @@ use Illuminate\Support\Facades\DB;
class UpdateProjectTicket
{
public function __construct(
private readonly BuildTicketAssigneeChangedPayload $buildTicketAssigneeChangedPayload,
private readonly CreateTicketWorklogEntry $createTicketWorklogEntry,
private readonly SyncProjectTicketFieldValues $syncProjectTicketFieldValues,
) {}
@@ -67,6 +71,7 @@ class UpdateProjectTicket
'parentTicket',
'project',
'ticketPriority',
'ticketStatus.boardColumn',
'ticketType',
]);
@@ -96,6 +101,7 @@ class UpdateProjectTicket
$changes = [];
$summaryLabels = [];
$assigneeChange = null;
$this->trackScalarChange(
$changes,
@@ -173,13 +179,23 @@ class UpdateProjectTicket
}
if ($ticket->assignee_user_id !== $ticketAttributes['assignee_user_id']) {
$toAssigneeId = $ticketAttributes['assignee_user_id'];
$toAssigneeName = $toAssigneeId === null
? null
: User::query()->find($toAssigneeId)?->name;
$changes['assignee'] = [
'from' => $ticket->assignee?->name,
'to' => $ticketAttributes['assignee_user_id'] === null
? null
: User::query()->find($ticketAttributes['assignee_user_id'])?->name,
'to' => $toAssigneeName,
];
$summaryLabels[] = 'assignee';
$assigneeChange = [
'from_id' => $ticket->assignee_user_id,
'from_name' => $ticket->assignee?->name,
'to_id' => $toAssigneeId,
'to_name' => $toAssigneeName,
];
}
if ($ticket->author_user_id !== $ticketAttributes['author_user_id']) {
@@ -225,6 +241,21 @@ class UpdateProjectTicket
$changes,
);
if ($assigneeChange !== null) {
TicketAssigneeChanged::dispatch(
$this->buildTicketAssigneeChangedPayload->handle(
$ticket->project,
$ticket,
$actor,
$assigneeChange['from_id'],
$assigneeChange['from_name'],
$assigneeChange['to_id'],
$assigneeChange['to_name'],
CarbonImmutable::now(),
),
);
}
return true;
});
}