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
@@ -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`)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user