2023-09-20 15:26:52 +02:00
|
|
|
import prisma from '@typebot.io/lib/prisma'
|
2024-04-06 15:08:57 +02:00
|
|
|
import { Prisma } from '@typebot.io/prisma'
|
2023-08-24 07:48:30 +02:00
|
|
|
import { TypebotInSession } from '@typebot.io/schemas'
|
2024-05-02 15:11:16 +02:00
|
|
|
import { filterNonSessionVariablesWithValues } from '@typebot.io/variables/filterVariablesWithValues'
|
2023-07-18 14:31:20 +02:00
|
|
|
|
|
|
|
type Props = {
|
2023-08-24 07:48:30 +02:00
|
|
|
resultId: string
|
|
|
|
typebot: TypebotInSession
|
|
|
|
hasStarted: boolean
|
2023-07-18 14:31:20 +02:00
|
|
|
isCompleted: boolean
|
|
|
|
}
|
2024-04-06 15:08:57 +02:00
|
|
|
export const upsertResult = ({
|
2023-08-24 07:48:30 +02:00
|
|
|
resultId,
|
|
|
|
typebot,
|
|
|
|
hasStarted,
|
|
|
|
isCompleted,
|
2024-04-06 15:08:57 +02:00
|
|
|
}: Props): Prisma.PrismaPromise<any> => {
|
2024-05-02 15:11:16 +02:00
|
|
|
const variablesWithValue = filterNonSessionVariablesWithValues(
|
|
|
|
typebot.variables
|
|
|
|
)
|
2024-04-06 15:08:57 +02:00
|
|
|
return prisma.result.upsert({
|
2023-08-24 07:48:30 +02:00
|
|
|
where: { id: resultId },
|
2024-04-06 15:08:57 +02:00
|
|
|
update: {
|
|
|
|
isCompleted: isCompleted ? true : undefined,
|
|
|
|
hasStarted,
|
|
|
|
variables: variablesWithValue,
|
|
|
|
},
|
|
|
|
create: {
|
|
|
|
id: resultId,
|
|
|
|
typebotId: typebot.id,
|
|
|
|
isCompleted: isCompleted ? true : false,
|
|
|
|
hasStarted,
|
|
|
|
variables: variablesWithValue,
|
|
|
|
},
|
2023-07-18 14:31:20 +02:00
|
|
|
select: { id: true },
|
|
|
|
})
|
|
|
|
}
|