🚀 مُصادَق: تحديث برمجي جديد 2026-05-03 13:39
This commit is contained in:
56
queue/Jobs/RiskAnalysisJob.php
Normal file
56
queue/Jobs/RiskAnalysisJob.php
Normal file
@@ -0,0 +1,56 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Queue\Jobs;
|
||||
|
||||
use App\Services\RiskAnalysisService;
|
||||
use App\Core\Database;
|
||||
use Throwable;
|
||||
|
||||
final class RiskAnalysisJob
|
||||
{
|
||||
public function __construct(
|
||||
private readonly RiskAnalysisService $riskService
|
||||
) {}
|
||||
|
||||
public function handle(array $payload): void
|
||||
{
|
||||
$companyId = $payload['company_id'];
|
||||
$tenantId = $payload['tenant_id'];
|
||||
|
||||
try {
|
||||
$analysis = $this->riskService->calculateCompanyRiskScore($companyId);
|
||||
|
||||
// Store or update risk score
|
||||
$db = Database::getInstance();
|
||||
|
||||
$stmt = $db->prepare("SELECT id FROM risk_scores WHERE company_id = ? LIMIT 1");
|
||||
$stmt->execute([$companyId]);
|
||||
$existing = $stmt->fetch();
|
||||
|
||||
if ($existing) {
|
||||
$stmt = $db->prepare("UPDATE risk_scores SET risk_level = ?, score = ?, factors = ?, calculated_at = NOW() WHERE company_id = ?");
|
||||
$stmt->execute([
|
||||
$analysis['level'],
|
||||
$analysis['score'],
|
||||
json_encode($analysis['factors'], JSON_UNESCAPED_UNICODE),
|
||||
$companyId
|
||||
]);
|
||||
} else {
|
||||
$stmt = $db->prepare("INSERT INTO risk_scores (id, tenant_id, company_id, risk_level, score, factors, calculated_at) VALUES (?, ?, ?, ?, ?, ?, NOW())");
|
||||
$stmt->execute([
|
||||
\Ramsey\Uuid\Uuid::uuid4()->toString(),
|
||||
$tenantId,
|
||||
$companyId,
|
||||
$analysis['level'],
|
||||
$analysis['score'],
|
||||
json_encode($analysis['factors'], JSON_UNESCAPED_UNICODE)
|
||||
]);
|
||||
}
|
||||
} catch (Throwable $e) {
|
||||
echo "[!] Risk Analysis failed for company {$companyId}: " . $e->getMessage() . "\n";
|
||||
throw $e;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user