Files
websitedev/proto/src/migrations/20260929_cta_page_links.ts
T
Do SikiandClaude Opus 5.5 e8ff7adeb5 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>
2026-09-29 01:26:42 +02:00

73 lines
2.7 KiB
TypeScript

/**
* MITHOME-123: a Home hero CTA-k szabad szöveges belső linkjeit
* (pl. "/kapcsolat" — a MITHOME-114 óta 404) oldalválasztóra alakítja:
* `{ linkType: 'page', page: 'contact' }`, a `href` törlődik. Külső URL
* (pl. webmail) esetén `linkType: 'url'`, a `href` változatlan marad.
*
* WHY mindkét `href`-alakot kezeli (`"…"` és `{ hu: "…" }`): így független
* a MITHOME-111 lokalizációs migrációtól való sorrendtől — a prodMigrations
* utána futtatja, a Payload CLI viszont fájlnév szerint (ábécé) rendez.
* Idempotens: ha a `linkType` már be van állítva, a CTA-hoz nem nyúl.
*/
import type { MigrateDownArgs, MigrateUpArgs } from '@payloadcms/db-mongodb'
import { internalPathToPageKey, resolveCtaHref, type CtaLink } from '../lib/cta-link'
import { rewriteGlobalDocs } from '../lib/migrate-global-docs'
type Data = Record<string, unknown>
const CTA_NAMES = ['primary', 'secondary'] as const
function isRecord(value: unknown): value is Data {
return typeof value === 'object' && value !== null && !Array.isArray(value)
}
function ctaGroups(data: Data): Data[] {
const cta = isRecord(data.hero) && isRecord(data.hero.cta) ? data.hero.cta : undefined
return cta ? CTA_NAMES.map((name) => cta[name]).filter(isRecord) : []
}
/** The Hungarian (default-locale) value of a possibly localized href. */
function storedPath(href: unknown): string | undefined {
if (typeof href === 'string') return href
return isRecord(href) && typeof href.hu === 'string' ? href.hu : undefined
}
export function toPageLink(cta: Data): number {
if (cta.linkType) return 0
const path = storedPath(cta.href)
const page = path === undefined ? undefined : internalPathToPageKey(path)
if (page) {
cta.linkType = 'page'
cta.page = page
delete cta.href
} else {
cta.linkType = 'url'
}
return 1
}
/** Inverse: restores a hu-locale href for page links, drops the new fields. */
export function toFreeTextLink(cta: Data): number {
if (!cta.linkType) return 0
if (cta.linkType === 'page') cta.href = { hu: resolveCtaHref(cta as CtaLink, 'hu') }
delete cta.linkType
delete cta.page
return 1
}
async function migrate(args: MigrateUpArgs | MigrateDownArgs, transform: (cta: Data) => number) {
const changed = await rewriteGlobalDocs(args, 'home', ['hero'], (data) =>
ctaGroups(data).reduce((sum, cta) => sum + transform(cta), 0),
)
args.payload.logger.info(`MITHOME-123 CTA link migration: home — ${changed} CTA(s) rewritten`)
}
export async function up(args: MigrateUpArgs): Promise<void> {
await migrate(args, toPageLink)
}
export async function down(args: MigrateDownArgs): Promise<void> {
await migrate(args, toFreeTextLink)
}