2023-11-13 15:27:36 +01:00
|
|
|
import { ContinueChatResponse, ChatSession } from '@typebot.io/schemas'
|
2023-09-20 15:26:52 +02:00
|
|
|
import { upsertResult } from './queries/upsertResult'
|
|
|
|
import { saveLogs } from './queries/saveLogs'
|
|
|
|
import { updateSession } from './queries/updateSession'
|
|
|
|
import { formatLogDetails } from './logs/helpers/formatLogDetails'
|
|
|
|
import { createSession } from './queries/createSession'
|
|
|
|
import { deleteSession } from './queries/deleteSession'
|
2023-10-23 14:47:44 +02:00
|
|
|
import * as Sentry from '@sentry/nextjs'
|
2023-11-08 15:34:16 +01:00
|
|
|
import { saveVisitedEdges } from './queries/saveVisitedEdges'
|
2024-04-06 15:08:57 +02:00
|
|
|
import { Prisma, VisitedEdge } from '@typebot.io/prisma'
|
|
|
|
import prisma from '@typebot.io/lib/prisma'
|
2023-07-18 14:31:20 +02:00
|
|
|
|
|
|
|
type Props = {
|
|
|
|
session: Pick<ChatSession, 'state'> & { id?: string }
|
2023-11-13 15:27:36 +01:00
|
|
|
input: ContinueChatResponse['input']
|
|
|
|
logs: ContinueChatResponse['logs']
|
|
|
|
clientSideActions: ContinueChatResponse['clientSideActions']
|
2023-11-08 15:34:16 +01:00
|
|
|
visitedEdges: VisitedEdge[]
|
2023-10-03 16:06:37 +02:00
|
|
|
forceCreateSession?: boolean
|
2024-01-22 14:35:12 +01:00
|
|
|
hasCustomEmbedBubble?: boolean
|
2023-07-18 14:31:20 +02:00
|
|
|
}
|
2023-08-24 07:48:30 +02:00
|
|
|
|
2023-07-18 14:31:20 +02:00
|
|
|
export const saveStateToDatabase = async ({
|
|
|
|
session: { state, id },
|
|
|
|
input,
|
|
|
|
logs,
|
|
|
|
clientSideActions,
|
2023-10-03 16:06:37 +02:00
|
|
|
forceCreateSession,
|
2023-11-08 15:34:16 +01:00
|
|
|
visitedEdges,
|
2024-01-22 14:35:12 +01:00
|
|
|
hasCustomEmbedBubble,
|
2023-07-18 14:31:20 +02:00
|
|
|
}: Props) => {
|
2023-08-29 10:01:28 +02:00
|
|
|
const containsSetVariableClientSideAction = clientSideActions?.some(
|
2023-09-04 14:52:16 +02:00
|
|
|
(action) => action.expectsDedicatedReply
|
2023-08-29 10:01:28 +02:00
|
|
|
)
|
2023-07-18 14:31:20 +02:00
|
|
|
|
2024-01-22 14:35:12 +01:00
|
|
|
const isCompleted = Boolean(
|
|
|
|
!input && !containsSetVariableClientSideAction && !hasCustomEmbedBubble
|
|
|
|
)
|
2023-07-18 14:31:20 +02:00
|
|
|
|
2024-04-06 15:08:57 +02:00
|
|
|
const queries: Prisma.PrismaPromise<any>[] = []
|
|
|
|
|
2023-08-24 07:48:30 +02:00
|
|
|
const resultId = state.typebotsQueue[0].resultId
|
|
|
|
|
2023-08-29 10:01:28 +02:00
|
|
|
if (id) {
|
2024-04-06 15:08:57 +02:00
|
|
|
if (isCompleted && resultId) queries.push(deleteSession(id))
|
|
|
|
else queries.push(updateSession({ id, state }))
|
2023-08-29 10:01:28 +02:00
|
|
|
}
|
2023-07-18 14:31:20 +02:00
|
|
|
|
2023-08-29 10:01:28 +02:00
|
|
|
const session =
|
2023-10-03 16:06:37 +02:00
|
|
|
id && !forceCreateSession
|
|
|
|
? { state, id }
|
|
|
|
: await createSession({ id, state })
|
2023-08-29 10:01:28 +02:00
|
|
|
|
2024-04-06 15:08:57 +02:00
|
|
|
if (!resultId) {
|
|
|
|
if (queries.length > 0) await prisma.$transaction(queries)
|
|
|
|
return session
|
|
|
|
}
|
2023-08-24 07:48:30 +02:00
|
|
|
|
|
|
|
const answers = state.typebotsQueue[0].answers
|
|
|
|
|
2024-04-06 15:08:57 +02:00
|
|
|
queries.push(
|
|
|
|
upsertResult({
|
|
|
|
resultId,
|
|
|
|
typebot: state.typebotsQueue[0].typebot,
|
|
|
|
isCompleted: Boolean(
|
|
|
|
!input && !containsSetVariableClientSideAction && answers.length > 0
|
|
|
|
),
|
|
|
|
hasStarted: answers.length > 0,
|
|
|
|
})
|
|
|
|
)
|
2023-07-18 14:31:20 +02:00
|
|
|
|
|
|
|
if (logs && logs.length > 0)
|
2023-10-23 14:47:44 +02:00
|
|
|
try {
|
|
|
|
await saveLogs(
|
|
|
|
logs.map((log) => ({
|
|
|
|
...log,
|
|
|
|
resultId,
|
|
|
|
details: formatLogDetails(log.details),
|
|
|
|
}))
|
|
|
|
)
|
|
|
|
} catch (e) {
|
|
|
|
console.error('Failed to save logs', e)
|
|
|
|
Sentry.captureException(e)
|
|
|
|
}
|
2023-07-18 14:31:20 +02:00
|
|
|
|
2024-04-06 15:08:57 +02:00
|
|
|
if (visitedEdges.length > 0) queries.push(saveVisitedEdges(visitedEdges))
|
|
|
|
|
|
|
|
await prisma.$transaction(queries)
|
2023-11-08 15:34:16 +01:00
|
|
|
|
2023-07-18 14:31:20 +02:00
|
|
|
return session
|
|
|
|
}
|