143 lines
4.8 KiB
PHP
143 lines
4.8 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 OnboardingConnectionDiagnosticsJob 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.');
|
|
}
|
|
|
|
$status = (string) ($connection->status ?? 'unknown');
|
|
$health = (string) ($connection->health_status ?? 'unknown');
|
|
|
|
$evidenceStatus = 'unknown';
|
|
$reasonCode = null;
|
|
$message = 'No health check data available yet.';
|
|
|
|
if ($status !== 'connected') {
|
|
$evidenceStatus = 'blocked';
|
|
$reasonCode = 'provider.needs_consent';
|
|
$message = 'Provider connection is not connected. Admin consent may be required.';
|
|
} elseif ($health === 'healthy') {
|
|
$evidenceStatus = 'ok';
|
|
$message = 'Provider connection appears healthy.';
|
|
} elseif ($health === 'unhealthy') {
|
|
$evidenceStatus = 'error';
|
|
$reasonCode = is_string($connection->last_error_reason_code) ? $connection->last_error_reason_code : 'provider.outage';
|
|
$message = is_string($connection->last_error_message) && trim($connection->last_error_message) !== ''
|
|
? $connection->last_error_message
|
|
: 'Provider connection health check indicates an error.';
|
|
}
|
|
|
|
$evidence->record(
|
|
tenant: $tenant,
|
|
taskType: OnboardingTaskType::ConnectionDiagnostics,
|
|
status: $evidenceStatus,
|
|
reasonCode: $reasonCode,
|
|
message: $message,
|
|
payload: [
|
|
'status' => $status,
|
|
'health_status' => $health,
|
|
'last_health_check_at' => $connection->last_health_check_at?->toIso8601String(),
|
|
'last_error_reason_code' => $connection->last_error_reason_code,
|
|
],
|
|
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.connection.diagnostics.failed',
|
|
'reason_code' => $reasonCode ?? 'connection.diagnostics.unknown',
|
|
'message' => $message,
|
|
]],
|
|
);
|
|
}
|
|
}
|