TenantAtlas/database/factories/AlertDeliveryFactory.php
2026-02-18 15:25:14 +01: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',
],
];
}
}