TenantAtlas/app/Services/Drift/DriftRunSelector.php
2026-02-12 13:39:24 +01:00

48 lines
1.3 KiB
PHP

<?php
namespace App\Services\Drift;
use App\Models\OperationRun;
use App\Models\Tenant;
use App\Support\OperationRunOutcome;
use App\Support\OperationRunStatus;
class DriftRunSelector
{
/**
* @return array{baseline:OperationRun,current:OperationRun}|null
*/
public function selectBaselineAndCurrent(Tenant $tenant, string $scopeKey): ?array
{
$runs = OperationRun::query()
->where('tenant_id', $tenant->getKey())
->where('type', 'inventory_sync')
->where('status', OperationRunStatus::Completed->value)
->whereIn('outcome', [
OperationRunOutcome::Succeeded->value,
OperationRunOutcome::PartiallySucceeded->value,
])
->where('context->selection_hash', $scopeKey)
->whereNotNull('completed_at')
->orderByDesc('completed_at')
->limit(2)
->get();
if ($runs->count() < 2) {
return null;
}
$current = $runs->first();
$baseline = $runs->last();
if (! $baseline instanceof OperationRun || ! $current instanceof OperationRun) {
return null;
}
return [
'baseline' => $baseline,
'current' => $current,
];
}
}