Fichas: cada campo vazio mostra se já foi verificado na fonte, e a gestão regista a verificação
Mapa de erros 4.9, decisão do proprietário: os campos «Não documentado» das fichas (posto administrativo, localidade, coordenadas, polígono, data da fonte) continuam a ser mostrados e o portal distingue omissão de falta de informação. Cada campo vazio passa a dizer o seu estado: «Não documentado — por verificar na fonte», «Não consta da fonte» ou «Consta da fonte; em carregamento no portal». O estado é decidido por uma pessoa, nunca pelo portal: a nova página de gestão «Lacunas nas fichas» (/management/lacunas) lista os campos vazios das fichas publicadas, filtra por estado e regista a verificação com nota e evento de auditoria. Só se pode registar um campo que esteja de facto vazio nessa localização. Nova tabela information_gaps (migração). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01MzJmxFy3izprXr5dSq91jM
This commit is contained in:
@@ -0,0 +1,138 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services;
|
||||
|
||||
use App\Http\Controllers\InvestmentPortfolioController;
|
||||
use App\Models\User;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
use Illuminate\Support\Facades\Validator;
|
||||
use Illuminate\Support\Str;
|
||||
use Illuminate\Validation\Rule;
|
||||
|
||||
/**
|
||||
* Empty fields of the published project and cluster records (mapa de erros 4.9). Every empty field is shown; its
|
||||
* state says whether it was checked against the source: «por verificar» (no review yet), «não consta da fonte» or
|
||||
* «consta da fonte e falta carregar». The portal never decides that by itself: a person compares with the source.
|
||||
*/
|
||||
final class InformationGaps
|
||||
{
|
||||
public const FIELDS = ['admin_post' => 'Posto administrativo', 'locality' => 'Localidade', 'coordinates' => 'Coordenadas',
|
||||
'polygon' => 'Polígono', 'source_date' => 'Data da fonte'];
|
||||
|
||||
public const STATUSES = ['to_verify' => 'Por verificar', 'not_in_source' => 'Não consta da fonte',
|
||||
'missing_in_portal' => 'Consta da fonte; falta carregar no portal'];
|
||||
|
||||
public function installed(): bool
|
||||
{
|
||||
return Schema::hasTable('information_gaps');
|
||||
}
|
||||
|
||||
/** @return array<string, array<string, string>> location id => field => status */
|
||||
public function statuses(string $dossierId): array
|
||||
{
|
||||
if (! $this->installed()) {
|
||||
return [];
|
||||
}
|
||||
$result = [];
|
||||
foreach (DB::table('information_gaps')->where('dossier_id', $dossierId)->get(['geo_location_id', 'field', 'status']) as $row) {
|
||||
$result[$row->geo_location_id][$row->field] = $row->status;
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds the state of each empty field to the locations of a dossier geography.
|
||||
*
|
||||
* @param array<string,mixed> $geography
|
||||
* @return array<string,mixed>
|
||||
*/
|
||||
public function annotate(string $dossierId, array $geography): array
|
||||
{
|
||||
$statuses = $this->statuses($dossierId);
|
||||
$mark = fn (array $locations) => array_map(function (array $location) use ($statuses) {
|
||||
$location['gaps'] = [];
|
||||
foreach ($this->empty($location) as $field) {
|
||||
$location['gaps'][$field] = $statuses[$location['id']][$field] ?? 'to_verify';
|
||||
}
|
||||
|
||||
return $location;
|
||||
}, $locations);
|
||||
$geography['locations'] = $mark($geography['locations'] ?? []);
|
||||
|
||||
return $geography;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<string,mixed> $location
|
||||
* @return list<string>
|
||||
*/
|
||||
public function empty(array $location): array
|
||||
{
|
||||
$types = array_column(array_map(fn ($node) => (array) $node, $location['path'] ?? []), 'location_type');
|
||||
$empty = [];
|
||||
foreach (['admin_post', 'locality'] as $type) {
|
||||
if (! in_array($type, $types, true)) {
|
||||
$empty[] = $type;
|
||||
}
|
||||
}
|
||||
if (empty($location['coordinates'])) {
|
||||
$empty[] = 'coordinates';
|
||||
}
|
||||
if (empty($location['boundaries'])) {
|
||||
$empty[] = 'polygon';
|
||||
}
|
||||
if (trim((string) ($location['source_date'] ?? '')) === '') {
|
||||
$empty[] = 'source_date';
|
||||
}
|
||||
|
||||
return $empty;
|
||||
}
|
||||
|
||||
/** @return list<array<string,mixed>> */
|
||||
public function pending(?string $status = 'to_verify'): array
|
||||
{
|
||||
$items = [];
|
||||
foreach (InvestmentPortfolioController::visible()->orderBy('title')->get() as $dossier) {
|
||||
$geography = $this->annotate($dossier->id, app(InvestmentRelations::class)->dossier($dossier, true));
|
||||
foreach ($geography['locations'] as $location) {
|
||||
foreach ($location['gaps'] as $field => $state) {
|
||||
if ($status === null || $state === $status) {
|
||||
$items[] = ['dossier_id' => $dossier->id, 'dossier' => $dossier->title, 'location_id' => $location['id'],
|
||||
'location' => $location['name'], 'field' => $field, 'field_label' => self::FIELDS[$field], 'status' => $state,
|
||||
'source_reference' => $location['source_reference'] ?? null];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $items;
|
||||
}
|
||||
|
||||
/** @param array<string,mixed> $input */
|
||||
public function record(User $actor, array $input): void
|
||||
{
|
||||
$data = Validator::make($input, ['dossier_id' => 'required|uuid', 'location_id' => 'required|uuid',
|
||||
'field' => ['required', Rule::in(array_keys(self::FIELDS))], 'status' => ['required', Rule::in(array_keys(self::STATUSES))],
|
||||
'note' => 'nullable|string|max:2000'])->validate();
|
||||
$dossier = InvestmentPortfolioController::visible()->whereKey($data['dossier_id'])->firstOrFail();
|
||||
$location = collect(app(InvestmentRelations::class)->dossier($dossier, true)['locations'])->firstWhere('id', $data['location_id']);
|
||||
abort_unless($location !== null && in_array($data['field'], $this->empty($location), true), 422,
|
||||
'Este campo já está preenchido nesta ficha ou não pertence a esta localização.');
|
||||
DB::transaction(function () use ($actor, $data) {
|
||||
$key = ['dossier_id' => $data['dossier_id'], 'geo_location_id' => $data['location_id'], 'field' => $data['field']];
|
||||
if ($data['status'] === 'to_verify') {
|
||||
DB::table('information_gaps')->where($key)->delete();
|
||||
} else {
|
||||
$existing = DB::table('information_gaps')->where($key)->value('id');
|
||||
DB::table('information_gaps')->updateOrInsert($key, ['id' => $existing ?? (string) Str::uuid(), 'status' => $data['status'],
|
||||
'note' => $data['note'] ?? null, 'reviewer_id' => $actor->id, 'reviewed_at' => now(), 'updated_at' => now(),
|
||||
...($existing ? [] : ['created_at' => now()])]);
|
||||
}
|
||||
DB::table('audit_events')->insert(['source_system' => 'information-gaps', 'entity_type' => 'investment_dossiers',
|
||||
'entity_id' => $data['dossier_id'], 'actor_user_id' => $actor->id, 'action' => 'information_gap_reviewed',
|
||||
'after_data' => json_encode($data, JSON_THROW_ON_ERROR)]);
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user