@@ -12,11 +12,26 @@ export const createSession = ({
|
||||
id,
|
||||
state,
|
||||
isReplying,
|
||||
}: Props): Prisma.PrismaPromise<any> =>
|
||||
prisma.chatSession.create({
|
||||
data: {
|
||||
}: Props): Prisma.PrismaPromise<any> => {
|
||||
if (!id) {
|
||||
return prisma.chatSession.create({
|
||||
data: {
|
||||
id,
|
||||
state,
|
||||
isReplying,
|
||||
},
|
||||
})
|
||||
}
|
||||
return prisma.chatSession.upsert({
|
||||
where: { id },
|
||||
update: {
|
||||
state,
|
||||
isReplying,
|
||||
},
|
||||
create: {
|
||||
id,
|
||||
state,
|
||||
isReplying,
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
@@ -4,24 +4,33 @@ import { Answer, Result } from '@typebot.io/schemas'
|
||||
type Props = {
|
||||
id: string
|
||||
}
|
||||
export const findResult = ({ id }: Props) =>
|
||||
prisma.result.findFirst({
|
||||
where: { id, isArchived: { not: true } },
|
||||
select: {
|
||||
id: true,
|
||||
variables: true,
|
||||
hasStarted: true,
|
||||
answers: {
|
||||
select: {
|
||||
content: true,
|
||||
blockId: true,
|
||||
variableId: true,
|
||||
export const findResult = async ({ id }: Props) => {
|
||||
const { answers, answersV2, ...result } =
|
||||
(await prisma.result.findFirst({
|
||||
where: { id, isArchived: { not: true } },
|
||||
select: {
|
||||
id: true,
|
||||
variables: true,
|
||||
hasStarted: true,
|
||||
answers: {
|
||||
select: {
|
||||
content: true,
|
||||
blockId: true,
|
||||
},
|
||||
},
|
||||
answersV2: {
|
||||
select: {
|
||||
content: true,
|
||||
blockId: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
}) as Promise<
|
||||
| (Pick<Result, 'id' | 'variables' | 'hasStarted'> & {
|
||||
answers: Pick<Answer, 'content' | 'blockId' | 'variableId'>[]
|
||||
})
|
||||
| null
|
||||
>
|
||||
})) ?? {}
|
||||
if (!result) return null
|
||||
return {
|
||||
...result,
|
||||
answers: (answersV2 ?? []).concat(answers ?? []),
|
||||
} as Pick<Result, 'id' | 'variables' | 'hasStarted'> & {
|
||||
answers: Pick<Answer, 'content' | 'blockId'>[]
|
||||
}
|
||||
}
|
||||
|
||||
16
packages/bot-engine/queries/saveAnswer.ts
Normal file
16
packages/bot-engine/queries/saveAnswer.ts
Normal file
@@ -0,0 +1,16 @@
|
||||
import prisma from '@typebot.io/lib/prisma'
|
||||
import { Prisma } from '@typebot.io/prisma'
|
||||
import { SessionState } from '@typebot.io/schemas'
|
||||
|
||||
type Props = {
|
||||
answer: Omit<Prisma.AnswerV2CreateManyInput, 'resultId'>
|
||||
reply: string
|
||||
state: SessionState
|
||||
}
|
||||
export const saveAnswer = async ({ answer, state }: Props) => {
|
||||
const resultId = state.typebotsQueue[0].resultId
|
||||
if (!resultId) return
|
||||
return prisma.answerV2.createMany({
|
||||
data: [{ ...answer, resultId }],
|
||||
})
|
||||
}
|
||||
16
packages/bot-engine/queries/saveSetVariableHistoryItems.ts
Normal file
16
packages/bot-engine/queries/saveSetVariableHistoryItems.ts
Normal file
@@ -0,0 +1,16 @@
|
||||
import prisma from '@typebot.io/lib/prisma'
|
||||
import { Prisma } from '@typebot.io/prisma'
|
||||
import { SetVariableHistoryItem } from '@typebot.io/schemas'
|
||||
|
||||
export const saveSetVariableHistoryItems = (
|
||||
setVariableHistory: SetVariableHistoryItem[]
|
||||
) =>
|
||||
prisma.setVariableHistoryItem.createMany({
|
||||
data: {
|
||||
...setVariableHistory.map((item) => ({
|
||||
...item,
|
||||
value: item.value === null ? Prisma.JsonNull : item.value,
|
||||
})),
|
||||
},
|
||||
skipDuplicates: true,
|
||||
})
|
||||
@@ -1,34 +0,0 @@
|
||||
import prisma from '@typebot.io/lib/prisma'
|
||||
import { Prisma } from '@typebot.io/prisma'
|
||||
import { InputBlock, SessionState } from '@typebot.io/schemas'
|
||||
|
||||
type Props = {
|
||||
answer: Omit<Prisma.AnswerUncheckedCreateInput, 'resultId'>
|
||||
reply: string
|
||||
state: SessionState
|
||||
}
|
||||
export const upsertAnswer = async ({ answer, state }: Props) => {
|
||||
const resultId = state.typebotsQueue[0].resultId
|
||||
if (!resultId) return
|
||||
const where = {
|
||||
resultId,
|
||||
blockId: answer.blockId,
|
||||
groupId: answer.groupId,
|
||||
}
|
||||
const existingAnswer = await prisma.answer.findUnique({
|
||||
where: {
|
||||
resultId_blockId_groupId: where,
|
||||
},
|
||||
select: { resultId: true },
|
||||
})
|
||||
if (existingAnswer)
|
||||
return prisma.answer.updateMany({
|
||||
where,
|
||||
data: {
|
||||
content: answer.content,
|
||||
},
|
||||
})
|
||||
return prisma.answer.createMany({
|
||||
data: [{ ...answer, resultId }],
|
||||
})
|
||||
}
|
||||
@@ -1,29 +1,79 @@
|
||||
import prisma from '@typebot.io/lib/prisma'
|
||||
import { Prisma } from '@typebot.io/prisma'
|
||||
import { TypebotInSession } from '@typebot.io/schemas'
|
||||
import { Prisma, SetVariableHistoryItem, VisitedEdge } from '@typebot.io/prisma'
|
||||
import { ContinueChatResponse, TypebotInSession } from '@typebot.io/schemas'
|
||||
import { filterNonSessionVariablesWithValues } from '@typebot.io/variables/filterVariablesWithValues'
|
||||
import { formatLogDetails } from '../logs/helpers/formatLogDetails'
|
||||
|
||||
type Props = {
|
||||
resultId: string
|
||||
typebot: TypebotInSession
|
||||
hasStarted: boolean
|
||||
isCompleted: boolean
|
||||
lastChatSessionId?: string
|
||||
logs?: ContinueChatResponse['logs']
|
||||
visitedEdges?: VisitedEdge[]
|
||||
setVariableHistory?: SetVariableHistoryItem[]
|
||||
}
|
||||
export const upsertResult = ({
|
||||
resultId,
|
||||
typebot,
|
||||
hasStarted,
|
||||
isCompleted,
|
||||
lastChatSessionId,
|
||||
logs,
|
||||
visitedEdges,
|
||||
setVariableHistory,
|
||||
}: Props): Prisma.PrismaPromise<any> => {
|
||||
const variablesWithValue = filterNonSessionVariablesWithValues(
|
||||
typebot.variables
|
||||
)
|
||||
const logsToCreate =
|
||||
logs && logs.length > 0
|
||||
? {
|
||||
createMany: {
|
||||
data: logs.map((log) => ({
|
||||
...log,
|
||||
details: formatLogDetails(log.details),
|
||||
})),
|
||||
},
|
||||
}
|
||||
: undefined
|
||||
|
||||
const setVariableHistoryToCreate =
|
||||
setVariableHistory && setVariableHistory.length > 0
|
||||
? ({
|
||||
createMany: {
|
||||
data: setVariableHistory.map((item) => ({
|
||||
...item,
|
||||
value: item.value === null ? Prisma.JsonNull : item.value,
|
||||
resultId: undefined,
|
||||
})),
|
||||
},
|
||||
} as Prisma.SetVariableHistoryItemUpdateManyWithoutResultNestedInput)
|
||||
: undefined
|
||||
|
||||
const visitedEdgesToCreate =
|
||||
visitedEdges && visitedEdges.length > 0
|
||||
? {
|
||||
createMany: {
|
||||
data: visitedEdges.map((edge) => ({
|
||||
...edge,
|
||||
resultId: undefined,
|
||||
})),
|
||||
},
|
||||
}
|
||||
: undefined
|
||||
|
||||
return prisma.result.upsert({
|
||||
where: { id: resultId },
|
||||
update: {
|
||||
isCompleted: isCompleted ? true : undefined,
|
||||
hasStarted,
|
||||
variables: variablesWithValue,
|
||||
lastChatSessionId,
|
||||
logs: logsToCreate,
|
||||
setVariableHistory: setVariableHistoryToCreate,
|
||||
edges: visitedEdgesToCreate,
|
||||
},
|
||||
create: {
|
||||
id: resultId,
|
||||
@@ -31,6 +81,10 @@ export const upsertResult = ({
|
||||
isCompleted: isCompleted ? true : false,
|
||||
hasStarted,
|
||||
variables: variablesWithValue,
|
||||
lastChatSessionId,
|
||||
logs: logsToCreate,
|
||||
setVariableHistory: setVariableHistoryToCreate,
|
||||
edges: visitedEdgesToCreate,
|
||||
},
|
||||
select: { id: true },
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user