2
0

🐛 Fix delete session with client side actions

This commit is contained in:
Baptiste Arnaud
2023-08-29 12:19:50 +02:00
parent b852b4af0b
commit 013c7a6265
10 changed files with 55 additions and 26 deletions

View File

@ -9,6 +9,7 @@ import {
chatReplySchema,
sendMessageInputSchema,
} from '@typebot.io/schemas/features/chat/schema'
import { TRPCError } from '@trpc/server'
export const sendMessage = publicProcedure
.meta({
@ -30,6 +31,11 @@ export const sendMessage = publicProcedure
const session = sessionId ? await getSession(sessionId) : null
if (!session) {
if (!startParams)
throw new TRPCError({
code: 'BAD_REQUEST',
message: 'Missing startParams',
})
const {
typebot,
messages,
@ -39,7 +45,7 @@ export const sendMessage = publicProcedure
logs,
clientSideActions,
newSessionState,
} = await startSession(startParams, user?.id)
} = await startSession({ startParams, userId: user?.id })
const allLogs = clientLogs ? [...(logs ?? []), ...clientLogs] : logs

View File

@ -22,7 +22,10 @@ export const saveStateToDatabase = async ({
clientSideActions,
}: Props) => {
const containsSetVariableClientSideAction = clientSideActions?.some(
(action) => 'setVariable' in action
(action) =>
'setVariable' in action ||
'webhookToExecute' in action ||
'streamOpenAiChatCompletion' in action
)
const isCompleted = Boolean(!input && !containsSetVariableClientSideAction)

View File

@ -27,10 +27,15 @@ import parse, { NodeType } from 'node-html-parser'
import { parseDynamicTheme } from './parseDynamicTheme'
import { env } from '@typebot.io/env'
export const startSession = async (
startParams?: StartParams,
userId?: string
): Promise<ChatReply & { newSessionState: SessionState }> => {
type Props = {
startParams: StartParams
userId: string | undefined
}
export const startSession = async ({
startParams,
userId,
}: Props): Promise<ChatReply & { newSessionState: SessionState }> => {
if (!startParams)
throw new TRPCError({
code: 'BAD_REQUEST',

View File

@ -70,10 +70,13 @@ export const startWhatsAppPreview = publicProcedure
const { newSessionState, messages, input, clientSideActions, logs } =
await startSession({
isOnlyRegistering: !canSendDirectMessagesToUser,
typebot: typebotId,
isPreview: true,
startGroupId,
startParams: {
isOnlyRegistering: !canSendDirectMessagesToUser,
typebot: typebotId,
isPreview: true,
startGroupId,
},
userId: user.id,
})
if (canSendDirectMessagesToUser) {

View File

@ -84,7 +84,10 @@ export const startWhatsAppSession = async ({
if (credentials.phoneNumberId !== phoneNumberId) return
const session = await startSession({
typebot: publicTypebot.typebot.publicId as string,
startParams: {
typebot: publicTypebot.typebot.publicId as string,
},
userId: undefined,
})
return {

View File

@ -21,12 +21,18 @@ const getAuthenticatedUser = async (
}
const authenticateByToken = async (
apiToken: string
token: string
): Promise<User | undefined> => {
if (typeof window !== 'undefined') return
return (await prisma.user.findFirst({
where: { apiTokens: { some: { token: apiToken } } },
})) as User
const apiToken = await prisma.apiToken.findFirst({
where: {
token,
},
select: {
owner: true,
},
})
return apiToken?.owner
}
const extractBearerToken = (req: NextApiRequest) =>