Mapa de erros 4.2: fundos mostram primeiro o local pesquisado

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 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01MzJmxFy3izprXr5dSq91jm
This commit is contained in:
Claude
2026-12-04 17:30:39 +02:00
committed by nala
parent 4f71ba7d20
commit bb2529f624
4 changed files with 129 additions and 2 deletions
@@ -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({
</section>
);
}
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.
</p>
)}
{visibleLocations.length > 0 && (
{byPlace && (
<div className="space-y-2">
<h3 className="font-semibold">Localização no local pesquisado</h3>
{byPlace.local.length > 0 ? (
<ul className="space-y-1">
{byPlace.local.map((l, i) => (
<li key={i}>
{locationRole(l.role)}
{l.address}
{l.hours ? " · " + l.hours : ""}
</li>
))}
</ul>
) : (
<p>Sem sede ou balcão documentado no local pesquisado.</p>
)}
{byPlace.others.length > 0 && (
<details>
<summary className="cursor-pointer underline">
Outras localizações (
{byPlace.others.reduce((n, g) => n + g.items.length, 0)})
</summary>
<div className="mt-2 space-y-2">
{byPlace.others.map((group) => (
<div key={group.province}>
<h4 className="font-medium">{group.province}</h4>
<ul className="space-y-1">
{group.items.map((l, i) => (
<li key={i}>
{locationRole(l.role)}
{l.address}
{l.hours ? " · " + l.hours : ""}
</li>
))}
</ul>
</div>
))}
</div>
</details>
)}
</div>
)}
{!byPlace && visibleLocations.length > 0 && (
<div>
<h3 className="font-semibold">
{physicalTerritory && compact
@@ -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<string, T[]>();
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 {
+1
View File
@@ -550,6 +550,7 @@ function Index({
? filters.territory
: undefined
}
focusTerritory={filters.territory || undefined}
value={
item.payload as InstitutionDetails & {
title: string;
@@ -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, []);
});