Keep card name and contact fields in sync after edits

Two bugs caused the cards list and integrations to show stale data
after a user edited a card in the detail view:

1. ResponseCard.name is a denormalized display string set only at
   OCR / survey-submit time. Editing firstName or lastName never
   recomputed it, so the table header and Name column kept the old
   value. PUT /api/cards/[id] now recomputes name from first + last
   whenever either changes (unless the caller passed an explicit
   name). The detail page header reads from in-flight edits so the
   title updates live as the user types.

2. The default form template marked only firstName/lastName as
   isCore. Every other field (email, cellPhone, address, etc.) was
   non-core, so dynamic-field edits landed in ResponseCard.fieldData
   JSON and never touched the top-level columns the list view,
   search, CSV export, and integrations read from. The PUT route
   now promotes any fieldData keys that match canonical columns up
   to those columns; the default template marks all canonical
   fields as isCore so new orgs avoid the problem in the first
   place.

Adds scripts/backfill-core-fields.ts (dry-run by default; pass
--apply to commit) to flip existing FormField rows to isCore = true
where the key matches a canonical column and to promote any
existing fieldData values into empty top-level columns + recompute
stale name values.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
Randall Stillwell 2026-05-21 08:30:27 -05:00
parent 532a818995
commit 7aedae02fc
4 changed files with 321 additions and 24 deletions

View file

