2
0

♻️ Export bot-engine code into its own package

This commit is contained in:
Baptiste Arnaud
2023-09-20 15:26:52 +02:00
parent 797685aa9d
commit 7d57e8dd06
242 changed files with 645 additions and 639 deletions

View 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),
},
],
})
}

View 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,
},
})

View File

@ -0,0 +1,8 @@
import prisma from '@typebot.io/lib/prisma'
export const deleteSession = (id: string) =>
prisma.chatSession.deleteMany({
where: {
id,
},
})

View 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,
},
},
},
},
},
})

View 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
>

View 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,
},
})

View 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) }
}

View 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,
},
})
}

View 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 })

View 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,
},
})

View 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 }],
})
}

View 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,
},
],
})
}