ultimatepos/Modules/AssetExchange/Console/AssetExchangeSmokeCheck.php

115 lines
3.8 KiB
PHP

<?php
namespace Modules\AssetExchange\Console;
use App\Business;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Schema;
use Modules\AssetExchange\Models\Appraisal;
use Modules\AssetExchange\Models\PriceInquiry;
use Modules\AssetExchange\Services\AppraisalService;
use Modules\AssetExchange\Services\InquirySlaService;
use Spatie\Permission\Models\Permission;
use Spatie\Permission\Models\Role;
class AssetExchangeSmokeCheck extends Command
{
protected $signature = 'pos:assetexchangeSmoke {business_id?}';
protected $description = 'Quick smoke checks for AssetExchange module';
public function __construct(
protected InquirySlaService $slaService,
protected AppraisalService $appraisalService
) {
parent::__construct();
}
public function handle(): int
{
$businessId = (int) ($this->argument('business_id') ?: 0);
if ($businessId <= 0) {
$businessId = (int) Business::query()->value('id');
}
if ($businessId <= 0) {
$this->error('No business found.');
return self::FAILURE;
}
$business = Business::find($businessId);
if (! $business) {
$this->error("Business {$businessId} not found.");
return self::FAILURE;
}
$ok = true;
$this->info("AssetExchange smoke check for business #{$businessId}");
$requiredTables = [
'aex_listings',
'aex_appraisals',
'aex_price_inquiries',
'aex_notifications',
'aex_research_tasks',
];
foreach ($requiredTables as $table) {
$exists = Schema::hasTable($table);
$this->line(($exists ? '[OK] ' : '[FAIL] ')."table {$table}");
$ok = $ok && $exists;
}
$permCount = Permission::where('name', 'like', 'aex.%')->count();
$this->line(($permCount > 0 ? '[OK] ' : '[FAIL] ')."permissions aex.* = {$permCount}");
$ok = $ok && ($permCount > 0);
$adminRole = Role::where('name', 'like', 'Admin#%')->first();
if ($adminRole) {
$hasAccess = $adminRole->hasPermissionTo('aex.access');
$this->line(($hasAccess ? '[OK] ' : '[FAIL] ')."role {$adminRole->name} has aex.access");
$ok = $ok && $hasAccess;
} else {
$this->warn('[WARN] no Admin# role found');
}
$sla = $this->slaService->snapshot($business);
$this->line('[OK] SLA snapshot: breached='.$sla['inquiry_breached'].' warning='.$sla['inquiry_warning'].' overdue_due='.$sla['inquiry_overdue_due']);
$inquiry = PriceInquiry::where('business_id', $businessId)->with('recipients')->latest('id')->first();
if ($inquiry) {
$status = $this->slaService->inquirySlaStatus($inquiry);
$this->line("[OK] inquiry {$inquiry->inquiry_code} SLA status={$status}");
} else {
$this->warn('[WARN] no inquiries found for SLA sample');
}
$appraisal = Appraisal::where('business_id', $businessId)->latest('id')->first();
if ($appraisal) {
try {
$bytes = strlen($this->appraisalService->renderReportPdf($appraisal));
$this->line(($bytes > 0 ? '[OK] ' : '[FAIL] ')."appraisal PDF rendered bytes={$bytes}");
$ok = $ok && ($bytes > 0);
} catch (\Throwable $e) {
$this->error('[FAIL] appraisal PDF render error: '.$e->getMessage());
$ok = false;
}
} else {
$this->warn('[WARN] no appraisals found for PDF sample');
}
$this->newLine();
if ($ok) {
$this->info('AssetExchange smoke check passed.');
return self::SUCCESS;
}
$this->error('AssetExchange smoke check failed.');
return self::FAILURE;
}
}