Merge pull request 'codex/v0.5-webhook-notification/02-project-notification-settings-ui' (#48) from codex/v0.5-webhook-notification/02-project-notification-settings-ui into main
Reviewed-on: OpenSource/KanbanGit#48
This commit was merged in pull request #48.
This commit is contained in:
@@ -0,0 +1,70 @@
|
||||
<?php
|
||||
|
||||
namespace App\Actions\Notifications;
|
||||
|
||||
use App\Enums\NotificationChannel;
|
||||
use App\Enums\NotificationEventType;
|
||||
use App\Http\Resources\NotificationActionResource;
|
||||
use App\Http\Resources\ProjectResource;
|
||||
use App\Models\Project;
|
||||
|
||||
class BuildProjectNotificationSettingsPayload
|
||||
{
|
||||
public function __construct(
|
||||
private readonly ListProjectNotificationActions $listProjectNotificationActions,
|
||||
) {}
|
||||
|
||||
/**
|
||||
* @return array<string, mixed>
|
||||
*/
|
||||
public function handle(Project $project, mixed $status = null): array
|
||||
{
|
||||
return [
|
||||
'channelOptions' => NotificationChannel::options(),
|
||||
'eventTypeOptions' => NotificationEventType::options(),
|
||||
'notificationActions' => NotificationActionResource::collection(
|
||||
$this->listProjectNotificationActions->handle($project),
|
||||
)->resolve(),
|
||||
'project' => ProjectResource::make($project)->resolve(),
|
||||
'status' => $status,
|
||||
'templateVariables' => $this->templateVariables(),
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<int, array{description: string, value: string}>
|
||||
*/
|
||||
private function templateVariables(): array
|
||||
{
|
||||
return [
|
||||
[
|
||||
'description' => 'Absolute ticket URL for the status change event.',
|
||||
'value' => '$url',
|
||||
],
|
||||
[
|
||||
'description' => 'Project ticket key such as CORE-42.',
|
||||
'value' => '$ticket_key',
|
||||
],
|
||||
[
|
||||
'description' => 'Ticket title from the changed ticket.',
|
||||
'value' => '$ticket_title',
|
||||
],
|
||||
[
|
||||
'description' => 'Project name that owns the ticket.',
|
||||
'value' => '$project_name',
|
||||
],
|
||||
[
|
||||
'description' => 'Origin status label before the move.',
|
||||
'value' => '$status_from',
|
||||
],
|
||||
[
|
||||
'description' => 'Destination status label after the move.',
|
||||
'value' => '$status_to',
|
||||
],
|
||||
[
|
||||
'description' => 'Actor name when the move has a known user.',
|
||||
'value' => '$actor_name',
|
||||
],
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
<?php
|
||||
|
||||
namespace App\Actions\Notifications;
|
||||
|
||||
use App\Models\NotificationAction;
|
||||
use App\Models\Project;
|
||||
|
||||
class DeleteProjectNotificationAction
|
||||
{
|
||||
public function handle(Project $project, NotificationAction $notificationAction): void
|
||||
{
|
||||
$project->notificationActions()
|
||||
->whereKey($notificationAction->id)
|
||||
->firstOrFail()
|
||||
->delete();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
<?php
|
||||
|
||||
namespace App\Actions\Notifications;
|
||||
|
||||
use App\Models\NotificationAction;
|
||||
use App\Models\Project;
|
||||
use Illuminate\Database\Eloquent\Collection;
|
||||
|
||||
class ListProjectNotificationActions
|
||||
{
|
||||
/**
|
||||
* @return Collection<int, NotificationAction>
|
||||
*/
|
||||
public function handle(Project $project): Collection
|
||||
{
|
||||
return $project->notificationActions()->get();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
<?php
|
||||
|
||||
namespace App\Actions\Notifications;
|
||||
|
||||
use App\Models\NotificationAction;
|
||||
use App\Models\Project;
|
||||
use Illuminate\Support\Arr;
|
||||
|
||||
class StoreProjectNotificationAction
|
||||
{
|
||||
/**
|
||||
* @param array{
|
||||
* channel: mixed,
|
||||
* channel_configuration?: array<string, mixed>|null,
|
||||
* delivery_url: string,
|
||||
* enabled: bool,
|
||||
* event_type: mixed,
|
||||
* message_template?: string|null,
|
||||
* name: string
|
||||
* } $validated
|
||||
*/
|
||||
public function handle(Project $project, array $validated): NotificationAction
|
||||
{
|
||||
return $project->notificationActions()->create([
|
||||
'channel' => Arr::get($validated, 'channel'),
|
||||
'channel_configuration' => Arr::get($validated, 'channel_configuration') ?? [],
|
||||
'delivery_url' => trim((string) Arr::get($validated, 'delivery_url')),
|
||||
'enabled' => (bool) Arr::get($validated, 'enabled'),
|
||||
'event_type' => Arr::get($validated, 'event_type'),
|
||||
'message_template' => $this->normalizeMessageTemplate(Arr::get($validated, 'message_template')),
|
||||
'name' => trim((string) Arr::get($validated, 'name')),
|
||||
]);
|
||||
}
|
||||
|
||||
private function normalizeMessageTemplate(mixed $messageTemplate): ?string
|
||||
{
|
||||
if (! is_string($messageTemplate)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$messageTemplate = trim($messageTemplate);
|
||||
|
||||
return $messageTemplate === '' ? null : $messageTemplate;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,87 @@
|
||||
<?php
|
||||
|
||||
namespace App\Actions\Notifications;
|
||||
|
||||
use App\Models\NotificationAction;
|
||||
use App\Models\Project;
|
||||
use Illuminate\Support\Arr;
|
||||
|
||||
class UpdateProjectNotificationAction
|
||||
{
|
||||
/**
|
||||
* @param array{
|
||||
* channel?: mixed,
|
||||
* channel_configuration?: array<string, mixed>|null,
|
||||
* delivery_url?: string|null,
|
||||
* enabled?: bool,
|
||||
* event_type?: mixed,
|
||||
* message_template?: string|null,
|
||||
* name?: string
|
||||
* } $validated
|
||||
*/
|
||||
public function handle(
|
||||
Project $project,
|
||||
NotificationAction $notificationAction,
|
||||
array $validated,
|
||||
): NotificationAction {
|
||||
$notificationAction = $this->resolveProjectNotificationAction($project, $notificationAction);
|
||||
|
||||
$attributes = [];
|
||||
|
||||
if (array_key_exists('name', $validated)) {
|
||||
$attributes['name'] = trim((string) Arr::get($validated, 'name'));
|
||||
}
|
||||
|
||||
if (array_key_exists('enabled', $validated)) {
|
||||
$attributes['enabled'] = (bool) Arr::get($validated, 'enabled');
|
||||
}
|
||||
|
||||
if (array_key_exists('event_type', $validated)) {
|
||||
$attributes['event_type'] = Arr::get($validated, 'event_type');
|
||||
}
|
||||
|
||||
if (array_key_exists('channel', $validated)) {
|
||||
$attributes['channel'] = Arr::get($validated, 'channel');
|
||||
}
|
||||
|
||||
if (array_key_exists('channel_configuration', $validated)) {
|
||||
$attributes['channel_configuration'] = Arr::get($validated, 'channel_configuration') ?? [];
|
||||
}
|
||||
|
||||
if (array_key_exists('message_template', $validated)) {
|
||||
$attributes['message_template'] = $this->normalizeMessageTemplate(
|
||||
Arr::get($validated, 'message_template'),
|
||||
);
|
||||
}
|
||||
|
||||
$deliveryUrl = Arr::get($validated, 'delivery_url');
|
||||
|
||||
if (is_string($deliveryUrl) && trim($deliveryUrl) !== '') {
|
||||
$attributes['delivery_url'] = trim($deliveryUrl);
|
||||
}
|
||||
|
||||
$notificationAction->update($attributes);
|
||||
|
||||
return $notificationAction->fresh();
|
||||
}
|
||||
|
||||
private function normalizeMessageTemplate(mixed $messageTemplate): ?string
|
||||
{
|
||||
if (! is_string($messageTemplate)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$messageTemplate = trim($messageTemplate);
|
||||
|
||||
return $messageTemplate === '' ? null : $messageTemplate;
|
||||
}
|
||||
|
||||
private function resolveProjectNotificationAction(
|
||||
Project $project,
|
||||
NotificationAction $notificationAction,
|
||||
): NotificationAction {
|
||||
return $project->notificationActions()
|
||||
->whereKey($notificationAction->id)
|
||||
->firstOrFail();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user