126 lines
3.9 KiB
PHP
126 lines
3.9 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Jobs\Onboarding;
|
|
|
|
use App\Jobs\Middleware\TrackOperationRun;
|
|
use App\Models\OnboardingSession;
|
|
use App\Models\OperationRun;
|
|
use App\Models\ProviderConnection;
|
|
use App\Models\Tenant;
|
|
use App\Models\User;
|
|
use App\Services\Onboarding\OnboardingEvidenceWriter;
|
|
use App\Services\OperationRunService;
|
|
use App\Support\Onboarding\OnboardingTaskType;
|
|
use App\Support\OperationRunOutcome;
|
|
use App\Support\OperationRunStatus;
|
|
use Illuminate\Bus\Queueable;
|
|
use Illuminate\Contracts\Queue\ShouldQueue;
|
|
use Illuminate\Foundation\Bus\Dispatchable;
|
|
use Illuminate\Queue\InteractsWithQueue;
|
|
use Illuminate\Queue\SerializesModels;
|
|
use RuntimeException;
|
|
|
|
class OnboardingInitialSyncJob implements ShouldQueue
|
|
{
|
|
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
|
|
|
|
public ?OperationRun $operationRun = null;
|
|
|
|
public function __construct(
|
|
public int $tenantId,
|
|
public int $userId,
|
|
public int $providerConnectionId,
|
|
public int $onboardingSessionId,
|
|
?OperationRun $operationRun = null,
|
|
) {
|
|
$this->operationRun = $operationRun;
|
|
}
|
|
|
|
/**
|
|
* @return array<int, object>
|
|
*/
|
|
public function middleware(): array
|
|
{
|
|
return [new TrackOperationRun];
|
|
}
|
|
|
|
public function handle(OnboardingEvidenceWriter $evidence, OperationRunService $runs): void
|
|
{
|
|
$tenant = Tenant::query()->find($this->tenantId);
|
|
if (! $tenant instanceof Tenant) {
|
|
throw new RuntimeException('Tenant not found.');
|
|
}
|
|
|
|
$user = User::query()->find($this->userId);
|
|
if (! $user instanceof User) {
|
|
throw new RuntimeException('User not found.');
|
|
}
|
|
|
|
$session = OnboardingSession::query()
|
|
->where('tenant_id', $tenant->getKey())
|
|
->find($this->onboardingSessionId);
|
|
|
|
if (! $session instanceof OnboardingSession) {
|
|
throw new RuntimeException('OnboardingSession not found.');
|
|
}
|
|
|
|
$connection = ProviderConnection::query()
|
|
->where('tenant_id', $tenant->getKey())
|
|
->find($this->providerConnectionId);
|
|
|
|
if (! $connection instanceof ProviderConnection) {
|
|
throw new RuntimeException('ProviderConnection not found.');
|
|
}
|
|
|
|
$connected = (string) ($connection->status ?? 'unknown') === 'connected';
|
|
|
|
$evidenceStatus = $connected ? 'ok' : 'blocked';
|
|
$reasonCode = $connected ? null : 'provider.not_connected';
|
|
$message = $connected
|
|
? 'Prerequisites for initial sync look good.'
|
|
: 'Provider connection is not connected. Resolve consent/credentials first.';
|
|
|
|
$evidence->record(
|
|
tenant: $tenant,
|
|
taskType: OnboardingTaskType::InitialSync,
|
|
status: $evidenceStatus,
|
|
reasonCode: $reasonCode,
|
|
message: $message,
|
|
payload: [
|
|
'provider_connection_status' => $connection->status,
|
|
],
|
|
session: $session,
|
|
providerConnection: $connection,
|
|
operationRun: $this->operationRun,
|
|
recordedBy: $user,
|
|
);
|
|
|
|
if (! $this->operationRun instanceof OperationRun) {
|
|
return;
|
|
}
|
|
|
|
if ($evidenceStatus === 'ok') {
|
|
$runs->updateRun(
|
|
$this->operationRun,
|
|
status: OperationRunStatus::Completed->value,
|
|
outcome: OperationRunOutcome::Succeeded->value,
|
|
);
|
|
|
|
return;
|
|
}
|
|
|
|
$runs->updateRun(
|
|
$this->operationRun,
|
|
status: OperationRunStatus::Completed->value,
|
|
outcome: OperationRunOutcome::Failed->value,
|
|
failures: [[
|
|
'code' => 'onboarding.initial_sync.blocked',
|
|
'reason_code' => $reasonCode ?? 'initial_sync.unknown',
|
|
'message' => $message,
|
|
]],
|
|
);
|
|
}
|
|
}
|