varias alteracoes
This commit is contained in:
@@ -16,7 +16,9 @@ class InvestmentCatalogue
|
||||
/** @return array<string,mixed> */
|
||||
public function filters(Request $request): array
|
||||
{
|
||||
return $request->validate(['q' => ['nullable', 'string', 'max:120'], 'sector' => ['nullable', 'string', 'max:80'],
|
||||
$filters = $request->validate(['q' => ['nullable', 'string', 'max:120'], 'sector' => ['nullable', 'string', 'max:80'],
|
||||
'opportunity_id' => ['nullable', 'integer', 'min:1'],
|
||||
'province_id' => ['nullable', 'uuid'], 'district_id' => ['nullable', 'uuid'], 'admin_post_id' => ['nullable', 'uuid'], 'locality_id' => ['nullable', 'uuid'],
|
||||
'province' => ['nullable', 'string', 'max:100'], 'district' => ['nullable', 'string', 'max:100'],
|
||||
'cluster_id' => ['nullable', 'uuid'], 'corridor_id' => ['nullable', 'uuid'], 'project_id' => ['nullable', 'uuid'],
|
||||
'geo_location_id' => ['nullable', 'uuid'], 'education' => ['nullable', 'boolean'],
|
||||
@@ -28,6 +30,9 @@ class InvestmentCatalogue
|
||||
'currency' => ['nullable', 'required_with:min_amount,max_amount', 'in:MZN,USD,EUR,ZAR'],
|
||||
'before' => ['nullable', 'date_format:Y-m-d'], 'sort' => ['nullable', 'in:recent,title,amount_asc,amount_desc'],
|
||||
'per_page' => ['nullable', 'integer', 'between:1,50'], 'page' => ['nullable', 'integer', 'min:1']]);
|
||||
app(InvestmentGeography::class)->selection($filters);
|
||||
|
||||
return $filters;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -37,6 +42,14 @@ class InvestmentCatalogue
|
||||
public function query(array $filters): Builder
|
||||
{
|
||||
$query = Opportunity::published()->with('sector:id,name,slug');
|
||||
if (! empty($filters['opportunity_id'])) {
|
||||
$query->whereKey($filters['opportunity_id']);
|
||||
}
|
||||
$path = app(InvestmentGeography::class)->selection($filters);
|
||||
if ($path !== []) {
|
||||
$ids = app(InvestmentGeography::class)->descendants($path[array_key_last($path)]->id);
|
||||
$query->whereIn('primary_territory_id', $ids);
|
||||
}
|
||||
if (in_array($filters['dimension'] ?? null, ['zee', 'zfi'], true)) {
|
||||
$query->whereIn('opportunities.id', app(InvestmentZones::class)->links()->join('investment_item_opportunities as mapping', 'mapping.item_id', '=', 'zl.item_id')
|
||||
->join('investment_zones as z', 'z.id', '=', 'zl.zone_id')->where('z.category', $filters['dimension'])->select('mapping.opportunity_id'));
|
||||
@@ -147,6 +160,7 @@ class InvestmentCatalogue
|
||||
$result['zones'] = app(InvestmentZones::class)->links()->join('investment_item_opportunities as m', 'm.item_id', '=', 'zl.item_id')
|
||||
->join('investment_zones as z', 'z.id', '=', 'zl.zone_id')->where('m.opportunity_id', $id)
|
||||
->get(['z.id', 'z.name', 'z.category', 'zl.association_type', 'zl.source_reference', 'zl.valid_from', 'zl.valid_until']);
|
||||
$result['primary_territory_path'] = app(InvestmentGeography::class)->path(DB::table('opportunities')->where('id', $id)->value('primary_territory_id'));
|
||||
|
||||
return $result;
|
||||
}
|
||||
@@ -157,14 +171,46 @@ class InvestmentCatalogue
|
||||
*/
|
||||
public function map(array $filters): array
|
||||
{
|
||||
$ids = $this->query($filters)->reorder()->select('opportunities.id');
|
||||
$points = DB::table('opportunity_locations as l')->join('geo_locations as g', 'g.id', '=', 'l.geo_location_id')
|
||||
->join('geo_geometries as shape', 'shape.geo_location_id', '=', 'g.id')->join('opportunities as o', 'o.id', '=', 'l.opportunity_id')
|
||||
$records = $this->query($filters)->limit(1000)->get();
|
||||
$total = $this->query($filters)->reorder()->count();
|
||||
$ids = $records->pluck('id');
|
||||
$points = DB::table('opportunities as o')->join('geo_locations as g', function ($join) {
|
||||
$join->on('g.id', '=', 'o.primary_territory_id')->orWhereExists(fn ($q) => $q->selectRaw('1')->from('opportunity_locations as l')->whereColumn('l.opportunity_id', 'o.id')->whereColumn('l.geo_location_id', 'g.id'));
|
||||
})->join('geo_geometries as shape', 'shape.geo_location_id', '=', 'g.id')
|
||||
->whereIn('o.id', $ids)->where('g.data_classification', 'public')->where('shape.data_classification', 'public')
|
||||
->whereNull('g.deleted_at')->whereNull('shape.deleted_at')->where('shape.geometry_type', 'point')->limit(1000)
|
||||
->get(['o.id', 'o.title', 'o.slug', 'g.name', DB::raw('ST_AsGeoJSON(shape.geom) as geometry')]);
|
||||
->where('g.location_type', 'site')->whereNull('g.deleted_at')->whereNull('shape.deleted_at')->where('shape.geometry_type', 'point')
|
||||
->whereRaw("NOT EXISTS (WITH RECURSIVE parents AS (
|
||||
SELECT id,parent_id,deleted_at,data_classification,ARRAY[id] AS visited,false AS cycle FROM geo_locations WHERE id=g.id
|
||||
UNION ALL SELECT p.id,p.parent_id,p.deleted_at,p.data_classification,a.visited||p.id,p.id=ANY(a.visited)
|
||||
FROM geo_locations p JOIN parents a ON p.id=a.parent_id WHERE NOT a.cycle
|
||||
) SELECT 1 FROM parents WHERE deleted_at IS NOT NULL OR data_classification<>'public' OR cycle)")
|
||||
->whereRaw('ST_IsValid(shape.geom) AND NOT ST_IsEmpty(shape.geom) AND ST_SRID(shape.geom)=4326')
|
||||
->orderBy('o.id')->orderByRaw("CASE WHEN g.source_system='investment-site-editor' THEN 0 ELSE 1 END")->orderBy('g.id')->orderBy('shape.id')
|
||||
->get(['o.id', 'g.name', DB::raw('ST_AsGeoJSON(shape.geom) as geometry')])->unique('id')->keyBy('id');
|
||||
$relations = [];
|
||||
foreach (['clusters', 'corridors', 'projects'] as $relation) {
|
||||
[$foreign, $table] = self::RELATIONS[$relation];
|
||||
$relations[$relation] = DB::table('opportunity_'.$relation.' as l')->join($table.' as t', 't.id', '=', 'l.'.$foreign)
|
||||
->whereIn('l.opportunity_id', $ids)->whereNull('t.deleted_at')
|
||||
->whereIn('t.id', InvestmentPortfolioController::visible()->whereNotNull($foreign)->select($foreign))
|
||||
->get(['l.opportunity_id', 't.name'])->groupBy('opportunity_id');
|
||||
}
|
||||
$features = $missing = [];
|
||||
foreach ($records as $record) {
|
||||
$properties = ['id' => $record->id, 'title' => $record->title, 'category' => $record->sector?->name,
|
||||
'location' => trim($record->province.' '.$record->district), 'url' => '/oportunidades/'.$record->slug];
|
||||
foreach ($relations as $relation => $groups) {
|
||||
$properties[$relation] = ($groups->get($record->id) ?? collect())->pluck('name')->unique()->values()->all();
|
||||
}
|
||||
if ($point = $points->get($record->id)) {
|
||||
$features[] = ['type' => 'Feature', 'geometry' => json_decode($point->geometry, true), 'properties' => [...$properties, 'location' => trim($properties['location'].' — '.$point->name)]];
|
||||
} else {
|
||||
$missing[] = [...$properties, 'reason' => 'missing_public_site_coordinates'];
|
||||
}
|
||||
}
|
||||
|
||||
return ['type' => 'FeatureCollection', 'limit' => 1000, 'features' => $points->map(fn ($p) => ['type' => 'Feature',
|
||||
'geometry' => json_decode($p->geometry, true), 'properties' => ['id' => $p->id, 'title' => $p->title, 'location' => $p->name, 'url' => '/oportunidades/'.$p->slug]])->all()];
|
||||
return ['type' => 'FeatureCollection', 'limit' => 1000, 'matched_count' => $total, 'truncated' => $total > $records->count(),
|
||||
'georeferenced_count' => count($features), 'unlocated' => $missing, 'features' => $features,
|
||||
'complete' => $total === count($features)];
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,83 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services;
|
||||
|
||||
use Illuminate\Database\Query\Builder;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Validation\ValidationException;
|
||||
|
||||
final class InvestmentGeography
|
||||
{
|
||||
public const FIELDS = ['province_id' => 'province', 'district_id' => 'district', 'admin_post_id' => 'admin_post', 'locality_id' => 'locality'];
|
||||
|
||||
public function publicReferences(): Builder
|
||||
{
|
||||
return DB::table('geo_locations')->whereNull('deleted_at')->where('data_classification', 'public')
|
||||
->whereRaw("NOT EXISTS (WITH RECURSIVE ancestors AS (
|
||||
SELECT id,parent_id,deleted_at,data_classification,ARRAY[id] AS visited,false AS cycle FROM geo_locations a WHERE a.id=geo_locations.id
|
||||
UNION ALL SELECT a.id,a.parent_id,a.deleted_at,a.data_classification,p.visited||a.id,a.id=ANY(p.visited)
|
||||
FROM geo_locations a JOIN ancestors p ON a.id=p.parent_id WHERE NOT p.cycle
|
||||
) SELECT 1 FROM ancestors WHERE deleted_at IS NOT NULL OR data_classification<>'public' OR cycle)")
|
||||
->where(function ($query) {
|
||||
$query->where(function ($workbook) {
|
||||
$workbook->where('source_system', 'cartografia-workbook')
|
||||
->where('external_id', 'like', '964a187ad32ea553b9e112cd159caa90b900050c42b7329594a75bcd798a96ff:%');
|
||||
})->orWhereIn('id', app(TerritorialRegistry::class)->published()->select('g.id'));
|
||||
});
|
||||
}
|
||||
|
||||
/** @param array<string,mixed> $input
|
||||
* @return list<\stdClass>
|
||||
*/
|
||||
public function selection(array $input): array
|
||||
{
|
||||
$path = [];
|
||||
$parent = null;
|
||||
$gap = false;
|
||||
foreach (self::FIELDS as $field => $type) {
|
||||
if (empty($input[$field])) {
|
||||
$gap = true;
|
||||
|
||||
continue;
|
||||
}
|
||||
$node = $this->publicReferences()->where('id', $input[$field])->where('location_type', $type)->first(['id', 'name', 'parent_id', 'location_type']);
|
||||
if ($gap || ! $node || ($parent && $node->parent_id !== $parent)) {
|
||||
throw ValidationException::withMessages([$field => 'Seleccione a localização dentro da hierarquia indicada.']);
|
||||
}
|
||||
$path[] = $node;
|
||||
$parent = $node->id;
|
||||
}
|
||||
|
||||
return $path;
|
||||
}
|
||||
|
||||
/** @return list<string> */
|
||||
public function descendants(string $id): array
|
||||
{
|
||||
$rows = DB::select('WITH RECURSIVE tree AS (
|
||||
SELECT id, ARRAY[id] AS visited FROM geo_locations WHERE id=? AND deleted_at IS NULL
|
||||
UNION ALL SELECT g.id, tree.visited||g.id FROM geo_locations g JOIN tree ON g.parent_id=tree.id
|
||||
WHERE g.deleted_at IS NULL AND NOT g.id=ANY(tree.visited) AND cardinality(tree.visited)<5
|
||||
) SELECT id FROM tree', [$id]);
|
||||
|
||||
return array_values($this->publicReferences()->whereIn('id', array_map(fn ($row) => $row->id, $rows))->pluck('id')->map(fn ($id) => (string) $id)->all());
|
||||
}
|
||||
|
||||
/** @return list<\stdClass> */
|
||||
public function path(?string $id): array
|
||||
{
|
||||
$path = [];
|
||||
for ($i = 0; $id && $i < 5; $i++) {
|
||||
$node = $this->publicReferences()->where('id', $id)->first(['id', 'name', 'parent_id', 'location_type']);
|
||||
if (! $node) {
|
||||
return [];
|
||||
}
|
||||
if ($node->location_type !== 'country') {
|
||||
array_unshift($path, $node);
|
||||
}
|
||||
$id = $node->parent_id;
|
||||
}
|
||||
|
||||
return $path;
|
||||
}
|
||||
}
|
||||
@@ -18,7 +18,7 @@ class PortalAudit
|
||||
$record->setRawAttributes($locked->getAttributes(), true);
|
||||
}
|
||||
// Exclude free text and personal data from the immutable event payload.
|
||||
$fields = ['version_no', 'status', 'is_active', 'is_featured', 'sort_order', 'sector_id', 'maturity_level',
|
||||
$fields = ['primary_territory_id', 'territory_version', 'version_no', 'status', 'is_active', 'is_featured', 'sort_order', 'sector_id', 'maturity_level',
|
||||
'estimated_investment_amount', 'estimated_investment_currency', 'owner_user_id', 'assignee_user_id', 'due_at',
|
||||
'cluster_id', 'project_id', 'corridor_id', 'dossier_id', 'interest_request_id', 'publication_status', 'editor_id', 'reviewer_id', 'role', 'stage', 'value_chain_id', 'from_value_chain_id', 'to_value_chain_id', 'link_type', 'deleted_at'];
|
||||
$before = array_intersect_key($record->getAttributes(), array_flip($fields));
|
||||
|
||||
@@ -0,0 +1,133 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services;
|
||||
|
||||
use RuntimeException;
|
||||
|
||||
/** Offline structural validation. This never approves a source or writes territories. */
|
||||
final class TerritorialCsvAudit
|
||||
{
|
||||
public const HEADERS = ['province_code', 'province_name', 'district_code', 'district_name', 'admin_post_code', 'admin_post_name', 'locality_code', 'locality_name', 'locality_type', 'latitude', 'longitude'];
|
||||
|
||||
/** @param list<string> $provinceCodes
|
||||
* @return array<string,mixed>
|
||||
*/
|
||||
public function inspect(string $path, array $provinceCodes): array
|
||||
{
|
||||
if (! is_file($path) || ! is_readable($path) || filesize($path) > 20 * 1024 * 1024) {
|
||||
throw new RuntimeException('CSV inacessível ou superior a 20 MiB.');
|
||||
}
|
||||
$bytes = file_get_contents($path);
|
||||
if ($bytes === false || ! mb_check_encoding($bytes, 'UTF-8') || str_contains($bytes, "\0")) {
|
||||
throw new RuntimeException('O CSV deve ser UTF-8 sem bytes nulos.');
|
||||
}
|
||||
// Parse the exact bytes hashed, avoiding a second read of a changing input.
|
||||
$stream = fopen('php://temp', 'w+');
|
||||
if ($stream === false) {
|
||||
throw new RuntimeException('Não foi possível preparar a leitura.');
|
||||
}
|
||||
fwrite($stream, preg_replace('/^\xEF\xBB\xBF/', '', $bytes) ?? $bytes);
|
||||
rewind($stream);
|
||||
try {
|
||||
if (fgetcsv($stream, null, ';', '"', '') !== self::HEADERS) {
|
||||
throw new RuntimeException('Cabeçalho inválido: usar o contrato INE com separador ponto e vírgula.');
|
||||
}
|
||||
$errors = $seen = $names = $totals = [];
|
||||
$duplicates = $count = 0;
|
||||
while (($values = fgetcsv($stream, null, ';', '"', '')) !== false) {
|
||||
$count++;
|
||||
if ($count > 50000) {
|
||||
throw new RuntimeException('O CSV excede 50.000 registos.');
|
||||
}
|
||||
$issues = [];
|
||||
if (count($values) !== count(self::HEADERS)) {
|
||||
$errors[] = ['record' => $count + 1, 'issues' => ['column_count']];
|
||||
|
||||
continue;
|
||||
}
|
||||
$row = array_combine(self::HEADERS, array_map(fn ($v) => trim((string) $v), $values));
|
||||
foreach ($row as $field => $value) {
|
||||
if (mb_strlen($value) > 255 || preg_match('/[\x00-\x1F\x7F]/', $value)) {
|
||||
$issues[] = 'invalid_field:'.$field;
|
||||
}
|
||||
}
|
||||
if (! preg_match('/^[0-9]{2}$/D', $row['province_code']) || ! in_array($row['province_code'], $provinceCodes, true)) {
|
||||
$issues[] = 'unknown_province';
|
||||
}
|
||||
foreach (['province', 'district', 'admin_post', 'locality'] as $kind) {
|
||||
$code = $row[$kind.'_code'];
|
||||
$name = $row[$kind.'_name'];
|
||||
if (($kind === 'province' || $kind === 'district' || $code !== '') && $name === '') {
|
||||
$issues[] = 'missing_name:'.$kind;
|
||||
}
|
||||
if (($kind === 'province' || $kind === 'district' || $name !== '') && $code === '') {
|
||||
$issues[] = 'missing_code:'.$kind;
|
||||
}
|
||||
if ($code === '') {
|
||||
continue;
|
||||
}
|
||||
if (! preg_match('/^[0-9]+$/D', $code)) {
|
||||
$issues[] = 'invalid_code:'.$kind;
|
||||
}
|
||||
$parent = match ($kind) {
|
||||
'province' => 'MZ', 'district' => $row['province_code'],
|
||||
'admin_post' => $row['district_code'], default => $row['admin_post_code'],
|
||||
};
|
||||
$identity = $kind.':'.$code;
|
||||
$definition = [$parent, $name];
|
||||
if ($kind === 'locality') {
|
||||
$definition = [...$definition, $row['locality_type'], $row['latitude'], $row['longitude']];
|
||||
}
|
||||
if (isset($seen[$identity]) && $seen[$identity] !== $definition) {
|
||||
$issues[] = 'conflicting_code:'.$kind;
|
||||
}
|
||||
$seen[$identity] ??= $definition;
|
||||
$nameKey = $kind.':'.$parent.':'.mb_strtolower($name);
|
||||
if (isset($names[$nameKey]) && $names[$nameKey] !== $code) {
|
||||
$issues[] = 'ambiguous_name:'.$kind;
|
||||
}
|
||||
$names[$nameKey] ??= $code;
|
||||
$totals[$row['province_code']][$kind][$code] = true;
|
||||
}
|
||||
if ($row['locality_code'] !== '' && $row['admin_post_code'] === '') {
|
||||
$issues[] = 'locality_requires_reviewed_parent_exception';
|
||||
}
|
||||
if (($row['latitude'] === '') !== ($row['longitude'] === '')) {
|
||||
$issues[] = 'incomplete_coordinates';
|
||||
}
|
||||
foreach (['latitude' => 90, 'longitude' => 180] as $field => $bound) {
|
||||
if ($row[$field] !== '' && (! preg_match('/^-?[0-9]+(?:\.[0-9]+)?$/D', $row[$field]) || abs((float) $row[$field]) > $bound)) {
|
||||
$issues[] = 'invalid_coordinate:'.$field;
|
||||
}
|
||||
}
|
||||
if ($row['locality_code'] === '' && ($row['latitude'] !== '' || $row['longitude'] !== '' || $row['locality_type'] !== '')) {
|
||||
$issues[] = 'locality_metadata_without_locality';
|
||||
}
|
||||
$fingerprint = hash('sha256', json_encode($row, JSON_THROW_ON_ERROR));
|
||||
if (isset($seen['row:'.$fingerprint])) {
|
||||
$duplicates++;
|
||||
}
|
||||
$seen['row:'.$fingerprint] = [];
|
||||
if ($issues !== []) {
|
||||
$errors[] = ['record' => $count + 1, 'issues' => array_values(array_unique($issues))];
|
||||
}
|
||||
}
|
||||
if ($count === 0) {
|
||||
$errors[] = ['record' => 1, 'issues' => ['empty_dataset']];
|
||||
}
|
||||
ksort($totals);
|
||||
$counts = [];
|
||||
foreach ($totals as $province => $kinds) {
|
||||
$counts[$province] = array_map('count', $kinds);
|
||||
}
|
||||
|
||||
return ['schema_version' => 1, 'sha256' => hash('sha256', $bytes), 'encoding' => 'UTF-8',
|
||||
'delimiter' => ';', 'coordinate_crs' => 'EPSG:4326', 'records' => $count,
|
||||
'duplicate_records' => $duplicates, 'rejected_records' => count($errors),
|
||||
'status' => $errors === [] ? 'structurally_valid_requires_review' : 'rejected',
|
||||
'candidate_counts_by_province' => $counts, 'errors' => $errors];
|
||||
} finally {
|
||||
fclose($stream);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user