This commit is contained in:
@@ -2,8 +2,9 @@
|
||||
|
||||
namespace App\Services;
|
||||
|
||||
use Illuminate\Database\Query\Builder;
|
||||
use App\Models\InvestmentDossier;
|
||||
use Illuminate\Database\Eloquent\Builder as EloquentBuilder;
|
||||
use Illuminate\Database\Query\Builder;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Validation\ValidationException;
|
||||
|
||||
@@ -13,14 +14,25 @@ final class InvestmentGeography
|
||||
|
||||
/**
|
||||
* 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>
|
||||
*
|
||||
* @param EloquentBuilder<InvestmentDossier> $query
|
||||
* @param list<string> $territoryIds
|
||||
* @return EloquentBuilder<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'))
|
||||
$matches->orWhereExists(fn ($coverage) => $coverage->selectRaw('1')->from('economic_clusters as coverage_cluster')
|
||||
->whereColumn('coverage_cluster.id', 'investment_dossiers.cluster_id')->whereNull('coverage_cluster.deleted_at')
|
||||
->whereRaw(app(InvestmentRelations::class)->coverageSql('coverage_cluster.metadata', $territoryIds), $territoryIds));
|
||||
$matches->orWhereExists(fn ($member) => $member->selectRaw('1')->fromSub(app(InvestmentRelations::class)->memberships()->select(['cp.cluster_id', 'cp.project_id']), 'documented_member')
|
||||
->join('projects as member_project', 'member_project.id', '=', 'documented_member.project_id')
|
||||
->whereColumn('documented_member.cluster_id', 'investment_dossiers.cluster_id')
|
||||
->where(fn ($area) => $area->whereIn('member_project.primary_territory_id', $territoryIds)
|
||||
->orWhereExists(fn ($locations) => $locations->selectRaw('1')->from('project_locations as member_location')
|
||||
->whereColumn('member_location.project_id', 'member_project.id')->whereNull('member_location.deleted_at')
|
||||
->where('member_location.data_classification', 'public')->whereIn('member_location.geo_location_id', $territoryIds))));
|
||||
$matches->orWhereIn('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))
|
||||
@@ -177,7 +189,18 @@ final class InvestmentGeography
|
||||
? 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());
|
||||
$treeIds = array_map(fn ($row) => $row->id, $rows);
|
||||
$ids = $query->whereIn('id', $treeIds)->pluck('id')->map(fn ($id) => (string) $id)->all();
|
||||
if (! $includeNonPublic) {
|
||||
// Documented sites need not be administrative registry entries themselves.
|
||||
// Their parent must belong to the same public, trusted administrative hierarchy.
|
||||
$sites = DB::table('geo_locations')->whereIn('id', $treeIds)->where('location_type', 'site')
|
||||
->whereNull('deleted_at')->where('data_classification', 'public')
|
||||
->whereIn('parent_id', $this->publicReferences()->select('id'))->pluck('id')->all();
|
||||
$ids = array_merge($ids, $sites);
|
||||
}
|
||||
|
||||
return array_values(array_unique($ids));
|
||||
}
|
||||
|
||||
/** @return list<\stdClass> */
|
||||
|
||||
Reference in New Issue
Block a user