Files
Investidor-V2/app/Services/InvestorContextConsumers.php
nala cf003c8623
tests / ci (push) Canceled after 0s
versao nrgocios 4
2026-10-18 15:57:06 +02:00

61 lines
2.3 KiB
PHP

<?php
namespace App\Services;
use App\Models\User;
use Illuminate\Support\Facades\DB;
final class InvestorContextConsumers
{
public function requestRevision(\stdClass $row): int
{
$desk = app(VirtualDesk::class);
if ($row->submitted_snapshot) {
return (int) ($desk->decode($row->submitted_snapshot)['investment_context']['revision'] ?? 0);
}
foreach (DB::table('desk_events')->where('request_id', $row->id)->whereIn('action', ['draft_created', 'request_context_reviewed'])->orderByDesc('event_order')->get(['payload']) as $e) {
$p = $desk->decode($e->payload);
if (isset($p['investment_context_revision'])) {
return (int) $p['investment_context_revision'];
}
}
return 0;
}
public function landRevision(string $id): int
{
foreach (DB::table('land_assistance_events')->where('case_id', $id)->orderByDesc('id')->get(['metadata']) as $e) {
$p = json_decode($e->metadata ?? '{}', true);
if (isset($p['investment_context_revision'])) {
return (int) $p['investment_context_revision'];
}
}
return 0;
}
public function submissionRevision(string $id): int
{
foreach (DB::table('audit_events')->where('source_system', 'investment-submission')->where('entity_id', $id)->orderByRaw("coalesce((after_data->>'version')::integer,0) desc")->orderByDesc('created_at')->orderByDesc('id')->get(['after_data']) as $e) {
$p = json_decode($e->after_data ?? '{}', true);
if (isset($p['investment_context_revision'])) {
return (int) $p['investment_context_revision'];
}
}
return 0;
}
public function assertReviewed(User $actor, string $id, array $input, int $bound): int
{
$case = app(VirtualDesk::class)->case($actor, $id, true, true);
$current = app(InvestorContext::class)->state($case)['revision'];
if ($current !== $bound) {
abort_unless(filter_var($input['context_reviewed'] ?? false, FILTER_VALIDATE_BOOLEAN) && (int) ($input['context_revision'] ?? -1) === $current, 422, 'O contexto do investimento mudou. Confira os campos preservados ou aplique a nova seleção e confirme a revisão antes de submeter.');
}
return $current;
}
}