DB-backed workspace + LangGraph agent
This commit is contained in:
8
Frontend/server/api/auth/demo.post.ts
Normal file
8
Frontend/server/api/auth/demo.post.ts
Normal file
@@ -0,0 +1,8 @@
|
||||
import { ensureDemoAuth, setSession } from "../../utils/auth";
|
||||
|
||||
export default defineEventHandler(async (event) => {
|
||||
const demo = await ensureDemoAuth();
|
||||
setSession(event, demo);
|
||||
return { ok: true };
|
||||
});
|
||||
|
||||
35
Frontend/server/api/auth/login.post.ts
Normal file
35
Frontend/server/api/auth/login.post.ts
Normal file
@@ -0,0 +1,35 @@
|
||||
import { readBody } from "h3";
|
||||
import { prisma } from "../../utils/prisma";
|
||||
import { setSession } from "../../utils/auth";
|
||||
|
||||
export default defineEventHandler(async (event) => {
|
||||
const body = await readBody<{ email?: string; name?: string; teamName?: string }>(event);
|
||||
const email = (body?.email ?? "").trim().toLowerCase();
|
||||
const name = (body?.name ?? "").trim();
|
||||
const teamName = (body?.teamName ?? "").trim() || "My Team";
|
||||
|
||||
if (!email || !email.includes("@")) {
|
||||
throw createError({ statusCode: 400, statusMessage: "valid email is required" });
|
||||
}
|
||||
if (!name) {
|
||||
throw createError({ statusCode: 400, statusMessage: "name is required" });
|
||||
}
|
||||
|
||||
const user = await prisma.user.upsert({
|
||||
where: { email },
|
||||
update: { name },
|
||||
create: { email, name },
|
||||
});
|
||||
|
||||
// For MVP: 1 user -> 1 team (created if missing)
|
||||
const team = await prisma.team.create({ data: { name: teamName } });
|
||||
await prisma.teamMember.create({ data: { teamId: team.id, userId: user.id, role: "OWNER" } });
|
||||
|
||||
const conversation = await prisma.chatConversation.create({
|
||||
data: { teamId: team.id, createdByUserId: user.id, title: "Pilot" },
|
||||
});
|
||||
|
||||
setSession(event, { teamId: team.id, userId: user.id, conversationId: conversation.id });
|
||||
return { ok: true };
|
||||
});
|
||||
|
||||
6
Frontend/server/api/auth/logout.post.ts
Normal file
6
Frontend/server/api/auth/logout.post.ts
Normal file
@@ -0,0 +1,6 @@
|
||||
import { clearAuthSession } from "../../utils/auth";
|
||||
|
||||
export default defineEventHandler(async (event) => {
|
||||
clearAuthSession(event);
|
||||
return { ok: true };
|
||||
});
|
||||
17
Frontend/server/api/auth/me.get.ts
Normal file
17
Frontend/server/api/auth/me.get.ts
Normal file
@@ -0,0 +1,17 @@
|
||||
import { getAuthContext } from "../../utils/auth";
|
||||
import { prisma } from "../../utils/prisma";
|
||||
|
||||
export default defineEventHandler(async (event) => {
|
||||
try {
|
||||
const auth = await getAuthContext(event);
|
||||
const [user, team, conv] = await Promise.all([
|
||||
prisma.user.findUnique({ where: { id: auth.userId } }),
|
||||
prisma.team.findUnique({ where: { id: auth.teamId } }),
|
||||
prisma.chatConversation.findUnique({ where: { id: auth.conversationId } }),
|
||||
]);
|
||||
if (!user || !team || !conv) throw new Error("unauth");
|
||||
return { user: { id: user.id, email: user.email, name: user.name }, team: { id: team.id, name: team.name }, conversation: { id: conv.id, title: conv.title } };
|
||||
} catch {
|
||||
throw createError({ statusCode: 401, statusMessage: "Unauthorized" });
|
||||
}
|
||||
});
|
||||
29
Frontend/server/api/calendar.get.ts
Normal file
29
Frontend/server/api/calendar.get.ts
Normal file
@@ -0,0 +1,29 @@
|
||||
import { prisma } from "../utils/prisma";
|
||||
import { getAuthContext } from "../utils/auth";
|
||||
|
||||
export default defineEventHandler(async (event) => {
|
||||
const auth = await getAuthContext(event);
|
||||
const query = getQuery(event) as any;
|
||||
|
||||
const from = query.from ? new Date(String(query.from)) : new Date(Date.now() - 1000 * 60 * 60 * 24 * 30);
|
||||
const to = query.to ? new Date(String(query.to)) : new Date(Date.now() + 1000 * 60 * 60 * 24 * 60);
|
||||
|
||||
const items = await prisma.calendarEvent.findMany({
|
||||
where: { teamId: auth.teamId, startsAt: { gte: from, lte: to } },
|
||||
include: { contact: { select: { name: true } } },
|
||||
orderBy: { startsAt: "asc" },
|
||||
take: 500,
|
||||
});
|
||||
|
||||
return {
|
||||
items: items.map((e) => ({
|
||||
id: e.id,
|
||||
title: e.title,
|
||||
start: e.startsAt.toISOString(),
|
||||
end: (e.endsAt ?? e.startsAt).toISOString(),
|
||||
contact: e.contact?.name ?? "",
|
||||
note: e.note ?? "",
|
||||
})),
|
||||
};
|
||||
});
|
||||
|
||||
51
Frontend/server/api/calendar.post.ts
Normal file
51
Frontend/server/api/calendar.post.ts
Normal file
@@ -0,0 +1,51 @@
|
||||
import { readBody } from "h3";
|
||||
import { prisma } from "../utils/prisma";
|
||||
import { getAuthContext } from "../utils/auth";
|
||||
|
||||
export default defineEventHandler(async (event) => {
|
||||
const auth = await getAuthContext(event);
|
||||
const body = await readBody<{
|
||||
title?: string;
|
||||
start?: string;
|
||||
end?: string;
|
||||
contact?: string;
|
||||
note?: string;
|
||||
status?: string;
|
||||
}>(event);
|
||||
|
||||
const title = (body?.title ?? "").trim();
|
||||
const start = body?.start ? new Date(body.start) : null;
|
||||
const end = body?.end ? new Date(body.end) : null;
|
||||
if (!title) throw createError({ statusCode: 400, statusMessage: "title is required" });
|
||||
if (!start || Number.isNaN(start.getTime())) throw createError({ statusCode: 400, statusMessage: "start is invalid" });
|
||||
|
||||
const contactName = (body?.contact ?? "").trim();
|
||||
const contact = contactName
|
||||
? await prisma.contact.findFirst({ where: { teamId: auth.teamId, name: contactName }, select: { id: true, name: true } })
|
||||
: null;
|
||||
|
||||
const created = await prisma.calendarEvent.create({
|
||||
data: {
|
||||
teamId: auth.teamId,
|
||||
contactId: contact?.id ?? null,
|
||||
title,
|
||||
startsAt: start,
|
||||
endsAt: end && !Number.isNaN(end.getTime()) ? end : null,
|
||||
note: (body?.note ?? "").trim() || null,
|
||||
status: (body?.status ?? "").trim() || null,
|
||||
},
|
||||
include: { contact: { select: { name: true } } },
|
||||
});
|
||||
|
||||
return {
|
||||
item: {
|
||||
id: created.id,
|
||||
title: created.title,
|
||||
start: created.startsAt.toISOString(),
|
||||
end: (created.endsAt ?? created.startsAt).toISOString(),
|
||||
contact: created.contact?.name ?? "",
|
||||
note: created.note ?? "",
|
||||
},
|
||||
};
|
||||
});
|
||||
|
||||
40
Frontend/server/api/communications.get.ts
Normal file
40
Frontend/server/api/communications.get.ts
Normal file
@@ -0,0 +1,40 @@
|
||||
import { prisma } from "../utils/prisma";
|
||||
import { getAuthContext } from "../utils/auth";
|
||||
|
||||
export default defineEventHandler(async (event) => {
|
||||
const auth = await getAuthContext(event);
|
||||
|
||||
const items = await prisma.contactMessage.findMany({
|
||||
where: { contact: { teamId: auth.teamId } },
|
||||
orderBy: { occurredAt: "asc" },
|
||||
take: 2000,
|
||||
include: {
|
||||
contact: { select: { id: true, name: true } },
|
||||
},
|
||||
});
|
||||
|
||||
return {
|
||||
items: items.map((m) => ({
|
||||
id: m.id,
|
||||
at: m.occurredAt.toISOString(),
|
||||
contactId: m.contactId,
|
||||
contact: m.contact.name,
|
||||
channel:
|
||||
m.channel === "TELEGRAM"
|
||||
? "Telegram"
|
||||
: m.channel === "WHATSAPP"
|
||||
? "WhatsApp"
|
||||
: m.channel === "INSTAGRAM"
|
||||
? "Instagram"
|
||||
: m.channel === "EMAIL"
|
||||
? "Email"
|
||||
: "Phone",
|
||||
kind: m.kind === "CALL" ? "call" : "message",
|
||||
direction: m.direction === "IN" ? "in" : "out",
|
||||
text: m.content,
|
||||
duration: m.durationSec ? new Date(m.durationSec * 1000).toISOString().slice(14, 19) : undefined,
|
||||
transcript: Array.isArray(m.transcriptJson) ? (m.transcriptJson as any) : undefined,
|
||||
})),
|
||||
};
|
||||
});
|
||||
|
||||
53
Frontend/server/api/communications.post.ts
Normal file
53
Frontend/server/api/communications.post.ts
Normal file
@@ -0,0 +1,53 @@
|
||||
import { readBody } from "h3";
|
||||
import { prisma } from "../utils/prisma";
|
||||
import { getAuthContext } from "../utils/auth";
|
||||
|
||||
function toDbChannel(channel: string) {
|
||||
const c = channel.toLowerCase();
|
||||
if (c === "telegram") return "TELEGRAM";
|
||||
if (c === "whatsapp") return "WHATSAPP";
|
||||
if (c === "instagram") return "INSTAGRAM";
|
||||
if (c === "email") return "EMAIL";
|
||||
return "PHONE";
|
||||
}
|
||||
|
||||
export default defineEventHandler(async (event) => {
|
||||
const auth = await getAuthContext(event);
|
||||
const body = await readBody<{
|
||||
contact?: string;
|
||||
channel?: string;
|
||||
kind?: "message" | "call";
|
||||
direction?: "in" | "out";
|
||||
text?: string;
|
||||
at?: string;
|
||||
durationSec?: number;
|
||||
transcript?: string[];
|
||||
}>(event);
|
||||
|
||||
const contactName = (body?.contact ?? "").trim();
|
||||
if (!contactName) throw createError({ statusCode: 400, statusMessage: "contact is required" });
|
||||
|
||||
const contact = await prisma.contact.findFirst({
|
||||
where: { teamId: auth.teamId, name: contactName },
|
||||
select: { id: true, name: true },
|
||||
});
|
||||
if (!contact) throw createError({ statusCode: 404, statusMessage: "contact not found" });
|
||||
|
||||
const occurredAt = body?.at ? new Date(body.at) : new Date();
|
||||
if (Number.isNaN(occurredAt.getTime())) throw createError({ statusCode: 400, statusMessage: "at is invalid" });
|
||||
|
||||
const created = await prisma.contactMessage.create({
|
||||
data: {
|
||||
contactId: contact.id,
|
||||
kind: body?.kind === "call" ? "CALL" : "MESSAGE",
|
||||
direction: body?.direction === "in" ? "IN" : "OUT",
|
||||
channel: toDbChannel(body?.channel ?? "Phone") as any,
|
||||
content: (body?.text ?? "").trim(),
|
||||
durationSec: typeof body?.durationSec === "number" ? body.durationSec : null,
|
||||
transcriptJson: Array.isArray(body?.transcript) ? body.transcript : undefined,
|
||||
occurredAt,
|
||||
},
|
||||
});
|
||||
|
||||
return { ok: true, id: created.id };
|
||||
});
|
||||
30
Frontend/server/api/contacts.get.ts
Normal file
30
Frontend/server/api/contacts.get.ts
Normal file
@@ -0,0 +1,30 @@
|
||||
import { prisma } from "../utils/prisma";
|
||||
import { getAuthContext } from "../utils/auth";
|
||||
|
||||
export default defineEventHandler(async (event) => {
|
||||
const auth = await getAuthContext(event);
|
||||
const items = await prisma.contact.findMany({
|
||||
where: { teamId: auth.teamId },
|
||||
include: {
|
||||
note: { select: { content: true, updatedAt: true } },
|
||||
messages: { select: { occurredAt: true }, orderBy: { occurredAt: "desc" }, take: 1 },
|
||||
},
|
||||
orderBy: { updatedAt: "desc" },
|
||||
take: 500,
|
||||
});
|
||||
|
||||
return {
|
||||
items: items.map((c) => ({
|
||||
id: c.id,
|
||||
name: c.name,
|
||||
avatar: c.avatarUrl ?? "",
|
||||
company: c.company ?? "",
|
||||
country: c.country ?? "",
|
||||
location: c.location ?? "",
|
||||
channels: [], // derived client-side from comm list for now
|
||||
lastContactAt: c.messages[0]?.occurredAt?.toISOString?.() ?? c.updatedAt.toISOString(),
|
||||
description: c.note?.content ?? "",
|
||||
})),
|
||||
};
|
||||
});
|
||||
|
||||
27
Frontend/server/api/contacts/[id].get.ts
Normal file
27
Frontend/server/api/contacts/[id].get.ts
Normal file
@@ -0,0 +1,27 @@
|
||||
import { prisma } from "../../utils/prisma";
|
||||
import { getAuthContext } from "../../utils/auth";
|
||||
|
||||
export default defineEventHandler(async (event) => {
|
||||
const auth = await getAuthContext(event);
|
||||
const id = getRouterParam(event, "id");
|
||||
if (!id) throw createError({ statusCode: 400, statusMessage: "id is required" });
|
||||
|
||||
const contact = await prisma.contact.findFirst({
|
||||
where: { id, teamId: auth.teamId },
|
||||
include: { note: { select: { content: true } } },
|
||||
});
|
||||
if (!contact) throw createError({ statusCode: 404, statusMessage: "not found" });
|
||||
|
||||
return {
|
||||
id: contact.id,
|
||||
name: contact.name,
|
||||
avatar: contact.avatarUrl ?? "",
|
||||
company: contact.company ?? "",
|
||||
country: contact.country ?? "",
|
||||
location: contact.location ?? "",
|
||||
email: contact.email ?? "",
|
||||
phone: contact.phone ?? "",
|
||||
description: contact.note?.content ?? "",
|
||||
};
|
||||
});
|
||||
|
||||
23
Frontend/server/api/contacts/[id]/note.put.ts
Normal file
23
Frontend/server/api/contacts/[id]/note.put.ts
Normal file
@@ -0,0 +1,23 @@
|
||||
import { readBody } from "h3";
|
||||
import { prisma } from "../../../utils/prisma";
|
||||
import { getAuthContext } from "../../../utils/auth";
|
||||
|
||||
export default defineEventHandler(async (event) => {
|
||||
const auth = await getAuthContext(event);
|
||||
const id = getRouterParam(event, "id");
|
||||
if (!id) throw createError({ statusCode: 400, statusMessage: "id is required" });
|
||||
const body = await readBody<{ content?: string }>(event);
|
||||
const content = (body?.content ?? "").toString();
|
||||
|
||||
const contact = await prisma.contact.findFirst({ where: { id, teamId: auth.teamId } });
|
||||
if (!contact) throw createError({ statusCode: 404, statusMessage: "not found" });
|
||||
|
||||
await prisma.contactNote.upsert({
|
||||
where: { contactId: id },
|
||||
update: { content },
|
||||
create: { contactId: id, content },
|
||||
});
|
||||
|
||||
return { ok: true };
|
||||
});
|
||||
|
||||
27
Frontend/server/api/deals.get.ts
Normal file
27
Frontend/server/api/deals.get.ts
Normal file
@@ -0,0 +1,27 @@
|
||||
import { prisma } from "../utils/prisma";
|
||||
import { getAuthContext } from "../utils/auth";
|
||||
|
||||
export default defineEventHandler(async (event) => {
|
||||
const auth = await getAuthContext(event);
|
||||
|
||||
const items = await prisma.deal.findMany({
|
||||
where: { teamId: auth.teamId },
|
||||
include: { contact: { select: { name: true, company: true } } },
|
||||
orderBy: { updatedAt: "desc" },
|
||||
take: 500,
|
||||
});
|
||||
|
||||
return {
|
||||
items: items.map((d) => ({
|
||||
id: d.id,
|
||||
contact: d.contact.name,
|
||||
title: d.title,
|
||||
company: d.contact.company ?? "",
|
||||
stage: d.stage,
|
||||
amount: d.amount ? String(d.amount) : "",
|
||||
nextStep: d.nextStep ?? "",
|
||||
summary: d.summary ?? "",
|
||||
})),
|
||||
};
|
||||
});
|
||||
|
||||
26
Frontend/server/api/documents.get.ts
Normal file
26
Frontend/server/api/documents.get.ts
Normal file
@@ -0,0 +1,26 @@
|
||||
import { prisma } from "../utils/prisma";
|
||||
import { getAuthContext } from "../utils/auth";
|
||||
|
||||
export default defineEventHandler(async (event) => {
|
||||
const auth = await getAuthContext(event);
|
||||
|
||||
const items = await prisma.workspaceDocument.findMany({
|
||||
where: { teamId: auth.teamId },
|
||||
orderBy: { updatedAt: "desc" },
|
||||
take: 200,
|
||||
});
|
||||
|
||||
return {
|
||||
items: items.map((d) => ({
|
||||
id: d.id,
|
||||
title: d.title,
|
||||
type: d.type,
|
||||
owner: d.owner,
|
||||
scope: d.scope,
|
||||
updatedAt: d.updatedAt.toISOString(),
|
||||
summary: d.summary,
|
||||
body: d.body,
|
||||
})),
|
||||
};
|
||||
});
|
||||
|
||||
27
Frontend/server/api/feed.get.ts
Normal file
27
Frontend/server/api/feed.get.ts
Normal file
@@ -0,0 +1,27 @@
|
||||
import { prisma } from "../utils/prisma";
|
||||
import { getAuthContext } from "../utils/auth";
|
||||
|
||||
export default defineEventHandler(async (event) => {
|
||||
const auth = await getAuthContext(event);
|
||||
|
||||
const items = await prisma.feedCard.findMany({
|
||||
where: { teamId: auth.teamId },
|
||||
include: { contact: { select: { name: true } } },
|
||||
orderBy: { happenedAt: "desc" },
|
||||
take: 200,
|
||||
});
|
||||
|
||||
return {
|
||||
items: items.map((c) => ({
|
||||
id: c.id,
|
||||
at: c.happenedAt.toISOString(),
|
||||
contact: c.contact?.name ?? "",
|
||||
text: c.text,
|
||||
proposal: c.proposalJson as any,
|
||||
decision:
|
||||
c.decision === "ACCEPTED" ? "accepted" : c.decision === "REJECTED" ? "rejected" : ("pending" as const),
|
||||
decisionNote: c.decisionNote ?? undefined,
|
||||
})),
|
||||
};
|
||||
});
|
||||
|
||||
23
Frontend/server/api/feed/[id].put.ts
Normal file
23
Frontend/server/api/feed/[id].put.ts
Normal file
@@ -0,0 +1,23 @@
|
||||
import { readBody } from "h3";
|
||||
import { prisma } from "../../utils/prisma";
|
||||
import { getAuthContext } from "../../utils/auth";
|
||||
|
||||
export default defineEventHandler(async (event) => {
|
||||
const auth = await getAuthContext(event);
|
||||
const id = String(getRouterParam(event, "id") ?? "");
|
||||
if (!id) throw createError({ statusCode: 400, statusMessage: "id is required" });
|
||||
|
||||
const body = await readBody<{ decision?: "accepted" | "rejected" | "pending"; decisionNote?: string }>(event);
|
||||
const decision = body?.decision;
|
||||
if (!decision) throw createError({ statusCode: 400, statusMessage: "decision is required" });
|
||||
|
||||
const nextDecision = decision === "accepted" ? "ACCEPTED" : decision === "rejected" ? "REJECTED" : "PENDING";
|
||||
|
||||
const res = await prisma.feedCard.updateMany({
|
||||
where: { id, teamId: auth.teamId },
|
||||
data: { decision: nextDecision, decisionNote: body?.decisionNote ?? null },
|
||||
});
|
||||
if (res.count === 0) throw createError({ statusCode: 404, statusMessage: "feed card not found" });
|
||||
|
||||
return { ok: true, id };
|
||||
});
|
||||
22
Frontend/server/api/pins.get.ts
Normal file
22
Frontend/server/api/pins.get.ts
Normal file
@@ -0,0 +1,22 @@
|
||||
import { prisma } from "../utils/prisma";
|
||||
import { getAuthContext } from "../utils/auth";
|
||||
|
||||
export default defineEventHandler(async (event) => {
|
||||
const auth = await getAuthContext(event);
|
||||
|
||||
const items = await prisma.contactPin.findMany({
|
||||
where: { teamId: auth.teamId },
|
||||
include: { contact: { select: { name: true } } },
|
||||
orderBy: { updatedAt: "desc" },
|
||||
take: 500,
|
||||
});
|
||||
|
||||
return {
|
||||
items: items.map((p) => ({
|
||||
id: p.id,
|
||||
contact: p.contact.name,
|
||||
text: p.text,
|
||||
})),
|
||||
};
|
||||
});
|
||||
|
||||
41
Frontend/server/api/telegram/messages.get.ts
Normal file
41
Frontend/server/api/telegram/messages.get.ts
Normal file
@@ -0,0 +1,41 @@
|
||||
import { getQuery } from "h3";
|
||||
import { prisma } from "../../utils/prisma";
|
||||
import { getAuthContext } from "../../utils/auth";
|
||||
|
||||
export default defineEventHandler(async (event) => {
|
||||
const auth = await getAuthContext(event);
|
||||
const q = getQuery(event);
|
||||
const threadId = typeof q.threadId === "string" ? q.threadId : "";
|
||||
if (!threadId) throw createError({ statusCode: 400, statusMessage: "threadId is required" });
|
||||
|
||||
const thread = await prisma.omniThread.findFirst({
|
||||
where: { id: threadId, teamId: auth.teamId, channel: "TELEGRAM" },
|
||||
});
|
||||
if (!thread) throw createError({ statusCode: 404, statusMessage: "thread not found" });
|
||||
|
||||
const items = await prisma.omniMessage.findMany({
|
||||
where: { teamId: auth.teamId, threadId: thread.id, channel: "TELEGRAM" },
|
||||
orderBy: { occurredAt: "asc" },
|
||||
take: 200,
|
||||
});
|
||||
|
||||
return {
|
||||
thread: {
|
||||
id: thread.id,
|
||||
contactId: thread.contactId,
|
||||
externalChatId: thread.externalChatId,
|
||||
businessConnectionId: thread.businessConnectionId,
|
||||
title: thread.title,
|
||||
updatedAt: thread.updatedAt,
|
||||
},
|
||||
items: items.map((m) => ({
|
||||
id: m.id,
|
||||
direction: m.direction,
|
||||
status: m.status,
|
||||
text: m.text,
|
||||
providerMessageId: m.providerMessageId,
|
||||
occurredAt: m.occurredAt,
|
||||
})),
|
||||
};
|
||||
});
|
||||
|
||||
36
Frontend/server/api/telegram/send.post.ts
Normal file
36
Frontend/server/api/telegram/send.post.ts
Normal file
@@ -0,0 +1,36 @@
|
||||
import { readBody } from "h3";
|
||||
import { prisma } from "../../utils/prisma";
|
||||
import { getAuthContext } from "../../utils/auth";
|
||||
import { enqueueTelegramSend } from "../../queues/telegramSend";
|
||||
|
||||
export default defineEventHandler(async (event) => {
|
||||
const auth = await getAuthContext(event);
|
||||
const body = await readBody<{ threadId?: string; text?: string }>(event);
|
||||
|
||||
const threadId = (body?.threadId || "").trim();
|
||||
const text = (body?.text || "").trim();
|
||||
if (!threadId) throw createError({ statusCode: 400, statusMessage: "threadId is required" });
|
||||
if (!text) throw createError({ statusCode: 400, statusMessage: "text is required" });
|
||||
|
||||
const thread = await prisma.omniThread.findFirst({
|
||||
where: { id: threadId, teamId: auth.teamId, channel: "TELEGRAM" },
|
||||
});
|
||||
if (!thread) throw createError({ statusCode: 404, statusMessage: "thread not found" });
|
||||
|
||||
const msg = await prisma.omniMessage.create({
|
||||
data: {
|
||||
teamId: auth.teamId,
|
||||
contactId: thread.contactId,
|
||||
threadId: thread.id,
|
||||
direction: "OUT",
|
||||
channel: "TELEGRAM",
|
||||
status: "PENDING",
|
||||
text,
|
||||
occurredAt: new Date(),
|
||||
},
|
||||
});
|
||||
|
||||
await enqueueTelegramSend({ omniMessageId: msg.id });
|
||||
return { ok: true, messageId: msg.id };
|
||||
});
|
||||
|
||||
37
Frontend/server/api/telegram/threads.get.ts
Normal file
37
Frontend/server/api/telegram/threads.get.ts
Normal file
@@ -0,0 +1,37 @@
|
||||
import { prisma } from "../../utils/prisma";
|
||||
import { getAuthContext } from "../../utils/auth";
|
||||
|
||||
export default defineEventHandler(async (event) => {
|
||||
const auth = await getAuthContext(event);
|
||||
|
||||
const threads = await prisma.omniThread.findMany({
|
||||
where: { teamId: auth.teamId, channel: "TELEGRAM" },
|
||||
orderBy: { updatedAt: "desc" },
|
||||
take: 50,
|
||||
include: {
|
||||
contact: true,
|
||||
messages: { orderBy: { occurredAt: "desc" }, take: 1 },
|
||||
},
|
||||
});
|
||||
|
||||
return {
|
||||
items: threads.map((t) => ({
|
||||
id: t.id,
|
||||
contact: { id: t.contact.id, name: t.contact.name },
|
||||
externalChatId: t.externalChatId,
|
||||
businessConnectionId: t.businessConnectionId,
|
||||
title: t.title,
|
||||
updatedAt: t.updatedAt,
|
||||
lastMessage: t.messages[0]
|
||||
? {
|
||||
id: t.messages[0].id,
|
||||
direction: t.messages[0].direction,
|
||||
status: t.messages[0].status,
|
||||
text: t.messages[0].text,
|
||||
occurredAt: t.messages[0].occurredAt,
|
||||
}
|
||||
: null,
|
||||
})),
|
||||
};
|
||||
});
|
||||
|
||||
163
Frontend/server/api/telegram/webhook.post.ts
Normal file
163
Frontend/server/api/telegram/webhook.post.ts
Normal file
@@ -0,0 +1,163 @@
|
||||
import { readBody, getQuery, getHeader } from "h3";
|
||||
import { prisma } from "../../utils/prisma";
|
||||
|
||||
function teamIdFromWebhook(event: any) {
|
||||
const q = getQuery(event);
|
||||
const fromQuery = typeof q.teamId === "string" ? q.teamId : null;
|
||||
return fromQuery || process.env.TELEGRAM_DEFAULT_TEAM_ID || "demo-team";
|
||||
}
|
||||
|
||||
function assertSecret(event: any) {
|
||||
const expected = process.env.TELEGRAM_WEBHOOK_SECRET;
|
||||
if (!expected) return;
|
||||
|
||||
const got = getHeader(event, "x-telegram-bot-api-secret-token");
|
||||
if (!got || got !== expected) {
|
||||
throw createError({ statusCode: 401, statusMessage: "invalid telegram secret token" });
|
||||
}
|
||||
}
|
||||
|
||||
function displayNameFromTelegram(obj: any) {
|
||||
const first = obj?.first_name || "";
|
||||
const last = obj?.last_name || "";
|
||||
const u = obj?.username ? `@${obj.username}` : "";
|
||||
const full = `${first} ${last}`.trim();
|
||||
return (full || u || "Telegram user").trim();
|
||||
}
|
||||
|
||||
async function upsertBusinessConnection(teamId: string, bc: any) {
|
||||
if (!bc?.id) return;
|
||||
const businessConnectionId = String(bc.id);
|
||||
|
||||
await prisma.telegramBusinessConnection.upsert({
|
||||
where: { teamId_businessConnectionId: { teamId, businessConnectionId } },
|
||||
update: {
|
||||
isEnabled: typeof bc.is_enabled === "boolean" ? bc.is_enabled : undefined,
|
||||
canReply: typeof bc.can_reply === "boolean" ? bc.can_reply : undefined,
|
||||
rawJson: bc,
|
||||
},
|
||||
create: {
|
||||
teamId,
|
||||
businessConnectionId,
|
||||
isEnabled: typeof bc.is_enabled === "boolean" ? bc.is_enabled : null,
|
||||
canReply: typeof bc.can_reply === "boolean" ? bc.can_reply : null,
|
||||
rawJson: bc,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
async function ensureContactForTelegramChat(teamId: string, externalChatId: string, tgUser: any) {
|
||||
const existing = await prisma.omniContactIdentity.findUnique({
|
||||
where: { teamId_channel_externalId: { teamId, channel: "TELEGRAM", externalId: externalChatId } },
|
||||
include: { contact: true },
|
||||
});
|
||||
if (existing) return existing.contact;
|
||||
|
||||
const contact = await prisma.contact.create({
|
||||
data: {
|
||||
teamId,
|
||||
name: displayNameFromTelegram(tgUser),
|
||||
},
|
||||
});
|
||||
|
||||
await prisma.omniContactIdentity.create({
|
||||
data: {
|
||||
teamId,
|
||||
contactId: contact.id,
|
||||
channel: "TELEGRAM",
|
||||
externalId: externalChatId,
|
||||
},
|
||||
});
|
||||
|
||||
return contact;
|
||||
}
|
||||
|
||||
async function ensureThread(input: {
|
||||
teamId: string;
|
||||
contactId: string;
|
||||
externalChatId: string;
|
||||
businessConnectionId?: string | null;
|
||||
title?: string | null;
|
||||
}) {
|
||||
return prisma.omniThread.upsert({
|
||||
where: {
|
||||
teamId_channel_externalChatId_businessConnectionId: {
|
||||
teamId: input.teamId,
|
||||
channel: "TELEGRAM",
|
||||
externalChatId: input.externalChatId,
|
||||
businessConnectionId: input.businessConnectionId ?? null,
|
||||
},
|
||||
},
|
||||
update: {
|
||||
contactId: input.contactId,
|
||||
title: input.title ?? undefined,
|
||||
},
|
||||
create: {
|
||||
teamId: input.teamId,
|
||||
contactId: input.contactId,
|
||||
channel: "TELEGRAM",
|
||||
externalChatId: input.externalChatId,
|
||||
businessConnectionId: input.businessConnectionId ?? null,
|
||||
title: input.title ?? null,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
export default defineEventHandler(async (event) => {
|
||||
assertSecret(event);
|
||||
const teamId = teamIdFromWebhook(event);
|
||||
|
||||
const update = (await readBody<any>(event)) || {};
|
||||
|
||||
// business_connection updates (user connected/disconnected bot)
|
||||
if (update.business_connection) {
|
||||
await upsertBusinessConnection(teamId, update.business_connection);
|
||||
return { ok: true };
|
||||
}
|
||||
|
||||
const msg = update.business_message || update.edited_business_message;
|
||||
if (!msg) return { ok: true };
|
||||
|
||||
const businessConnectionId = msg.business_connection_id ? String(msg.business_connection_id) : null;
|
||||
const chatId = msg.chat?.id != null ? String(msg.chat.id) : null;
|
||||
const providerMessageId = msg.message_id != null ? String(msg.message_id) : null;
|
||||
|
||||
if (!chatId || !providerMessageId) return { ok: true };
|
||||
|
||||
const text = typeof msg.text === "string" ? msg.text : typeof msg.caption === "string" ? msg.caption : "";
|
||||
const occurredAt = msg.date ? new Date(Number(msg.date) * 1000) : new Date();
|
||||
|
||||
const contact = await ensureContactForTelegramChat(teamId, chatId, msg.from || msg.chat);
|
||||
const thread = await ensureThread({
|
||||
teamId,
|
||||
contactId: contact.id,
|
||||
externalChatId: chatId,
|
||||
businessConnectionId,
|
||||
title: msg.chat?.title ? String(msg.chat.title) : null,
|
||||
});
|
||||
|
||||
// Dedupe on (threadId, providerMessageId). If duplicate, ignore.
|
||||
try {
|
||||
await prisma.omniMessage.create({
|
||||
data: {
|
||||
teamId,
|
||||
contactId: contact.id,
|
||||
threadId: thread.id,
|
||||
direction: "IN",
|
||||
channel: "TELEGRAM",
|
||||
status: "DELIVERED",
|
||||
text: text || "",
|
||||
providerMessageId,
|
||||
providerUpdateId: update.update_id != null ? String(update.update_id) : null,
|
||||
rawJson: update,
|
||||
occurredAt,
|
||||
},
|
||||
});
|
||||
} catch (e: any) {
|
||||
// Prisma unique constraint violation => duplicate delivery
|
||||
if (e?.code !== "P2002") throw e;
|
||||
}
|
||||
|
||||
return { ok: true };
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user