Files
websitedev/proto/src/lib/content-revision.ts
T
Do SikiandClaude Opus 5.5 d0dafa8471 feat(cms): align service claims with the real product range via migration (MITHOME-129)
The site promised more than the production server delivers (99.9%,
"nearly 100%", 1–100 GB SSD, unlimited traffic, 1–20 GB per mailbox,
Python/Node/PostgreSQL, DNSSEC, developer API, worldwide name servers,
immediate response). The 20260929_service_claims migration applies the
owner-confirmed texts C1–C14 (docs/termekkor-es-szerverkapacitas-
elemzes.md, ch. 9) and adds the 90% mailbox warning feature.

reviseContent() only replaces an exact Hungarian match, only rewrites
English that is still the old AI draft (human edits are kept and
reported), inserts list items once, and sends an approved page back to
ai-draft when its English changed. Published doc + latest version only.
Legal texts (terms, privacy) are left for legal review. The translation
memory gains the new pairs. Verified on a staging DB copy.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-09-29 16:03:46 +02:00

97 lines
4.0 KiB
TypeScript

/**
* MITHOME-129: exact-match content revision on localized fields, in place.
*
* Used by data migrations that correct published copy (e.g. service claims
* aligned with the real product range). Rules:
* - a value changes only when its current Hungarian text is EXACTLY the
* expected old text — anything an editor already rewrote is left alone;
* - English is replaced only when it is empty or still the old AI draft
* (`enWas`) — a human English edit is kept and reported instead;
* - if English changed on a page whose translation was approved, the page
* goes back to "ai-draft": the new English has not been reviewed yet.
* Works for localized text leaves and whole-array localized string lists
* ({ value } rows), where it can also insert a new item after an anchor.
*/
import { applyAll, isPlainObject, type LocalizedPath } from './localize-paths'
export type TextRevision = { from: string; to: string; enWas?: string; enTo: string }
export type ListInsertion = { afterHu: string; hu: string; afterEn: string; en: string }
export type RevisionReport = { changed: number; enKept: string[] }
type Row = { value?: unknown; id?: unknown }
const isEmpty = (value: unknown) => value === undefined || value === null || value === ''
function reviseText(value: Record<string, unknown>, revisions: readonly TextRevision[], report: RevisionReport) {
const revision = revisions.find((r) => r.from === value.hu)
if (!revision) return { hu: false, en: false }
value.hu = revision.to
if (isEmpty(value.en) || value.en === revision.enWas) {
value.en = revision.enTo
return { hu: true, en: true }
}
report.enKept.push(revision.from)
return { hu: true, en: false }
}
function reviseRows(rows: Row[], match: (text: string) => string | undefined): number {
let count = 0
for (const row of rows) {
const next = typeof row.value === 'string' ? match(row.value) : undefined
if (next !== undefined) {
row.value = next
count += 1
}
}
return count
}
function insertRow(rows: Row[], after: string, text: string, newId: () => string): boolean {
if (rows.some((row) => row.value === text)) return false
const index = rows.findIndex((row) => row.value === after)
if (index === -1) return false
rows.splice(index + 1, 0, { value: text, id: newId() })
return true
}
export function reviseContent(
doc: Record<string, unknown>,
paths: readonly LocalizedPath[],
revisions: readonly TextRevision[],
insertions: readonly ListInsertion[],
newId: () => string,
): RevisionReport {
const report: RevisionReport = { changed: 0, enKept: [] }
let enChanged = false
applyAll(doc, paths, (value) => {
if (!isPlainObject(value)) return { value, changed: false }
if (typeof value.hu === 'string') {
const result = reviseText(value, revisions, report)
enChanged ||= result.en
if (result.hu) report.changed += 1
return { value, changed: result.hu }
}
if (!Array.isArray(value.hu)) return { value, changed: false }
const hu = value.hu as Row[]
const en = Array.isArray(value.en) ? (value.en as Row[]) : undefined
const huMatched = revisions.filter((r) => hu.some((row) => row.value === r.from))
let count = reviseRows(hu, (text) => revisions.find((r) => r.from === text)?.to)
const enCount = en ? reviseRows(en, (text) => revisions.find((r) => r.enWas !== undefined && r.enWas === text)?.enTo) : 0
// A Hungarian row changed but its English row was not the old AI draft.
if (en && enCount < huMatched.length) report.enKept.push(...huMatched.map((r) => r.from).slice(enCount))
for (const insertion of insertions) {
if (insertRow(hu, insertion.afterHu, insertion.hu, newId)) count += 1
if (en && insertRow(en, insertion.afterEn, insertion.en, newId)) enChanged = true
}
enChanged ||= enCount > 0
report.changed += count
return { value, changed: count > 0 || enCount > 0 }
})
if (enChanged && doc.enTranslationStatus === 'approved') doc.enTranslationStatus = 'ai-draft'
return report
}