TenantAtlas/database/factories/AlertDeliveryFactory.php
ahmido 3ed275cef3 feat(alerts): Monitoring cluster + v1 resources (spec 099) (#121)
Implements spec `099-alerts-v1-teams-email`.

- Monitoring navigation: Alerts as a cluster under Monitoring; default landing is Alert deliveries.
- Tenant panel: Alerts points to `/admin/alerts` and the cluster navigation is hidden in tenant panel.
- Guard compliance: removes direct `Gate::` usage from Alert resources so `NoAdHocFilamentAuthPatternsTest` passes.

Verification:
- Full suite: `1348 passed, 7 skipped` (EXIT=0).

Co-authored-by: Ahmed Darrazi <ahmed.darrazi@live.de>
Reviewed-on: #121
2026-02-18 15:20:43 +00:00

79 lines
2.7 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' => 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',
],
];
}
}