286 lines
12 KiB
TypeScript
286 lines
12 KiB
TypeScript
import { Link } from "@inertiajs/react";
|
|
import { useRef, useState } from "react";
|
|
import {
|
|
financialCategories,
|
|
financialTypes,
|
|
} from "@/components/funding-classification";
|
|
import { profileJson } from "@/lib/profile-json";
|
|
import { Frame, card, button } from "@/pages/investment/shared";
|
|
|
|
type Item = {
|
|
code: string;
|
|
title: string;
|
|
summary: string;
|
|
source_document: string | null;
|
|
source_sha256: string | null;
|
|
categories: string[];
|
|
types: string[];
|
|
status: string;
|
|
locations: unknown[];
|
|
contacts: unknown[];
|
|
published: boolean;
|
|
};
|
|
type Receipt = {
|
|
id: string;
|
|
matches_reference: boolean;
|
|
publication: { version: number; hash: string } | null;
|
|
published?: boolean;
|
|
};
|
|
|
|
export default function Assisted({
|
|
batch,
|
|
}: {
|
|
batch: { dataset_hash: string; items: Item[] };
|
|
}) {
|
|
const [selected, setSelected] = useState<string[]>([]);
|
|
const [receipts, setReceipts] = useState<Record<string, Receipt>>({});
|
|
const [reviewed, setReviewed] = useState(false);
|
|
const [confirmed, setConfirmed] = useState(false);
|
|
const [reason, setReason] = useState("");
|
|
const [busy, setBusy] = useState(false);
|
|
const [message, setMessage] = useState("");
|
|
const running = useRef(false);
|
|
const stop = useRef(false);
|
|
const pending = useRef<Record<string, object>>({});
|
|
async function run(publish: boolean) {
|
|
if (
|
|
running.current ||
|
|
!reviewed ||
|
|
!selected.length ||
|
|
(publish && (!confirmed || reason.trim().length < 20))
|
|
)
|
|
return;
|
|
running.current = true;
|
|
stop.current = false;
|
|
setBusy(true);
|
|
setMessage("");
|
|
try {
|
|
for (const code of selected) {
|
|
if (stop.current) break;
|
|
setMessage(
|
|
`${publish ? "Publicação" : "Preparação"}: ${batch.items.find((i) => i.code === code)?.title}`,
|
|
);
|
|
if (publish) {
|
|
const receipt = receipts[code];
|
|
if (
|
|
!receipt?.matches_reference ||
|
|
!receipt.publication ||
|
|
receipt.published
|
|
)
|
|
continue;
|
|
const body = pending.current[code] ?? {
|
|
...receipt.publication,
|
|
reason: reason.trim(),
|
|
confirm: true,
|
|
};
|
|
pending.current[code] = body;
|
|
await profileJson(
|
|
`/management/fundos/${receipt.id}/publicar`,
|
|
body,
|
|
);
|
|
setReceipts((old) => ({
|
|
...old,
|
|
[code]: { ...receipt, published: true },
|
|
}));
|
|
delete pending.current[code];
|
|
} else {
|
|
const plan = await profileJson(
|
|
`/management/fundos/referencias/${code}`,
|
|
{},
|
|
"GET",
|
|
);
|
|
if (plan.package_sha256 !== batch.dataset_hash)
|
|
throw new Error(
|
|
"As fontes mudaram. Actualize e confira novamente antes de continuar.",
|
|
);
|
|
const receipt = await profileJson(
|
|
`/management/fundos/referencias/${code}/preparar`,
|
|
{ hash: plan.hash },
|
|
);
|
|
setReceipts((old) => ({ ...old, [code]: receipt }));
|
|
}
|
|
}
|
|
setMessage(
|
|
stop.current
|
|
? "Pausa concluída. As operações confirmadas foram conservadas."
|
|
: publish
|
|
? "Publicação das fichas conferidas concluída. Consulte o catálogo."
|
|
: "Preparação concluída. Confira os resultados antes de confirmar a publicação.",
|
|
);
|
|
} catch (error) {
|
|
setMessage(
|
|
`${error instanceof Error ? error.message : "Operação interrompida."} Os resultados anteriores foram conservados. Se a sessão ou MFA expirou, autentique-se no portal e retome. A publicação incerta pode ser repetida com a mesma confirmação.`,
|
|
);
|
|
} finally {
|
|
running.current = false;
|
|
setBusy(false);
|
|
}
|
|
}
|
|
return (
|
|
<Frame title="Conferir instituições e serviços financeiros">
|
|
<p className={card}>
|
|
A preparação cria rascunhos privados. A publicação exige uma
|
|
confirmação separada e MFA recente no portal. As fichas
|
|
conservam as incertezas das fontes; a sua presença não garante
|
|
licença actual, crédito ou seguro.
|
|
</p>
|
|
<p>
|
|
Fonte do conjunto:{" "}
|
|
<code className="break-all">{batch.dataset_hash}</code>
|
|
</p>
|
|
<fieldset disabled={busy} className={card}>
|
|
<button
|
|
type="button"
|
|
className={button}
|
|
onClick={() => {
|
|
setSelected(
|
|
batch.items
|
|
.filter(
|
|
(i) =>
|
|
i.source_sha256 ===
|
|
"119bb5514d2777849b70aebfec21d28a41885cbdc7d9827fbebd764fe994c108",
|
|
)
|
|
.map((i) => i.code),
|
|
);
|
|
setConfirmed(false);
|
|
}}
|
|
>
|
|
Seleccionar instituições do documento
|
|
</button>
|
|
<p>{selected.length} seleccionadas</p>
|
|
{batch.items.map((item) => (
|
|
<details key={item.code} className="border-b py-2">
|
|
<summary>
|
|
<label>
|
|
<input
|
|
type="checkbox"
|
|
checked={selected.includes(item.code)}
|
|
onChange={(e) => {
|
|
setSelected((old) =>
|
|
e.target.checked
|
|
? [...old, item.code]
|
|
: old.filter(
|
|
(c) => c !== item.code,
|
|
),
|
|
);
|
|
setConfirmed(false);
|
|
}}
|
|
/>{" "}
|
|
{item.title}
|
|
</label>{" "}
|
|
—{" "}
|
|
{receipts[item.code]?.published
|
|
? "Publicação confirmada"
|
|
: receipts[item.code]
|
|
? receipts[item.code].matches_reference
|
|
? "Conferida, aguarda publicação"
|
|
: "Edições existentes: rever individualmente"
|
|
: item.published
|
|
? "Já tem publicação"
|
|
: "Por preparar"}
|
|
</summary>
|
|
<p>{item.summary}</p>
|
|
<p>
|
|
{item.categories.map(c => financialCategories[c] ?? c).join(", ")} —{" "}
|
|
{item.types.map(t => financialTypes[t] ?? t).join(", ")} — {item.status}
|
|
</p>
|
|
<p>
|
|
{item.source_document}{" "}
|
|
<code className="break-all">
|
|
{item.source_sha256}
|
|
</code>
|
|
</p>
|
|
<p>
|
|
{item.locations.length} localizações;{" "}
|
|
{item.contacts.length} contactos.
|
|
</p>
|
|
<Link
|
|
className="underline"
|
|
href={`/management/fundos/referencias/${item.code}`}
|
|
>
|
|
Conferir fonte e dados completos
|
|
</Link>
|
|
{receipts[item.code] && (
|
|
<Link
|
|
className="ml-4 underline"
|
|
href={`/management/fundos/${receipts[item.code].id}`}
|
|
>
|
|
Alterar ficha existente
|
|
</Link>
|
|
)}
|
|
</details>
|
|
))}
|
|
<label className="block py-3">
|
|
<input
|
|
type="checkbox"
|
|
checked={reviewed}
|
|
onChange={(e) => setReviewed(e.target.checked)}
|
|
/>{" "}
|
|
Conferi as fontes e as fichas seleccionadas, incluindo dados
|
|
por confirmar.
|
|
</label>
|
|
<button
|
|
type="button"
|
|
className={button}
|
|
disabled={!reviewed || !selected.length}
|
|
onClick={() => void run(false)}
|
|
>
|
|
1. Preparar e conferir seleccionadas
|
|
</button>
|
|
<label className="block py-3">
|
|
Fundamento da publicação (mínimo 20 caracteres)
|
|
<textarea
|
|
className="block w-full border p-2"
|
|
value={reason}
|
|
onChange={(e) => setReason(e.target.value)}
|
|
maxLength={2000}
|
|
/>
|
|
</label>
|
|
<label className="block py-3">
|
|
<input
|
|
type="checkbox"
|
|
checked={confirmed}
|
|
onChange={(e) => setConfirmed(e.target.checked)}
|
|
/>{" "}
|
|
Confirmo a publicação pública das versões conferidas,
|
|
preservando os dados e as incertezas apresentados.
|
|
</label>
|
|
<button
|
|
type="button"
|
|
className={button}
|
|
disabled={
|
|
!reviewed ||
|
|
!confirmed ||
|
|
reason.trim().length < 20 ||
|
|
!selected.some(
|
|
(c) =>
|
|
receipts[c]?.matches_reference &&
|
|
!receipts[c].published,
|
|
)
|
|
}
|
|
onClick={() => void run(true)}
|
|
>
|
|
2. Publicar conferidas
|
|
</button>
|
|
</fieldset>
|
|
{busy && (
|
|
<button
|
|
type="button"
|
|
className={button}
|
|
onClick={() => {
|
|
stop.current = true;
|
|
}}
|
|
>
|
|
Pausar após esta ficha
|
|
</button>
|
|
)}
|
|
<p role="status" aria-live="polite">
|
|
{message}
|
|
</p>
|
|
<Link className="underline" href="/fundos">
|
|
Consultar catálogo
|
|
</Link>
|
|
</Frame>
|
|
);
|
|
}
|