From bb2529f624e038fb0ea4febd4c1e4a7f2c9b55ba Mon Sep 17 00:00:00 2001 From: Claude Date: Fri, 4 Dec 2026 16:37:19 +0200 Subject: [PATCH] Mapa de erros 4.2: fundos mostram primeiro o local pesquisado MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Numa pesquisa por cobertura, cada instituição listava todos os balcões do país sem destacar a província escolhida. Passa a mostrar primeiro a sede e os balcões do local pesquisado (ou a indicação de que não há), e os restantes numa secção «Outras localizações», agrupados por província. Notas de recolha que não são moradas («Não encontrei agência…», «Ver secção 7…») deixam de ser mostradas. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01MzJmxFy3izprXr5dSq91jm --- .../js/components/funding-institution.tsx | 55 ++++++++++++++++++- .../js/lib/funding-public-presentation.ts | 36 +++++++++++- resources/js/pages/funding/index.tsx | 1 + .../funding-public-presentation.test.mjs | 39 +++++++++++++ 4 files changed, 129 insertions(+), 2 deletions(-) diff --git a/resources/js/components/funding-institution.tsx b/resources/js/components/funding-institution.tsx index 012cd5f0..358210ff 100644 --- a/resources/js/components/funding-institution.tsx +++ b/resources/js/components/funding-institution.tsx @@ -8,6 +8,7 @@ import { publicInstitutionContacts, publicInstitutionLocations, matchingInstitutionLocations, + institutionLocationsByPlace, } from "@/lib/funding-public-presentation"; import { financialCategories, financialTypes } from "@/components/funding-classification"; import { card, input, button } from "@/pages/investment/shared"; @@ -500,14 +501,20 @@ export function InstitutionFields({ ); } +const locationRole = (role: string) => + role === "headquarters" ? "Sede: " : role === "branch" ? "Balcão: " : ""; + export function InstitutionView({ value, compact = false, physicalTerritory, + focusTerritory, }: { value: InstitutionDetails & { title?: string; summary?: string }; compact?: boolean; physicalTerritory?: string; + /** Searched place (coverage search): its points first, the rest grouped by province. */ + focusTerritory?: string; }) { const presentation = fundingPublicPresentation({ ...value, @@ -533,6 +540,10 @@ export function InstitutionView({ })), ) : locations; + const byPlace = + compact && !physicalTerritory && focusTerritory + ? institutionLocationsByPlace(value.locations, focusTerritory) + : null; const category = (value.institution_categories ?? []) .map((k) => financialCategories[k]) .filter(Boolean) @@ -640,7 +651,49 @@ export function InstitutionView({ Presença física por confirmar no local escolhido.

)} - {visibleLocations.length > 0 && ( + {byPlace && ( +
+

Localização no local pesquisado

+ {byPlace.local.length > 0 ? ( +
    + {byPlace.local.map((l, i) => ( +
  • + {locationRole(l.role)} + {l.address} + {l.hours ? " · " + l.hours : ""} +
  • + ))} +
+ ) : ( +

Sem sede ou balcão documentado no local pesquisado.

+ )} + {byPlace.others.length > 0 && ( +
+ + Outras localizações ( + {byPlace.others.reduce((n, g) => n + g.items.length, 0)}) + +
+ {byPlace.others.map((group) => ( +
+

{group.province}

+
    + {group.items.map((l, i) => ( +
  • + {locationRole(l.role)} + {l.address} + {l.hours ? " · " + l.hours : ""} +
  • + ))} +
+
+ ))} +
+
+ )} +
+ )} + {!byPlace && visibleLocations.length > 0 && (

{physicalTerritory && compact diff --git a/resources/js/lib/funding-public-presentation.ts b/resources/js/lib/funding-public-presentation.ts index bc929ec5..3571b60f 100644 --- a/resources/js/lib/funding-public-presentation.ts +++ b/resources/js/lib/funding-public-presentation.ts @@ -125,7 +125,7 @@ export function publicInstitutionLocations< for (const row of [...rows].sort( (a, b) => (a.role === "headquarters" ? 0 : 1) - (b.role === "headquarters" ? 0 : 1), )) { - if (!row.address?.trim()) continue; + if (!row.address?.trim() || isResearchNote(row.address)) continue; const address = normal(row.address); const duplicate = result.some((previous) => { const other = normal(previous.address ?? ""); @@ -152,6 +152,40 @@ export function publicInstitutionLocations< return result; } +/** Notes left during data collection ("não encontrei…", "ver secção 7…") are not addresses. */ +export function isResearchNote(address: string) { + return /^\s*(n[ãa]o encontrei|ver sec[çc][ãa]o\b|sem morada\b)/i.test(address); +} + +/** + * The searched place first; every other documented point grouped by province, + * so a national list of branches does not hide the one the investor asked about. + */ +export function institutionLocationsByPlace< + T extends { + role: string; + address?: string | null; + location_text?: string; + territory_id?: string; + territory_path?: { id?: string; name: string }[]; + evidence_status?: string | null; + }, +>(rows: T[], territory: string) { + const visible = publicInstitutionLocations(rows); + const local = matchingInstitutionLocations(visible, territory); + const unknown = "Localização por confirmar"; + const groups = new Map(); + for (const row of visible) { + if (local.includes(row)) continue; + const province = row.territory_path?.[0]?.name?.trim() || unknown; + groups.set(province, [...(groups.get(province) ?? []), row]); + } + const others = [...groups.entries()] + .sort(([a], [b]) => (a === unknown ? 1 : b === unknown ? -1 : a.localeCompare(b, "pt"))) + .map(([province, items]) => ({ province, items })); + return { local, others }; +} + /** Only a documented point inside the requested territory establishes local presence. */ export function matchingInstitutionLocations< T extends { diff --git a/resources/js/pages/funding/index.tsx b/resources/js/pages/funding/index.tsx index 70a96e61..c45239c6 100644 --- a/resources/js/pages/funding/index.tsx +++ b/resources/js/pages/funding/index.tsx @@ -550,6 +550,7 @@ function Index({ ? filters.territory : undefined } + focusTerritory={filters.territory || undefined} value={ item.payload as InstitutionDetails & { title: string; diff --git a/tests/frontend/funding-public-presentation.test.mjs b/tests/frontend/funding-public-presentation.test.mjs index 0d4baafd..f900f66e 100644 --- a/tests/frontend/funding-public-presentation.test.mjs +++ b/tests/frontend/funding-public-presentation.test.mjs @@ -313,3 +313,42 @@ test("compact institutions show local address and explicitly identify unconfirme assert.match(render("tete", false), /Sede Maputo/); assert.match(render("tete", false), /Balcão de Tete/); }); + +test('a coverage search shows the searched place first and groups other branches by province, without research notes (mapa de erros 4.2)', async () => { + const { institutionLocationsByPlace, isResearchNote } = await import( + '../../resources/js/lib/funding-public-presentation.ts' + ); + const cabo = { id: 'cabo', name: 'Cabo Delgado' }; + const maputo = { id: 'maputo', name: 'Maputo Cidade' }; + const nampula = { id: 'nampula', name: 'Nampula' }; + const point = (role, address, path) => ({ + role, + address, + location_text: '', + territory_id: path.at(-1).id, + territory_path: path, + evidence_status: 'reported', + }); + const rows = [ + point('headquarters', 'Av. 25 de Setembro, nº 4', [maputo]), + point('branch', 'Av. Eduardo Mondlane, nº 323', [nampula]), + point('branch', 'Rua do Comércio, Pemba', [cabo, { id: 'pemba', name: 'Pemba' }]), + point('branch', 'Av. Karl Marx', [maputo]), + point('branch', 'Não encontrei agência nem morada', [cabo]), + point('branch', 'Ver secção 7 (balcões Mpoint por província)', [cabo]), + ]; + const { local, others } = institutionLocationsByPlace(rows, 'cabo'); + assert.deepEqual(local.map((row) => row.address), ['Rua do Comércio, Pemba']); + assert.deepEqual( + others.map((group) => [group.province, group.items.map((row) => row.address)]), + [ + ['Maputo Cidade', ['Av. 25 de Setembro, nº 4', 'Av. Karl Marx']], + ['Nampula', ['Av. Eduardo Mondlane, nº 323']], + ], + ); + assert.equal(isResearchNote('Não encontrei agência nem morada'), true); + assert.equal(isResearchNote('Ver secção 7 (balcões Mpoint por província)'), true); + assert.equal(isResearchNote('Av. 25 de Setembro, nº 4'), false); + // A place without documented points says so instead of showing the whole country first. + assert.deepEqual(institutionLocationsByPlace(rows, 'tete').local, []); +});