'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> 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 $geography * @return array */ 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 $location * @return list */ 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> */ 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 $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)]); }); } }