fix: persist annual demonstration through existing simulation drafts
This commit is contained in:
@@ -82,6 +82,8 @@ function validCurrency(value: string): boolean {
|
||||
}
|
||||
|
||||
export type BusinessFinancialPlanInput = {
|
||||
/** Synthetic annual demonstration; excluded from investor forecast inputs. */
|
||||
annualExampleSnapshot?: string;
|
||||
currency: string;
|
||||
annualGrowthPercent?: string;
|
||||
presentationCurrency?: string;
|
||||
|
||||
@@ -201,6 +201,88 @@ export function newAnnualReferencePlan(): AnnualReferencePlan {
|
||||
};
|
||||
}
|
||||
|
||||
/** Preserve incomplete numeric drafts as entered, but accept only the example's schema. */
|
||||
export function restoreAnnualExampleSnapshot(
|
||||
snapshot?: string,
|
||||
): AnnualReferencePlan | null {
|
||||
if (!snapshot || snapshot.length > 12000) return null;
|
||||
try {
|
||||
const decoded = JSON.parse(snapshot);
|
||||
if (
|
||||
decoded.version !== 1 ||
|
||||
decoded.kind !== 'synthetic-annual-example'
|
||||
)
|
||||
return null;
|
||||
const plan = decoded.plan;
|
||||
const template = newAnnualReferencePlan();
|
||||
const numericDraft = (value: unknown) =>
|
||||
typeof value === 'string' && value.length <= 40;
|
||||
for (const key of [
|
||||
'openingCash',
|
||||
'otherCurrentAssets',
|
||||
'longTermAssets',
|
||||
'ownerCapital',
|
||||
'investorCapital',
|
||||
'loans',
|
||||
'additionalMoney',
|
||||
] as const)
|
||||
if (!numericDraft(plan[key])) return null;
|
||||
if (
|
||||
!Array.isArray(plan.startupExpenses) ||
|
||||
plan.startupExpenses.length !== template.startupExpenses.length ||
|
||||
plan.startupExpenses.some(
|
||||
(row: { label: unknown; amount: unknown }, i: number) =>
|
||||
row.label !== template.startupExpenses[i].label ||
|
||||
!numericDraft(row.amount),
|
||||
)
|
||||
)
|
||||
return null;
|
||||
if (
|
||||
!Array.isArray(plan.years) ||
|
||||
plan.years.length !== template.years.length ||
|
||||
plan.years.some(
|
||||
(row: Record<string, unknown>, i: number) =>
|
||||
row.year !== template.years[i].year ||
|
||||
Object.keys(template.years[i]).some(
|
||||
(key) => key !== 'year' && !numericDraft(row[key]),
|
||||
),
|
||||
)
|
||||
)
|
||||
return null;
|
||||
// Do not accept unrelated fields or investor data into this example.
|
||||
if (
|
||||
JSON.stringify(Object.keys(plan).sort()) !==
|
||||
JSON.stringify(Object.keys(template).sort()) ||
|
||||
plan.years.some(
|
||||
(row: Record<string, unknown>, i: number) =>
|
||||
JSON.stringify(Object.keys(row).sort()) !==
|
||||
JSON.stringify(Object.keys(template.years[i]).sort()),
|
||||
) ||
|
||||
plan.startupExpenses.some(
|
||||
(row: Record<string, unknown>) =>
|
||||
Object.keys(row).sort().join(',') !== 'amount,label',
|
||||
)
|
||||
)
|
||||
return null;
|
||||
return plan as AnnualReferencePlan;
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
export function serializeAnnualExampleSnapshot(
|
||||
plan: AnnualReferencePlan,
|
||||
): string {
|
||||
const snapshot = JSON.stringify({
|
||||
version: 1,
|
||||
kind: 'synthetic-annual-example',
|
||||
plan,
|
||||
});
|
||||
if (!restoreAnnualExampleSnapshot(snapshot))
|
||||
throw new Error('Não foi possível guardar o cenário de exemplo.');
|
||||
return snapshot;
|
||||
}
|
||||
|
||||
function money(value: string): bigint {
|
||||
if (value.length > 40 || !/^\d+(?:\.\d{1,2})?$/.test(value.trim()))
|
||||
throw new Error(
|
||||
|
||||
Reference in New Issue
Block a user