echos-ocr/src/lib/form-templates.ts
Randall Stillwell 7aedae02fc 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>
2026-05-21 08:30:27 -05:00

70 lines
4.9 KiB
TypeScript

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", 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) {
const existing = await prisma.formTemplate.findFirst({
where: { organizationId, isDefault: true },
});
if (existing) return existing;
const template = await prisma.formTemplate.create({
data: {
organizationId,
name: "Connect Card",
slug: "connect-card",
description: "Default connect card template",
isDefault: true,
isActive: true,
fields: {
create: DEFAULT_FIELDS.map((f) => ({
key: f.key,
label: f.label,
type: f.type,
section: f.section,
required: f.required ?? false,
removable: f.removable ?? true,
isCore: f.isCore ?? false,
sortOrder: f.sortOrder,
options: f.options ?? Prisma.JsonNull,
})),
},
},
include: { fields: true },
});
return template;
}
export { DEFAULT_FIELDS };