TenantAtlas/database/factories/AlertDeliveryFactory.php
ahmido d49d33ac27 feat(alerts): test message + last test status + deep links (#122)
Implements feature 100 (Alert Targets):

- US1: “Send test message” action (RBAC + confirmation + rate limit + audit + async job)
- US2: Derived “Last test” status badge (Never/Sent/Failed/Pending) on view + edit surfaces
- US3: “View last delivery” deep link + deliveries viewer filters (event_type, destination) incl. tenantless test deliveries

Tests:
- Full suite green (1348 passed, 7 skipped)
- Added focused feature tests for send test, last test resolver/badges, and deep-link filters

Co-authored-by: Ahmed Darrazi <ahmed.darrazi@live.de>
Reviewed-on: #122
2026-02-18 23:12:38 +00:00

99 lines
3.5 KiB
PHP

<?php
namespace Database\Factories;
use App\Models\AlertDelivery;
use App\Models\AlertDestination;
use App\Models\AlertRule;
use App\Models\Tenant;
use App\Models\Workspace;
use Illuminate\Database\Eloquent\Factories\Factory;
/**
* @extends Factory<AlertDelivery>
*/
class AlertDeliveryFactory extends Factory
{
protected $model = AlertDelivery::class;
public function definition(): array
{
return [
// tenant_id and alert_rule_id are nullable; test() state sets them to null.
// Default definition creates a tenant-scoped delivery.
'tenant_id' => Tenant::factory(),
'workspace_id' => function (array $attributes): int {
$tenantId = $attributes['tenant_id'] ?? null;
if (! is_numeric($tenantId)) {
return (int) Workspace::factory()->create()->getKey();
}
$tenant = Tenant::query()->whereKey((int) $tenantId)->first();
if (! $tenant instanceof Tenant) {
return (int) Workspace::factory()->create()->getKey();
}
if ($tenant->workspace_id === null) {
$workspaceId = (int) Workspace::factory()->create()->getKey();
$tenant->forceFill(['workspace_id' => $workspaceId])->save();
return $workspaceId;
}
return (int) $tenant->workspace_id;
},
'alert_rule_id' => function (array $attributes): int {
$workspaceId = is_numeric($attributes['workspace_id'] ?? null)
? (int) $attributes['workspace_id']
: (int) Workspace::factory()->create()->getKey();
return (int) AlertRule::factory()->create([
'workspace_id' => $workspaceId,
])->getKey();
},
'alert_destination_id' => function (array $attributes): int {
$workspaceId = is_numeric($attributes['workspace_id'] ?? null)
? (int) $attributes['workspace_id']
: (int) Workspace::factory()->create()->getKey();
return (int) AlertDestination::factory()->create([
'workspace_id' => $workspaceId,
])->getKey();
},
'event_type' => AlertRule::EVENT_HIGH_DRIFT,
'severity' => 'high',
'status' => AlertDelivery::STATUS_QUEUED,
'fingerprint_hash' => hash('sha256', fake()->uuid()),
'send_after' => null,
'attempt_count' => 0,
'last_error_code' => null,
'last_error_message' => null,
'sent_at' => null,
'payload' => [
'title' => 'Alert',
'body' => 'Delivery payload',
],
];
}
/**
* A tenantless test delivery for a specific destination.
*/
public function test(): static
{
return $this->state(fn (array $attributes): array => [
'tenant_id' => null,
'alert_rule_id' => null,
'event_type' => AlertDelivery::EVENT_TYPE_TEST,
'severity' => null,
'fingerprint_hash' => 'test:'.($attributes['alert_destination_id'] instanceof \Closure ? 0 : ($attributes['alert_destination_id'] ?? 0)),
'payload' => [
'title' => 'Test alert',
'body' => 'This is a test delivery for destination verification.',
],
]);
}
}