fix: complete InvesteMoz validation and error corrections
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
import { inputText } from '../lib/input-text.ts';
|
||||
import { InvestmentConsumerIdentity } from './investment-consumer-identity';
|
||||
import InvestmentRequirements from './investment-requirements';
|
||||
import { useEffect, useRef, useState } from 'react';
|
||||
@@ -33,12 +34,23 @@ const empty = (v: unknown) =>
|
||||
const text = (v: unknown) =>
|
||||
typeof v === 'string' ? v : JSON.stringify(v ?? '');
|
||||
function savedOpportunity(values: Record<string, unknown>): string {
|
||||
if (values.context_opportunity) return String(values.context_opportunity);
|
||||
try { return JSON.parse(String(values.context_applicability_snapshot ?? '')).consumer_opportunity ?? ''; } catch { return ''; }
|
||||
if (values.context_opportunity)
|
||||
return inputText(values.context_opportunity);
|
||||
try {
|
||||
return (
|
||||
JSON.parse(inputText(values.context_applicability_snapshot ?? ''))
|
||||
.consumer_opportunity ?? ''
|
||||
);
|
||||
} catch {
|
||||
return '';
|
||||
}
|
||||
}
|
||||
const displayField = (values: Record<string, unknown>, key: string) =>
|
||||
key.endsWith('_id')
|
||||
? text(values[`${key}_label`]) || (empty(values[key]) ? 'não informado' : 'Referência escolhida; designação por recuperar')
|
||||
? text(values[`${key}_label`]) ||
|
||||
(empty(values[key])
|
||||
? 'não informado'
|
||||
: 'Referência escolhida; designação por recuperar')
|
||||
: text(values[key]) || 'não informado';
|
||||
export function DossierPrefill(props: Props) {
|
||||
const {
|
||||
@@ -57,9 +69,22 @@ export function DossierPrefill(props: Props) {
|
||||
const [reviewed, setReviewed] = useState(false);
|
||||
const [message, setMessage] = useState('');
|
||||
const userChose = useRef(false);
|
||||
const [opportunity, setOpportunity] = useState(() => String(props.initialOpportunity || (typeof window !== 'undefined' ? new URLSearchParams(window.location.search).get('opportunity') || '' : '') || savedOpportunity(props.values)));
|
||||
const [opportunity, setOpportunity] = useState(() =>
|
||||
String(
|
||||
props.initialOpportunity ||
|
||||
(typeof window !== 'undefined'
|
||||
? new URLSearchParams(window.location.search).get(
|
||||
'opportunity',
|
||||
) || ''
|
||||
: '') ||
|
||||
savedOpportunity(props.values),
|
||||
),
|
||||
);
|
||||
useEffect(() => {
|
||||
if (!userChose.current && !opportunity && enabled) { const saved = savedOpportunity(props.values); if (saved) setOpportunity(saved); }
|
||||
if (!userChose.current && !opportunity && enabled) {
|
||||
const saved = savedOpportunity(props.values);
|
||||
if (saved) setOpportunity(saved);
|
||||
}
|
||||
}, [enabled, props.values, opportunity]);
|
||||
const loadedOpportunity = useRef('');
|
||||
useEffect(() => {
|
||||
@@ -67,7 +92,9 @@ export function DossierPrefill(props: Props) {
|
||||
let stopped = false;
|
||||
setView(null);
|
||||
setError('');
|
||||
void contextApi<ContextView>(`/investimentos/${contextId}/contexto${opportunity ? '?opportunity='+encodeURIComponent(opportunity) : ''}`)
|
||||
void contextApi<ContextView>(
|
||||
`/investimentos/${contextId}/contexto${opportunity ? '?opportunity=' + encodeURIComponent(opportunity) : ''}`,
|
||||
)
|
||||
.then((v) => {
|
||||
if (!stopped) {
|
||||
loadedOpportunity.current = opportunity;
|
||||
@@ -83,16 +110,50 @@ export function DossierPrefill(props: Props) {
|
||||
};
|
||||
}, [contextId, retry, opportunity]);
|
||||
useEffect(() => {
|
||||
if (!view || loadedOpportunity.current !== opportunity || view.case.id !== contextId || !enabled || readOnly || !view.can_write) return;
|
||||
if (
|
||||
!view ||
|
||||
loadedOpportunity.current !== opportunity ||
|
||||
view.case.id !== contextId ||
|
||||
!enabled ||
|
||||
readOnly ||
|
||||
!view.can_write
|
||||
)
|
||||
return;
|
||||
const choices = view.context.opportunity_choices ?? [];
|
||||
const chosen = opportunity || (choices.length === 1 ? choices[0].opportunity.key : '');
|
||||
const chosen =
|
||||
opportunity ||
|
||||
(choices.length === 1 ? choices[0].opportunity.key : '');
|
||||
const source = view.prefill[consumer] ?? {};
|
||||
const now = latest.current;
|
||||
if ((chosen || consumer === 'simulator') && now.values.context_opportunity !== chosen && (!now.allowedFields || now.allowedFields.includes('context_opportunity'))) now.onApply({context_opportunity:chosen},true);
|
||||
const inherited = inheritCompatibleFields(`dossier:${contextId}:${consumer}`, now.values, source, now.values.context_reuse, now.allowedFields);
|
||||
if (Object.keys(inherited.patch).length || (now.persistInheritance !== false && now.values.context_reuse !== inherited.metadata)) {
|
||||
if (
|
||||
(chosen || consumer === 'simulator') &&
|
||||
now.values.context_opportunity !== chosen &&
|
||||
(!now.allowedFields ||
|
||||
now.allowedFields.includes('context_opportunity'))
|
||||
)
|
||||
now.onApply({ context_opportunity: chosen }, true);
|
||||
const inherited = inheritCompatibleFields(
|
||||
`dossier:${contextId}:${consumer}`,
|
||||
now.values,
|
||||
source,
|
||||
now.values.context_reuse,
|
||||
now.allowedFields,
|
||||
);
|
||||
if (
|
||||
Object.keys(inherited.patch).length ||
|
||||
(now.persistInheritance !== false &&
|
||||
now.values.context_reuse !== inherited.metadata)
|
||||
) {
|
||||
// The shared reconciliation has already selected safe fields; metadata travels with the same saved form.
|
||||
now.onApply({...inherited.patch, ...(now.persistInheritance === false ? {} : {context_reuse:inherited.metadata})}, true);
|
||||
now.onApply(
|
||||
{
|
||||
...inherited.patch,
|
||||
...(now.persistInheritance === false
|
||||
? {}
|
||||
: { context_reuse: inherited.metadata }),
|
||||
},
|
||||
true,
|
||||
);
|
||||
setMessage(
|
||||
'Dados compatíveis reutilizados do mesmo dossier. Os valores herdados acompanham o contexto; as suas alterações locais foram preservadas.',
|
||||
);
|
||||
@@ -126,10 +187,19 @@ export function DossierPrefill(props: Props) {
|
||||
([k]) => !props.allowedFields || props.allowedFields.includes(k),
|
||||
),
|
||||
);
|
||||
const chosenKey = opportunity || (view.context.opportunity_choices?.length===1 ? view.context.opportunity_choices[0].opportunity.key : '');
|
||||
const chosenChoice = view.context.opportunity_choices?.find(c=>c.opportunity.key===chosenKey);
|
||||
const chosenKey =
|
||||
opportunity ||
|
||||
(view.context.opportunity_choices?.length === 1
|
||||
? view.context.opportunity_choices[0].opportunity.key
|
||||
: '');
|
||||
const chosenChoice = view.context.opportunity_choices?.find(
|
||||
(c) => c.opportunity.key === chosenKey,
|
||||
);
|
||||
const differences = Object.keys(source).filter(
|
||||
(k) => k !== 'context_applicability_snapshot' && !k.endsWith('_label') && text(props.values[k]) !== text(source[k]),
|
||||
(k) =>
|
||||
k !== 'context_applicability_snapshot' &&
|
||||
!k.endsWith('_label') &&
|
||||
text(props.values[k]) !== text(source[k]),
|
||||
);
|
||||
const linkedRevision =
|
||||
consumer === 'simulator'
|
||||
@@ -141,29 +211,110 @@ export function DossierPrefill(props: Props) {
|
||||
className="my-4 space-y-3 rounded border border-emerald-300 bg-emerald-50 p-4 [overflow-wrap:anywhere]"
|
||||
aria-label="Dados do dossier neste formulário"
|
||||
>
|
||||
<h2 className="font-bold">
|
||||
Dados do seu investimento
|
||||
</h2>
|
||||
<h2 className="font-bold">Dados do seu investimento</h2>
|
||||
<p>
|
||||
<Link
|
||||
className="underline"
|
||||
href={`/investimentos/${contextId}${chosenKey ? '?opportunity='+encodeURIComponent(chosenKey) : ''}`}
|
||||
href={`/investimentos/${contextId}${chosenKey ? '?opportunity=' + encodeURIComponent(chosenKey) : ''}`}
|
||||
>
|
||||
{view.case.title}
|
||||
</Link>
|
||||
{' · '}<Link className="underline" href={`/investimentos/${contextId}${chosenKey ? '?opportunity='+encodeURIComponent(chosenKey) : ''}`}>Voltar ao dossier</Link>
|
||||
{' · '}
|
||||
<Link
|
||||
className="underline"
|
||||
href={`/investimentos/${contextId}${chosenKey ? '?opportunity=' + encodeURIComponent(chosenKey) : ''}`}
|
||||
>
|
||||
Voltar ao dossier
|
||||
</Link>
|
||||
</p>
|
||||
{(opportunity||view.context.opportunity_choices?.length===1)&&<InvestmentContextAssessment contextId={contextId} raw={view.prefill.simulator?.context_applicability_snapshot} />}
|
||||
<InvestmentConsumerIdentity raw={props.values.context_opportunity_snapshot}/>
|
||||
{contextId&&<InvestmentRequirements contextId={contextId} excludedSteps={view.steps.filter(step=>step.state==='dispensed').map(step=>step.code)} assessment={view.context.requirements_assessment ? {...view.context.requirements_assessment,requirements:view.context.requirements_assessment.requirements.filter(r=>r.opportunity_key===chosenKey)} : null}/>}
|
||||
{!!view.context.opportunity_choices?.length && <label className="block font-semibold">Oportunidade em tratamento
|
||||
<select className="ml-2 rounded border bg-white p-2" disabled={readOnly || !view.can_write || (!!props.allowedFields&&!props.allowedFields.includes('context_opportunity'))} value={opportunity || (view.context.opportunity_choices.length===1 ? view.context.opportunity_choices[0].opportunity.key : '')} onChange={e=>{
|
||||
const key=e.target.value; userChose.current=true; setOpportunity(key); setSelected([]);setReviewed(false);
|
||||
if (consumer !== 'simulator') props.onApply({context_opportunity:key},true);
|
||||
props.onReview?.(view.context.revision,false);
|
||||
}}><option value="">Escolha a oportunidade deste pedido</option>{view.context.opportunity_choices.map(c=><option key={c.opportunity.key} value={c.opportunity.key}>{c.opportunity.title} — {c.territory?.title??'Localização por definir'}</option>)}</select>
|
||||
{!opportunity && view.context.opportunity_choices.length>1 && <p>Escolha a oportunidade para recuperar a localização e os requisitos específicos. Os dados comuns e as suas edições conservam-se.</p>}
|
||||
</label>}
|
||||
{(opportunity ||
|
||||
view.context.opportunity_choices?.length === 1) && (
|
||||
<InvestmentContextAssessment
|
||||
contextId={contextId}
|
||||
raw={view.prefill.simulator?.context_applicability_snapshot}
|
||||
/>
|
||||
)}
|
||||
<InvestmentConsumerIdentity
|
||||
raw={props.values.context_opportunity_snapshot}
|
||||
/>
|
||||
{contextId && (
|
||||
<InvestmentRequirements
|
||||
contextId={contextId}
|
||||
excludedSteps={view.steps
|
||||
.filter((step) => step.state === 'dispensed')
|
||||
.map((step) => step.code)}
|
||||
assessment={
|
||||
view.context.requirements_assessment
|
||||
? {
|
||||
...view.context.requirements_assessment,
|
||||
requirements:
|
||||
view.context.requirements_assessment.requirements.filter(
|
||||
(r) =>
|
||||
r.opportunity_key === chosenKey,
|
||||
),
|
||||
}
|
||||
: null
|
||||
}
|
||||
/>
|
||||
)}
|
||||
{!!view.context.opportunity_choices?.length && (
|
||||
<label className="block font-semibold">
|
||||
Oportunidade em tratamento
|
||||
<select
|
||||
className="ml-2 rounded border bg-white p-2"
|
||||
disabled={
|
||||
readOnly ||
|
||||
!view.can_write ||
|
||||
(!!props.allowedFields &&
|
||||
!props.allowedFields.includes(
|
||||
'context_opportunity',
|
||||
))
|
||||
}
|
||||
value={
|
||||
opportunity ||
|
||||
(view.context.opportunity_choices.length === 1
|
||||
? view.context.opportunity_choices[0]
|
||||
.opportunity.key
|
||||
: '')
|
||||
}
|
||||
onChange={(e) => {
|
||||
const key = e.target.value;
|
||||
userChose.current = true;
|
||||
setOpportunity(key);
|
||||
setSelected([]);
|
||||
setReviewed(false);
|
||||
if (consumer !== 'simulator')
|
||||
props.onApply(
|
||||
{ context_opportunity: key },
|
||||
true,
|
||||
);
|
||||
props.onReview?.(view.context.revision, false);
|
||||
}}
|
||||
>
|
||||
<option value="">
|
||||
Escolha a oportunidade deste pedido
|
||||
</option>
|
||||
{view.context.opportunity_choices.map((c) => (
|
||||
<option
|
||||
key={c.opportunity.key}
|
||||
value={c.opportunity.key}
|
||||
>
|
||||
{c.opportunity.title} —{' '}
|
||||
{c.territory?.title ??
|
||||
'Localização por definir'}
|
||||
</option>
|
||||
))}
|
||||
</select>
|
||||
{!opportunity &&
|
||||
view.context.opportunity_choices.length > 1 && (
|
||||
<p>
|
||||
Escolha a oportunidade para recuperar a
|
||||
localização e os requisitos específicos. Os
|
||||
dados comuns e as suas edições conservam-se.
|
||||
</p>
|
||||
)}
|
||||
</label>
|
||||
)}
|
||||
{stale && (
|
||||
<p className="font-semibold" role="status">
|
||||
{linkedRevision > 0
|
||||
@@ -174,20 +325,42 @@ export function DossierPrefill(props: Props) {
|
||||
</p>
|
||||
)}
|
||||
{message && <p role="status">{message}</p>}
|
||||
{!!chosenChoice && <OpportunityChoices choices={(view.context.opportunity_choices ?? []).filter(c=>c.opportunity.key===chosenKey)}/>}
|
||||
{consumer!=='simulator'&&chosenChoice?.opportunity.fields.sector&&<p>Sector desta oportunidade: {chosenChoice.opportunity.fields.sector}</p>}
|
||||
<SelectedComplements items={view.context.complements ?? []}/>
|
||||
{Object.entries(view.context.selection)
|
||||
.filter(([slot]) => !view.context.opportunity_choices?.length || !['opportunity', 'territory', 'park', ...(consumer==='simulator'?[]:['sector'])].includes(slot))
|
||||
.map(([, r]) =>
|
||||
r && (
|
||||
<p key={r.key}>
|
||||
<Link className="underline" href={r.href}>
|
||||
{r.title}
|
||||
</Link>
|
||||
</p>
|
||||
),
|
||||
{!!chosenChoice && (
|
||||
<OpportunityChoices
|
||||
choices={(view.context.opportunity_choices ?? []).filter(
|
||||
(c) => c.opportunity.key === chosenKey,
|
||||
)}
|
||||
/>
|
||||
)}
|
||||
{consumer !== 'simulator' &&
|
||||
chosenChoice?.opportunity.fields.sector && (
|
||||
<p>
|
||||
Sector desta oportunidade:{' '}
|
||||
{chosenChoice.opportunity.fields.sector}
|
||||
</p>
|
||||
)}
|
||||
<SelectedComplements items={view.context.complements ?? []} />
|
||||
{Object.entries(view.context.selection)
|
||||
.filter(
|
||||
([slot]) =>
|
||||
!view.context.opportunity_choices?.length ||
|
||||
![
|
||||
'opportunity',
|
||||
'territory',
|
||||
'park',
|
||||
...(consumer === 'simulator' ? [] : ['sector']),
|
||||
].includes(slot),
|
||||
)
|
||||
.map(
|
||||
([, r]) =>
|
||||
r && (
|
||||
<p key={r.key}>
|
||||
<Link className="underline" href={r.href}>
|
||||
{r.title}
|
||||
</Link>
|
||||
</p>
|
||||
),
|
||||
)}
|
||||
{!readOnly && view.can_write && differences.length > 0 && (
|
||||
<details>
|
||||
<summary className="cursor-pointer font-semibold">
|
||||
@@ -265,22 +438,49 @@ export function DossierPrefill(props: Props) {
|
||||
disabled={!selected.length || !enabled}
|
||||
onClick={() => {
|
||||
const patch: Record<string, unknown> = {};
|
||||
for (const k of selected)
|
||||
{
|
||||
for (const k of selected) {
|
||||
patch[k] = source[k] ?? '';
|
||||
if (`${k}_label` in source) patch[`${k}_label`] = source[`${k}_label`];
|
||||
if (`${k}_label` in source)
|
||||
patch[`${k}_label`] = source[`${k}_label`];
|
||||
}
|
||||
const scope = `dossier:${contextId}:${consumer}`;
|
||||
let raw = String(props.values.context_reuse ?? '');
|
||||
let raw = inputText(
|
||||
props.values.context_reuse ?? '',
|
||||
);
|
||||
try {
|
||||
const metadata = JSON.parse(raw);
|
||||
if(metadata.scope === scope) {
|
||||
for(const key of Object.keys(patch)) delete metadata.inherited?.[key];
|
||||
raw=JSON.stringify(metadata);
|
||||
if (metadata.scope === scope) {
|
||||
for (const key of Object.keys(patch))
|
||||
delete metadata.inherited?.[key];
|
||||
raw = JSON.stringify(metadata);
|
||||
}
|
||||
} catch { /* Legacy metadata does not grant ownership of other fields. */ }
|
||||
const adopted=inheritCompatibleFields(scope,{...props.values,...Object.fromEntries(Object.keys(patch).map(key=>[key,undefined]))},patch,raw,props.allowedFields);
|
||||
props.onApply({...patch,...(props.persistInheritance === false ? {} : {context_reuse:adopted.metadata})}, true);
|
||||
} catch {
|
||||
/* Legacy metadata does not grant ownership of other fields. */
|
||||
}
|
||||
const adopted = inheritCompatibleFields(
|
||||
scope,
|
||||
{
|
||||
...props.values,
|
||||
...Object.fromEntries(
|
||||
Object.keys(patch).map((key) => [
|
||||
key,
|
||||
undefined,
|
||||
]),
|
||||
),
|
||||
},
|
||||
patch,
|
||||
raw,
|
||||
props.allowedFields,
|
||||
);
|
||||
props.onApply(
|
||||
{
|
||||
...patch,
|
||||
...(props.persistInheritance === false
|
||||
? {}
|
||||
: { context_reuse: adopted.metadata }),
|
||||
},
|
||||
true,
|
||||
);
|
||||
setSelected([]);
|
||||
setReviewed(false);
|
||||
props.onReview?.(view.context.revision, false);
|
||||
@@ -293,7 +493,12 @@ export function DossierPrefill(props: Props) {
|
||||
</button>
|
||||
</details>
|
||||
)}
|
||||
{!readOnly && reviewed && <p>Os dados disponíveis foram recuperados. Preencha o que falta; as suas alterações conservam-se.</p>}
|
||||
{!readOnly && reviewed && (
|
||||
<p>
|
||||
Os dados disponíveis foram recuperados. Preencha o que
|
||||
falta; as suas alterações conservam-se.
|
||||
</p>
|
||||
)}
|
||||
<p className="text-xs">
|
||||
Associar uma referência não confirma incentivos, licenças, terra
|
||||
disponível, reserva ou autorização.
|
||||
|
||||
Reference in New Issue
Block a user