feat(GRO-106): inbound Telnyx webhook + persistence #378

Merged
groombook-engineer[bot] merged 11 commits from feat/GRO-106-inbound-webhook into dev 2026-05-11 00:43:40 +00:00
2 changed files with 45 additions and 6 deletions
Showing only changes of commit 932d9bb72a - Show all commits
@@ -13,6 +13,7 @@ vi.mock("@groombook/db", () => ({
conversations: { id: "", businessId: "", clientId: "", externalNumber: "", businessNumber: "", channel: "", lastMessageAt: null, status: "", createdAt: null, updatedAt: null },
messages: { id: "", conversationId: "", direction: "", body: "", status: "", providerMessageId: "", sentByStaffId: null, createdAt: null, deliveredAt: null, readByClientAt: null },
businessSettings: { id: "", messagingPhoneNumber: "" },
clients: { id: "", name: "", email: "", phone: "", status: "" },
eq: vi.fn(),
and: vi.fn(),
sql: vi.fn(),
@@ -127,6 +128,33 @@ describe("findOrCreateConversation", () => {
const result = await findOrCreateConversation("biz-1", "+1555111", "+1555222");
expect(result.id).toBe("conv-2");
});
it("creates placeholder client for unknown phone then creates conversation", async () => {
mockDb.select
.mockReturnValueOnce({
from: vi.fn().mockReturnValue({
where: vi.fn().mockReturnValue({
limit: vi.fn().mockReturnValue([]),
}),
}),
})
.mockReturnValueOnce({
from: vi.fn().mockReturnValue({
where: vi.fn().mockReturnValue({
limit: vi.fn().mockReturnValue([]),
}),
}),
});
mockDb.insert.mockReturnValue({
values: vi.fn().mockReturnValue({
returning: vi.fn().mockReturnValue([{ id: "conv-3", clientId: "client-3" }]),
}),
});
const result = await findOrCreateConversation("biz-1", "+1555111", "+1555222");
expect(result.id).toBe("conv-3");
expect(result.clientId).toBe("client-3");
});
});
describe("upsertMessage", () => {
+17 -6
View File
@@ -1,4 +1,5 @@
import { getDb, conversations, messages, businessSettings, eq, and, sql } from "@groombook/db";
import { getDb, conversations, messages, businessSettings, clients, eq, and } from "@groombook/db";
import { v4 as uuidv4 } from "uuid";
export interface TelnyxMessageReceivedPayload {
data: {
@@ -41,13 +42,23 @@ export async function findOrCreateConversation(
return { id: existing.id, clientId: existing.clientId };
}
const [business] = await db
.select({ primaryClientId: sql<string>`${businessSettings.id}` })
.from(businessSettings)
.where(eq(businessSettings.id, businessId))
const [existingClient] = await db
.select({ id: clients.id })
.from(clients)
.where(eq(clients.phone, clientPhone))
.limit(1);
const clientId = business?.primaryClientId ?? crypto.randomUUID();
const clientId = existingClient?.id ?? uuidv4();
if (!existingClient) {
await db.insert(clients).values({
id: clientId,
name: clientPhone,
email: `sms-${uuidv4()}@placeholder.local`,
phone: clientPhone,
status: "active",
});
}
const [created] = await db
.insert(conversations)