Reuse investment context as editable simulator summaries
This commit is contained in:
@@ -19,6 +19,16 @@ export function inheritCompatibleFields(scope:string, values:Record<string,unkno
|
||||
const geoConflict=geo.some(key=>(!empty(values[key]) || key in previous) && signature(values[key])!==signature(source[key]) && previous[key]!==signature(values[key]));
|
||||
for(const [key,value] of Object.entries(source)) {
|
||||
if ((allowed && !allowed.includes(key)) || (empty(value) && !(key in previous))) continue;
|
||||
if (key.endsWith('_label')) {
|
||||
const base = key.slice(0, -6);
|
||||
const baseOwned = previous[base] === signature(values[base]);
|
||||
const baseVacant = empty(values[base]) && !(base in previous);
|
||||
if (!(base in source) || (geo.includes(base) && geoConflict) || (!baseOwned && !baseVacant && signature(values[base]) !== signature(source[base]))) continue;
|
||||
// A label describes its identifier; it must never lag behind an adopted identifier.
|
||||
if (signature(values[key]) !== signature(value)) patch[key] = value;
|
||||
next[key] = signature(value); inherited.push(key);
|
||||
continue;
|
||||
}
|
||||
const current=values[key];
|
||||
const track = (item:unknown) => key==='parcels' ? Object.fromEntries(geo.map(field=>[field,(item as Array<Record<string,unknown>>)?.[0]?.[field] ?? ''])) : item;
|
||||
const owned=previous[key]===signature(track(current));
|
||||
@@ -42,6 +52,20 @@ export function inheritCompatibleFields(scope:string, values:Record<string,unkno
|
||||
return {patch,metadata:JSON.stringify({version:1,scope,inherited:next}),inherited,conflicts};
|
||||
}
|
||||
|
||||
/** Presentation only: a saved, unedited inherited value can be shown without asking again. */
|
||||
export function reusedInvestmentValue(scope: string, key: string, values: Record<string, unknown>): string | null {
|
||||
if (!['journey_type', 'purpose', 'project_title', 'project_summary', 'sector_id', 'province_id', 'district_id', 'admin_post_id', 'locality_id'].includes(key)) return null;
|
||||
try {
|
||||
const metadata = JSON.parse(String(values.context_reuse ?? ''));
|
||||
if (metadata.version !== 1 || metadata.scope !== scope || empty(values[key]) || metadata.inherited?.[key] !== signature(values[key])) return null;
|
||||
if (key.endsWith('_id') || ['journey_type', 'purpose'].includes(key)) {
|
||||
const label = values[`${key}_label`];
|
||||
return typeof label === 'string' && label.trim() && metadata.inherited?.[`${key}_label`] === signature(label) ? label : null;
|
||||
}
|
||||
return typeof values[key] === 'string' ? values[key] : null;
|
||||
} catch { return null; }
|
||||
}
|
||||
|
||||
export function inheritSimulatorFields<T extends Record<string,unknown>>(draft:T): T {
|
||||
const source:Record<string,unknown>={};
|
||||
const plan=draft.business_plan_snapshot ? restoreBusinessPlanSnapshot(String(draft.business_plan_snapshot)) : null;
|
||||
|
||||
Reference in New Issue
Block a user