user(); if (! $user instanceof User) { abort(403, 'Not allowed'); } $latestSuccessful = InventorySyncRun::query() ->where('tenant_id', $tenant->getKey()) ->where('status', InventorySyncRun::STATUS_SUCCESS) ->whereNotNull('finished_at') ->orderByDesc('finished_at') ->first(); if (! $latestSuccessful instanceof InventorySyncRun) { return; } $scopeKey = (string) $latestSuccessful->selection_hash; $selector = app(DriftRunSelector::class); $comparison = $selector->selectBaselineAndCurrent($tenant, $scopeKey); if ($comparison === null) { return; } $baseline = $comparison['baseline']; $current = $comparison['current']; $exists = Finding::query() ->where('tenant_id', $tenant->getKey()) ->where('finding_type', Finding::FINDING_TYPE_DRIFT) ->where('scope_key', $scopeKey) ->where('baseline_run_id', $baseline->getKey()) ->where('current_run_id', $current->getKey()) ->exists(); if ($exists) { return; } GenerateDriftFindingsJob::dispatch( tenantId: (int) $tenant->getKey(), userId: (int) $user->getKey(), baselineRunId: (int) $baseline->getKey(), currentRunId: (int) $current->getKey(), scopeKey: $scopeKey, ); } public function getFindingsUrl(): string { return FindingResource::getUrl('index', tenant: Tenant::current()); } }