2023-09-20 15:26:52 +02:00
|
|
|
import prisma from '@typebot.io/lib/prisma'
|
2023-08-24 07:48:30 +02:00
|
|
|
import { getDefinedVariables } from '@typebot.io/lib/results'
|
|
|
|
|
import { TypebotInSession } from '@typebot.io/schemas'
|
|
|
|
|
|
|
|
|
|
type Props = {
|
|
|
|
|
resultId: string
|
|
|
|
|
typebot: TypebotInSession
|
|
|
|
|
hasStarted: boolean
|
|
|
|
|
isCompleted: boolean
|
|
|
|
|
}
|
2023-11-08 15:34:16 +01:00
|
|
|
|
2023-08-24 07:48:30 +02:00
|
|
|
export const createResultIfNotExist = async ({
|
|
|
|
|
resultId,
|
|
|
|
|
typebot,
|
|
|
|
|
hasStarted,
|
|
|
|
|
isCompleted,
|
|
|
|
|
}: Props) => {
|
|
|
|
|
const existingResult = await prisma.result.findUnique({
|
|
|
|
|
where: { id: resultId },
|
|
|
|
|
select: { id: true },
|
|
|
|
|
})
|
|
|
|
|
if (existingResult) return
|
|
|
|
|
return prisma.result.createMany({
|
|
|
|
|
data: [
|
|
|
|
|
{
|
|
|
|
|
id: resultId,
|
|
|
|
|
typebotId: typebot.id,
|
|
|
|
|
isCompleted: isCompleted ? true : false,
|
|
|
|
|
hasStarted,
|
|
|
|
|
variables: getDefinedVariables(typebot.variables),
|
|
|
|
|
},
|
|
|
|
|
],
|
|
|
|
|
})
|
|
|
|
|
}
|