fix: earliest patient record wins when duplicates share a phone (inbound SMS/voice)

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
2026-08-06 23:11:55 -04:00
co-authored by Claude Fable 5
parent 0cd095330a
commit ff79cd1643
+5 -1
View File
@@ -193,8 +193,11 @@ router.post("/webhook/sms", async (req: Request, res: Response): Promise<any> =>
const { From, To, Body, MessageSid } = req.body;
const normalizedFrom = (From || "").replace(/\D/g, "");
// Ordered by id so that when duplicate patients share a phone number
// (one record per insurance), the earliest-created record is the default.
const allPatients = await db.patient.findMany({
select: { id: true, phone: true, userId: true, preferredLanguage: true },
orderBy: { id: "asc" },
});
const patient = allPatients.find(
(p) => p.phone && p.phone.replace(/\D/g, "") === normalizedFrom
@@ -635,7 +638,8 @@ router.post("/webhook/voice", async (req: Request, res: Response): Promise<any>
const { From, CallSid } = req.body;
const normalizedFrom = (From || "").replace(/\D/g, "");
const allPatients = await db.patient.findMany({ select: { id: true, phone: true, userId: true } });
// Ordered by id — earliest-created record wins when duplicates share a phone.
const allPatients = await db.patient.findMany({ select: { id: true, phone: true, userId: true }, orderBy: { id: "asc" } });
const patient = allPatients.find(
(p: { id: number; phone: string | null; userId: number }) => p.phone && p.phone.replace(/\D/g, "") === normalizedFrom
);