feat(cms): localize translatable Globals fields with lossless migration (MITHOME-111)

Mark every translatable text/textarea field of the Home, About, Services,
Contact and Common globals as localized. Icons, row ids, the external
flag and the postal address stay shared; CTA hrefs are localized because
internal routes differ per locale. String lists (paragraphs, bullets,
features, spec items) are localized at array level so locales can have
different item counts; structured rows share order/icons and localize
only their inner text.

Existing data is not re-seeded (that would overwrite admin edits): a
Payload migration wraps legacy values into { hu: value } on the published
global and every draft/version record, idempotently, and runs on
production startup via prodMigrations. Paths are derived from the Global
configs themselves so the migration cannot drift from the schema.

OpenSpec change: openspec/changes/localize-globals-mithome-111

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Do Siki
2026-09-29 00:52:26 +02:00
co-authored by Claude Opus 5.5
parent 9b5ca94030
commit 829f543962
21 changed files with 839 additions and 111 deletions
@@ -0,0 +1,82 @@
/**
* MITHOME-111: a Global mezők utólagos `localized: true` jelölése után a
* meglévő (nem lokalizált alakú) értékeket `{ hu: érték }` alakba csomagolja.
*
* WHY nem a JSON-seed újrafuttatása (mint MITHOME-110-nél): az felülírná az
* ügyfél admin-szerkesztéseit. Ez a migráció a tárolt értéket őrzi meg —
* a publikált dokumentumon ÉS a draft/verzió rekordokon is.
*
* DECISION: közvetlen Mongo `updateOne` a Local API helyett — így nem
* keletkezik új verzió, nem futnak hookok, és az `updatedAt` sem változik.
*
* Futás: productionben induláskor automatikusan (`prodMigrations`,
* payload.config.ts); dev-ben `npx payload migrate`. Idempotens.
*/
import type { MigrateDownArgs, MigrateUpArgs, MongooseAdapter } from '@payloadcms/db-mongodb'
import type { GlobalConfig } from 'payload'
import { Home } from '../globals/Home'
import { About } from '../globals/About'
import { Services } from '../globals/Services'
import { Contact } from '../globals/Contact'
import { Common } from '../globals/Common'
import {
collectLocalizedPaths,
topLevelKeys,
unwrapLocalized,
wrapLocalized,
type LocalizedPath,
} from '../lib/localize-paths'
const LOCALIZED_GLOBALS: readonly GlobalConfig[] = [Home, About, Services, Contact, Common]
type Transform = (doc: Record<string, unknown>, paths: readonly LocalizedPath[]) => number
type Args = MigrateUpArgs | MigrateDownArgs
function pick(doc: Record<string, unknown>, keys: readonly string[]) {
return Object.fromEntries(keys.filter((key) => key in doc).map((key) => [key, doc[key]]))
}
async function migrateGlobal(global: GlobalConfig, { payload, session }: Args, transform: Transform) {
const db = payload.db as unknown as MongooseAdapter
const paths = collectLocalizedPaths(global.fields as never)
let changed = 0
const main = await db.globals.collection.findOne({ globalType: global.slug }, { session })
if (main) {
const count = transform(main, paths)
if (count > 0) {
await db.globals.collection.updateOne({ _id: main._id }, { $set: pick(main, topLevelKeys(paths)) }, { session })
changed += count
}
}
const versions = db.versions[global.slug]
if (versions) {
const versionPaths = paths.map((path) => ['version', ...path])
for await (const doc of versions.collection.find({}, { session })) {
const count = transform(doc, versionPaths)
if (count > 0) {
await versions.collection.updateOne({ _id: doc._id }, { $set: { version: doc.version } }, { session })
changed += count
}
}
}
payload.logger.info(`MITHOME-111 locale migration: ${global.slug} — ${changed} value(s) rewritten`)
}
async function migrateAll(args: Args, transform: Transform) {
for (const global of LOCALIZED_GLOBALS) {
await migrateGlobal(global, args, transform)
}
}
export async function up(args: MigrateUpArgs): Promise<void> {
await migrateAll(args, (doc, paths) => wrapLocalized(doc, paths))
}
/** TRADEOFF: drops every non-hu value — restore from a DB dump instead if possible. */
export async function down(args: MigrateDownArgs): Promise<void> {
await migrateAll(args, (doc, paths) => unwrapLocalized(doc, paths))
}