@@ -3,6 +3,7 @@
|
||||
namespace App\Services;
|
||||
|
||||
use Illuminate\Database\Query\Builder;
|
||||
use Illuminate\Database\Eloquent\Builder as EloquentBuilder;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Validation\ValidationException;
|
||||
|
||||
@@ -10,6 +11,44 @@ final class InvestmentGeography
|
||||
{
|
||||
public const FIELDS = ['province_id' => 'province', 'district_id' => 'district', 'admin_post_id' => 'admin_post', 'locality_id' => 'locality'];
|
||||
|
||||
/**
|
||||
* Restrict published dossier searches using direct territories and public, validated structure links.
|
||||
* @param EloquentBuilder<\App\Models\InvestmentDossier> $query
|
||||
* @param list<string> $territoryIds
|
||||
* @return EloquentBuilder<\App\Models\InvestmentDossier>
|
||||
*/
|
||||
public function constrainDossiersToTerritories(EloquentBuilder $query, array $territoryIds): EloquentBuilder
|
||||
{
|
||||
return $query->where(function ($matches) use ($territoryIds): void {
|
||||
$matches->whereIn('project_id', DB::table('projects')->whereNull('deleted_at')->whereIn('primary_territory_id', $territoryIds)->select('id'))
|
||||
->orWhereExists(fn ($locations) => $locations->selectRaw('1')->from('project_locations as pl')
|
||||
->whereColumn('pl.project_id', 'investment_dossiers.project_id')->whereNull('pl.deleted_at')
|
||||
->where('pl.data_classification', 'public')->whereIn('pl.geo_location_id', $territoryIds))
|
||||
->orWhereIn('cluster_id', DB::table('economic_clusters')->whereNull('deleted_at')->whereIn('primary_territory_id', $territoryIds)->select('id'))
|
||||
->orWhereIn('corridor_id', DB::table('development_corridors')->whereNull('deleted_at')->whereIn('primary_territory_id', $territoryIds)->select('id'))
|
||||
->orWhereExists(fn ($locations) => $locations->selectRaw('1')->from('corridor_locations as cl')
|
||||
->whereColumn('cl.corridor_id', 'investment_dossiers.corridor_id')->whereNull('cl.deleted_at')
|
||||
->where('cl.data_classification', 'public')->whereIn('cl.geo_location_id', $territoryIds));
|
||||
|
||||
foreach ([['cluster_projects', 'cluster_id'], ['corridor_projects', 'corridor_id']] as [$linkTable, $ownerColumn]) {
|
||||
$matches->orWhereExists(fn ($links) => $links->selectRaw('1')->from($linkTable.' as territorial_link')
|
||||
->join('projects as linked_project', 'linked_project.id', '=', 'territorial_link.project_id')
|
||||
->whereColumn('territorial_link.'.$ownerColumn, 'investment_dossiers.'.$ownerColumn)
|
||||
->whereNull('territorial_link.deleted_at')->where('territorial_link.data_classification', 'public')
|
||||
->where('territorial_link.validation_status', 'validated')
|
||||
->whereDate('territorial_link.valid_from', '<=', today()->toDateString())
|
||||
->where(fn ($validity) => $validity->whereNull('territorial_link.valid_until')->orWhereDate('territorial_link.valid_until', '>=', today()->toDateString()))
|
||||
->whereNull('linked_project.deleted_at')->where('linked_project.status', '!=', 'draft')
|
||||
->where(function ($projectArea) use ($territoryIds): void {
|
||||
$projectArea->whereIn('linked_project.primary_territory_id', $territoryIds)
|
||||
->orWhereExists(fn ($locations) => $locations->selectRaw('1')->from('project_locations as linked_location')
|
||||
->whereColumn('linked_location.project_id', 'linked_project.id')->whereNull('linked_location.deleted_at')
|
||||
->where('linked_location.data_classification', 'public')->whereIn('linked_location.geo_location_id', $territoryIds));
|
||||
}));
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public function publicReferences(): Builder
|
||||
{
|
||||
return DB::table('geo_locations')->whereNull('deleted_at')->where('data_classification', 'public')
|
||||
@@ -33,6 +72,73 @@ final class InvestmentGeography
|
||||
: $this->publicReferences();
|
||||
}
|
||||
|
||||
/** @param array<string,mixed> $payload
|
||||
* @return list<list<string>>
|
||||
*/
|
||||
public function displayPaths(array $payload): array
|
||||
{
|
||||
$locations = [];
|
||||
if (($payload['geography_scope'] ?? null) === 'multiple') {
|
||||
$decoded = json_decode((string) ($payload['locations_json'] ?? ''), true);
|
||||
$locations = is_array($decoded) ? $decoded : [];
|
||||
} elseif (($payload['geography_scope'] ?? null) === 'specific') {
|
||||
$locations = [array_intersect_key($payload, array_flip(array_keys(self::FIELDS)))];
|
||||
} else {
|
||||
return [];
|
||||
}
|
||||
|
||||
$leafIds = [];
|
||||
foreach ($locations as $location) {
|
||||
$leafIds[] = is_array($location)
|
||||
? ($location['locality_id'] ?? $location['admin_post_id'] ?? $location['district_id'] ?? $location['province_id'] ?? null)
|
||||
: null;
|
||||
}
|
||||
$selected = array_filter($leafIds, fn ($id) => is_string($id) && $id !== '');
|
||||
if (! $selected) {
|
||||
return array_fill(0, count($locations), []);
|
||||
}
|
||||
|
||||
$values = [];
|
||||
$bindings = [];
|
||||
foreach ($selected as $index => $id) {
|
||||
$values[] = '(?, ?::uuid)';
|
||||
$bindings[] = $index;
|
||||
$bindings[] = $id;
|
||||
}
|
||||
$rows = DB::select('WITH RECURSIVE selected(location_index, id) AS (VALUES '.implode(',', $values).'),
|
||||
tree AS (
|
||||
SELECT s.location_index, g.id, g.name, g.location_type, g.parent_id, ARRAY[g.id] AS visited, 0 AS depth
|
||||
FROM selected s JOIN geo_locations g ON g.id=s.id
|
||||
UNION ALL
|
||||
SELECT tree.location_index, parent.id, parent.name, parent.location_type, parent.parent_id,
|
||||
tree.visited||parent.id, tree.depth+1
|
||||
FROM tree JOIN geo_locations parent ON parent.id=tree.parent_id
|
||||
WHERE tree.depth<4 AND NOT parent.id=ANY(tree.visited)
|
||||
)
|
||||
SELECT location_index, id, name, location_type FROM tree ORDER BY location_index, depth DESC', $bindings);
|
||||
$nodeIds = array_values(array_unique(array_map(fn ($row) => (string) $row->id, $rows)));
|
||||
$trustedIds = $this->publicReferences()->whereIn('id', $nodeIds)->pluck('id')->map(fn ($id) => (string) $id)->all();
|
||||
$trusted = array_fill_keys($trustedIds, true);
|
||||
$grouped = [];
|
||||
foreach ($rows as $row) {
|
||||
$grouped[(int) $row->location_index][] = $row;
|
||||
}
|
||||
|
||||
$display = [];
|
||||
foreach ($locations as $index => $_location) {
|
||||
$path = $grouped[$index] ?? [];
|
||||
if (! $path || array_filter($path, fn ($node) => ! isset($trusted[(string) $node->id]))) {
|
||||
$display[] = [];
|
||||
|
||||
continue;
|
||||
}
|
||||
$display[] = array_values(array_map(fn ($node) => (string) $node->name,
|
||||
array_filter($path, fn ($node) => $node->location_type !== 'country')));
|
||||
}
|
||||
|
||||
return $display;
|
||||
}
|
||||
|
||||
/** @param array<string,mixed> $input
|
||||
* @return list<\stdClass>
|
||||
*/
|
||||
@@ -59,7 +165,7 @@ final class InvestmentGeography
|
||||
}
|
||||
|
||||
/** @return list<string> */
|
||||
public function descendants(string $id): array
|
||||
public function descendants(string $id, bool $includeNonPublic = false): array
|
||||
{
|
||||
$rows = DB::select('WITH RECURSIVE tree AS (
|
||||
SELECT id, ARRAY[id] AS visited FROM geo_locations WHERE id=? AND deleted_at IS NULL
|
||||
@@ -67,7 +173,11 @@ final class InvestmentGeography
|
||||
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());
|
||||
$query = $includeNonPublic
|
||||
? DB::table('geo_locations')->whereNull('deleted_at')
|
||||
: $this->publicReferences();
|
||||
|
||||
return array_values($query->whereIn('id', array_map(fn ($row) => $row->id, $rows))->pluck('id')->map(fn ($id) => (string) $id)->all());
|
||||
}
|
||||
|
||||
/** @return list<\stdClass> */
|
||||
|
||||
Reference in New Issue
Block a user