Add notification action foundation

This commit is contained in:
Eric Peters
2026-04-20 21:59:57 +02:00
parent 8df9622f1b
commit e4762f8e32
22 changed files with 1047 additions and 0 deletions
@@ -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();
}
}