feat: protect owner-only supply aggregates and record target CAE blocker

This commit is contained in:
2026-11-15 23:10:17 +02:00
parent e0b18413ac
commit c8957ecb9d
15 changed files with 1255 additions and 49 deletions
+211 -2
View File
@@ -4,7 +4,10 @@ namespace App\Services;
use App\Models\Opportunity;
use App\Models\User;
use App\Support\IsolatedQaDatabase;
use Illuminate\Database\Query\Builder;
use Illuminate\Support\Arr;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;
use Illuminate\Support\Facades\Validator;
@@ -36,14 +39,219 @@ final class ProactiveSupply
});
}
/** Aggregate access never confers the existing internal row/source permission. */
public function aggregateAvailable(): bool
{
return (bool) config('investor-path.protected_supply_enabled')
|| (config('investor-path.aggregate_supply_qa_enabled') && IsolatedQaDatabase::allowsSchemaChanges());
}
/** Fixed district/division cells; filters select released cells, never recalculate intersections.
* @param array<string,mixed> $fields
* @return array<string,mixed>
*/
public function protectedAggregate(User $actor, string $caseId, string $territory, array $fields): array
{
abort_unless(config('investor-path.protected_supply_enabled'), 404);
$case = app(VirtualDesk::class)->case($actor, $caseId, true, true);
abort_unless((int) $case->user_id === (int) $actor->id, 403);
app(VirtualDesk::class)->assertOpen($case);
$geo = app(InvestmentGeography::class);
$path = collect($geo->path($territory));
$province = $path->firstWhere('location_type', 'province');
abort_unless($province !== null, 422, 'Guarde uma localização com província conhecida no dossier.');
$scope = $path->firstWhere('location_type', 'district') ?? $province;
$nodes = $geo->publicReferences()->whereIn('id', $geo->descendants($province->id))->get(['id', 'parent_id', 'name', 'location_type'])->keyBy('id');
$mapping = [];
$scopes = [$province->id => $province];
foreach ($nodes as $node) {
$current = $node;
for ($depth = 0; $current && $depth < 5; $depth++) {
if ($current->location_type === 'district' || $current->location_type === 'province') {
break;
}
$current = $nodes->get($current->parent_id);
}
if ($current && in_array($current->location_type, ['district', 'province'], true)) {
$mapping[$node->id] = $current->id;
$scopes[$current->id] = $current;
}
}
$activities = $this->publicAggregateActivities();
$divisions = $activities->filter(fn ($a) => preg_match('/^[0-9]{2}$/D', $a->code) === 1)->groupBy('code');
$sector = isset($fields['sector_id']) ? (int) $fields['sector_id'] : null;
abort_unless($divisions->contains(fn ($references) => $references->contains(fn ($a) => ! $sector || (int) $a->effective_sector_id === $sector)), 422,
'A classificação de actividades aplicável a este sector ainda não está disponível para esta consulta. O dossier mantém-se guardado; esta limitação não significa ausência de empresas.');
$activity = ! empty($fields['activity_id']) ? $activities->firstWhere('id', $fields['activity_id']) : null;
abort_if(! empty($fields['activity_id']) && ! $activity, 422, 'A actividade guardada não está disponível nesta classificação. Reveja o contexto.');
abort_if($activity && $sector && (int) $activity->effective_sector_id !== $sector, 422, 'A actividade não corresponde ao sector guardado. Reveja o contexto.');
$rawActivityCode = $activity?->code;
abort_if($activity && ! is_string($rawActivityCode), 422, 'A referência de actividade não tem um código válido.');
$activityCode = is_string($rawActivityCode) ? str_replace('.', '', $rawActivityCode) : null;
abort_if($activityCode && ! preg_match('/^[0-9]{2,5}$/D', $activityCode), 422, 'Seleccione uma actividade com código CAE disponível.');
$division = $activityCode ? substr($activityCode, 0, 2) : null;
$previous = DB::selectOne("select current_setting('app.actor_user_id',true) as actor,current_setting('app.proactive_supply_read',true) as scope");
return $this->read($actor, function () use ($mapping, $scopes, $province, $scope, $territory, $path, $divisions, $sector, $division, $activity, $previous): array {
try {
$parts = [];
$bindings = [];
foreach ($mapping as $id => $parent) {
$parts[] = 'WHEN ? THEN ?';
$bindings[] = $id;
$bindings[] = $parent;
}
$scopeSql = 'CASE o.territory_id::text '.implode(' ', $parts).' ELSE NULL END';
$codeSql = "CASE WHEN replace(o.cae,'.','') ~ '^[0-9]{2,5}$' THEN substring(replace(o.cae,'.','') from 1 for 2) ELSE NULL END";
$rows = DB::table('proactive_supply_observations as o')->join('proactive_supply_sources as s', 's.sha256', '=', 'o.source_hash')->whereIn('o.territory_id', array_keys($mapping))
->whereRaw("s.receipt->>'candidate' = ? AND s.receipt->>'access' = 'internal'", ['a022ed9848b41ba342b4209414c2bbf2ebd0a7a3'])
->whereNull('o.duplicate_of')->where('o.identity_conflict', false)->whereRaw("trim(o.name) <> ''")
// The accepted importer uses an observation-specific key when NUIT is unresolved.
// Those keys must never masquerade as distinct documentary entities in the cohort.
->whereRaw("o.entity_key <> encode(sha256(convert_to('unresolved:' || o.source_hash || '|' || o.sheet || '|' || o.row_number::text,'UTF8')),'hex')")
->selectRaw($scopeSql.' as scope_id,'.$codeSql.' as code,count(DISTINCT o.entity_key) as entities,count(*) as observations', $bindings)
->groupByRaw('1,2')->orderByRaw('1,2')->get();
$groups = [];
foreach ($rows as $row) {
// Suppressed and absent cells have the same public representation. No marginal totals.
if ((int) $row->entities < 5 || ! $row->code || ! $divisions->has($row->code)) {
continue;
}
$references = $divisions->get($row->code);
if ($sector && ! $references->contains(fn ($a) => (int) $a->effective_sector_id === $sector)) {
continue;
}
if ($division && $row->code !== $division) {
continue;
}
if ($scope->id !== $province->id && ! in_array($row->scope_id, [$scope->id, $province->id], true)) {
continue;
}
$labels = $references->pluck('label_pt_mz')->unique();
$cellScope = $scopes[$row->scope_id];
$groups[] = ['code' => $row->code, 'label' => $labels->count() === 1 ? $labels->first() : 'Grupo CAE '.$row->code,
'total' => null, 'entities_band' => $this->countBand((int) $row->entities), 'observations_band' => $this->countBand((int) $row->observations),
'scope' => ['id' => $cellScope->id, 'name' => $cellScope->name, 'level' => $cellScope->location_type], 'first_year' => null, 'last_year' => null];
}
$years = [];
$unknownPeriod = false;
foreach (DB::table('proactive_supply_sources')->whereRaw("receipt->>'candidate' = ? AND receipt->>'access' = 'internal'", ['a022ed9848b41ba342b4209414c2bbf2ebd0a7a3'])->pluck('period') as $period) {
preg_match_all('/\b(?:19|20)[0-9]{2}\b/', $period, $matches);
if (! $matches[0]) {
$unknownPeriod = true;
}
$years = [...$years, ...$matches[0]];
}
sort($years);
$period = $years ? $years[0].($years[0] !== $years[array_key_last($years)] ? '–'.$years[array_key_last($years)] : '') : 'Por confirmar';
return ['privacy_version' => 1, 'privacy_policy' => 'fixed_district_cae_division_v1',
'requested_territory' => ['id' => $territory, 'name' => $path->last()->name],
'scope' => ['id' => $scope->id, 'name' => $scope->name, 'level' => $scope->location_type],
'broader_context' => $scope->id !== $territory, 'entities' => null, 'repeated' => null, 'uncertain' => null,
'groups' => $groups, 'truncated' => false, 'coverage_unknown' => true,
'filters' => ['sector_id' => $sector, 'activity_id' => $activity?->id, 'activity_label' => $activity?->label_pt_mz, 'division_code' => $division],
'source_summary' => ['label' => 'Base administrativa empresarial histórica incorporada no portal', 'period' => $period, 'incomplete_periods' => $unknownPeriod],
'limitations' => self::LIMITATIONS,
'interpretation' => 'Intervalos de cinco identidades documentais, nunca empresas únicas confirmadas. Observações consolidadas e identidades são medidas distintas. Uma identidade pode constar em várias actividades; não some grupos. Grupos com menos de cinco identidades, conflitos e dados sem classificação publicada não são divulgados; nenhum resultado pode significar ausência de registos ou protecção de informação.',
'privacy_notice' => 'A precisão mínima é distrital e a actividade é apresentada por divisão CAE. Não há totais marginais, contagens exactas, filtros livres ou datas por grupo. Dados apenas provinciais são contexto provincial, não implantação no distrito. Estes controlos reduzem a identificação indirecta; não constituem garantia matemática contra conhecimento externo.'];
} finally {
DB::select("select set_config('app.actor_user_id',?,true),set_config('app.proactive_supply_read',?,true)", [$previous->actor ?? '', $previous->scope ?? '']);
}
});
}
private function countBand(int $count): string
{
$lower = intdiv($count, 5) * 5;
return $lower.'–'.($lower + 4);
}
/** @return Collection<int,\stdClass> */
private function publicAggregateActivities(): Collection
{
$rows = app(EconomicReferenceCatalog::class)->activities()->keyBy('id');
return $rows->map(function ($row) use ($rows) {
$result = clone $row;
$current = $row;
$result->effective_sector_id = null;
for ($i = 0; $current && $i < 10; $i++) {
if ($current->sector_id !== null) {
$result->effective_sector_id = (int) $current->sector_id;
break;
}
$current = $current->parent_id ? $rows->get($current->parent_id) : null;
}
return $result;
})->values();
}
/** @return list<array{id:string,code:string,label:string}> */
public function aggregateActivityOptions(?int $sector): array
{
$rows = $this->publicAggregateActivities();
$divisions = $rows->filter(fn ($a) => preg_match('/^[0-9]{2}$/D', $a->code) === 1)->pluck('code')->all();
$result = [];
foreach ($rows as $a) {
if (! is_string($a->id) || ! is_string($a->code) || ! is_string($a->label_pt_mz)) {
continue;
}
if (($sector && $a->effective_sector_id !== $sector) || ! preg_match('/^[0-9]{2,5}$/D', str_replace('.', '', $a->code))
|| ! in_array(substr(str_replace('.', '', $a->code), 0, 2), $divisions, true)) {
continue;
}
$result[] = ['id' => $a->id, 'code' => $a->code, 'label' => $a->label_pt_mz];
}
return $result;
}
/** Old exact QA snapshots stay stored, but are not exposed by the protected capability.
* @param list<array<string,mixed>> $history
* @return list<array<string,mixed>>
*/
public function protectedHistory(array $history): array
{
$result = [];
foreach ($history as $row) {
if (($row['privacy_version'] ?? null) !== 1 || ($row['privacy_policy'] ?? null) !== 'fixed_district_cae_division_v1') {
continue;
}
$safe = Arr::only($row, ['privacy_version', 'privacy_policy', 'requested_territory', 'scope', 'broader_context', 'truncated', 'coverage_unknown', 'limitations', 'interpretation', 'privacy_notice', 'context_revision', 'recorded_at', 'operation_key']);
$safe['entities'] = $safe['repeated'] = $safe['uncertain'] = null;
$safe['groups'] = [];
foreach ($row['groups'] ?? [] as $group) {
if (! preg_match('/^[0-9]{2}$/D', $group['code'] ?? '') || ! $this->validBand($group['entities_band'] ?? '') || ! $this->validBand($group['observations_band'] ?? '')) {
continue;
}
$safe['groups'][] = [...Arr::only($group, ['code', 'label', 'entities_band', 'observations_band', 'scope']), 'total' => null, 'first_year' => null, 'last_year' => null];
}
$safe['filters'] = Arr::only($row['filters'] ?? [], ['sector_id', 'activity_id', 'activity_label', 'division_code']);
$safe['source_summary'] = Arr::only($row['source_summary'] ?? [], ['label', 'period', 'incomplete_periods']);
$result[] = $safe;
}
return $result;
}
private function validBand(string $band): bool
{
return preg_match('/^([0-9]{1,12})–([0-9]{1,12})$/uD', $band, $m) === 1
&& (int) $m[1] >= 5 && (int) $m[1] % 5 === 0 && (int) $m[2] - (int) $m[1] === 4;
}
/** No names, contacts, row identifiers or free-text activity descriptions leave this boundary.
* @return array<string,mixed>
*/
public function aggregate(User $actor, string $territory): array
{
abort_unless(config('investor-path.aggregate_supply_qa_enabled') && \App\Support\IsolatedQaDatabase::allowsSchemaChanges(), 404);
abort_unless(config('investor-path.aggregate_supply_qa_enabled') && IsolatedQaDatabase::allowsSchemaChanges(), 404);
$path = app(InvestmentGeography::class)->path($territory);
abort_if($path === [], 422, 'Confirme primeiro a localização do dossier.');
return $this->read($actor, function () use ($path, $territory): array {
$labels = app(EconomicReferenceCatalog::class)->activities()->groupBy('code')->map(fn ($rows) => $rows->pluck('label_pt_mz')->unique()->count() === 1 ? $rows->first()->label_pt_mz : null);
$scope = collect($path)->last();
@@ -58,10 +266,11 @@ final class ProactiveSupply
$base = $query($district->id);
}
}
$counts = (clone $base)->selectRaw("count(DISTINCT entity_key) FILTER (WHERE duplicate_of IS NULL AND NOT identity_conflict) AS entities, count(*) FILTER (WHERE duplicate_of IS NOT NULL) AS repeated, count(*) FILTER (WHERE identity_conflict) AS uncertain")->first();
$counts = (clone $base)->selectRaw('count(DISTINCT entity_key) FILTER (WHERE duplicate_of IS NULL AND NOT identity_conflict) AS entities, count(*) FILTER (WHERE duplicate_of IS NOT NULL) AS repeated, count(*) FILTER (WHERE identity_conflict) AS uncertain')->first();
$code = "CASE WHEN cae ~ '^[0-9.]{1,10}$' THEN cae ELSE '' END";
$groups = (clone $base)->whereNull('duplicate_of')->where('identity_conflict', false)->selectRaw($code." AS code, count(DISTINCT entity_key) AS total, min(substring(source_date from '(19[0-9]{2}|20[0-9]{2})')) AS first_year, max(substring(source_date from '(19[0-9]{2}|20[0-9]{2})')) AS last_year")
->groupByRaw($code)->orderByRaw($code)->limit(101)->get()->map(fn ($row) => ['code' => $row->code, 'label' => $row->code ? ($labels->get($row->code) ?? 'CAE '.$row->code.' — descrição por confirmar') : 'Actividade por classificar', 'total' => (int) $row->total, 'first_year' => $row->first_year, 'last_year' => $row->last_year])->all();
return ['requested_territory' => ['id' => $territory, 'name' => collect($path)->last()->name], 'scope' => ['id' => $scope->id, 'name' => $scope->name, 'level' => $scope->location_type],
'broader_context' => $scope->id !== $territory, 'entities' => (int) $counts->entities, 'repeated' => (int) $counts->repeated, 'uncertain' => (int) $counts->uncertain,
'groups' => array_slice($groups, 0, 100), 'truncated' => count($groups) > 100, 'coverage_unknown' => true, 'limitations' => self::LIMITATIONS,