- Add 3-message intro (self-intro → empathetic ack → new/existing question) via single TwiML response to guarantee delivery order - Detect reschedule intent from first message; look up existing appointment date - New patient flow: ask insurance type → MassHealth consent → member ID + DOB → Selenium eligibility check - Post-eligibility: active → ask appointment date/time with office-hours validation; inactive → ask other insurance or collect contact info - Date/time collection mirrors reschedule flow: check office day open, ask time, validate against office hours - Auto-create appointment in schedule for known patients on confirmation; use first available staff member - Add openPhoneReply toggle (Settings → AI Chat) to respond to any number at any time - Add 5-minute inactivity timeout: reset conversation to initial stage and clear pending state - Normalize MassHealth DOB to zero-padded MM/DD/YYYY before Selenium submission - Expand isExistingPatient classifier to recognize "old patient", "old", "previous", "prior" - Existing patient confirmation message now acknowledges patient type before asking about insurance Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
101 lines
3.7 KiB
TypeScript
101 lines
3.7 KiB
TypeScript
import express, { Request, Response } from "express";
|
|
import { storage } from "../storage";
|
|
|
|
const router = express.Router();
|
|
|
|
// GET /api/ai/settings
|
|
router.get("/settings", async (req: Request, res: Response): Promise<any> => {
|
|
try {
|
|
const userId = req.user?.id;
|
|
if (!userId) return res.status(401).json({ message: "Unauthorized" });
|
|
|
|
const settings = await storage.getAiSettings(userId);
|
|
if (!settings) return res.status(200).json(null);
|
|
|
|
return res.status(200).json({
|
|
id: settings.id,
|
|
apiKey: settings.apiKey,
|
|
openPhoneReply: settings.openPhoneReply ?? false,
|
|
});
|
|
} catch (err) {
|
|
return res.status(500).json({ error: "Failed to fetch AI settings", details: String(err) });
|
|
}
|
|
});
|
|
|
|
// PUT /api/ai/settings
|
|
router.put("/settings", async (req: Request, res: Response): Promise<any> => {
|
|
try {
|
|
const userId = req.user?.id;
|
|
if (!userId) return res.status(401).json({ message: "Unauthorized" });
|
|
|
|
const { apiKey } = req.body;
|
|
if (!apiKey?.trim()) {
|
|
return res.status(400).json({ message: "apiKey is required" });
|
|
}
|
|
|
|
const settings = await storage.upsertAiSettings(userId, apiKey.trim());
|
|
return res.status(200).json({ id: settings.id, apiKey: settings.apiKey });
|
|
} catch (err) {
|
|
return res.status(500).json({ error: "Failed to save AI settings", details: String(err) });
|
|
}
|
|
});
|
|
|
|
// GET /api/ai/advanced-settings
|
|
router.get("/advanced-settings", async (req: Request, res: Response): Promise<any> => {
|
|
try {
|
|
const userId = req.user?.id;
|
|
if (!userId) return res.status(401).json({ message: "Unauthorized" });
|
|
|
|
const openPhoneReply = await storage.getOpenPhoneReply(userId);
|
|
return res.status(200).json({ openPhoneReply });
|
|
} catch (err) {
|
|
return res.status(500).json({ error: "Failed to fetch advanced settings", details: String(err) });
|
|
}
|
|
});
|
|
|
|
// PUT /api/ai/advanced-settings
|
|
router.put("/advanced-settings", async (req: Request, res: Response): Promise<any> => {
|
|
try {
|
|
const userId = req.user?.id;
|
|
if (!userId) return res.status(401).json({ message: "Unauthorized" });
|
|
|
|
const { openPhoneReply } = req.body;
|
|
if (typeof openPhoneReply !== "boolean") {
|
|
return res.status(400).json({ message: "openPhoneReply must be a boolean" });
|
|
}
|
|
|
|
await storage.setOpenPhoneReply(userId, openPhoneReply);
|
|
return res.status(200).json({ openPhoneReply });
|
|
} catch (err) {
|
|
return res.status(500).json({ error: "Failed to save advanced settings", details: String(err) });
|
|
}
|
|
});
|
|
|
|
// GET /api/ai/chat-templates
|
|
router.get("/chat-templates", async (req: Request, res: Response): Promise<any> => {
|
|
try {
|
|
const userId = req.user?.id;
|
|
if (!userId) return res.status(401).json({ message: "Unauthorized" });
|
|
const templates = await storage.getAiChatTemplates(userId);
|
|
return res.status(200).json(templates);
|
|
} catch (err) {
|
|
return res.status(500).json({ error: "Failed to fetch AI chat templates", details: String(err) });
|
|
}
|
|
});
|
|
|
|
// PUT /api/ai/chat-templates
|
|
router.put("/chat-templates", async (req: Request, res: Response): Promise<any> => {
|
|
try {
|
|
const userId = req.user?.id;
|
|
if (!userId) return res.status(401).json({ message: "Unauthorized" });
|
|
const { reminderGreeting, newPatientGreeting, generalFallback, rescheduleGreeting, reminderSms } = req.body;
|
|
await storage.saveAiChatTemplates(userId, { reminderGreeting, newPatientGreeting, generalFallback, rescheduleGreeting, reminderSms });
|
|
const updated = await storage.getAiChatTemplates(userId);
|
|
return res.status(200).json(updated);
|
|
} catch (err) {
|
|
return res.status(500).json({ error: "Failed to save AI chat templates", details: String(err) });
|
|
}
|
|
});
|
|
|
|
export default router;
|