feat: office address, multi-template SMS manager, hardcoded defaults with auto-seed

- Add streetAddress/city/state/zipCode fields to OfficeContact (schema + storage + UI)
- Support {officeAddress} variable in batch reminder SMS
- Replace single SMS template field with full CRUD template list (add/rename/edit/delete)
- Store SMS template list under _sms_template_list; first template synced to batch reminder
- Hardcode all AI chat template defaults into codebase (reminder SMS, greetings, fallback)
- Add seed-templates.ts that auto-seeds default templates for all users on server boot
- Update README: note that templates are auto-configured on first boot

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
Gitead
2026-05-11 23:18:04 -04:00
parent 11244ace7f
commit 7929dc6e19
56 changed files with 763 additions and 46 deletions

View File

@@ -12,6 +12,10 @@ export const officeContactStorage = {
phoneNumber?: string;
email?: string;
fax?: string;
streetAddress?: string;
city?: string;
state?: string;
zipCode?: string;
}) {
return db.officeContact.upsert({
where: { userId },

View File

@@ -0,0 +1,65 @@
import { prisma as db } from "@repo/db/client";
// ── Default template values ───────────────────────────────────────────────────
// Keep these in sync with the frontend constants in ai-chat-settings-card.tsx
export const DEFAULT_REMINDER_SMS =
"Hi {firstName}, this is a reminder from {officeName}. You have an appointment on {appointmentDate} at {appointmentTime}. Please come to our office at {officeAddress}. Please reply YES to confirm or NO to reschedule. Thank you!";
export const DEFAULT_AI_CHAT_TEMPLATES = {
_ai_chat_reminder_sms: DEFAULT_REMINDER_SMS,
_ai_chat_reminder_greeting: "Hi! My name is Lisa, the dedicated AI assistant at {officeName}. I can confirm or reschedule your appointment and answer general questions 24/7. I will reply you message at any time you need.",
_ai_chat_new_patient_greeting: "Hi! My name is Lisa, the dedicated AI assistant at {officeName}. I can help you schedule an appointment, check your insurance, and answer general questions 24/7. How can I help you today?",
_ai_chat_general_fallback: "Hi! My name is Lisa, the dedicated AI assistant at {officeName}. How can I help you today?",
_ai_chat_reschedule_greeting: "Hi! My name is Lisa, the dedicated AI assistant at {officeName}. I can help you find a new appointment time that works for you. Would you like to reschedule your appointment?",
};
export const DEFAULT_SMS_TEMPLATE_LIST = JSON.stringify([
{
id: "default-appt-reminder",
name: "Appointment Reminder SMS",
body: DEFAULT_REMINDER_SMS,
},
{
id: "default-follow-up",
name: "Follow up reminder",
body: "Hi {firstName}, this is a follow-up from {officeName}. We wanted to check in with you after your recent appointment. Please don't hesitate to call us if you have any questions.",
},
]);
// ── Auto-seed for a single user ───────────────────────────────────────────────
// Writes defaults only for keys that are not yet set, so existing data is never
// overwritten. Safe to call on every boot.
export async function seedTemplatesForUser(userId: number): Promise<void> {
const settings = await db.twilioSettings.findUnique({ where: { userId } });
const existing = (settings?.templates as Record<string, string>) || {};
const patch: Record<string, string> = {};
for (const [key, value] of Object.entries(DEFAULT_AI_CHAT_TEMPLATES)) {
if (!existing[key]) patch[key] = value;
}
if (!existing["_sms_template_list"]) {
patch["_sms_template_list"] = DEFAULT_SMS_TEMPLATE_LIST;
}
if (Object.keys(patch).length === 0) return; // nothing to seed
const updated = { ...existing, ...patch };
await db.twilioSettings.upsert({
where: { userId },
update: { templates: updated },
create: { userId, accountSid: "", authToken: "", phoneNumber: "", templates: updated },
});
}
// ── Seed all existing users ───────────────────────────────────────────────────
export async function seedAllUsersTemplates(): Promise<void> {
const users = await db.user.findMany({ select: { id: true } });
await Promise.all(users.map((u: { id: number }) => seedTemplatesForUser(u.id)));
if (users.length > 0) {
console.log(`✅ Seeded AI chat templates for ${users.length} user(s)`);
}
}

View File

@@ -87,6 +87,30 @@ export const twilioStorage = {
});
},
async getSmsTemplateList(userId: number): Promise<{ id: string; name: string; body: string }[]> {
const settings = await db.twilioSettings.findUnique({ where: { userId } });
const all = (settings?.templates as Record<string, string>) || {};
const raw = all["_sms_template_list"];
if (!raw) return [];
try { return JSON.parse(raw); } catch { return []; }
},
async saveSmsTemplateList(userId: number, templates: { id: string; name: string; body: string }[]) {
const settings = await db.twilioSettings.findUnique({ where: { userId } });
const existing = (settings?.templates as Record<string, string>) || {};
const updated: Record<string, string> = {
...existing,
"_sms_template_list": JSON.stringify(templates),
};
// Keep _ai_chat_reminder_sms in sync with the first template for batch sends
if (templates.length > 0) updated["_ai_chat_reminder_sms"] = templates[0]!.body;
return db.twilioSettings.upsert({
where: { userId },
update: { templates: updated },
create: { userId, accountSid: "", authToken: "", phoneNumber: "", templates: updated },
});
},
async getRecentCommunicationsByUser(userId: number, limit = 20) {
return db.communication.findMany({
where: { patient: { userId } },