feat: improve backup management, settings UI, and Twilio webhooks

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
Gitead
2026-05-04 00:52:42 -04:00
parent 5689269690
commit 79e20b693d
13 changed files with 468 additions and 545 deletions

View File

@@ -1,6 +1,7 @@
import express, { Request, Response } from "express";
import { storage } from "../storage";
import { prisma as db } from "@repo/db/client";
import { runReminderGraph } from "../ai/reminder-graph";
const router = express.Router();
@@ -10,12 +11,13 @@ router.post("/webhook/sms", async (req: Request, res: Response): Promise<any> =>
const { From, Body, MessageSid } = req.body;
const normalizedFrom = (From || "").replace(/\D/g, "");
const allPatients = await db.patient.findMany({ select: { id: true, phone: true } });
const allPatients = await db.patient.findMany({ select: { id: true, phone: true, userId: true } });
const patient = allPatients.find(
(p: { id: number; phone: string | null }) => p.phone && p.phone.replace(/\D/g, "") === normalizedFrom
(p: { id: number; phone: string | null; userId: number }) => p.phone && p.phone.replace(/\D/g, "") === normalizedFrom
);
if (patient) {
// Save the inbound message
await storage.createCommunication({
patientId: patient.id,
channel: "sms",
@@ -24,6 +26,28 @@ router.post("/webhook/sms", async (req: Request, res: Response): Promise<any> =>
body: Body,
twilioSid: MessageSid,
});
// Run AI graph if API key is configured
const aiSettings = await storage.getAiSettings(patient.userId);
if (aiSettings?.apiKey) {
const { reply, intent } = await runReminderGraph(Body, aiSettings.apiKey);
if (reply) {
// Save the AI outbound reply
await storage.createCommunication({
patientId: patient.id,
channel: "sms",
direction: "outbound",
status: "sent",
body: reply,
});
res.set("Content-Type", "text/xml");
return res.send(
`<?xml version="1.0" encoding="UTF-8"?><Response><Message>${escapeXml(reply)}</Message></Response>`
);
}
}
}
res.set("Content-Type", "text/xml");
@@ -104,4 +128,13 @@ router.post("/webhook/voice-recording", async (req: Request, res: Response): Pro
}
});
function escapeXml(text: string): string {
return text
.replace(/&/g, "&amp;")
.replace(/</g, "&lt;")
.replace(/>/g, "&gt;")
.replace(/"/g, "&quot;")
.replace(/'/g, "&apos;");
}
export default router;