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:
co-authored by
Claude Opus 5.5
parent
38d34fab4c
commit
e8ff7adeb5
@@ -0,0 +1,72 @@
|
||||
/**
|
||||
* 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)
|
||||
}
|
||||
@@ -6,13 +6,13 @@
|
||||
* ü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.
|
||||
* Az írás módja (közvetlen Mongo, hookok/új verzió nélkül): lásd
|
||||
* src/lib/migrate-global-docs.ts.
|
||||
*
|
||||
* 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 { MigrateDownArgs, MigrateUpArgs } from '@payloadcms/db-mongodb'
|
||||
import type { GlobalConfig } from 'payload'
|
||||
|
||||
import { Home } from '../globals/Home'
|
||||
@@ -27,48 +27,18 @@ import {
|
||||
wrapLocalized,
|
||||
type LocalizedPath,
|
||||
} from '../lib/localize-paths'
|
||||
import { rewriteGlobalDocs } from '../lib/migrate-global-docs'
|
||||
|
||||
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)
|
||||
const paths = collectLocalizedPaths(global.fields as never)
|
||||
const changed = await rewriteGlobalDocs(args, global.slug, topLevelKeys(paths), (data) => transform(data, paths))
|
||||
args.payload.logger.info(`MITHOME-111 locale migration: ${global.slug} — ${changed} value(s) rewritten`)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import * as migration_20260929_localize_globals from './20260929_localize_globals'
|
||||
import * as migration_20260929_cta_page_links from './20260929_cta_page_links'
|
||||
|
||||
/**
|
||||
* Payload migrations, in order. Passed to `mongooseAdapter({ prodMigrations })`
|
||||
@@ -10,4 +11,10 @@ export const migrations = [
|
||||
down: migration_20260929_localize_globals.down,
|
||||
name: '20260929_localize_globals',
|
||||
},
|
||||
{
|
||||
// MITHOME-123 — order-independent from the locale migration (handles both href shapes).
|
||||
up: migration_20260929_cta_page_links.up,
|
||||
down: migration_20260929_cta_page_links.down,
|
||||
name: '20260929_cta_page_links',
|
||||
},
|
||||
]
|
||||
|
||||
Reference in New Issue
Block a user