2023-09-20 15:26:52 +02:00
|
|
|
import prisma from '@typebot.io/lib/prisma'
|
2023-07-18 14:31:20 +02:00
|
|
|
import { Prisma } from '@typebot.io/prisma'
|
2023-09-19 15:42:33 +02:00
|
|
|
import { InputBlock, SessionState } from '@typebot.io/schemas'
|
2023-07-18 14:31:20 +02:00
|
|
|
|
|
|
|
|
type Props = {
|
|
|
|
|
answer: Omit<Prisma.AnswerUncheckedCreateInput, 'resultId'>
|
|
|
|
|
reply: string
|
|
|
|
|
state: SessionState
|
|
|
|
|
}
|
2023-11-08 15:34:16 +01:00
|
|
|
export const upsertAnswer = async ({ answer, state }: Props) => {
|
2023-08-24 07:48:30 +02:00
|
|
|
const resultId = state.typebotsQueue[0].resultId
|
|
|
|
|
if (!resultId) return
|
2023-07-18 14:31:20 +02:00
|
|
|
const where = {
|
2023-08-24 07:48:30 +02:00
|
|
|
resultId,
|
2023-11-08 15:34:16 +01:00
|
|
|
blockId: answer.blockId,
|
|
|
|
|
groupId: answer.groupId,
|
2023-07-18 14:31:20 +02:00
|
|
|
}
|
|
|
|
|
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({
|
2023-08-24 07:48:30 +02:00
|
|
|
data: [{ ...answer, resultId }],
|
2023-07-18 14:31:20 +02:00
|
|
|
})
|
|
|
|
|
}
|