Convert the dossier personal excerpt from a plain textarea to Lexical rich text (matching the intel excerpt) with a 1000-character plain-text limit, and replace the static read-only cards on the profile page with inline DossierExcerptEditors: owners edit their personal excerpt, profiles:intel_excerpt:update holders edit the intelligence record. Changes are validated server-side (lexicalPlaintextLength) and emit dossier:personal-excerpt-update / dossier:intel-excerpt-update events.
93 lines
3.4 KiB
TypeScript
93 lines
3.4 KiB
TypeScript
import { MigrateUpArgs, MigrateDownArgs, sql } from '@payloadcms/db-postgres'
|
|
import { plaintextToLexical } from '../lib/tickets/lexical'
|
|
|
|
// dossier.personalExcerpt changed from textarea (varchar) to lexical richText
|
|
// (jsonb). Existing plain-text values are wrapped into a minimal Lexical
|
|
// document using the same plaintextToLexical helper the app uses elsewhere,
|
|
// guaranteeing the produced shape matches what the editor/RichText expect.
|
|
// The conversion runs row-by-row in TypeScript instead of SQL to keep the
|
|
// logic in one place; profile counts are small (per-unit personnel records).
|
|
|
|
interface LegacyRow extends Record<string, unknown> {
|
|
id: number
|
|
dossier_personal_excerpt: string | null
|
|
}
|
|
|
|
interface JsonbRow extends Record<string, unknown> {
|
|
id: number
|
|
dossier_personal_excerpt: string | null
|
|
}
|
|
|
|
/** Flattens a serialized Lexical state back to the legacy textarea string. */
|
|
function lexicalToLegacyText(value: string): string {
|
|
let parsed: unknown
|
|
try {
|
|
parsed = JSON.parse(value)
|
|
} catch {
|
|
return value
|
|
}
|
|
const children =
|
|
parsed && typeof parsed === "object"
|
|
? ((parsed as { root?: { children?: unknown[] } }).root?.children ?? [])
|
|
: []
|
|
const paragraphs: string[] = []
|
|
for (const para of children) {
|
|
if (!para || typeof para !== "object") continue
|
|
const inline = (para as { children?: unknown[] }).children ?? []
|
|
let text = ""
|
|
for (const node of inline) {
|
|
if (!node || typeof node !== "object") continue
|
|
const type = (node as { type?: string }).type
|
|
if (type === "text") text += (node as { text?: string }).text ?? ""
|
|
if (type === "linebreak") text += "\n"
|
|
}
|
|
paragraphs.push(text)
|
|
}
|
|
return paragraphs.join("\n\n")
|
|
}
|
|
|
|
export async function up({ db }: MigrateUpArgs): Promise<void> {
|
|
await db.execute(
|
|
sql`ALTER TABLE "profiles" ADD COLUMN "dossier_personal_excerpt_new" jsonb;`,
|
|
)
|
|
|
|
const result = await db.execute<LegacyRow>(
|
|
sql`SELECT "id", "dossier_personal_excerpt" FROM "profiles" WHERE "dossier_personal_excerpt" IS NOT NULL`,
|
|
)
|
|
const rows = result.rows ?? []
|
|
|
|
for (const row of rows) {
|
|
const lexical = plaintextToLexical(row.dossier_personal_excerpt ?? "")
|
|
await db.execute(
|
|
sql`UPDATE "profiles" SET "dossier_personal_excerpt_new" = ${JSON.stringify(lexical)}::jsonb WHERE "id" = ${row.id}`,
|
|
)
|
|
}
|
|
|
|
await db.execute(sql`ALTER TABLE "profiles" DROP COLUMN "dossier_personal_excerpt";`)
|
|
await db.execute(
|
|
sql`ALTER TABLE "profiles" RENAME COLUMN "dossier_personal_excerpt_new" TO "dossier_personal_excerpt";`,
|
|
)
|
|
}
|
|
|
|
export async function down({ db }: MigrateDownArgs): Promise<void> {
|
|
await db.execute(
|
|
sql`ALTER TABLE "profiles" ADD COLUMN "dossier_personal_excerpt_old" varchar;`,
|
|
)
|
|
|
|
const result = await db.execute<JsonbRow>(
|
|
sql`SELECT "id", "dossier_personal_excerpt"::text AS "dossier_personal_excerpt" FROM "profiles" WHERE "dossier_personal_excerpt" IS NOT NULL`,
|
|
)
|
|
const rows = result.rows ?? []
|
|
|
|
for (const row of rows) {
|
|
const legacy = lexicalToLegacyText(row.dossier_personal_excerpt ?? "")
|
|
await db.execute(
|
|
sql`UPDATE "profiles" SET "dossier_personal_excerpt_old" = ${legacy} WHERE "id" = ${row.id}`,
|
|
)
|
|
}
|
|
|
|
await db.execute(sql`ALTER TABLE "profiles" DROP COLUMN "dossier_personal_excerpt";`)
|
|
await db.execute(
|
|
sql`ALTER TABLE "profiles" RENAME COLUMN "dossier_personal_excerpt_old" TO "dossier_personal_excerpt";`,
|
|
)
|
|
}
|