Files
Investidor-V2/tools/record-territorial-owner-acceptance.php
T
2026-09-29 08:41:57 +02:00

74 lines
3.1 KiB
PHP

<?php
use Illuminate\Contracts\Console\Kernel;
use Illuminate\Support\Facades\DB;
require __DIR__.'/../vendor/autoload.php';
$app = require __DIR__.'/../bootstrap/app.php';
$app->make(Kernel::class)->bootstrap();
if (! $app->environment('local') || DB::connection()->getDriverName() !== 'pgsql') {
throw new RuntimeException('This recorded instruction applies to the local PostgreSQL installation only.');
}
// Specific owner instruction received in this workspace; never impersonate an application user.
$directory = storage_path('app/private/geo-ine-review-20260928-v2');
$expected = [
'original.pdf' => '5c69b6f189a1d3f5307e0cf7dd8224554705158a682637d3105afad615db26e2',
'layout.txt' => '783a7886ed5a27b8f9d0063a787928e9a4b09723c28a260c019c2de215a670a3',
];
foreach ($expected as $file => $hash) {
if (! is_file($directory.'/'.$file) || hash_file('sha256', $directory.'/'.$file) !== $hash) {
throw new RuntimeException('Source differs from the reviewed package: '.$file);
}
}
$receipt = [
'schema_version' => 1,
'decision_date' => '2026-09-28',
'decision' => 'accepted_by_owner',
'validation' => 'declared_validated_by_owner',
'authenticity' => 'declared_authenticated_by_owner',
'approval_basis' => 'explicit_workspace_user_instruction',
'instruction' => 'aplicar os resultados e aceite os dados como validados e autenticados',
'source_checksums' => $expected,
'scope' => 'INE PDF and extraction package geo-ine-review-20260928-v2',
'independent_authentication_performed' => false,
'technical_mapping_status' => 'unresolved_column_alignment',
'territories_imported_by_this_decision' => 0,
];
$json = json_encode($receipt, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_THROW_ON_ERROR).PHP_EOL;
$hash = hash('sha256', $json);
$path = $directory.'/owner-acceptance-'.$hash.'.json';
if (is_file($path)) {
if (file_get_contents($path) !== $json) {
throw new RuntimeException('Existing acceptance receipt differs.');
}
} else {
// Exclusive creation preserves any existing evidence.
$stream = fopen($path, 'x');
if ($stream === false) {
throw new RuntimeException('Could not create acceptance receipt.');
}
try {
if (fwrite($stream, $json) !== strlen($json)) {
throw new RuntimeException('Incomplete acceptance receipt.');
}
} finally {
fclose($stream);
}
}
$created = DB::transaction(function () use ($receipt, $hash): bool {
DB::select('SELECT pg_advisory_xact_lock(280926, 150001)');
if (DB::table('audit_events')->where('source_system', 'territorial-owner-acceptance')->where('external_id', $hash)->exists()) {
return false;
}
DB::table('audit_events')->insert([
'source_system' => 'territorial-owner-acceptance', 'external_id' => $hash,
'entity_type' => 'territorial_source_package', 'action' => 'owner_accepted',
'after_data' => json_encode($receipt, JSON_THROW_ON_ERROR),
]);
return true;
});
echo json_encode(['status' => 'accepted_by_owner', 'audit_created' => $created,
'receipt' => $path, 'territories_imported' => 0], JSON_UNESCAPED_SLASHES | JSON_THROW_ON_ERROR).PHP_EOL;