75 lines
2.3 KiB
TypeScript
75 lines
2.3 KiB
TypeScript
import {
|
|
calculateBreakEven,
|
|
calculateFundingGap,
|
|
restoreFinancialToolsSnapshot,
|
|
} from './financial-calculators.ts';
|
|
|
|
export const pathStates: Record<string, string> = {
|
|
not_started: 'Por iniciar',
|
|
preparing: 'Em preparação',
|
|
ready_to_submit: 'Pronto para submeter',
|
|
submitted: 'Submetido',
|
|
reviewing: 'Validação interna',
|
|
correction_required: 'Correcção pedida',
|
|
waiting_external: 'Aguarda informação externa',
|
|
approved: 'Acto externo favorável documentado',
|
|
rejected: 'Acto externo desfavorável documentado',
|
|
dispensed: 'Preparação dispensada pelo promotor',
|
|
completed: 'Concluído',
|
|
expired: 'Acto externo expirado',
|
|
};
|
|
export type PathResult = {
|
|
context_opportunity?: string | null;
|
|
id: string;
|
|
financial_snapshot: string;
|
|
currency: string | null;
|
|
draft_id: string;
|
|
draft_version: number;
|
|
engine: string;
|
|
recorded_at: string;
|
|
context_state?: string;
|
|
context_revision?: number;
|
|
context_invalidated_revision?: number;
|
|
};
|
|
export function recoverPathResult(result: PathResult) {
|
|
if (result.engine !== 'financial-calculators:v1')
|
|
throw new Error(
|
|
'Versão do cálculo indisponível; as entradas foram preservadas.',
|
|
);
|
|
const snapshot = restoreFinancialToolsSnapshot(result.financial_snapshot);
|
|
if (!snapshot)
|
|
throw new Error(
|
|
'Entradas incompatíveis; a evidência original foi preservada.',
|
|
);
|
|
const independent = <T>(
|
|
run: () => T,
|
|
): { value: T | null; error: string | null } => {
|
|
try {
|
|
return { value: run(), error: null };
|
|
} catch (error) {
|
|
return {
|
|
value: null,
|
|
error:
|
|
error instanceof Error
|
|
? error.message
|
|
: 'Cálculo indeterminado',
|
|
};
|
|
}
|
|
};
|
|
return {
|
|
breakEven: independent(() =>
|
|
calculateBreakEven(
|
|
snapshot.breakEven.fixed,
|
|
snapshot.breakEven.price,
|
|
snapshot.breakEven.variable,
|
|
),
|
|
),
|
|
funding: independent(() =>
|
|
calculateFundingGap(
|
|
snapshot.funding.total,
|
|
snapshot.funding.secured,
|
|
),
|
|
),
|
|
};
|
|
}
|