merge: retain installed funding correction alongside isolated sector QA

This commit is contained in:
2026-11-15 20:32:04 +02:00
13 changed files with 858 additions and 764 deletions
+162 -211
View File
@@ -2,17 +2,15 @@ import {
ClassificationFields,
evidenceLabels,
type Classification,
} from '@/components/funding-classification';
} from "@/components/funding-classification";
import {
fundingPublicPresentation,
publicInstitutionContacts,
publicInstitutionLocations,
} from '@/lib/funding-public-presentation';
import {
financialCategories,
financialTypes,
} from '@/components/funding-classification';
import { card, input, button } from '@/pages/investment/shared';
matchingInstitutionLocations,
} from "@/lib/funding-public-presentation";
import { financialCategories, financialTypes } from "@/components/funding-classification";
import { card, input, button } from "@/pages/investment/shared";
export type FundingSource = {
source_url: string | null;
@@ -33,7 +31,7 @@ export type FundingLocation = FundingSource & {
label: string;
address: string;
territory_id: string;
territory_path?: { name: string }[];
territory_path?: { id?: string; name: string }[];
};
export type FundingProduct = FundingSource & {
code: string;
@@ -57,22 +55,22 @@ export type InstitutionDetails = Classification & {
products: FundingProduct[];
};
export const emptyInstitution: InstitutionDetails = {
institution_key: '',
institution_nature: 'unknown',
institution_key: "",
institution_nature: "unknown",
contacts: [],
locations: [],
products: [],
};
const source: FundingSource = {
source_url: '',
source_reference: '',
checked_on: '',
source_url: "",
source_reference: "",
checked_on: "",
};
function Text({
label,
value,
change,
type = 'text',
type = "text",
}: {
label: string;
value: string;
@@ -85,8 +83,8 @@ function Text({
<input
className={input}
type={type}
value={value ?? ''}
maxLength={type === 'text' ? 1500 : undefined}
value={value ?? ""}
maxLength={type === "text" ? 1500 : undefined}
onChange={(e) => change(e.target.value)}
/>
</label>
@@ -105,24 +103,20 @@ function Sources({
<Text
label="Endereço da fonte"
type="url"
value={value.source_url ?? ''}
value={value.source_url ?? ""}
change={(source_url) => change({ source_url })}
/>
{!value.source_url && (
<>
<Text
label="Documento de origem"
value={value.source_document_name ?? ''}
change={(source_document_name) =>
change({ source_document_name })
}
value={value.source_document_name ?? ""}
change={(source_document_name) => change({ source_document_name })}
/>
<Text
label="SHA-256 do original"
value={value.source_document_sha256 ?? ''}
change={(source_document_sha256) =>
change({ source_document_sha256 })
}
value={value.source_document_sha256 ?? ""}
change={(source_document_sha256) => change({ source_document_sha256 })}
/>
</>
)}
@@ -131,10 +125,8 @@ function Sources({
Situação desta informação
<select
className={input}
value={value.evidence_status ?? 'to_confirm'}
onChange={(e) =>
change({ evidence_status: e.target.value })
}
value={value.evidence_status ?? "to_confirm"}
onChange={(e) => change({ evidence_status: e.target.value })}
>
{Object.entries(evidenceLabels).map(([key, label]) => (
<option key={key} value={key}>
@@ -171,22 +163,20 @@ export function InstitutionFields({
natures: Record<string, string>;
needs: Record<string, string>;
}) {
const update = <K extends 'contacts' | 'locations' | 'products'>(
const update = <K extends "contacts" | "locations" | "products">(
key: K,
index: number,
patch: Partial<InstitutionDetails[K][number]>,
) =>
change({
[key]: value[key].map((row, i) =>
i === index ? { ...row, ...patch } : row,
),
[key]: value[key].map((row, i) => (i === index ? { ...row, ...patch } : row)),
});
return (
<section className={card}>
<h2>Instituição, presença física e produtos</h2>
<p>
A sede ou balcão não determina a cobertura do financiamento. Não
preencha condições desconhecidas por inferência.
A sede ou balcão não determina a cobertura do financiamento. Não preencha condições
desconhecidas por inferência.
</p>
<ClassificationFields value={value} change={change} needs={needs} />
<Text
@@ -199,9 +189,7 @@ export function InstitutionFields({
<select
className={input}
value={value.institution_nature}
onChange={(e) =>
change({ institution_nature: e.target.value })
}
onChange={(e) => change({ institution_nature: e.target.value })}
>
{Object.entries(natures).map(([id, label]) => (
<option key={id} value={id}>
@@ -218,15 +206,13 @@ export function InstitutionFields({
<select
className={input}
value={row.kind}
onChange={(e) =>
update('contacts', i, { kind: e.target.value })
}
onChange={(e) => update("contacts", i, { kind: e.target.value })}
>
{Object.entries({
website: 'Página institucional',
email: 'Email',
phone: 'Telefone',
address: 'Endereço postal',
website: "Página institucional",
email: "Email",
phone: "Telefone",
address: "Endereço postal",
}).map(([id, label]) => (
<option key={id} value={id}>
{label}
@@ -236,19 +222,14 @@ export function InstitutionFields({
<Text
label="Contacto documentado"
value={row.value}
change={(v) => update('contacts', i, { value: v })}
/>
<Sources
value={row}
change={(v) => update('contacts', i, v)}
change={(v) => update("contacts", i, { value: v })}
/>
<Sources value={row} change={(v) => update("contacts", i, v)} />
<button
type="button"
onClick={() =>
change({
contacts: value.contacts.filter(
(_, n) => n !== i,
),
contacts: value.contacts.filter((_, n) => n !== i),
})
}
>
@@ -264,7 +245,7 @@ export function InstitutionFields({
change({
contacts: [
...value.contacts,
{ ...source, kind: 'website', value: '' },
{ ...source, kind: "website", value: "" },
],
})
}
@@ -273,18 +254,14 @@ export function InstitutionFields({
</button>
</details>
<details>
<summary>
Sede e balcões ({value.locations.length}) — Alterar
</summary>
<summary>Sede e balcões ({value.locations.length}) — Alterar</summary>
{value.locations.map((row, i) => (
<fieldset className={card} key={i}>
<legend>Local físico {i + 1}</legend>
<select
className={input}
value={row.role}
onChange={(e) =>
update('locations', i, { role: e.target.value })
}
onChange={(e) => update("locations", i, { role: e.target.value })}
>
<option value="headquarters">Sede</option>
<option value="branch">Balcão</option>
@@ -295,31 +272,25 @@ export function InstitutionFields({
<Text
label="Designação"
value={row.label}
change={(label) =>
update('locations', i, { label })
}
change={(label) => update("locations", i, { label })}
/>
<Text
label="Morada documentada"
value={row.address}
change={(address) =>
update('locations', i, { address })
}
change={(address) => update("locations", i, { address })}
/>
<label>
Local do cadastro
<select
className={input}
value={row.territory_id ?? ''}
value={row.territory_id ?? ""}
onChange={(e) =>
update('locations', i, {
update("locations", i, {
territory_id: e.target.value,
})
}
>
<option value="">
Correspondência por confirmar
</option>
<option value="">Correspondência por confirmar</option>
{territories.map((t) => (
<option key={t.id} value={t.id}>
{t.name}
@@ -327,17 +298,12 @@ export function InstitutionFields({
))}
</select>
</label>
<Sources
value={row}
change={(v) => update('locations', i, v)}
/>
<Sources value={row} change={(v) => update("locations", i, v)} />
<button
type="button"
onClick={() =>
change({
locations: value.locations.filter(
(_, n) => n !== i,
),
locations: value.locations.filter((_, n) => n !== i),
})
}
>
@@ -355,10 +321,10 @@ export function InstitutionFields({
...value.locations,
{
...source,
role: 'headquarters',
label: '',
address: '',
territory_id: '',
role: "headquarters",
label: "",
address: "",
territory_id: "",
},
],
})
@@ -368,23 +334,19 @@ export function InstitutionFields({
</button>
</details>
<details>
<summary>
Programas/produtos ({value.products.length}) — Alterar
</summary>
<summary>Programas/produtos ({value.products.length}) — Alterar</summary>
{value.products.map((row, i) => (
<fieldset className={card} key={row.code}>
<legend>Produto {i + 1}</legend>
<Text
label="Nome documentado"
value={row.title}
change={(title) => update('products', i, { title })}
change={(title) => update("products", i, { title })}
/>
<Text
label="Instrumento"
value={row.instrument}
change={(instrument) =>
update('products', i, { instrument })
}
change={(instrument) => update("products", i, { instrument })}
/>
<label>
Necessidades documentadas
@@ -393,11 +355,8 @@ export function InstitutionFields({
className={input}
value={row.needs}
onChange={(e) =>
update('products', i, {
needs: Array.from(
e.target.selectedOptions,
(o) => o.value,
),
update("products", i, {
needs: Array.from(e.target.selectedOptions, (o) => o.value),
})
}
>
@@ -414,22 +373,18 @@ export function InstitutionFields({
className={input}
value={row.coverage}
onChange={(e) =>
update('products', i, {
update("products", i, {
coverage: e.target.value,
territory_ids: [],
})
}
>
<option value="unknown">Por confirmar</option>
<option value="national">
Nacional, segundo a fonte
</option>
<option value="territorial">
Locais documentados
</option>
<option value="national">Nacional, segundo a fonte</option>
<option value="territorial">Locais documentados</option>
</select>
</label>
{row.coverage === 'territorial' && (
{row.coverage === "territorial" && (
<label>
Locais abrangidos
<select
@@ -437,7 +392,7 @@ export function InstitutionFields({
className={input}
value={row.territory_ids}
onChange={(e) =>
update('products', i, {
update("products", i, {
territory_ids: Array.from(
e.target.selectedOptions,
(o) => o.value,
@@ -456,9 +411,7 @@ export function InstitutionFields({
<Text
label="Explicação da cobertura"
value={row.coverage_text}
change={(coverage_text) =>
update('products', i, { coverage_text })
}
change={(coverage_text) => update("products", i, { coverage_text })}
/>
<label>
Condições — vazio significa por confirmar
@@ -467,7 +420,7 @@ export function InstitutionFields({
value={row.conditions}
maxLength={4000}
onChange={(e) =>
update('products', i, {
update("products", i, {
conditions: e.target.value,
})
}
@@ -476,9 +429,7 @@ export function InstitutionFields({
<Text
label="Montantes/moeda/prazos documentados — se conhecidos"
value={row.amounts}
change={(amounts) =>
update('products', i, { amounts })
}
change={(amounts) => update("products", i, { amounts })}
/>
<label>
Disponibilidade
@@ -486,40 +437,30 @@ export function InstitutionFields({
className={input}
value={row.availability}
onChange={(e) =>
update('products', i, {
update("products", i, {
availability: e.target.value,
})
}
>
<option value="unknown">Por confirmar</option>
<option value="documented">
Oferta identificada na fonte (não é
concessão)
</option>
<option value="closed">
Encerrada segundo a fonte
Oferta identificada na fonte (não é concessão)
</option>
<option value="closed">Encerrada segundo a fonte</option>
</select>
</label>
<Text
label="Canal de contacto do produto"
type="url"
value={row.contact_url}
change={(contact_url) =>
update('products', i, { contact_url })
}
/>
<Sources
value={row}
change={(v) => update('products', i, v)}
change={(contact_url) => update("products", i, { contact_url })}
/>
<Sources value={row} change={(v) => update("products", i, v)} />
<button
type="button"
onClick={() =>
change({
products: value.products.filter(
(_, n) => n !== i,
),
products: value.products.filter((_, n) => n !== i),
})
}
>
@@ -538,16 +479,16 @@ export function InstitutionFields({
{
...source,
code: crypto.randomUUID(),
title: '',
instrument: '',
title: "",
instrument: "",
needs: [],
coverage: 'unknown',
coverage_text: '',
coverage: "unknown",
coverage_text: "",
territory_ids: [],
conditions: '',
amounts: '',
availability: 'unknown',
contact_url: '',
conditions: "",
amounts: "",
availability: "unknown",
contact_url: "",
},
],
})
@@ -562,35 +503,51 @@ export function InstitutionFields({
export function InstitutionView({
value,
compact = false,
physicalTerritory,
}: {
value: InstitutionDetails & { title?: string; summary?: string };
compact?: boolean;
physicalTerritory?: string;
}) {
const presentation = fundingPublicPresentation({
...value,
title: value.title ?? '',
summary: value.summary ?? '',
kind: 'institution',
title: value.title ?? "",
summary: value.summary ?? "",
kind: "institution",
}),
contacts = publicInstitutionContacts(value.contacts),
locations = publicInstitutionLocations(value.locations);
const localPresence = physicalTerritory
? matchingInstitutionLocations(value.locations, physicalTerritory)
: [];
const visibleLocations =
physicalTerritory && compact
? publicInstitutionLocations(
localPresence.map((location) => ({
...location,
address:
location.address?.trim() ||
location.territory_path?.map((node) => node.name).join(" / ") ||
location.location_text ||
location.label,
})),
)
: locations;
const category = (value.institution_categories ?? [])
.map((k) => financialCategories[k])
.filter(Boolean)
.join(' · '),
.join(" · "),
types = (value.institution_types ?? [])
.map((k) => financialTypes[k])
.filter(Boolean)
.join(' · ');
.join(" · ");
const urls = new Set<string>(
compact
? []
: presentation.products
.map((p) => p.url)
.filter((url): url is string => !!url),
: presentation.products.map((p) => p.url).filter((url): url is string => !!url),
);
const sites = contacts
.filter((c) => c.kind === 'website')
.filter((c) => c.kind === "website")
.filter((c) => {
if (urls.has(c.value)) return false;
urls.add(c.value);
@@ -598,11 +555,7 @@ export function InstitutionView({
});
return (
<section className="space-y-4" aria-label="Informação da instituição">
{!compact && (
<h2 className="text-lg font-semibold">
Instituição financeira
</h2>
)}
{!compact && <h2 className="text-lg font-semibold">Instituição financeira</h2>}
<div>
<h3 className="font-semibold">Descrição</h3>
<p>{presentation.description}</p>
@@ -610,13 +563,13 @@ export function InstitutionView({
{(category || types) && (
<p>
{category}
{category && types ? ': ' : ''}
{category && types ? ": " : ""}
{types}
</p>
)}
{['historical', 'merged'].includes(
value.institution_status ?? '',
) && <p>Instituição histórica ou integrada noutra entidade.</p>}
{["historical", "merged"].includes(value.institution_status ?? "") && (
<p>Instituição histórica ou integrada noutra entidade.</p>
)}
<div>
<h3 className="font-semibold">Produtos e serviços</h3>
{presentation.products.length ? (
@@ -635,77 +588,75 @@ export function InstitutionView({
) : (
<strong>{p.title}</strong>
)}
{p.detail ? ': ' + p.detail : ''}
{p.detail ? ": " + p.detail : ""}
</li>
))}
</ul>
) : presentation.services.length ? (
<p>{presentation.services.join(' · ')}</p>
<p>{presentation.services.join(" · ")}</p>
) : null}
</div>
{!compact &&
(sites.length > 0 ||
contacts.some((c) => c.kind !== 'website')) && (
<div>
<h3 className="font-semibold">Contactos</h3>
<ul className="space-y-1">
{sites.map((c) => (
<li key={c.value}>
<a
className="underline"
href={c.value}
target="_blank"
rel="noopener noreferrer"
>
Website
</a>
{!compact && (sites.length > 0 || contacts.some((c) => c.kind !== "website")) && (
<div>
<h3 className="font-semibold">Contactos</h3>
<ul className="space-y-1">
{sites.map((c) => (
<li key={c.value}>
<a
className="underline"
href={c.value}
target="_blank"
rel="noopener noreferrer"
>
Website
</a>
</li>
))}
{contacts
.filter((c) => c.kind !== "website")
.map((c) => (
<li key={c.kind + c.value}>
{c.kind === "email" ? (
<a className="underline" href={"mailto:" + c.value}>
{c.value}
</a>
) : c.kind === "phone" ? (
<a
className="underline"
href={"tel:" + c.value.replace(/[^+0-9]/g, "")}
>
{c.value}
</a>
) : (
c.value
)}
</li>
))}
{contacts
.filter((c) => c.kind !== 'website')
.map((c) => (
<li key={c.kind + c.value}>
{c.kind === 'email' ? (
<a
className="underline"
href={'mailto:' + c.value}
>
{c.value}
</a>
) : c.kind === 'phone' ? (
<a
className="underline"
href={
'tel:' +
c.value.replace(
/[^+0-9]/g,
'',
)
}
>
{c.value}
</a>
) : (
c.value
)}
</li>
))}
</ul>
</div>
)}
{!compact && locations.length > 0 && (
</ul>
</div>
)}
{physicalTerritory && localPresence.length === 0 && (
<p className="rounded border border-amber-600 p-2">
Presença física por confirmar no local escolhido.
</p>
)}
{visibleLocations.length > 0 && (
<div>
<h3 className="font-semibold">Localização</h3>
<h3 className="font-semibold">
{physicalTerritory && compact
? "Presença no local escolhido"
: "Localização"}
</h3>
<ul className="space-y-1">
{locations.map((l, i) => (
{visibleLocations.map((l, i) => (
<li key={i}>
{l.role === 'headquarters'
? 'Sede: '
: l.role === 'branch'
? 'Balcão: '
: ''}
{l.role === "headquarters"
? "Sede: "
: l.role === "branch"
? "Balcão: "
: ""}
{l.address}
{l.hours ? ' · ' + l.hours : ''}
{l.hours ? " · " + l.hours : ""}
</li>
))}
</ul>