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,55 @@
|
||||
/**
|
||||
* Shared plumbing for Payload data migrations on Globals (MITHOME-111/123).
|
||||
*
|
||||
* Applies `transform` in place to a global's field data — the published
|
||||
* document AND every draft/version record (`version` sub-document) — and
|
||||
* writes back only what changed.
|
||||
*
|
||||
* DECISION: direct Mongo `updateOne` instead of the Local API — no new
|
||||
* version is created, no hooks run, `updatedAt` stays untouched.
|
||||
*/
|
||||
import type { MigrateDownArgs, MigrateUpArgs, MongooseAdapter } from '@payloadcms/db-mongodb'
|
||||
|
||||
type Args = MigrateUpArgs | MigrateDownArgs
|
||||
type Data = Record<string, unknown>
|
||||
|
||||
function pick(doc: Data, keys: readonly string[]) {
|
||||
return Object.fromEntries(keys.filter((key) => key in doc).map((key) => [key, doc[key]]))
|
||||
}
|
||||
|
||||
/**
|
||||
* @param keys top-level field names `transform` may touch (the `$set` scope)
|
||||
* @returns the number of values `transform` reported as changed
|
||||
*/
|
||||
export async function rewriteGlobalDocs(
|
||||
{ payload, session }: Args,
|
||||
slug: string,
|
||||
keys: readonly string[],
|
||||
transform: (data: Data) => number,
|
||||
): Promise<number> {
|
||||
const db = payload.db as unknown as MongooseAdapter
|
||||
let changed = 0
|
||||
|
||||
const main = await db.globals.collection.findOne({ globalType: slug }, { session })
|
||||
if (main) {
|
||||
const count = transform(main)
|
||||
if (count > 0) {
|
||||
await db.globals.collection.updateOne({ _id: main._id }, { $set: pick(main, keys) }, { session })
|
||||
changed += count
|
||||
}
|
||||
}
|
||||
|
||||
const versions = db.versions[slug]
|
||||
if (versions) {
|
||||
for await (const doc of versions.collection.find({}, { session })) {
|
||||
if (typeof doc.version !== 'object' || doc.version === null) continue
|
||||
const count = transform(doc.version)
|
||||
if (count > 0) {
|
||||
await versions.collection.updateOne({ _id: doc._id }, { $set: { version: doc.version } }, { session })
|
||||
changed += count
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return changed
|
||||
}
|
||||
Reference in New Issue
Block a user