Add notification action foundation #47
@@ -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();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
<?php
|
||||
|
||||
namespace App\Enums;
|
||||
|
||||
enum NotificationChannel: string
|
||||
{
|
||||
case Discord = 'discord';
|
||||
case Webhook = 'webhook';
|
||||
|
||||
public function label(): string
|
||||
{
|
||||
return match ($this) {
|
||||
self::Discord => 'Discord',
|
||||
self::Webhook => 'Webhook',
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<int, array{label: string, value: string}>
|
||||
*/
|
||||
public static function options(): array
|
||||
{
|
||||
return array_map(
|
||||
fn (self $channel): array => [
|
||||
'label' => $channel->label(),
|
||||
'value' => $channel->value,
|
||||
],
|
||||
self::cases(),
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
<?php
|
||||
|
||||
namespace App\Enums;
|
||||
|
||||
enum NotificationDeliveryStatus: string
|
||||
{
|
||||
case Failed = 'failed';
|
||||
case Pending = 'pending';
|
||||
case Retryable = 'retryable';
|
||||
case Succeeded = 'succeeded';
|
||||
|
||||
public function label(): string
|
||||
{
|
||||
return match ($this) {
|
||||
self::Failed => 'Failed',
|
||||
self::Pending => 'Pending',
|
||||
self::Retryable => 'Retryable',
|
||||
self::Succeeded => 'Succeeded',
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
<?php
|
||||
|
||||
namespace App\Enums;
|
||||
|
||||
enum NotificationEventType: string
|
||||
{
|
||||
case TicketStatusChanged = 'ticket_status_changed';
|
||||
|
||||
public function label(): string
|
||||
{
|
||||
return match ($this) {
|
||||
self::TicketStatusChanged => 'Ticket status changed',
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<int, array{label: string, value: string}>
|
||||
*/
|
||||
public static function options(): array
|
||||
{
|
||||
return array_map(
|
||||
fn (self $eventType): array => [
|
||||
'label' => $eventType->label(),
|
||||
'value' => $eventType->value,
|
||||
],
|
||||
self::cases(),
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Resources;
|
||||
|
||||
use App\Models\NotificationAction;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Http\Resources\Json\JsonResource;
|
||||
|
||||
class NotificationActionResource extends JsonResource
|
||||
{
|
||||
/**
|
||||
* Transform the resource into an array.
|
||||
*
|
||||
* @return array<string, mixed>
|
||||
*/
|
||||
public function toArray(Request $request): array
|
||||
{
|
||||
return [
|
||||
'id' => $this->id,
|
||||
'project_id' => $this->project_id,
|
||||
'name' => $this->name,
|
||||
'enabled' => $this->enabled,
|
||||
'event_type' => $this->event_type->value,
|
||||
'event_type_label' => $this->event_type->label(),
|
||||
'channel' => $this->channel->value,
|
||||
'channel_label' => $this->channel->label(),
|
||||
'channel_configuration' => $this->channel_configuration ?? [],
|
||||
'has_delivery_url' => $this->resource instanceof NotificationAction && filled($this->delivery_url),
|
||||
'message_template' => $this->message_template,
|
||||
'created_at' => $this->created_at?->toISOString(),
|
||||
'updated_at' => $this->updated_at?->toISOString(),
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Resources;
|
||||
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Http\Resources\Json\JsonResource;
|
||||
|
||||
class NotificationDeliveryLogResource extends JsonResource
|
||||
{
|
||||
/**
|
||||
* Transform the resource into an array.
|
||||
*
|
||||
* @return array<string, mixed>
|
||||
*/
|
||||
public function toArray(Request $request): array
|
||||
{
|
||||
return [
|
||||
'id' => $this->id,
|
||||
'notification_action_id' => $this->notification_action_id,
|
||||
'event_type' => $this->event_type->value,
|
||||
'event_type_label' => $this->event_type->label(),
|
||||
'channel' => $this->channel->value,
|
||||
'channel_label' => $this->channel->label(),
|
||||
'delivery_status' => $this->delivery_status->value,
|
||||
'delivery_status_label' => $this->delivery_status->label(),
|
||||
'attempt_count' => $this->attempt_count,
|
||||
'http_status' => $this->http_status,
|
||||
'response_excerpt' => $this->response_excerpt,
|
||||
'error_excerpt' => $this->error_excerpt,
|
||||
'payload_metadata' => $this->payload_metadata,
|
||||
'last_attempted_at' => $this->last_attempted_at?->toISOString(),
|
||||
'delivered_at' => $this->delivered_at?->toISOString(),
|
||||
'created_at' => $this->created_at?->toISOString(),
|
||||
'updated_at' => $this->updated_at?->toISOString(),
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,62 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use App\Enums\NotificationChannel;
|
||||
use App\Enums\NotificationEventType;
|
||||
use Database\Factories\NotificationActionFactory;
|
||||
use Illuminate\Database\Eloquent\Attributes\Fillable;
|
||||
use Illuminate\Database\Eloquent\Attributes\Hidden;
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
use Illuminate\Database\Eloquent\Relations\HasMany;
|
||||
|
||||
#[Fillable([
|
||||
'channel',
|
||||
'channel_configuration',
|
||||
'delivery_url',
|
||||
'enabled',
|
||||
'event_type',
|
||||
'message_template',
|
||||
'name',
|
||||
'project_id',
|
||||
])]
|
||||
#[Hidden(['delivery_url'])]
|
||||
class NotificationAction extends Model
|
||||
{
|
||||
/** @use HasFactory<NotificationActionFactory> */
|
||||
use HasFactory;
|
||||
|
||||
/**
|
||||
* @return array<string, string>
|
||||
*/
|
||||
protected function casts(): array
|
||||
{
|
||||
return [
|
||||
'channel' => NotificationChannel::class,
|
||||
'channel_configuration' => 'array',
|
||||
'delivery_url' => 'encrypted',
|
||||
'enabled' => 'boolean',
|
||||
'event_type' => NotificationEventType::class,
|
||||
];
|
||||
}
|
||||
|
||||
public function scopeEnabled(Builder $query): Builder
|
||||
{
|
||||
return $query->where('enabled', true);
|
||||
}
|
||||
|
||||
public function deliveryLogs(): HasMany
|
||||
{
|
||||
return $this->hasMany(NotificationDeliveryLog::class)
|
||||
->orderByDesc('created_at')
|
||||
->orderByDesc('id');
|
||||
}
|
||||
|
||||
public function project(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(Project::class);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use App\Enums\NotificationChannel;
|
||||
use App\Enums\NotificationDeliveryStatus;
|
||||
use App\Enums\NotificationEventType;
|
||||
use Database\Factories\NotificationDeliveryLogFactory;
|
||||
use Illuminate\Database\Eloquent\Attributes\Fillable;
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
|
||||
#[Fillable([
|
||||
'attempt_count',
|
||||
'channel',
|
||||
'delivered_at',
|
||||
'delivery_status',
|
||||
'error_excerpt',
|
||||
'event_type',
|
||||
'http_status',
|
||||
'last_attempted_at',
|
||||
'notification_action_id',
|
||||
'payload_metadata',
|
||||
'response_excerpt',
|
||||
])]
|
||||
class NotificationDeliveryLog extends Model
|
||||
{
|
||||
/** @use HasFactory<NotificationDeliveryLogFactory> */
|
||||
use HasFactory;
|
||||
|
||||
/**
|
||||
* @return array<string, string>
|
||||
*/
|
||||
protected function casts(): array
|
||||
{
|
||||
return [
|
||||
'attempt_count' => 'integer',
|
||||
'channel' => NotificationChannel::class,
|
||||
'delivered_at' => 'datetime',
|
||||
'delivery_status' => NotificationDeliveryStatus::class,
|
||||
'event_type' => NotificationEventType::class,
|
||||
'http_status' => 'integer',
|
||||
'last_attempted_at' => 'datetime',
|
||||
'payload_metadata' => 'array',
|
||||
];
|
||||
}
|
||||
|
||||
public function notificationAction(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(NotificationAction::class);
|
||||
}
|
||||
}
|
||||
@@ -88,6 +88,13 @@ class Project extends Model
|
||||
->orderBy('repository_full_name');
|
||||
}
|
||||
|
||||
public function notificationActions(): HasMany
|
||||
{
|
||||
return $this->hasMany(NotificationAction::class)
|
||||
->orderBy('name')
|
||||
->orderBy('id');
|
||||
}
|
||||
|
||||
public function tickets(): HasMany
|
||||
{
|
||||
return $this->hasMany(Ticket::class)
|
||||
|
||||
@@ -0,0 +1,58 @@
|
||||
<?php
|
||||
|
||||
namespace Database\Factories;
|
||||
|
||||
use App\Enums\NotificationChannel;
|
||||
use App\Enums\NotificationEventType;
|
||||
use App\Models\NotificationAction;
|
||||
use App\Models\Project;
|
||||
use Illuminate\Database\Eloquent\Factories\Factory;
|
||||
|
||||
/**
|
||||
* @extends Factory<NotificationAction>
|
||||
*/
|
||||
class NotificationActionFactory extends Factory
|
||||
{
|
||||
/**
|
||||
* Define the model's default state.
|
||||
*
|
||||
* @return array<string, mixed>
|
||||
*/
|
||||
public function definition(): array
|
||||
{
|
||||
return [
|
||||
'channel' => NotificationChannel::Webhook,
|
||||
'channel_configuration' => [],
|
||||
'delivery_url' => 'https://hooks.'.fake()->domainName().'/kanbangit/'.fake()->uuid(),
|
||||
'enabled' => true,
|
||||
'event_type' => NotificationEventType::TicketStatusChanged,
|
||||
'message_template' => null,
|
||||
'name' => fake()->unique()->sentence(3),
|
||||
'project_id' => Project::factory(),
|
||||
];
|
||||
}
|
||||
|
||||
public function discord(): static
|
||||
{
|
||||
return $this->state(fn (): array => [
|
||||
'channel' => NotificationChannel::Discord,
|
||||
'delivery_url' => 'https://discord.com/api/webhooks/'.fake()->numberBetween(100_000, 999_999).'/'.fake()->uuid(),
|
||||
'message_template' => '$ticket_key moved from $status_from to $status_to by $actor_name: $url',
|
||||
]);
|
||||
}
|
||||
|
||||
public function disabled(): static
|
||||
{
|
||||
return $this->state(fn (): array => [
|
||||
'enabled' => false,
|
||||
]);
|
||||
}
|
||||
|
||||
public function webhook(): static
|
||||
{
|
||||
return $this->state(fn (): array => [
|
||||
'channel' => NotificationChannel::Webhook,
|
||||
'message_template' => null,
|
||||
]);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,98 @@
|
||||
<?php
|
||||
|
||||
namespace Database\Factories;
|
||||
|
||||
use App\Enums\NotificationChannel;
|
||||
use App\Enums\NotificationDeliveryStatus;
|
||||
use App\Enums\NotificationEventType;
|
||||
use App\Models\NotificationAction;
|
||||
use App\Models\NotificationDeliveryLog;
|
||||
use Illuminate\Database\Eloquent\Factories\Factory;
|
||||
use Illuminate\Support\Carbon;
|
||||
|
||||
/**
|
||||
* @extends Factory<NotificationDeliveryLog>
|
||||
*/
|
||||
class NotificationDeliveryLogFactory extends Factory
|
||||
{
|
||||
/**
|
||||
* Define the model's default state.
|
||||
*
|
||||
* @return array<string, mixed>
|
||||
*/
|
||||
public function definition(): array
|
||||
{
|
||||
return [
|
||||
'attempt_count' => 0,
|
||||
'channel' => NotificationChannel::Webhook,
|
||||
'delivered_at' => null,
|
||||
'delivery_status' => NotificationDeliveryStatus::Pending,
|
||||
'error_excerpt' => null,
|
||||
'event_type' => NotificationEventType::TicketStatusChanged,
|
||||
'http_status' => null,
|
||||
'last_attempted_at' => null,
|
||||
'notification_action_id' => NotificationAction::factory()->webhook(),
|
||||
'payload_metadata' => [
|
||||
'project_key' => fake()->regexify('[A-Z]{4}'),
|
||||
'ticket_key' => fake()->regexify('[A-Z]{4}-[0-9]{1,3}'),
|
||||
],
|
||||
'response_excerpt' => null,
|
||||
];
|
||||
}
|
||||
|
||||
public function forNotificationAction(NotificationAction $notificationAction): static
|
||||
{
|
||||
return $this->for($notificationAction, 'notificationAction')->state(fn (): array => [
|
||||
'channel' => $notificationAction->channel,
|
||||
'event_type' => $notificationAction->event_type,
|
||||
]);
|
||||
}
|
||||
|
||||
public function failed(): static
|
||||
{
|
||||
return $this->state(fn (): array => [
|
||||
'attempt_count' => 3,
|
||||
'delivery_status' => NotificationDeliveryStatus::Failed,
|
||||
'error_excerpt' => 'Webhook returned a permanent validation error.',
|
||||
'http_status' => 422,
|
||||
'last_attempted_at' => Carbon::now(),
|
||||
]);
|
||||
}
|
||||
|
||||
public function pending(): static
|
||||
{
|
||||
return $this->state(fn (): array => [
|
||||
'attempt_count' => 0,
|
||||
'delivered_at' => null,
|
||||
'delivery_status' => NotificationDeliveryStatus::Pending,
|
||||
'error_excerpt' => null,
|
||||
'http_status' => null,
|
||||
'last_attempted_at' => null,
|
||||
'response_excerpt' => null,
|
||||
]);
|
||||
}
|
||||
|
||||
public function retryable(): static
|
||||
{
|
||||
return $this->state(fn (): array => [
|
||||
'attempt_count' => 1,
|
||||
'delivery_status' => NotificationDeliveryStatus::Retryable,
|
||||
'error_excerpt' => 'Connection timed out while contacting the remote endpoint.',
|
||||
'http_status' => null,
|
||||
'last_attempted_at' => Carbon::now(),
|
||||
]);
|
||||
}
|
||||
|
||||
public function succeeded(): static
|
||||
{
|
||||
return $this->state(fn (): array => [
|
||||
'attempt_count' => 1,
|
||||
'delivered_at' => Carbon::now(),
|
||||
'delivery_status' => NotificationDeliveryStatus::Succeeded,
|
||||
'error_excerpt' => null,
|
||||
'http_status' => 204,
|
||||
'last_attempted_at' => Carbon::now(),
|
||||
'response_excerpt' => 'No Content',
|
||||
]);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
return new class extends Migration
|
||||
{
|
||||
/**
|
||||
* Run the migrations.
|
||||
*/
|
||||
public function up(): void
|
||||
{
|
||||
Schema::create('notification_actions', function (Blueprint $table) {
|
||||
$table->id();
|
||||
$table->foreignId('project_id')->constrained()->cascadeOnDelete();
|
||||
$table->string('name');
|
||||
$table->boolean('enabled')->default(true);
|
||||
$table->string('event_type');
|
||||
$table->string('channel');
|
||||
$table->json('channel_configuration');
|
||||
$table->text('message_template')->nullable();
|
||||
$table->text('delivery_url');
|
||||
$table->timestamps();
|
||||
|
||||
$table->index(['project_id', 'event_type', 'enabled']);
|
||||
$table->index(['project_id', 'channel']);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverse the migrations.
|
||||
*/
|
||||
public function down(): void
|
||||
{
|
||||
Schema::dropIfExists('notification_actions');
|
||||
}
|
||||
};
|
||||
@@ -0,0 +1,41 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
return new class extends Migration
|
||||
{
|
||||
/**
|
||||
* Run the migrations.
|
||||
*/
|
||||
public function up(): void
|
||||
{
|
||||
Schema::create('notification_delivery_logs', function (Blueprint $table) {
|
||||
$table->id();
|
||||
$table->foreignId('notification_action_id')->constrained()->cascadeOnDelete();
|
||||
$table->string('event_type');
|
||||
$table->string('channel');
|
||||
$table->string('delivery_status');
|
||||
$table->unsignedInteger('attempt_count')->default(0);
|
||||
$table->unsignedSmallInteger('http_status')->nullable();
|
||||
$table->text('response_excerpt')->nullable();
|
||||
$table->text('error_excerpt')->nullable();
|
||||
$table->json('payload_metadata')->nullable();
|
||||
$table->timestamp('last_attempted_at')->nullable();
|
||||
$table->timestamp('delivered_at')->nullable();
|
||||
$table->timestamps();
|
||||
|
||||
$table->index(['notification_action_id', 'delivery_status']);
|
||||
$table->index(['notification_action_id', 'created_at']);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverse the migrations.
|
||||
*/
|
||||
public function down(): void
|
||||
{
|
||||
Schema::dropIfExists('notification_delivery_logs');
|
||||
}
|
||||
};
|
||||
@@ -16,6 +16,8 @@ class DatabaseSeeder extends Seeder
|
||||
UserSeeder::class,
|
||||
UserGitConnectionSeeder::class,
|
||||
ProjectSeeder::class,
|
||||
NotificationActionSeeder::class,
|
||||
NotificationDeliveryLogSeeder::class,
|
||||
RepositoryConnectionSeeder::class,
|
||||
BoardColumnSeeder::class,
|
||||
ProjectTicketMetadataSeeder::class,
|
||||
|
||||
@@ -0,0 +1,52 @@
|
||||
<?php
|
||||
|
||||
namespace Database\Seeders;
|
||||
|
||||
use App\Enums\NotificationChannel;
|
||||
use App\Enums\NotificationEventType;
|
||||
use App\Models\Project;
|
||||
use Illuminate\Database\Seeder;
|
||||
|
||||
class NotificationActionSeeder extends Seeder
|
||||
{
|
||||
/**
|
||||
* Run the database seeds.
|
||||
*/
|
||||
public function run(): void
|
||||
{
|
||||
$project = Project::query()->where('key', 'CORE')->first()
|
||||
?? Project::query()->orderBy('id')->first();
|
||||
|
||||
if ($project === null) {
|
||||
return;
|
||||
}
|
||||
|
||||
$project->notificationActions()->updateOrCreate(
|
||||
[
|
||||
'name' => 'Core Status Webhook',
|
||||
],
|
||||
[
|
||||
'channel' => NotificationChannel::Webhook,
|
||||
'channel_configuration' => [],
|
||||
'delivery_url' => 'https://hooks.example.test/kanbangit/core-status',
|
||||
'enabled' => true,
|
||||
'event_type' => NotificationEventType::TicketStatusChanged,
|
||||
'message_template' => null,
|
||||
],
|
||||
);
|
||||
|
||||
$project->notificationActions()->updateOrCreate(
|
||||
[
|
||||
'name' => 'Core Status Discord',
|
||||
],
|
||||
[
|
||||
'channel' => NotificationChannel::Discord,
|
||||
'channel_configuration' => [],
|
||||
'delivery_url' => 'https://discord.com/api/webhooks/123456/core-status',
|
||||
'enabled' => false,
|
||||
'event_type' => NotificationEventType::TicketStatusChanged,
|
||||
'message_template' => '$ticket_key moved from $status_from to $status_to: $url',
|
||||
],
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
<?php
|
||||
|
||||
namespace Database\Seeders;
|
||||
|
||||
use App\Models\NotificationAction;
|
||||
use App\Models\NotificationDeliveryLog;
|
||||
use Illuminate\Database\Seeder;
|
||||
|
||||
class NotificationDeliveryLogSeeder extends Seeder
|
||||
{
|
||||
/**
|
||||
* Run the database seeds.
|
||||
*/
|
||||
public function run(): void
|
||||
{
|
||||
$notificationAction = NotificationAction::query()->orderBy('id')->first();
|
||||
|
||||
if ($notificationAction === null || $notificationAction->deliveryLogs()->exists()) {
|
||||
return;
|
||||
}
|
||||
|
||||
NotificationDeliveryLog::factory()
|
||||
->forNotificationAction($notificationAction)
|
||||
->succeeded()
|
||||
->create([
|
||||
'payload_metadata' => [
|
||||
'project_key' => 'CORE',
|
||||
'ticket_key' => 'CORE-1',
|
||||
],
|
||||
]);
|
||||
|
||||
NotificationDeliveryLog::factory()
|
||||
->forNotificationAction($notificationAction)
|
||||
->retryable()
|
||||
->create([
|
||||
'payload_metadata' => [
|
||||
'project_key' => 'CORE',
|
||||
'ticket_key' => 'CORE-2',
|
||||
],
|
||||
]);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,65 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Feature\Models;
|
||||
|
||||
use App\Enums\NotificationChannel;
|
||||
use App\Enums\NotificationEventType;
|
||||
use App\Http\Resources\NotificationActionResource;
|
||||
use App\Models\NotificationAction;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Tests\TestCase;
|
||||
|
||||
class NotificationActionTest extends TestCase
|
||||
{
|
||||
use RefreshDatabase;
|
||||
|
||||
/**
|
||||
* Ensure webhook notification actions encrypt their delivery URL and cast enum fields.
|
||||
*/
|
||||
public function test_factory_creates_an_encrypted_webhook_notification_action(): void
|
||||
{
|
||||
$notificationAction = NotificationAction::factory()->webhook()->create([
|
||||
'delivery_url' => 'https://hooks.example.test/kanbangit/core-status',
|
||||
]);
|
||||
|
||||
$storedDeliveryUrl = DB::table('notification_actions')
|
||||
->where('id', $notificationAction->id)
|
||||
->value('delivery_url');
|
||||
|
||||
$this->assertNotSame('https://hooks.example.test/kanbangit/core-status', $storedDeliveryUrl);
|
||||
$this->assertSame('https://hooks.example.test/kanbangit/core-status', $notificationAction->fresh()->delivery_url);
|
||||
$this->assertSame(NotificationEventType::TicketStatusChanged, $notificationAction->fresh()->event_type);
|
||||
$this->assertSame(NotificationChannel::Webhook, $notificationAction->fresh()->channel);
|
||||
$this->assertSame([], $notificationAction->fresh()->channel_configuration);
|
||||
}
|
||||
|
||||
/**
|
||||
* Ensure the Discord factory state creates message-driven Discord actions.
|
||||
*/
|
||||
public function test_factory_can_create_discord_notification_actions(): void
|
||||
{
|
||||
$notificationAction = NotificationAction::factory()->discord()->create();
|
||||
|
||||
$this->assertSame(NotificationChannel::Discord, $notificationAction->fresh()->channel);
|
||||
$this->assertNotNull($notificationAction->fresh()->message_template);
|
||||
$this->assertStringStartsWith('https://discord.com/api/webhooks/', $notificationAction->fresh()->delivery_url);
|
||||
}
|
||||
|
||||
/**
|
||||
* Ensure notification actions belong to a project and keep secret URLs out of serialized output.
|
||||
*/
|
||||
public function test_notification_actions_belong_to_projects_and_hide_delivery_urls(): void
|
||||
{
|
||||
$notificationAction = NotificationAction::factory()->create();
|
||||
$resource = (new NotificationActionResource($notificationAction->fresh()))
|
||||
->toArray(Request::create('/'));
|
||||
|
||||
$this->assertTrue($notificationAction->project()->exists());
|
||||
$this->assertTrue($notificationAction->project->is($notificationAction->fresh()->project));
|
||||
$this->assertArrayNotHasKey('delivery_url', $notificationAction->fresh()->toArray());
|
||||
$this->assertArrayNotHasKey('delivery_url', $resource);
|
||||
$this->assertTrue($resource['has_delivery_url']);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,66 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Feature\Models;
|
||||
|
||||
use App\Enums\NotificationChannel;
|
||||
use App\Enums\NotificationDeliveryStatus;
|
||||
use App\Models\NotificationDeliveryLog;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Tests\TestCase;
|
||||
|
||||
class NotificationDeliveryLogTest extends TestCase
|
||||
{
|
||||
use RefreshDatabase;
|
||||
|
||||
/**
|
||||
* Ensure delivery log factories create related notification actions with consistent casts.
|
||||
*/
|
||||
public function test_delivery_log_factory_creates_a_related_notification_action(): void
|
||||
{
|
||||
$deliveryLog = NotificationDeliveryLog::factory()->create();
|
||||
|
||||
$this->assertTrue($deliveryLog->notificationAction()->exists());
|
||||
$this->assertTrue($deliveryLog->notificationAction->is($deliveryLog->fresh()->notificationAction));
|
||||
$this->assertSame(NotificationChannel::Webhook, $deliveryLog->fresh()->channel);
|
||||
$this->assertSame(NotificationDeliveryStatus::Pending, $deliveryLog->fresh()->delivery_status);
|
||||
}
|
||||
|
||||
/**
|
||||
* Ensure delivery logs can represent every supported delivery state.
|
||||
*/
|
||||
public function test_delivery_logs_support_pending_succeeded_failed_and_retryable_statuses(): void
|
||||
{
|
||||
NotificationDeliveryLog::factory()->pending()->create();
|
||||
NotificationDeliveryLog::factory()->succeeded()->create();
|
||||
NotificationDeliveryLog::factory()->failed()->create();
|
||||
NotificationDeliveryLog::factory()->retryable()->create();
|
||||
|
||||
$this->assertSame(
|
||||
[
|
||||
NotificationDeliveryStatus::Pending,
|
||||
NotificationDeliveryStatus::Succeeded,
|
||||
NotificationDeliveryStatus::Failed,
|
||||
NotificationDeliveryStatus::Retryable,
|
||||
],
|
||||
NotificationDeliveryLog::query()->orderBy('id')->get()->pluck('delivery_status')->all(),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Ensure delivery logs cast metadata and attempt information for later delivery tracking.
|
||||
*/
|
||||
public function test_delivery_logs_cast_metadata_and_attempt_information(): void
|
||||
{
|
||||
$deliveryLog = NotificationDeliveryLog::factory()->succeeded()->create([
|
||||
'payload_metadata' => [
|
||||
'project_key' => 'CORE',
|
||||
'ticket_key' => 'CORE-27',
|
||||
],
|
||||
])->fresh();
|
||||
|
||||
$this->assertSame('CORE-27', $deliveryLog->payload_metadata['ticket_key']);
|
||||
$this->assertSame(204, $deliveryLog->http_status);
|
||||
$this->assertNotNull($deliveryLog->last_attempted_at);
|
||||
$this->assertNotNull($deliveryLog->delivered_at);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,144 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Feature\Notifications;
|
||||
|
||||
use App\Actions\Notifications\DeleteProjectNotificationAction;
|
||||
use App\Actions\Notifications\ListProjectNotificationActions;
|
||||
use App\Actions\Notifications\StoreProjectNotificationAction;
|
||||
use App\Actions\Notifications\UpdateProjectNotificationAction;
|
||||
use App\Enums\NotificationChannel;
|
||||
use App\Enums\NotificationEventType;
|
||||
use App\Models\NotificationAction;
|
||||
use App\Models\Project;
|
||||
use Illuminate\Database\Eloquent\ModelNotFoundException;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Tests\TestCase;
|
||||
|
||||
class ProjectNotificationActionManagementTest extends TestCase
|
||||
{
|
||||
use RefreshDatabase;
|
||||
|
||||
/**
|
||||
* Ensure notification actions can be created and listed for a single project.
|
||||
*/
|
||||
public function test_notification_actions_can_be_created_and_listed_for_a_project(): void
|
||||
{
|
||||
$storeAction = app(StoreProjectNotificationAction::class);
|
||||
$listAction = app(ListProjectNotificationActions::class);
|
||||
$project = Project::factory()->create();
|
||||
$otherProject = Project::factory()->create();
|
||||
|
||||
$storeAction->handle($project, [
|
||||
'name' => 'Discord alerts',
|
||||
'enabled' => false,
|
||||
'event_type' => NotificationEventType::TicketStatusChanged,
|
||||
'channel' => NotificationChannel::Discord,
|
||||
'channel_configuration' => ['mentions' => ['ops']],
|
||||
'message_template' => '$ticket_key changed to $status_to',
|
||||
'delivery_url' => 'https://discord.com/api/webhooks/123456/discord-alerts',
|
||||
]);
|
||||
|
||||
$webhookAction = $storeAction->handle($project, [
|
||||
'name' => 'Webhook alerts',
|
||||
'enabled' => true,
|
||||
'event_type' => NotificationEventType::TicketStatusChanged,
|
||||
'channel' => NotificationChannel::Webhook,
|
||||
'channel_configuration' => ['source' => 'n8n'],
|
||||
'message_template' => null,
|
||||
'delivery_url' => 'https://hooks.example.test/kanbangit/webhook-alerts',
|
||||
]);
|
||||
|
||||
NotificationAction::factory()->for($otherProject)->create([
|
||||
'name' => 'Foreign project webhook',
|
||||
]);
|
||||
|
||||
$listedActions = $listAction->handle($project);
|
||||
|
||||
$this->assertModelExists($webhookAction);
|
||||
$this->assertCount(2, $listedActions);
|
||||
$this->assertSame(['Discord alerts', 'Webhook alerts'], $listedActions->pluck('name')->all());
|
||||
$this->assertSame(['mentions' => ['ops']], $listedActions->first()->channel_configuration);
|
||||
}
|
||||
|
||||
/**
|
||||
* Ensure notification actions can be updated without overwriting their saved secret URL.
|
||||
*/
|
||||
public function test_notification_actions_can_be_updated_without_replacing_the_existing_delivery_url(): void
|
||||
{
|
||||
$updateAction = app(UpdateProjectNotificationAction::class);
|
||||
$project = Project::factory()->create();
|
||||
$notificationAction = NotificationAction::factory()->discord()->for($project)->create([
|
||||
'delivery_url' => 'https://discord.com/api/webhooks/123456/original-secret',
|
||||
'message_template' => '$ticket_key moved to $status_to',
|
||||
]);
|
||||
|
||||
$updatedAction = $updateAction->handle($project, $notificationAction, [
|
||||
'enabled' => false,
|
||||
'message_template' => '$ticket_key moved from $status_from to $status_to',
|
||||
'channel_configuration' => ['mentions' => ['support']],
|
||||
'delivery_url' => '',
|
||||
]);
|
||||
|
||||
$this->assertFalse($updatedAction->enabled);
|
||||
$this->assertSame(
|
||||
'$ticket_key moved from $status_from to $status_to',
|
||||
$updatedAction->message_template,
|
||||
);
|
||||
$this->assertSame(['mentions' => ['support']], $updatedAction->channel_configuration);
|
||||
$this->assertSame(
|
||||
'https://discord.com/api/webhooks/123456/original-secret',
|
||||
$updatedAction->delivery_url,
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Ensure notification actions can replace their delivery URL when a new secret is submitted.
|
||||
*/
|
||||
public function test_notification_actions_can_replace_delivery_urls(): void
|
||||
{
|
||||
$updateAction = app(UpdateProjectNotificationAction::class);
|
||||
$project = Project::factory()->create();
|
||||
$notificationAction = NotificationAction::factory()->for($project)->create([
|
||||
'delivery_url' => 'https://hooks.example.test/kanbangit/original-secret',
|
||||
]);
|
||||
|
||||
$updatedAction = $updateAction->handle($project, $notificationAction, [
|
||||
'delivery_url' => 'https://hooks.example.test/kanbangit/replaced-secret',
|
||||
'name' => 'Primary webhook',
|
||||
]);
|
||||
|
||||
$this->assertSame('Primary webhook', $updatedAction->name);
|
||||
$this->assertSame('https://hooks.example.test/kanbangit/replaced-secret', $updatedAction->delivery_url);
|
||||
}
|
||||
|
||||
/**
|
||||
* Ensure notification actions can be deleted from their owning project.
|
||||
*/
|
||||
public function test_notification_actions_can_be_deleted_from_their_project(): void
|
||||
{
|
||||
$deleteAction = app(DeleteProjectNotificationAction::class);
|
||||
$project = Project::factory()->create();
|
||||
$notificationAction = NotificationAction::factory()->for($project)->create();
|
||||
|
||||
$deleteAction->handle($project, $notificationAction);
|
||||
|
||||
$this->assertModelMissing($notificationAction);
|
||||
}
|
||||
|
||||
/**
|
||||
* Ensure project-scoped actions reject notification actions from other projects.
|
||||
*/
|
||||
public function test_project_scoped_management_rejects_foreign_notification_actions(): void
|
||||
{
|
||||
$updateAction = app(UpdateProjectNotificationAction::class);
|
||||
$project = Project::factory()->create();
|
||||
$otherProject = Project::factory()->create();
|
||||
$notificationAction = NotificationAction::factory()->for($otherProject)->create();
|
||||
|
||||
$this->expectException(ModelNotFoundException::class);
|
||||
|
||||
$updateAction->handle($project, $notificationAction, [
|
||||
'name' => 'Should not update',
|
||||
]);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user