TenantAtlas/database/seeders/PoliciesSeeder.php
ahmido 03127a670b Spec 096: Ops polish (assignment summaries + dedupe + reconcile tracking + seed DX) (#115)
Implements Spec 096 ops polish bundle:

- Persist durable OperationRun.summary_counts for assignment fetch/restore (final attempt wins)
- Server-side dedupe for assignment jobs (15-minute cooldown + non-canonical skip)
- Track ReconcileAdapterRunsJob via workspace-scoped OperationRun + stable failure codes + overlap prevention
- Seed DX: ensure seeded tenants use UUID v4 external_id and seed satisfies workspace_id NOT NULL constraints

Verification (local / evidence-based):
- `vendor/bin/sail artisan test --compact tests/Feature/Operations/AssignmentRunSummaryCountsTest.php tests/Feature/Operations/AssignmentJobDedupeTest.php tests/Feature/Operations/ReconcileAdapterRunsJobTrackingTest.php tests/Feature/Seed/PoliciesSeederExternalIdTest.php`
- `vendor/bin/sail bin pint --dirty`

Spec artifacts included under `specs/096-ops-polish-assignment-dedupe-system-tracking/` (spec/plan/tasks/checklists).

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

73 lines
2.2 KiB
PHP

<?php
namespace Database\Seeders;
use App\Models\Policy;
use App\Models\Tenant;
use App\Models\Workspace;
use Illuminate\Database\Seeder;
use Illuminate\Support\Str;
class PoliciesSeeder extends Seeder
{
public function run(): void
{
$seedTenantId = env('INTUNE_TENANT_ID', 'local-tenant');
$workspace = Workspace::query()->firstOrCreate(
['slug' => 'default'],
['name' => 'Default Workspace', 'slug' => 'default'],
);
$tenant = Tenant::firstOrCreate([
'tenant_id' => $seedTenantId,
], [
'workspace_id' => (int) $workspace->getKey(),
'name' => 'Default Tenant',
'external_id' => (string) Str::uuid(),
'domain' => null,
'metadata' => [],
]);
if ($tenant->workspace_id === null) {
$tenant->forceFill([
'workspace_id' => (int) $workspace->getKey(),
])->saveQuietly();
}
$externalId = (string) ($tenant->external_id ?? '');
$isUuidV4 = Str::isUuid($externalId) && substr($externalId, 14, 1) === '4';
if (! $isUuidV4) {
$tenant->forceFill([
'external_id' => (string) Str::uuid(),
])->saveQuietly();
}
$supported = config('tenantpilot.supported_policy_types', []);
$now = now();
foreach ($supported as $index => $type) {
$policyType = $type['type'];
$platform = $type['platform'] ?? null;
$externalId = 'seed-'.$policyType.'-'.($platform ?? 'any').'-'.$index;
Policy::updateOrCreate(
[
'workspace_id' => (int) $workspace->getKey(),
'tenant_id' => $tenant->id,
'external_id' => $externalId,
'policy_type' => $policyType,
],
[
'workspace_id' => (int) $workspace->getKey(),
'display_name' => $type['label'] ?? ucfirst($policyType),
'platform' => $platform,
'last_synced_at' => $now,
'metadata' => ['seeded' => true],
]
);
}
}
}