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',
]);
}
}