/** * MITHOME-111: pure helpers for the "field became `localized: true`" * data migration (design D4). * * WHY config-driven paths: the Global configs are the single source of * truth for which fields are localized — deriving the migration paths from * them means the migration can never drift from the schema. * * Storage shapes (Payload mongooseAdapter): * non-localized text -> "szöveg" localized -> { hu: "szöveg" } * non-localized array -> [{ value }] localized -> { hu: [{ value }] } * A path segment `[]` means "every row of this (shared) array". */ type FieldNode = { name?: string; type: string; localized?: boolean; fields?: FieldNode[] } export type LocalizedPath = readonly string[] const ARRAY_ROWS = '[]' /** Layout fields that could hide localized children — not traversed here. */ const UNSUPPORTED_CONTAINERS = new Set(['tabs', 'row', 'collapsible', 'blocks']) export function collectLocalizedPaths( fields: readonly FieldNode[], prefix: LocalizedPath = [], ): LocalizedPath[] { const paths: LocalizedPath[] = [] for (const field of fields) { if (UNSUPPORTED_CONTAINERS.has(field.type)) { // DECISION: fail fast rather than silently skip localized data. throw new Error(`collectLocalizedPaths: unsupported field type "${field.type}"`) } if (!field.name) continue const path = [...prefix, field.name] if (field.localized) { paths.push(path) } else if (field.type === 'group') { paths.push(...collectLocalizedPaths(field.fields ?? [], path)) } else if (field.type === 'array') { paths.push(...collectLocalizedPaths(field.fields ?? [], [...path, ARRAY_ROWS])) } } return paths } type Transform = (value: unknown) => { value: unknown; changed: boolean } function isPlainObject(value: unknown): value is Record { return typeof value === 'object' && value !== null && !Array.isArray(value) } function applyAtPath(node: unknown, path: LocalizedPath, transform: Transform): number { if (typeof node !== 'object' || node === null) return 0 const [head, ...rest] = path if (head === ARRAY_ROWS) { if (!Array.isArray(node)) return 0 return node.reduce((sum, row) => sum + applyAtPath(row, rest, transform), 0) } if (!isPlainObject(node)) return 0 if (rest.length > 0) return applyAtPath(node[head], rest, transform) const result = transform(node[head]) if (result.changed) node[head] = result.value return result.changed ? 1 : 0 } // WHY in-place mutation instead of structuredClone: raw Mongo documents // carry BSON class instances (ObjectId) that a structured clone would // silently turn into plain objects. Callers pass freshly read documents. function applyAll(doc: Record, paths: readonly LocalizedPath[], transform: Transform) { return paths.reduce((sum, path) => sum + applyAtPath(doc, path, transform), 0) } /** Top-level document keys touched by `paths` — the `$set` scope of a write. */ export function topLevelKeys(paths: readonly LocalizedPath[]): string[] { return [...new Set(paths.map((path) => path[0]))] } /** * Wraps legacy (non-localized) values into `{ [locale]: value }`, in place. * Idempotent: values already stored as a locale map are left untouched, * null/undefined are skipped (nothing to preserve). Returns the number of * values changed. */ export function wrapLocalized( doc: Record, paths: readonly LocalizedPath[], locale = 'hu', ) { return applyAll(doc, paths, (value) => typeof value === 'string' || Array.isArray(value) ? { value: { [locale]: value }, changed: true } : { value, changed: false }, ) } /** * Inverse of wrapLocalized (down migration), in place: keeps only * `locale`'s value. * TRADEOFF: every other locale's value is dropped — take a DB dump first. */ export function unwrapLocalized( doc: Record, paths: readonly LocalizedPath[], locale = 'hu', ) { return applyAll(doc, paths, (value) => isPlainObject(value) ? { value: value[locale] ?? null, changed: true } : { value, changed: false }, ) }