2
0
Files
bot/packages/bot-engine/queries/upsertResult.ts
2024-01-12 10:16:01 +01:00

45 lines
1.1 KiB
TypeScript

import prisma from '@typebot.io/lib/prisma'
import { TypebotInSession } from '@typebot.io/schemas'
import { filterVariablesWithValues } from '@typebot.io/variables/filterVariablesWithValues'
type Props = {
resultId: string
typebot: TypebotInSession
hasStarted: boolean
isCompleted: boolean
}
export const upsertResult = async ({
resultId,
typebot,
hasStarted,
isCompleted,
}: Props) => {
const existingResult = await prisma.result.findUnique({
where: { id: resultId },
select: { id: true },
})
const variablesWithValue = filterVariablesWithValues(typebot.variables)
if (existingResult) {
return prisma.result.updateMany({
where: { id: resultId },
data: {
isCompleted: isCompleted ? true : undefined,
hasStarted,
variables: variablesWithValue,
},
})
}
return prisma.result.createMany({
data: [
{
id: resultId,
typebotId: typebot.id,
isCompleted: isCompleted ? true : false,
hasStarted,
variables: variablesWithValue,
},
],
})
}