Files
clientsflow/frontend/server/api/omni/telegram/business/connect/start.post.ts

52 lines
1.4 KiB
TypeScript

import { getAuthContext } from "../../../../../utils/auth";
import { prisma } from "../../../../../utils/prisma";
import { buildTelegramStartUrl, issueLinkToken } from "../../../../../utils/telegramBusinessConnect";
export default defineEventHandler(async (event) => {
const auth = await getAuthContext(event);
const { token, payload } = issueLinkToken({ teamId: auth.teamId, userId: auth.userId });
const pendingId = `pending:${payload.nonce}`;
await prisma.telegramBusinessConnection.upsert({
where: {
teamId_businessConnectionId: {
teamId: auth.teamId,
businessConnectionId: pendingId,
},
},
create: {
teamId: auth.teamId,
businessConnectionId: pendingId,
rawJson: {
state: "pending_link",
link: {
nonce: payload.nonce,
exp: payload.exp,
createdAt: new Date().toISOString(),
createdByUserId: auth.userId,
},
},
},
update: {
isEnabled: null,
canReply: null,
rawJson: {
state: "pending_link",
link: {
nonce: payload.nonce,
exp: payload.exp,
createdAt: new Date().toISOString(),
createdByUserId: auth.userId,
},
},
},
});
return {
ok: true,
status: "pending_link",
connectUrl: buildTelegramStartUrl(token),
expiresAt: new Date(payload.exp * 1000).toISOString(),
};
});