feat: implement token-complete telegram connect flow via bot button
This commit is contained in:
@@ -0,0 +1,81 @@
|
||||
import { readBody } from "h3";
|
||||
import { prisma } from "../../../../../utils/prisma";
|
||||
import { verifyLinkToken } from "../../../../../utils/telegramBusinessConnect";
|
||||
|
||||
type CompleteBody = {
|
||||
token?: string;
|
||||
};
|
||||
|
||||
export default defineEventHandler(async (event) => {
|
||||
const body = await readBody<CompleteBody>(event);
|
||||
const token = String(body?.token ?? "").trim();
|
||||
if (!token) {
|
||||
throw createError({ statusCode: 400, statusMessage: "token is required" });
|
||||
}
|
||||
|
||||
const payload = verifyLinkToken(token);
|
||||
if (!payload) {
|
||||
return { ok: false, status: "invalid_or_expired_token" };
|
||||
}
|
||||
|
||||
const pendingId = `pending:${payload.nonce}`;
|
||||
const pending = await prisma.telegramBusinessConnection.findFirst({
|
||||
where: {
|
||||
teamId: payload.teamId,
|
||||
businessConnectionId: pendingId,
|
||||
},
|
||||
});
|
||||
|
||||
if (!pending) {
|
||||
return { ok: false, status: "session_not_found" };
|
||||
}
|
||||
|
||||
const raw = (pending.rawJson ?? {}) as any;
|
||||
const telegramUserId = raw?.link?.telegramUserId != null ? String(raw.link.telegramUserId).trim() : "";
|
||||
if (!telegramUserId) {
|
||||
return { ok: false, status: "awaiting_telegram_start" };
|
||||
}
|
||||
|
||||
const linkedConnectionId = `link:${telegramUserId}`;
|
||||
await prisma.$transaction([
|
||||
prisma.telegramBusinessConnection.upsert({
|
||||
where: {
|
||||
teamId_businessConnectionId: {
|
||||
teamId: payload.teamId,
|
||||
businessConnectionId: linkedConnectionId,
|
||||
},
|
||||
},
|
||||
create: {
|
||||
teamId: payload.teamId,
|
||||
businessConnectionId: linkedConnectionId,
|
||||
isEnabled: true,
|
||||
canReply: true,
|
||||
rawJson: {
|
||||
state: "connected",
|
||||
mode: "token_link",
|
||||
linkedAt: new Date().toISOString(),
|
||||
telegramUserId,
|
||||
tokenNonce: payload.nonce,
|
||||
},
|
||||
},
|
||||
update: {
|
||||
isEnabled: true,
|
||||
canReply: true,
|
||||
rawJson: {
|
||||
state: "connected",
|
||||
mode: "token_link",
|
||||
linkedAt: new Date().toISOString(),
|
||||
telegramUserId,
|
||||
tokenNonce: payload.nonce,
|
||||
},
|
||||
},
|
||||
}),
|
||||
prisma.telegramBusinessConnection.delete({ where: { id: pending.id } }),
|
||||
]);
|
||||
|
||||
return {
|
||||
ok: true,
|
||||
status: "connected",
|
||||
businessConnectionId: linkedConnectionId,
|
||||
};
|
||||
});
|
||||
Reference in New Issue
Block a user