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
+31 -6
View File
@@ -148,7 +148,7 @@ describe('Payload globals', () => {
const SHARED_TEXT_FIELDS = new Set(['icon', 'id', 'address'])
type Node = FieldLike & { localized?: boolean; fields?: Node[] }
type Finding = { path: string; localized: boolean }
type Finding = { path: string; type?: string; localized: boolean }
const isStringArray = (f: Node) =>
f.type === 'array' && f.fields?.length === 1 && f.fields[0].name === 'value'
@@ -163,7 +163,7 @@ describe('Payload globals', () => {
out.push({ path: `${path}(container)`, localized: f.localized === true })
walk(f.fields ?? [], `${path}.`, out)
} else {
out.push({ path, localized: f.localized === true })
out.push({ path, type: f.type, localized: f.localized === true })
}
}
return out
@@ -172,9 +172,9 @@ describe('Payload globals', () => {
function expected(finding: Finding): boolean {
if (finding.path.endsWith('(container)')) return false
if (finding.path.endsWith('[]')) return true
const leaf = finding.path.split('.').pop()!
if (leaf === 'external') return false
return !SHARED_TEXT_FIELDS.has(leaf)
// Checkbox/radio/select (external, linkType, page) are structural.
if (finding.type !== 'text' && finding.type !== 'textarea') return false
return !SHARED_TEXT_FIELDS.has(finding.path.split('.').pop()!)
}
it.each([Home, About, Services, Contact, Common].map((g) => [g.slug, g] as const))(
@@ -186,11 +186,36 @@ describe('Payload globals', () => {
},
)
it('should localize the locale-dependent CTA hrefs but share the external flag', () => {
it('should localize the CTA URL but share the link type, page and external flag', () => {
const findings = walk(Home.fields as Node[], '', [])
const byPath = Object.fromEntries(findings.map((f) => [f.path, f.localized]))
expect(byPath['hero.cta.primary.href']).toBe(true)
expect(byPath['hero.cta.primary.linkType']).toBe(false)
expect(byPath['hero.cta.primary.page']).toBe(false)
expect(byPath['hero.cta.primary.external']).toBe(false)
})
})
describe('MITHOME-123 CTA links', () => {
type Validate = (value: unknown, opts: { siblingData: { linkType?: string } }) => true | string
const cta = (name: string) => {
const hero = findField(Home.fields, 'hero') as { fields: unknown[] }
const group = findField(hero.fields, 'cta') as { fields: unknown[] }
return (findField(group.fields, name) as { fields: unknown[] }).fields
}
const validate = (field: string) => (findField(cta('primary'), field) as unknown as { validate: Validate }).validate
it('should offer every routable page, including home, in the page selector', () => {
const page = findField(cta('primary'), 'page') as unknown as { options: { value: string }[] }
expect(page.options.map((o) => o.value)).toEqual(['home', 'about', 'services', 'contact', 'privacy', 'terms'])
})
it('should require a page only for internal links and a URL only for external links', () => {
expect(validate('page')(null, { siblingData: { linkType: 'page' } })).toEqual(expect.any(String))
expect(validate('page')(null, { siblingData: { linkType: 'url' } })).toBe(true)
expect(validate('href')('', { siblingData: { linkType: 'url' } })).toEqual(expect.any(String))
expect(validate('href')(null, { siblingData: { linkType: 'page' } })).toBe(true)
expect(validate('href')('https://mail.mozdit.hu', { siblingData: { linkType: 'url' } })).toBe(true)
})
})
})