@ -0,0 +1,216 @@
/**
* Backfill script for the "core fields" bug fix.
*
* Background
*
* The default form template marked only `firstName` / `lastName` as
* `isCore: true`. Every other field (email, cellPhone, address, ) was
* treated as non-core, so user edits in the dynamic-field form landed
* inside `ResponseCard.fieldData` (JSON) instead of the matching top-level
* column. The list view, search, integrations, and CSV export all read the
* top-level columns, so they showed stale data after edits.
*
* Likewise, `ResponseCard.name` is a denormalized display string that was
* never recomputed when `firstName` / `lastName` changed, so the table's
* Name column and any integration that reads `name` stayed stale.
*
* What this script does
*
* 1. Flips `isCore = true` on every existing `FormTemplateField` whose
* `key` corresponds to a real top-level `ResponseCard` column.
* 2. Walks every `ResponseCard` and:
* a. promotes any matching keys from `fieldData` up to the top-level
* columns (only when the column is currently empty explicit
* column values win over stale-or-newer fieldData values);
* b. recomputes `name` from `firstName` / `lastName` when it's stale
* or missing.
*
* Usage
*
* npx tsx scripts/backfill-core-fields.ts # dry-run (default)
* npx tsx scripts/backfill-core-fields.ts --apply # actually write
*/
import { PrismaClient } from "../src/generated/prisma/client.js";
import { PrismaPg } from "@prisma/adapter-pg";
import pg from "pg";
const APPLY = process.argv.includes("--apply");
const url = new URL(process.env.DATABASE_URL!);
url.searchParams.delete("sslmode");
const pool = new pg.Pool({
connectionString: url.toString(),
max: 5,
ssl: { rejectUnauthorized: false },
});
const prisma = new PrismaClient({ adapter: new PrismaPg(pool) });
const PROMOTABLE_STRING = new Set([
"firstName", "lastName", "name",
"gender", "dateOfBirth",
"maritalStatus", "maritalStatusOther", "visitType",
"cellPhone", "homePhone", "email",
"address", "aptNumber", "city", "state", "zip",
"prayerRequests", "messageTopicsOther", "attendanceDuration",
"campusPreferenceOther", "howHeardOther", "serviceAttended",
"followUp", "notes", "serviceTime", "planningCenter",
]);
const PROMOTABLE_BOOL = new Set([
"prayerForTeam", "prayerConfidential",
"iSaidYesBookSent", "ftGuestLetterSent",
]);
const PROMOTABLE_ARRAY = new Set([
"messageTopics", "nextStep", "campusPreference", "howHeard",
]);
const PROMOTABLE_DATE = new Set([
"firstTimeGuestDate", "salvationDate",
]);
const ALL_PROMOTABLE_KEYS = new Set<string>([
...PROMOTABLE_STRING,
...PROMOTABLE_BOOL,
...PROMOTABLE_ARRAY,
...PROMOTABLE_DATE,
]);
function isEmpty(v: unknown): boolean {
if (v === null || v === undefined) return true;
if (typeof v === "string") return v.trim() === "";
if (Array.isArray(v)) return v.length === 0;
return false;
}
async function flipIsCoreOnTemplateFields(): Promise<void> {
console.log("\n1. Promoting FormField rows to isCore = true…");
const candidates = await prisma.formField.findMany({
where: { isCore: false, key: { in: Array.from(ALL_PROMOTABLE_KEYS) } },
select: { id: true, key: true, label: true, formTemplateId: true },
});
console.log(` Found ${candidates.length} non-core field(s) that should be core`);
if (candidates.length === 0) return;
for (const f of candidates.slice(0, 10)) {
console.log(` - ${f.key} (template ${f.formTemplateId})`);
}
if (candidates.length > 10) console.log(` … and ${candidates.length - 10} more`);
if (!APPLY) return;
const ids = candidates.map((c) => c.id);
const res = await prisma.formField.updateMany({
where: { id: { in: ids } },
data: { isCore: true },
});
console.log(` Updated ${res.count} field(s)`);
}
async function backfillResponseCards(): Promise<void> {
console.log("\n2. Backfilling ResponseCard top-level columns from fieldData…");
const BATCH = 200;
let cursor: string | undefined;
let scanned = 0;
let cardsWithPromotion = 0;
let cardsWithNameFix = 0;
let totalFieldsPromoted = 0;
// Sample logging
const sample: string[] = [];
while (true) {
const cards = await prisma.responseCard.findMany({
take: BATCH,
...(cursor ? { skip: 1, cursor: { id: cursor } } : {}),
orderBy: { id: "asc" },
});
if (cards.length === 0) break;
for (const card of cards) {
scanned++;
const updates: Record<string, unknown> = {};
// (a) Promote fieldData keys → matching top-level columns when the
// top-level column is currently empty.
const fd = (card.fieldData as Record<string, unknown> | null) ?? null;
if (fd && typeof fd === "object") {
const cardAsAny = card as unknown as Record<string, unknown>;
for (const [k, v] of Object.entries(fd)) {
if (!ALL_PROMOTABLE_KEYS.has(k)) continue;
if (!isEmpty(cardAsAny[k])) continue; // don't clobber existing values
if (isEmpty(v)) continue;
if (PROMOTABLE_STRING.has(k)) updates[k] = String(v);
else if (PROMOTABLE_BOOL.has(k)) updates[k] = Boolean(v);
else if (PROMOTABLE_ARRAY.has(k)) updates[k] = Array.isArray(v) ? v : null;
else if (PROMOTABLE_DATE.has(k)) updates[k] = new Date(String(v));
}
}
// (b) Recompute `name` from firstName/lastName when stale or missing.
const nextFirst =
("firstName" in updates ? (updates.firstName as string | null) : card.firstName) ?? null;
const nextLast =
("lastName" in updates ? (updates.lastName as string | null) : card.lastName) ?? null;
const combined = [nextFirst, nextLast].filter(Boolean).join(" ").trim();
const desiredName = combined || null;
// Only touch `name` if we have first/last to derive from AND the current
// stored value differs. This avoids wiping a legacy name with null.
if (desiredName && desiredName !== card.name) {
updates.name = desiredName;
}
if (Object.keys(updates).length === 0) continue;
const promotedFieldKeys = Object.keys(updates).filter((k) => k !== "name");
if (promotedFieldKeys.length > 0) {
cardsWithPromotion++;
totalFieldsPromoted += promotedFieldKeys.length;
}
if ("name" in updates) cardsWithNameFix++;
if (sample.length < 5) {
sample.push(
` - ${card.id}: ${Object.keys(updates).map((k) => `${k}=${JSON.stringify(updates[k])}`).join(", ")}`
);
}
if (APPLY) {
await prisma.responseCard.update({
where: { id: card.id },
data: updates as Parameters<typeof prisma.responseCard.update>[0]["data"],
});
}
}
cursor = cards[cards.length - 1].id;
if (cards.length < BATCH) break;
}
console.log(` Scanned ${scanned} card(s)`);
console.log(` Promotion candidates: ${cardsWithPromotion} card(s), ${totalFieldsPromoted} field(s) total`);
console.log(` Name-recompute candidates: ${cardsWithNameFix} card(s)`);
if (sample.length) {
console.log(" Sample changes:");
for (const line of sample) console.log(line);
}
}
async function main(): Promise<void> {
console.log("=== ResponseCard core-fields backfill ===");
console.log(APPLY ? "Mode: APPLY (writes enabled)" : "Mode: DRY-RUN (no writes — pass --apply to commit)");
await flipIsCoreOnTemplateFields();
await backfillResponseCards();
console.log("\nDone.");
}
main()
.catch((err) => {
console.error("Backfill failed:", err);
process.exitCode = 1;
})
.finally(async () => {
await prisma.$disconnect();
await pool.end();
});

