♻️ Export bot-engine code into its own package
This commit is contained in:
33
packages/bot-engine/queries/createResultIfNotExist.ts
Normal file
33
packages/bot-engine/queries/createResultIfNotExist.ts
Normal file
@ -0,0 +1,33 @@
|
||||
import prisma from '@typebot.io/lib/prisma'
|
||||
import { getDefinedVariables } from '@typebot.io/lib/results'
|
||||
import { TypebotInSession } from '@typebot.io/schemas'
|
||||
|
||||
type Props = {
|
||||
resultId: string
|
||||
typebot: TypebotInSession
|
||||
hasStarted: boolean
|
||||
isCompleted: boolean
|
||||
}
|
||||
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),
|
||||
},
|
||||
],
|
||||
})
|
||||
}
|
15
packages/bot-engine/queries/createSession.ts
Normal file
15
packages/bot-engine/queries/createSession.ts
Normal file
@ -0,0 +1,15 @@
|
||||
import prisma from '@typebot.io/lib/prisma'
|
||||
import { SessionState } from '@typebot.io/schemas'
|
||||
|
||||
type Props = {
|
||||
id?: string
|
||||
state: SessionState
|
||||
}
|
||||
|
||||
export const createSession = async ({ id, state }: Props) =>
|
||||
prisma.chatSession.create({
|
||||
data: {
|
||||
id,
|
||||
state,
|
||||
},
|
||||
})
|
8
packages/bot-engine/queries/deleteSession.ts
Normal file
8
packages/bot-engine/queries/deleteSession.ts
Normal file
@ -0,0 +1,8 @@
|
||||
import prisma from '@typebot.io/lib/prisma'
|
||||
|
||||
export const deleteSession = (id: string) =>
|
||||
prisma.chatSession.deleteMany({
|
||||
where: {
|
||||
id,
|
||||
},
|
||||
})
|
35
packages/bot-engine/queries/findPublicTypebot.ts
Normal file
35
packages/bot-engine/queries/findPublicTypebot.ts
Normal file
@ -0,0 +1,35 @@
|
||||
import prisma from '@typebot.io/lib/prisma'
|
||||
|
||||
type Props = {
|
||||
publicId: string
|
||||
}
|
||||
|
||||
export const findPublicTypebot = ({ publicId }: Props) =>
|
||||
prisma.publicTypebot.findFirst({
|
||||
where: { typebot: { publicId } },
|
||||
select: {
|
||||
version: true,
|
||||
groups: true,
|
||||
edges: true,
|
||||
settings: true,
|
||||
theme: true,
|
||||
variables: true,
|
||||
typebotId: true,
|
||||
typebot: {
|
||||
select: {
|
||||
isArchived: true,
|
||||
isClosed: true,
|
||||
workspace: {
|
||||
select: {
|
||||
id: true,
|
||||
plan: true,
|
||||
additionalChatsIndex: true,
|
||||
customChatsLimit: true,
|
||||
isQuarantined: true,
|
||||
isSuspended: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
})
|
27
packages/bot-engine/queries/findResult.ts
Normal file
27
packages/bot-engine/queries/findResult.ts
Normal file
@ -0,0 +1,27 @@
|
||||
import prisma from '@typebot.io/lib/prisma'
|
||||
import { Answer, Result } from '@typebot.io/schemas'
|
||||
|
||||
type Props = {
|
||||
id: string
|
||||
}
|
||||
export const findResult = ({ id }: Props) =>
|
||||
prisma.result.findFirst({
|
||||
where: { id },
|
||||
select: {
|
||||
id: true,
|
||||
variables: true,
|
||||
hasStarted: true,
|
||||
answers: {
|
||||
select: {
|
||||
content: true,
|
||||
blockId: true,
|
||||
variableId: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
}) as Promise<
|
||||
| (Pick<Result, 'id' | 'variables' | 'hasStarted'> & {
|
||||
answers: Pick<Answer, 'content' | 'blockId' | 'variableId'>[]
|
||||
})
|
||||
| null
|
||||
>
|
21
packages/bot-engine/queries/findTypebot.ts
Normal file
21
packages/bot-engine/queries/findTypebot.ts
Normal file
@ -0,0 +1,21 @@
|
||||
import prisma from '@typebot.io/lib/prisma'
|
||||
|
||||
type Props = {
|
||||
id: string
|
||||
userId?: string
|
||||
}
|
||||
|
||||
export const findTypebot = ({ id, userId }: Props) =>
|
||||
prisma.typebot.findFirst({
|
||||
where: { id, workspace: { members: { some: { userId } } } },
|
||||
select: {
|
||||
version: true,
|
||||
id: true,
|
||||
groups: true,
|
||||
edges: true,
|
||||
settings: true,
|
||||
theme: true,
|
||||
variables: true,
|
||||
isArchived: true,
|
||||
},
|
||||
})
|
13
packages/bot-engine/queries/getSession.ts
Normal file
13
packages/bot-engine/queries/getSession.ts
Normal file
@ -0,0 +1,13 @@
|
||||
import prisma from '@typebot.io/lib/prisma'
|
||||
import { ChatSession, sessionStateSchema } from '@typebot.io/schemas'
|
||||
|
||||
export const getSession = async (
|
||||
sessionId: string
|
||||
): Promise<Pick<ChatSession, 'state' | 'id'> | null> => {
|
||||
const session = await prisma.chatSession.findUnique({
|
||||
where: { id: sessionId },
|
||||
select: { id: true, state: true },
|
||||
})
|
||||
if (!session) return null
|
||||
return { ...session, state: sessionStateSchema.parse(session.state) }
|
||||
}
|
24
packages/bot-engine/queries/restartSession.ts
Normal file
24
packages/bot-engine/queries/restartSession.ts
Normal file
@ -0,0 +1,24 @@
|
||||
import prisma from '@typebot.io/lib/prisma'
|
||||
import { SessionState } from '@typebot.io/schemas'
|
||||
|
||||
type Props = {
|
||||
id?: string
|
||||
state: SessionState
|
||||
}
|
||||
|
||||
export const restartSession = async ({ id, state }: Props) => {
|
||||
if (id) {
|
||||
await prisma.chatSession.deleteMany({
|
||||
where: {
|
||||
id,
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
return prisma.chatSession.create({
|
||||
data: {
|
||||
id,
|
||||
state,
|
||||
},
|
||||
})
|
||||
}
|
5
packages/bot-engine/queries/saveLogs.ts
Normal file
5
packages/bot-engine/queries/saveLogs.ts
Normal file
@ -0,0 +1,5 @@
|
||||
import prisma from '@typebot.io/lib/prisma'
|
||||
import { Log } from '@typebot.io/schemas'
|
||||
|
||||
export const saveLogs = (logs: Omit<Log, 'id' | 'createdAt'>[]) =>
|
||||
prisma.log.createMany({ data: logs })
|
15
packages/bot-engine/queries/updateSession.ts
Normal file
15
packages/bot-engine/queries/updateSession.ts
Normal file
@ -0,0 +1,15 @@
|
||||
import prisma from '@typebot.io/lib/prisma'
|
||||
import { SessionState } from '@typebot.io/schemas'
|
||||
|
||||
type Props = {
|
||||
id: string
|
||||
state: SessionState
|
||||
}
|
||||
|
||||
export const updateSession = async ({ id, state }: Props) =>
|
||||
prisma.chatSession.updateMany({
|
||||
where: { id },
|
||||
data: {
|
||||
state,
|
||||
},
|
||||
})
|
37
packages/bot-engine/queries/upsertAnswer.ts
Normal file
37
packages/bot-engine/queries/upsertAnswer.ts
Normal file
@ -0,0 +1,37 @@
|
||||
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'>
|
||||
block: InputBlock
|
||||
reply: string
|
||||
itemId?: string
|
||||
state: SessionState
|
||||
}
|
||||
export const upsertAnswer = async ({ answer, block, state }: Props) => {
|
||||
const resultId = state.typebotsQueue[0].resultId
|
||||
if (!resultId) return
|
||||
const where = {
|
||||
resultId,
|
||||
blockId: block.id,
|
||||
groupId: block.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,
|
||||
itemId: answer.itemId,
|
||||
},
|
||||
})
|
||||
return prisma.answer.createMany({
|
||||
data: [{ ...answer, resultId }],
|
||||
})
|
||||
}
|
44
packages/bot-engine/queries/upsertResult.ts
Normal file
44
packages/bot-engine/queries/upsertResult.ts
Normal file
@ -0,0 +1,44 @@
|
||||
import prisma from '@typebot.io/lib/prisma'
|
||||
import { getDefinedVariables } from '@typebot.io/lib/results'
|
||||
import { TypebotInSession } from '@typebot.io/schemas'
|
||||
|
||||
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 = getDefinedVariables(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,
|
||||
},
|
||||
],
|
||||
})
|
||||
}
|
Reference in New Issue
Block a user