Entrega dashboard e correccoes transversais preservando Ofertas instalado
tests / ci (push) Canceled after 0s

This commit is contained in:
2026-11-15 18:56:39 +02:00
parent 1349789226
commit fb076c174f
117 changed files with 10380 additions and 1905 deletions
+164 -2
View File
@@ -8,6 +8,114 @@ export type SimulationDraft = Record<string, string | string[]>;
export const projectQuestionnaireVersion =
'investmoz-project-dossier-2026-10-05.1';
export function isAssistedInvestmentJourney(draft: SimulationDraft): boolean {
return (
draft.journey_type === 'investor' &&
['simulate_only', 'own_investment'].includes(String(draft.purpose)) &&
draft.detail_level !== 'advanced'
);
}
export const assistedInvestmentSections = [
{ id: 'purpose', title: 'Intenção, entidade e localização' },
{ id: 'market', title: 'Mercado e dimensão do negócio' },
{ id: 'investment', title: 'Investimento inicial' },
{ id: 'financial', title: 'Financiamento' },
{ id: 'operating', title: 'Receitas e custos' },
{ id: 'cashflow', title: 'Tesouraria e cenários' },
{ id: 'tax', title: 'Enquadramento fiscal' },
{ id: 'review', title: 'Resultados e próximos passos' },
];
/** One field contract for rendering, validation and the final review. */
export function simulationFieldsForSection(
draft: SimulationDraft,
section: string,
) {
if (!isAssistedInvestmentJourney(draft))
return simulationFieldGroups[section] ?? [];
const unique = (fields: (typeof simulationFieldGroups)[string]) =>
fields.filter(
(field, index) =>
fields.findIndex((other) => other.key === field.key) === index,
);
if (section === 'purpose')
return unique([
...simulationFieldGroups.purpose.filter((field) =>
[
'journey_type',
'detail_level',
'purpose',
'preferred_language',
].includes(field.key),
),
...simulationFieldGroups.project.filter((field) =>
[
'project_title',
'project_summary',
'products_services',
'sector_id',
'activity_id',
].includes(field.key),
),
...simulationFieldGroups.location.filter((field) =>
[
'province_id',
'district_id',
'admin_post_id',
'locality_id',
].includes(field.key),
),
]);
if (section === 'market')
return unique([
...simulationFieldGroups.project.filter(
(field) =>
![
'project_title',
'project_summary',
'products_services',
'sector_id',
'activity_id',
].includes(field.key),
),
...simulationFieldGroups.market,
]);
if (section === 'investment')
return simulationFieldGroups.financial.filter((field) =>
['cost_estimate_status', 'currency', 'total_investment'].includes(
field.key,
),
);
if (section === 'financial')
return simulationFieldGroups.financial.filter(
(field) =>
![
'cost_estimate_status',
'currency',
'total_investment',
'annual_revenue',
'annual_operating_costs',
'expected_profit',
'receivables_timing_status',
'planning_loan',
].includes(field.key),
);
if (section === 'operating')
return simulationFieldGroups.financial.filter((field) =>
[
'annual_revenue',
'annual_operating_costs',
'expected_profit',
].includes(field.key),
);
if (section === 'cashflow')
return simulationFieldGroups.financial.filter((field) =>
['receivables_timing_status', 'planning_loan'].includes(field.key),
);
return simulationFieldGroups[section] ?? [];
}
function isProjectGuidedJourney(draft: SimulationDraft): boolean {
return (
draft.purpose === 'simulate_only' &&
@@ -80,6 +188,7 @@ export const simulationSections = [
/** Keep the anonymous arithmetic journey short; formal submissions retain the full questionnaire. */
export function getSimulationSections(draft: SimulationDraft) {
if (isAssistedInvestmentJourney(draft)) return assistedInvestmentSections;
if (isProjectGuidedJourney(draft)) {
if (draft.journey_type === 'small_business') {
return [
@@ -192,7 +301,28 @@ export const advancedSimulationFields = new Set([
'required_documents',
]);
export function simulationFieldAvailableAtDetailLevel(
draft: SimulationDraft,
section: string,
key: string,
): boolean {
return (
draft.detail_level === 'advanced' ||
(isAssistedInvestmentJourney(draft) &&
[
'secured_amount',
'sought_amount',
'annual_revenue',
'annual_operating_costs',
'expected_profit',
].includes(key)) ||
(section === 'procedures' && key === 'required_documents') ||
!advancedSimulationFields.has(key)
);
}
export const simulationOptions: Record<string, string[]> = {
planning_loan: ['yes', 'no', 'unknown'],
journey_type: ['personal', 'small_business', 'company', 'investor'],
detail_level: ['simple', 'advanced'],
purpose: [
@@ -784,6 +914,30 @@ export const simulationFieldGroups: Record<
},
],
financial: [
{
key: 'planning_loan',
label: 'Pretende estudar um empréstimo neste cenário?',
options: 'planning_loan',
showWhen: isAssistedInvestmentJourney,
},
{
key: 'planned_funding',
label: 'Financiamento previsto (ainda não mobilizado)',
type: 'number',
showWhen: isAssistedInvestmentJourney,
},
{
key: 'requested_funding',
label: 'Financiamento solicitado (ainda não mobilizado)',
type: 'number',
showWhen: isAssistedInvestmentJourney,
},
{
key: 'approved_funding',
label: 'Financiamento aprovado (ainda não mobilizado)',
type: 'number',
showWhen: isAssistedInvestmentJourney,
},
{
key: 'cost_estimate_status',
label: 'Já identificou os principais custos e respectiva periodicidade?',
@@ -1540,6 +1694,9 @@ export function restoreSimulationDraft(
export function simulationDraftFingerprint(draft: SimulationDraft): string {
const sort = (value: unknown): unknown => {
// Nullable empty form inputs are normalised to null by the HTTP
// middleware. They remain unknown, never numeric zero.
if (value === null) return '';
if (Array.isArray(value)) return value.map(sort);
if (value && typeof value === 'object')
return Object.fromEntries(
@@ -1561,7 +1718,7 @@ export function validateSimulationDraft(
if (sectionId === 'tax' && draft.fiscal_scenario_mode === 'custom_planning')
return {};
const errors: Record<string, string> = {};
for (const field of simulationFieldGroups[sectionId] ?? []) {
for (const field of simulationFieldsForSection(draft, sectionId)) {
if (field.showWhen && !field.showWhen(draft)) continue;
if (
sectionId === 'tax' &&
@@ -1581,7 +1738,12 @@ export function validateSimulationDraft(
if (
required &&
empty &&
!(sectionId === 'tax' && isInvestorProjectJourney(draft))
!(isAssistedInvestmentJourney(draft) && sectionId !== 'purpose') &&
!(
sectionId === 'tax' &&
(isInvestorProjectJourney(draft) ||
isAssistedInvestmentJourney(draft))
)
) {
errors[field.key] = 'Este campo é obrigatório.';
}