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:
co-authored by
Claude Opus 5.5
parent
9b5ca94030
commit
829f543962
@@ -0,0 +1,80 @@
|
||||
import { down, up } from '../migrations/20260929_localize_globals'
|
||||
|
||||
type Doc = Record<string, unknown>
|
||||
|
||||
function fakeCollection(docs: Doc[]) {
|
||||
return {
|
||||
findOne: jest.fn(async (filter: { globalType: string }) => docs.find((d) => d.globalType === filter.globalType) ?? null),
|
||||
find: jest.fn(() => ({
|
||||
async *[Symbol.asyncIterator]() {
|
||||
yield* docs
|
||||
},
|
||||
})),
|
||||
updateOne: jest.fn(async (_filter: Doc, _update: { $set: Doc }, _options?: unknown) => ({})),
|
||||
}
|
||||
}
|
||||
|
||||
function setup() {
|
||||
const globals = fakeCollection([
|
||||
{ _id: 'g-home', globalType: 'home', hero: { title: 'Cím', trustBullets: [{ value: 'Egy' }] }, cta: { title: 'CTA' } },
|
||||
{ _id: 'g-common', globalType: 'common', buttons: { contact: { hu: 'Kapcsolat', en: 'Contact' } } },
|
||||
])
|
||||
const homeVersions = fakeCollection([
|
||||
{ _id: 'v1', version: { hero: { title: 'Régi cím' } } },
|
||||
{ _id: 'v2', version: { hero: { title: { hu: 'Már migrált' } } } },
|
||||
])
|
||||
const payload = {
|
||||
db: { globals: { collection: globals }, versions: { home: { collection: homeVersions } } },
|
||||
logger: { info: jest.fn() },
|
||||
}
|
||||
const args = { payload, session: undefined, req: {} } as never
|
||||
return { args, globals, homeVersions, payload }
|
||||
}
|
||||
|
||||
describe('20260929_localize_globals migration', () => {
|
||||
it('should wrap legacy values of the published global under hu and $set only touched keys', async () => {
|
||||
const { args, globals } = setup()
|
||||
await up(args)
|
||||
|
||||
const homeCall = globals.updateOne.mock.calls.find(([filter]) => filter._id === 'g-home')
|
||||
expect(homeCall).toBeDefined()
|
||||
const [, update] = homeCall!
|
||||
expect(update.$set.hero).toEqual({ title: { hu: 'Cím' }, trustBullets: { hu: [{ value: 'Egy' }] } })
|
||||
expect(update.$set.cta).toEqual({ title: { hu: 'CTA' } })
|
||||
expect(update.$set).not.toHaveProperty('_id')
|
||||
expect(update.$set).not.toHaveProperty('globalType')
|
||||
})
|
||||
|
||||
it('should not rewrite a global whose values are already localized', async () => {
|
||||
const { args, globals } = setup()
|
||||
await up(args)
|
||||
const ids = globals.updateOne.mock.calls.map(([filter]) => filter._id)
|
||||
expect(ids).not.toContain('g-common')
|
||||
})
|
||||
|
||||
it('should migrate legacy version records and skip migrated ones', async () => {
|
||||
const { args, homeVersions } = setup()
|
||||
await up(args)
|
||||
|
||||
expect(homeVersions.updateOne).toHaveBeenCalledTimes(1)
|
||||
expect(homeVersions.updateOne).toHaveBeenCalledWith(
|
||||
{ _id: 'v1' },
|
||||
{ $set: { version: { hero: { title: { hu: 'Régi cím' } } } } },
|
||||
{ session: undefined },
|
||||
)
|
||||
})
|
||||
|
||||
it('should log a per-global summary', async () => {
|
||||
const { args, payload } = setup()
|
||||
await up(args)
|
||||
expect(payload.logger.info).toHaveBeenCalledWith('MITHOME-111 locale migration: home — 4 value(s) rewritten')
|
||||
})
|
||||
|
||||
it('should restore the hu value on down', async () => {
|
||||
const { args, globals } = setup()
|
||||
await down(args)
|
||||
const commonCall = globals.updateOne.mock.calls.find(([filter]) => filter._id === 'g-common')
|
||||
const [, update] = commonCall!
|
||||
expect(update.$set.buttons).toEqual({ contact: 'Kapcsolat' })
|
||||
})
|
||||
})
|
||||
Reference in New Issue
Block a user