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,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');
}
};
+2
View File
@@ -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',
],
]);
}
}