454 lines
19 KiB
TypeScript
454 lines
19 KiB
TypeScript
import { Link, useForm } from '@inertiajs/react';
|
|
import { Management } from './shared';
|
|
import TerritorialTraceabilityPlan, {
|
|
type TraceabilityPlan,
|
|
} from '@/components/territorial-traceability-plan';
|
|
|
|
type Plan = {
|
|
target: { database: string; port: number };
|
|
locations: {
|
|
code: string;
|
|
project_id: string;
|
|
name: string;
|
|
role: string;
|
|
source_reference: string;
|
|
source_quote: string;
|
|
reason: string;
|
|
existing: boolean;
|
|
}[];
|
|
memberships: {
|
|
cluster_code: string;
|
|
project_code: string;
|
|
source_reference: string;
|
|
source_quote: string;
|
|
action: string;
|
|
}[];
|
|
prepared_opportunities: {
|
|
reference: string;
|
|
title: string;
|
|
project_code: string | null;
|
|
location_text: string;
|
|
source_reference: string;
|
|
identity_status: string;
|
|
unknown: string[];
|
|
reason: string;
|
|
}[];
|
|
pending: { code: string; reason: string; name?: string }[];
|
|
};
|
|
type Territory = {
|
|
row_id: string;
|
|
validation_hash: string;
|
|
status: 'grounded' | 'pending' | 'contradictory';
|
|
applied: boolean;
|
|
issues: string[];
|
|
warnings: string[];
|
|
current: {
|
|
id: string;
|
|
name: string;
|
|
role: string;
|
|
path: { name: string; type: string }[];
|
|
}[];
|
|
proposed: {
|
|
name: string;
|
|
role: string;
|
|
path: { name: string; type: string }[];
|
|
};
|
|
coordinates: { source: string; coordinates: number[]; srid: number }[];
|
|
coordinate_checks: {
|
|
territory_id: string;
|
|
source: string;
|
|
compatible: boolean;
|
|
}[];
|
|
};
|
|
type History = {
|
|
id: string;
|
|
date: string;
|
|
actor_id: number;
|
|
actor: string | null;
|
|
data: {
|
|
sources?: {
|
|
row_id: string;
|
|
source_reference: string;
|
|
reason: string;
|
|
}[];
|
|
};
|
|
};
|
|
const roles: Record<string, string> = {
|
|
primary: 'Principal',
|
|
secondary: 'Secundária',
|
|
impact: 'Área de impacto',
|
|
};
|
|
const levels: Record<string, string> = {
|
|
country: 'País',
|
|
province: 'Província',
|
|
district: 'Distrito',
|
|
admin_post: 'Posto administrativo',
|
|
locality: 'Localidade',
|
|
site: 'Implantação',
|
|
};
|
|
function pathText(path: { name: string; type: string }[]) {
|
|
return (
|
|
path
|
|
.map((node) => `${levels[node.type] ?? node.type}: ${node.name}`)
|
|
.join(' → ') || 'Hierarquia não indicada'
|
|
);
|
|
}
|
|
function IndividualAssociation({
|
|
hash,
|
|
item,
|
|
}: {
|
|
hash: string;
|
|
item: Territory;
|
|
}) {
|
|
const form = useForm({
|
|
hash,
|
|
row_id: item.row_id,
|
|
validation_hash: item.validation_hash,
|
|
});
|
|
return (
|
|
<form
|
|
onSubmit={(event) => {
|
|
event.preventDefault();
|
|
form.transform(() => ({
|
|
hash,
|
|
row_id: item.row_id,
|
|
validation_hash: item.validation_hash,
|
|
}));
|
|
form.post('/management/relationship-repair', {
|
|
preserveScroll: true,
|
|
});
|
|
}}
|
|
>
|
|
<button
|
|
className="rounded bg-emerald-800 px-4 py-2 text-white disabled:opacity-50"
|
|
disabled={
|
|
form.processing ||
|
|
item.applied ||
|
|
item.status !== 'grounded'
|
|
}
|
|
>
|
|
{item.applied
|
|
? 'Associação aplicada'
|
|
: 'Aplicar esta associação'}
|
|
</button>
|
|
{Object.values(form.errors).map((error) => (
|
|
<p key={error} role="alert">
|
|
{error}
|
|
</p>
|
|
))}
|
|
</form>
|
|
);
|
|
}
|
|
type Unproposed = {
|
|
code: string;
|
|
current: { name: string; role: string }[];
|
|
source: string | null;
|
|
location_text: string | null;
|
|
reasons: string[];
|
|
};
|
|
export default function RelationshipRepair({
|
|
hash,
|
|
plan,
|
|
territorial,
|
|
history,
|
|
unproposed,
|
|
traceability,
|
|
}: {
|
|
hash: string;
|
|
plan: Plan;
|
|
territorial: Territory[];
|
|
history: History[];
|
|
unproposed: Unproposed[];
|
|
traceability?: TraceabilityPlan | null;
|
|
}) {
|
|
const form = useForm({ hash });
|
|
return (
|
|
<Management title="Associações documentadas de projectos âncora e clusters">
|
|
{traceability && (
|
|
<TerritorialTraceabilityPlan preview={traceability} />
|
|
)}
|
|
<p>
|
|
Pré-visualização para {plan.target.database}:{plan.target.port}.
|
|
A execução verifica o backup e o restauro, a versão dos dossiers
|
|
e os identificadores. As associações existentes são preservadas.
|
|
</p>
|
|
<p>
|
|
O plano corrige localizações documentadas. As oportunidades
|
|
abaixo ficam preparadas para cadastro; não são publicadas nem
|
|
ligadas a clusters por pertencerem ao mesmo projecto.
|
|
</p>
|
|
<form
|
|
onSubmit={(event) => {
|
|
event.preventDefault();
|
|
form.post('/management/relationship-repair');
|
|
}}
|
|
>
|
|
<button
|
|
className="rounded bg-emerald-800 px-4 py-3 text-white disabled:opacity-50"
|
|
disabled={form.processing}
|
|
>
|
|
Aplicar associações fundamentadas
|
|
</button>
|
|
{Object.values(form.errors).map((error) => (
|
|
<p key={error} role="alert">
|
|
{error}
|
|
</p>
|
|
))}
|
|
</form>
|
|
<section className="space-y-4">
|
|
<h2 className="text-xl font-bold">
|
|
Localizações: {plan.locations.length}
|
|
</h2>
|
|
<p>
|
|
As propostas acrescentam localizações documentadas e
|
|
preservam as atuais. Cada projeto pode ter várias
|
|
localizações principais, secundárias e de impacto. A
|
|
localização não é deduzida de clusters ou da proximidade a
|
|
zonas.
|
|
</p>
|
|
{(['grounded', 'pending', 'contradictory'] as const).map(
|
|
(status) => (
|
|
<section
|
|
key={status}
|
|
className="space-y-4"
|
|
aria-label={
|
|
status === 'grounded'
|
|
? 'Associações fundamentadas'
|
|
: status === 'pending'
|
|
? 'Associações pendentes'
|
|
: 'Associações contraditórias'
|
|
}
|
|
>
|
|
<h3 className="text-lg font-bold">
|
|
{status === 'grounded'
|
|
? 'Fundamentadas'
|
|
: status === 'pending'
|
|
? 'Pendentes'
|
|
: 'Contraditórias'}
|
|
:{' '}
|
|
{territorial.filter(
|
|
(item) => item.status === status,
|
|
).length +
|
|
(status === 'pending'
|
|
? unproposed.length
|
|
: 0)}
|
|
</h3>
|
|
{status === 'pending' &&
|
|
unproposed.map((item) => (
|
|
<article
|
|
key={item.code}
|
|
className="space-y-2 rounded border p-4"
|
|
>
|
|
<h4 className="font-bold">
|
|
{item.code} · Sem associação
|
|
territorial individualizada
|
|
</h4>
|
|
<p>
|
|
Localização atual:{' '}
|
|
{item.current
|
|
.map(
|
|
(location) =>
|
|
`${location.name} · ${roles[location.role]}`,
|
|
)
|
|
.join('; ') ||
|
|
'Sem localização cadastrada'}
|
|
</p>
|
|
<p>
|
|
Localização documental proposta:{' '}
|
|
{item.location_text ??
|
|
'Não indicada'}
|
|
</p>
|
|
<p>
|
|
Documento de origem:{' '}
|
|
{item.source ?? 'Não indicado'}
|
|
</p>
|
|
{item.reasons.map((reason) => (
|
|
<p key={reason}>{reason}</p>
|
|
))}
|
|
</article>
|
|
))}
|
|
{territorial.map((validation, index) =>
|
|
validation.status !== status ? null : (
|
|
<article
|
|
key={validation.row_id}
|
|
data-row-id={validation.row_id}
|
|
className="space-y-3 rounded border p-4"
|
|
>
|
|
<h4 className="font-bold">
|
|
{plan.locations[index].code} →{' '}
|
|
{validation.proposed.name} ·{' '}
|
|
{roles[validation.proposed.role] ??
|
|
validation.proposed.role}
|
|
</h4>
|
|
<div>
|
|
<strong>Localização atual</strong>
|
|
{validation.current.length ? (
|
|
<ul>
|
|
{validation.current.map(
|
|
(current, i) => (
|
|
<li key={i}>
|
|
{current.name} ·{' '}
|
|
{roles[
|
|
current.role
|
|
] ??
|
|
current.role}{' '}
|
|
—{' '}
|
|
{pathText(
|
|
current.path,
|
|
)}
|
|
</li>
|
|
),
|
|
)}
|
|
</ul>
|
|
) : (
|
|
<p>
|
|
Sem localização cadastrada.
|
|
</p>
|
|
)}
|
|
</div>
|
|
<p>
|
|
<strong>
|
|
Localização proposta:
|
|
</strong>{' '}
|
|
{pathText(validation.proposed.path)}{' '}
|
|
· {roles[validation.proposed.role]}
|
|
</p>
|
|
<p>
|
|
Documento de origem:{' '}
|
|
{
|
|
plan.locations[index]
|
|
.source_reference
|
|
}
|
|
</p>
|
|
<blockquote>
|
|
{plan.locations[index].source_quote}
|
|
</blockquote>
|
|
<p>
|
|
Motivo:{' '}
|
|
{plan.locations[index].reason}
|
|
</p>
|
|
{validation.coordinates.map(
|
|
(point, i) => (
|
|
<p key={i}>
|
|
Coordenadas (longitude,
|
|
latitude):{' '}
|
|
{point.coordinates.join(
|
|
', ',
|
|
)}{' '}
|
|
· SRID {point.srid} · Fonte:{' '}
|
|
{point.source}
|
|
</p>
|
|
),
|
|
)}
|
|
{validation.coordinate_checks.map(
|
|
(check, i) => (
|
|
<p key={i}>
|
|
{check.compatible
|
|
? 'Coordenadas compatíveis com o limite territorial'
|
|
: 'Coordenadas incompatíveis com o limite territorial'}{' '}
|
|
· Fonte cartográfica:{' '}
|
|
{check.source}
|
|
</p>
|
|
),
|
|
)}
|
|
{validation.issues.map((issue) => (
|
|
<p
|
|
key={issue}
|
|
className="font-semibold text-red-700"
|
|
>
|
|
{issue}
|
|
</p>
|
|
))}
|
|
{validation.warnings.map((warning) => (
|
|
<p key={warning}>{warning}</p>
|
|
))}
|
|
<IndividualAssociation
|
|
hash={hash}
|
|
item={validation}
|
|
/>
|
|
</article>
|
|
),
|
|
)}
|
|
</section>
|
|
),
|
|
)}
|
|
</section>
|
|
<section className="space-y-3">
|
|
<h2 className="text-xl font-bold">Histórico de aplicação</h2>
|
|
{history.length ? (
|
|
history.map((event) => (
|
|
<article key={event.id} className="rounded border p-4">
|
|
<p>
|
|
Autor: {event.actor ?? 'Utilizador'} (ID{' '}
|
|
{event.actor_id}) · Data: {event.date}
|
|
</p>
|
|
{event.data.sources?.map((source) => (
|
|
<p key={source.row_id}>
|
|
Fonte: {source.source_reference} · Motivo:{' '}
|
|
{source.reason}
|
|
</p>
|
|
))}
|
|
</article>
|
|
))
|
|
) : (
|
|
<p>Nenhuma associação aplicada por este plano.</p>
|
|
)}
|
|
</section>
|
|
<section className="space-y-4">
|
|
<h2 className="text-xl font-bold">
|
|
Projectos nos clusters: {plan.memberships.length}
|
|
</h2>
|
|
{plan.memberships.map((item, index) => (
|
|
<article key={index} className="rounded border p-4">
|
|
<h3 className="font-bold">
|
|
{item.cluster_code} ↔ {item.project_code}
|
|
</h3>
|
|
<p>
|
|
{item.action === 'present_existing'
|
|
? 'Relação existente, agora apresentada como proposta documental pendente'
|
|
: 'Relação ausente'}
|
|
</p>
|
|
<p>Fonte: {item.source_reference}</p>
|
|
<blockquote>{item.source_quote}</blockquote>
|
|
</article>
|
|
))}
|
|
</section>
|
|
<section className="space-y-4">
|
|
<h2 className="text-xl font-bold">
|
|
Registos de oportunidades preparados:{' '}
|
|
{plan.prepared_opportunities.length}
|
|
</h2>
|
|
{plan.prepared_opportunities.map((item, index) => (
|
|
<details key={index} className="rounded border p-4">
|
|
<summary>
|
|
{item.reference} · {item.title}
|
|
</summary>
|
|
<p>
|
|
Projecto:{' '}
|
|
{item.project_code ??
|
|
'Identidade em conflito; associação pendente'}
|
|
</p>
|
|
<p>{item.location_text}</p>
|
|
<p>Fonte: {item.source_reference}</p>
|
|
<p>{item.reason}</p>
|
|
<p>Dados por completar: {item.unknown.join(', ')}.</p>
|
|
</details>
|
|
))}
|
|
</section>
|
|
<section>
|
|
<h2 className="text-xl font-bold">Pendências concretas</h2>
|
|
<ul className="list-disc pl-5">
|
|
{plan.pending.map((item, index) => (
|
|
<li key={index}>
|
|
{item.code}: {item.reason} {item.name}
|
|
</li>
|
|
))}
|
|
</ul>
|
|
</section>
|
|
<Link href="/carteira" className="underline">
|
|
Consultar carteira
|
|
</Link>
|
|
</Management>
|
|
);
|
|
}
|