Exige motivo na reversão territorial e prepara novo plano

This commit is contained in:
2026-10-09 02:15:36 +02:00
parent 6a71aa47e3
commit 6c7a9c9442
9 changed files with 129 additions and 11 deletions
@@ -37,7 +37,12 @@ class InvestmentTerritorialTraceability
$last = DB::table('audit_events')->where('source_system', 'territory-traceability-v2')->where('after_data->plan_hash', $hash)->orderByDesc('occurred_at')->orderByDesc('id')->first();
return ['hash' => $hash, 'plan' => $plan, 'code_matches' => ($plan['code_hash'] ?? '') === $this->fingerprint(),
'applied' => $last?->action === 'traceability_applied', 'backup_ready' => $this->backupReady($plan['target'], $hash)];
'applied' => $last?->action === 'traceability_applied', 'reversed' => $last?->action === 'traceability_reversed',
'history' => DB::table('audit_events')->where('source_system', 'territory-traceability-v2')->orderByDesc('occurred_at')->limit(20)->get(['id', 'action', 'actor_user_id', 'occurred_at', 'after_data'])->map(function ($event) {
$data = json_decode($event->after_data, true);
return ['id' => $event->id, 'action' => $event->action, 'actor_user_id' => $event->actor_user_id, 'occurred_at' => $event->occurred_at, 'reason' => $data['reason'] ?? null];
})->all(), 'backup_ready' => $this->backupReady($plan['target'], $hash)];
}
/** @param array<string,mixed> $target */
@@ -67,8 +72,9 @@ class InvestmentTerritorialTraceability
}
/** @return array<string,mixed> */
public function apply(string $hash, int $actor, bool $reverse = false): array
public function apply(string $hash, int $actor, bool $reverse = false, ?string $reason = null): array
{
abort_if($reverse && trim($reason ?? '') === '', 422, 'Indique o motivo da reversão.');
$preview = $this->preview();
abort_unless($preview && hash_equals($preview['hash'], $hash) && $preview['code_matches'], 409, 'Plano ou código alterado. Reveja a pré-visualização.');
$plan = $preview['plan'];
@@ -79,7 +85,7 @@ class InvestmentTerritorialTraceability
}
abort_unless($preview['backup_ready'], 422, 'Backup/restauro/reversão verificados e atualizados são necessários.');
return DB::transaction(function () use ($plan, $hash, $actor, $reverse) {
return DB::transaction(function () use ($plan, $hash, $actor, $reverse, $reason) {
DB::select('SELECT pg_advisory_xact_lock(81026, 160000)');
$last = DB::table('audit_events')->where('source_system', 'territory-traceability-v2')->where('after_data->plan_hash', $hash)->orderByDesc('occurred_at')->orderByDesc('id')->lockForUpdate()->first();
if ((! $reverse && $last?->action === 'traceability_applied') || ($reverse && $last?->action === 'traceability_reversed')) {
@@ -96,6 +102,7 @@ class InvestmentTerritorialTraceability
$values = $update['before'];
$values['version_no'] = $update['after']['version_no'] + 1;
DB::table('cluster_projects')->where('id', $update['id'])->update($values);
$before[] = ['id' => $update['id'], 'before' => $update['after'], 'after' => $values];
}
} else {
foreach ($plan['guards'] as $guard) {
@@ -111,7 +118,7 @@ class InvestmentTerritorialTraceability
}
DB::table('audit_events')->insert(['source_system' => 'territory-traceability-v2', 'entity_type' => 'territorial_sources', 'actor_user_id' => $actor,
'action' => $reverse ? 'traceability_reversed' : 'traceability_applied', 'data_classification' => 'internal', 'occurred_at' => DB::raw('clock_timestamp()'),
'after_data' => json_encode(['plan_hash' => $hash, 'rows' => $plan['rows'], 'updates' => $before, 'no_new_geography' => true, 'official_policy' => 'accepted_without_revalidation'], JSON_THROW_ON_ERROR)]);
'after_data' => json_encode(['plan_hash' => $hash, 'rows' => $plan['rows'], 'updates' => $before, 'reason' => $reverse ? trim($reason) : null, 'no_new_geography' => true, 'official_policy' => 'accepted_without_revalidation'], JSON_THROW_ON_ERROR)]);
return ['replayed' => false, 'changed' => $reverse ? count($applied['updates']) : count($before), 'associations' => count($plan['rows'])];
});