Add project notification settings UI

This commit is contained in:
Eric Peters
2026-04-20 22:29:46 +02:00
parent e4762f8e32
commit ee15efe739
17 changed files with 1374 additions and 0 deletions
@@ -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,94 @@
<?php
namespace App\Http\Controllers;
use App\Actions\Notifications\BuildProjectNotificationSettingsPayload;
use App\Actions\Notifications\DeleteProjectNotificationAction;
use App\Actions\Notifications\StoreProjectNotificationAction;
use App\Actions\Notifications\UpdateProjectNotificationAction;
use App\Actions\Permissions\PermissionService;
use App\Enums\Permission;
use App\Http\Requests\StoreProjectNotificationActionRequest;
use App\Http\Requests\UpdateProjectNotificationActionRequest;
use App\Models\NotificationAction;
use App\Models\Project;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Inertia\Inertia;
use Inertia\Response;
class ProjectNotificationSettingsController extends Controller
{
public function __construct(
private readonly BuildProjectNotificationSettingsPayload $buildProjectNotificationSettingsPayload,
private readonly DeleteProjectNotificationAction $deleteProjectNotificationAction,
private readonly PermissionService $permissionService,
private readonly StoreProjectNotificationAction $storeProjectNotificationAction,
private readonly UpdateProjectNotificationAction $updateProjectNotificationAction,
) {}
public function show(Request $request, Project $project): Response
{
$this->abortUnlessCanManage($request, $project);
return Inertia::render(
'projects/settings/Notifications',
$this->buildProjectNotificationSettingsPayload->handle($project, session('status')),
);
}
public function store(
StoreProjectNotificationActionRequest $request,
Project $project,
): RedirectResponse {
$this->abortUnlessCanManage($request, $project);
$notificationAction = $this->storeProjectNotificationAction->handle($project, $request->validated());
return to_route('projects.settings.notifications', $project)
->with('status', "{$notificationAction->name} notification was added.");
}
public function update(
UpdateProjectNotificationActionRequest $request,
Project $project,
NotificationAction $notificationAction,
): RedirectResponse {
$this->abortUnlessCanManage($request, $project);
$notificationAction = $this->updateProjectNotificationAction->handle(
$project,
$notificationAction,
$request->validated(),
);
return to_route('projects.settings.notifications', $project)
->with('status', "{$notificationAction->name} notification was updated.");
}
public function destroy(
Request $request,
Project $project,
NotificationAction $notificationAction,
): RedirectResponse {
$this->abortUnlessCanManage($request, $project);
$notificationAction = $project->notificationActions()
->whereKey($notificationAction->id)
->firstOrFail();
$deletedName = $notificationAction->name;
$this->deleteProjectNotificationAction->handle($project, $notificationAction);
return to_route('projects.settings.notifications', $project)
->with('status', "{$deletedName} notification was deleted.");
}
private function abortUnlessCanManage(Request $request, Project $project): void
{
abort_unless(
$this->permissionService->allows($request->user(), Permission::ProjectAdmin, $project),
403,
);
}
}
@@ -0,0 +1,71 @@
<?php
namespace App\Http\Requests;
use App\Enums\NotificationChannel;
use App\Enums\NotificationEventType;
use Illuminate\Contracts\Validation\ValidationRule;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rule;
class StoreProjectNotificationActionRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*/
public function authorize(): bool
{
return $this->user() !== null;
}
/**
* Get the validation rules that apply to the request.
*
* @return array<string, ValidationRule|array<mixed>|string>
*/
public function rules(): array
{
return [
'name' => ['required', 'string', 'max:255'],
'event_type' => ['required', 'string', Rule::in($this->eventTypes())],
'channel' => ['required', 'string', Rule::in($this->channels())],
'enabled' => ['required', 'boolean'],
'delivery_url' => ['required', 'string', 'max:2048', 'url'],
'message_template' => $this->messageTemplateRules(),
];
}
/**
* @return array<int, string>
*/
private function channels(): array
{
return array_map(
static fn (NotificationChannel $channel): string => $channel->value,
NotificationChannel::cases(),
);
}
/**
* @return array<int, string>
*/
private function eventTypes(): array
{
return array_map(
static fn (NotificationEventType $eventType): string => $eventType->value,
NotificationEventType::cases(),
);
}
/**
* @return array<int, ValidationRule|string>
*/
private function messageTemplateRules(): array
{
if ($this->input('channel') === NotificationChannel::Discord->value) {
return ['required', 'string', 'max:2000'];
}
return ['nullable', 'string', 'max:2000'];
}
}
@@ -0,0 +1,71 @@
<?php
namespace App\Http\Requests;
use App\Enums\NotificationChannel;
use App\Enums\NotificationEventType;
use Illuminate\Contracts\Validation\ValidationRule;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rule;
class UpdateProjectNotificationActionRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*/
public function authorize(): bool
{
return $this->user() !== null;
}
/**
* Get the validation rules that apply to the request.
*
* @return array<string, ValidationRule|array<mixed>|string>
*/
public function rules(): array
{
return [
'name' => ['required', 'string', 'max:255'],
'event_type' => ['required', 'string', Rule::in($this->eventTypes())],
'channel' => ['required', 'string', Rule::in($this->channels())],
'enabled' => ['required', 'boolean'],
'delivery_url' => ['nullable', 'string', 'max:2048', 'url'],
'message_template' => $this->messageTemplateRules(),
];
}
/**
* @return array<int, string>
*/
private function channels(): array
{
return array_map(
static fn (NotificationChannel $channel): string => $channel->value,
NotificationChannel::cases(),
);
}
/**
* @return array<int, string>
*/
private function eventTypes(): array
{
return array_map(
static fn (NotificationEventType $eventType): string => $eventType->value,
NotificationEventType::cases(),
);
}
/**
* @return array<int, ValidationRule|string>
*/
private function messageTemplateRules(): array
{
if ($this->input('channel') === NotificationChannel::Discord->value) {
return ['required', 'string', 'max:2000'];
}
return ['nullable', 'string', 'max:2000'];
}
}