View file

@ -377,6 +377,7 @@ export default function CardDetailPage() {
}
toast.success("Card updated");
await fetchCard();
router.refresh();
} catch (err) {
toast.error(err instanceof Error ? err.message : "Failed to save");
} finally {
@ -407,6 +408,7 @@ export default function CardDetailPage() {
toast.success("Review complete — card will sync to Monday.com");
await fetchCard();
setEdits({});
router.refresh();
} catch (err) {
toast.error(err instanceof Error ? err.message : "Failed to complete review");
} finally {
@ -539,6 +541,26 @@ export default function CardDetailPage() {
const reviewStatus = card.reviewStatus;
const hasEdits = Object.keys(edits).length > 0 || Object.keys(fieldDataEdits).length > 0;
// Compute the header title from in-flight edits so it updates live as the
// user types. Falls back through edits → fieldData edits → card columns →
// legacy `card.name` so it works for both core and non-core templates.
const editedFirst =
"firstName" in edits
? String(edits.firstName ?? "")
: "firstName" in fieldDataEdits
? String(fieldDataEdits.firstName ?? "")
: (card.firstName ?? "");
const editedLast =
"lastName" in edits
? String(edits.lastName ?? "")
: "lastName" in fieldDataEdits
? String(fieldDataEdits.lastName ?? "")
: (card.lastName ?? "");
const displayName =
[editedFirst, editedLast].filter(Boolean).join(" ").trim() ||
card.name ||
"Unnamed Card";
return (
<div className="space-y-6">
<div className="flex flex-col gap-3 sm:flex-row sm:items-center sm:gap-4">
@ -592,7 +614,7 @@ export default function CardDetailPage() {
</div>
)}
<Header title={String(card.name || "Unnamed Card")} icon={ScanLine}>
<Header title={displayName} icon={ScanLine}>
<div className="flex flex-wrap gap-2">
{canReprocess && ocrStatus !== "processing" && (
<Button variant="outline" size="sm" className="rounded-xl" onClick={handleReprocess} disabled={reprocessing}>

View file

@ -133,6 +133,60 @@ export async function PUT(
if (body.rawOcrResponse != null) data.rawOcrResponse = body.rawOcrResponse;
if (body.fieldData !== undefined) data.fieldData = body.fieldData;
// Promote any fieldData keys that match canonical top-level ResponseCard
// columns up to those columns. Form templates may mark contact/address
// fields as non-core, which means user edits land in `fieldData` only —
// promotion keeps the top-level columns (used by the list view, search,
// integrations, CSV export) in sync. Explicit body fields always win.
if (body.fieldData && typeof body.fieldData === "object") {
const fd = body.fieldData as Record<string, unknown>;
const PROMOTABLE_STRING = new Set([
"firstName", "lastName", "name",
"gender", "dateOfBirth",
"maritalStatus", "maritalStatusOther", "visitType",
"cellPhone", "homePhone", "email",
"address", "aptNumber", "city", "state", "zip",
"prayerRequests", "messageTopicsOther", "attendanceDuration",
"campusPreferenceOther", "howHeardOther", "serviceAttended",
"followUp", "notes", "serviceTime", "planningCenter",
]);
const PROMOTABLE_BOOL = new Set([
"prayerForTeam", "prayerConfidential",
"iSaidYesBookSent", "ftGuestLetterSent",
]);
const PROMOTABLE_ARRAY = new Set([
"messageTopics", "nextStep", "campusPreference", "howHeard",
]);
const PROMOTABLE_DATE = new Set([
"firstTimeGuestDate", "salvationDate",
]);
for (const [k, v] of Object.entries(fd)) {
if (k in data) continue;
if (PROMOTABLE_STRING.has(k)) {
data[k] = v == null || v === "" ? null : String(v);
} else if (PROMOTABLE_BOOL.has(k)) {
data[k] = Boolean(v);
} else if (PROMOTABLE_ARRAY.has(k)) {
data[k] = Array.isArray(v) ? v : null;
} else if (PROMOTABLE_DATE.has(k)) {
data[k] = v ? new Date(String(v)) : null;
}
}
}
// Keep the denormalized `name` field in sync with firstName/lastName.
// `name` is read by the list view, search, sort, integrations, and CSV
// export, so whenever first/last changes (and the caller didn't already
// pass an explicit `name`), recompute it.
if (("firstName" in data || "lastName" in data) && !("name" in data)) {
const nextFirst =
"firstName" in data ? (data.firstName as string | null) : card.firstName;
const nextLast =
"lastName" in data ? (data.lastName as string | null) : card.lastName;
const combined = [nextFirst, nextLast].filter(Boolean).join(" ").trim();
data.name = combined || null;
}
for (const assignField of ["assignedToId", "assignedById", "reviewedById"] as const) {
if (body[assignField] !== undefined) {
data[assignField] = body[assignField] || null;

View file

@ -1,32 +1,37 @@
import { Prisma } from "@/generated/prisma/client";
import { prisma } from "./db";
// Every default field maps to a canonical top-level column on ResponseCard,
// so they're all marked `isCore: true`. That way, edits flow through the
// `edits` channel and write to top-level columns (read by the list view,
// search, integrations, etc.) instead of the `fieldData` JSON blob.
// `fieldData` is reserved for user-defined custom fields.
const DEFAULT_FIELDS = [
{ key: "firstName", label: "First Name", type: "text", section: "personal", required: true, removable: false, isCore: true, sortOrder: 0 },
{ key: "lastName", label: "Last Name", type: "text", section: "personal", required: true, removable: false, isCore: true, sortOrder: 1 },
{ key: "email", label: "Email", type: "email", section: "contact", sortOrder: 2 },
{ key: "cellPhone", label: "Cell Phone", type: "phone", section: "contact", sortOrder: 3 },
{ key: "homePhone", label: "Home Phone", type: "phone", section: "contact", sortOrder: 4 },
{ key: "gender", label: "Gender", type: "select", section: "personal", options: ["Male", "Female"], sortOrder: 5 },
{ key: "dateOfBirth", label: "Date of Birth", type: "date", section: "personal", sortOrder: 6 },
{ key: "maritalStatus", label: "Marital Status", type: "select", section: "personal", options: ["Married", "Single", "Other"], sortOrder: 7 },
{ key: "visitType", label: "Visit Type", type: "select", section: "survey", options: ["First/Second Time Guest", "Update My Information"], sortOrder: 8 },
{ key: "address", label: "Address", type: "text", section: "address", sortOrder: 9 },
{ key: "aptNumber", label: "Apt #", type: "text", section: "address", sortOrder: 10 },
{ key: "city", label: "City", type: "text", section: "address", sortOrder: 11 },
{ key: "state", label: "State", type: "text", section: "address", sortOrder: 12 },
{ key: "zip", label: "Zip", type: "text", section: "address", sortOrder: 13 },
{ key: "prayerRequests", label: "Prayer Requests", type: "textarea", section: "survey", sortOrder: 14 },
{ key: "prayerForTeam", label: "For Prayer Team", type: "checkbox", section: "survey", sortOrder: 15 },
{ key: "prayerConfidential", label: "Confidential", type: "checkbox", section: "survey", sortOrder: 16 },
{ key: "messageTopics", label: "Message Topics", type: "multiselect", section: "survey", options: ["Stress/Anxiety", "Marriage", "Hearing God's Voice", "Dealing With Doubt", "Parenting", "Grief & Loss", "Forgiveness", "Finances", "Purpose/Calling", "Prayer", "Healthy Boundaries", "Understanding The Bible", "Emotional Health", "Sharing My Faith", "Decision Making", "Spiritual Disciplines", "Spiritual Gifts"], sortOrder: 17 },
{ key: "nextStep", label: "Next Steps", type: "multiselect", section: "survey", options: ["Baptism", "Next Steps"], sortOrder: 18 },
{ key: "attendanceDuration", label: "Attendance Duration", type: "radio", section: "survey", options: ["Less than 6 months", "6 Months - 1 Year", "1-3 Years", "4-6 Years", "7+ Years"], sortOrder: 19 },
{ key: "campusPreference", label: "Campus Preference", type: "multiselect", section: "survey", options: ["Beulah", "Pace/Milton", "Gulf Breeze", "Warrington"], sortOrder: 20 },
{ key: "howHeard", label: "How Did You Hear About Us?", type: "multiselect", section: "survey", options: ["This is my church home", "Regular Attender", "Drove by", "Social Media", "Google", "Personal Invite"], sortOrder: 21 },
{ key: "serviceAttended", label: "Service Attended", type: "select", section: "survey", options: ["A", "B", "C", "D"], sortOrder: 22 },
{ key: "followUp", label: "Follow-Up", type: "text", section: "followup", sortOrder: 23 },
{ key: "notes", label: "Notes", type: "textarea", section: "followup", sortOrder: 24 },
{ key: "email", label: "Email", type: "email", section: "contact", isCore: true, sortOrder: 2 },
{ key: "cellPhone", label: "Cell Phone", type: "phone", section: "contact", isCore: true, sortOrder: 3 },
{ key: "homePhone", label: "Home Phone", type: "phone", section: "contact", isCore: true, sortOrder: 4 },
{ key: "gender", label: "Gender", type: "select", section: "personal", options: ["Male", "Female"], isCore: true, sortOrder: 5 },
{ key: "dateOfBirth", label: "Date of Birth", type: "date", section: "personal", isCore: true, sortOrder: 6 },
{ key: "maritalStatus", label: "Marital Status", type: "select", section: "personal", options: ["Married", "Single", "Other"], isCore: true, sortOrder: 7 },
{ key: "visitType", label: "Visit Type", type: "select", section: "survey", options: ["First/Second Time Guest", "Update My Information"], isCore: true, sortOrder: 8 },
{ key: "address", label: "Address", type: "text", section: "address", isCore: true, sortOrder: 9 },
{ key: "aptNumber", label: "Apt #", type: "text", section: "address", isCore: true, sortOrder: 10 },
{ key: "city", label: "City", type: "text", section: "address", isCore: true, sortOrder: 11 },
{ key: "state", label: "State", type: "text", section: "address", isCore: true, sortOrder: 12 },
{ key: "zip", label: "Zip", type: "text", section: "address", isCore: true, sortOrder: 13 },
{ key: "prayerRequests", label: "Prayer Requests", type: "textarea", section: "survey", isCore: true, sortOrder: 14 },
{ key: "prayerForTeam", label: "For Prayer Team", type: "checkbox", section: "survey", isCore: true, sortOrder: 15 },
{ key: "prayerConfidential", label: "Confidential", type: "checkbox", section: "survey", isCore: true, sortOrder: 16 },
{ key: "messageTopics", label: "Message Topics", type: "multiselect", section: "survey", options: ["Stress/Anxiety", "Marriage", "Hearing God's Voice", "Dealing With Doubt", "Parenting", "Grief & Loss", "Forgiveness", "Finances", "Purpose/Calling", "Prayer", "Healthy Boundaries", "Understanding The Bible", "Emotional Health", "Sharing My Faith", "Decision Making", "Spiritual Disciplines", "Spiritual Gifts"], isCore: true, sortOrder: 17 },
{ key: "nextStep", label: "Next Steps", type: "multiselect", section: "survey", options: ["Baptism", "Next Steps"], isCore: true, sortOrder: 18 },
{ key: "attendanceDuration", label: "Attendance Duration", type: "radio", section: "survey", options: ["Less than 6 months", "6 Months - 1 Year", "1-3 Years", "4-6 Years", "7+ Years"], isCore: true, sortOrder: 19 },
{ key: "campusPreference", label: "Campus Preference", type: "multiselect", section: "survey", options: ["Beulah", "Pace/Milton", "Gulf Breeze", "Warrington"], isCore: true, sortOrder: 20 },
{ key: "howHeard", label: "How Did You Hear About Us?", type: "multiselect", section: "survey", options: ["This is my church home", "Regular Attender", "Drove by", "Social Media", "Google", "Personal Invite"], isCore: true, sortOrder: 21 },
{ key: "serviceAttended", label: "Service Attended", type: "select", section: "survey", options: ["A", "B", "C", "D"], isCore: true, sortOrder: 22 },
{ key: "followUp", label: "Follow-Up", type: "text", section: "followup", isCore: true, sortOrder: 23 },
{ key: "notes", label: "Notes", type: "textarea", section: "followup", isCore: true, sortOrder: 24 },
];
export async function seedDefaultTemplate(organizationId: string) {