fix(cms): resolve internal CTA links per locale via a page selector (MITHOME-123)

The home hero CTA stored a free-text href "/kapcsolat" that predated the
locale-prefixed routing (MITHOME-114), so the main call-to-action led to
a 404. Internal targets were typed by hand and could never follow the
locale or its translated slug.

CTA links now choose between an internal page (home/about/services/
contact/privacy/terms) and an external URL. Page links are resolved per
request with localePath(), giving /hu/kapcsolat and /en/contact; external
links (webmail) keep their localized href. A Payload migration converts
existing internal paths to page links on the published global and every
draft/version, idempotently and independent of the locale migration's
order. The global-doc iteration shared with MITHOME-111 moved to
src/lib/migrate-global-docs.ts; the JSON seed emits the new shape too.

Verified on a copy of the staging DB: both migrations ran on startup,
/hu and /en CTAs point to their contact pages (200), webmail unchanged,
admin shows the selector, toggles the fields and blocks publishing a URL
link without a URL.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Do Siki
2026-09-29 01:26:42 +02:00
co-authored by Claude Opus 5.5
parent 38d34fab4c
commit e8ff7adeb5
12 changed files with 460 additions and 65 deletions
@@ -0,0 +1,95 @@
import { down, toPageLink, up } from '../migrations/20260929_cta_page_links'
import { up as localizeUp } 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) => ({})),
}
}
const heroWith = (primaryHref: unknown, secondaryHref: unknown) => ({
title: 'Cím',
cta: {
primary: { text: { hu: 'Kapcsolat' }, href: primaryHref, external: false },
secondary: { text: { hu: 'Webmail' }, href: secondaryHref, external: true },
},
})
function setup(primaryHref: unknown = { hu: '/kapcsolat' }, secondaryHref: unknown = { hu: 'https://mail.mozdit.hu' }) {
const home: Doc = { _id: 'g-home', globalType: 'home', hero: heroWith(primaryHref, secondaryHref) }
const version: Doc = { _id: 'v1', version: { hero: heroWith({ hu: '/hu/kapcsolat' }, 'https://mail.mozdit.hu') } }
const globals = fakeCollection([home])
const versions = fakeCollection([version])
const payload = {
db: { globals: { collection: globals }, versions: { home: { collection: versions } } },
logger: { info: jest.fn() },
}
return { args: { payload, session: undefined, req: {} } as never, home, version, payload }
}
const cta = (doc: Doc) => (doc.hero as { cta: Record<string, Doc> }).cta
describe('20260929_cta_page_links migration (MITHOME-123)', () => {
it('should turn the legacy internal path into a page link and drop the href', async () => {
const { args, home } = setup()
await up(args)
expect(cta(home).primary).toEqual({ text: { hu: 'Kapcsolat' }, linkType: 'page', page: 'contact', external: false })
})
it('should keep external URLs as URL links', async () => {
const { args, home } = setup()
await up(args)
expect(cta(home).secondary).toMatchObject({ linkType: 'url', href: { hu: 'https://mail.mozdit.hu' } })
})
it('should migrate version records, including legacy string hrefs', async () => {
const { args, version } = setup()
await up(args)
const versionCta = cta(version.version as Doc)
expect(versionCta.primary).toMatchObject({ linkType: 'page', page: 'contact' })
expect(versionCta.secondary).toMatchObject({ linkType: 'url', href: 'https://mail.mozdit.hu' })
})
it('should be idempotent', async () => {
const { args, home, payload } = setup()
await up(args)
const snapshot = JSON.stringify(home)
await up(args)
expect(JSON.stringify(home)).toBe(snapshot)
expect(payload.logger.info).toHaveBeenLastCalledWith('MITHOME-123 CTA link migration: home — 0 CTA(s) rewritten')
})
it('should give the same result regardless of order with the locale migration', async () => {
const localeFirst = setup('/kapcsolat', 'https://mail.mozdit.hu')
await localizeUp(localeFirst.args)
await up(localeFirst.args)
const ctaFirst = setup('/kapcsolat', 'https://mail.mozdit.hu')
await up(ctaFirst.args)
await localizeUp(ctaFirst.args)
expect(cta(ctaFirst.home)).toEqual(cta(localeFirst.home))
})
it('should treat a missing href as a URL link without inventing a target', () => {
const link: Doc = { text: { hu: 'x' } }
expect(toPageLink(link)).toBe(1)
expect(link).toEqual({ text: { hu: 'x' }, linkType: 'url' })
})
it('should restore a hu href on down', async () => {
const { args, home } = setup()
await up(args)
await down(args)
expect(cta(home).primary).toEqual({ text: { hu: 'Kapcsolat' }, href: { hu: '/hu/kapcsolat' }, external: false })
expect(cta(home).secondary).toEqual({ text: { hu: 'Webmail' }, href: { hu: 'https://mail.mozdit.hu' }, external: true })
})
})