2
0

(setVariable) Add Transcription system var (#1507)

Closes #1484
This commit is contained in:
Baptiste Arnaud
2024-05-15 14:24:55 +02:00
committed by GitHub
parent ec7ff8d9ca
commit 40f21203b5
102 changed files with 2911 additions and 986 deletions

View File

@ -1,29 +1,79 @@
import prisma from '@typebot.io/lib/prisma'
import { Prisma } from '@typebot.io/prisma'
import { TypebotInSession } from '@typebot.io/schemas'
import { Prisma, SetVariableHistoryItem, VisitedEdge } from '@typebot.io/prisma'
import { ContinueChatResponse, TypebotInSession } from '@typebot.io/schemas'
import { filterNonSessionVariablesWithValues } from '@typebot.io/variables/filterVariablesWithValues'
import { formatLogDetails } from '../logs/helpers/formatLogDetails'
type Props = {
resultId: string
typebot: TypebotInSession
hasStarted: boolean
isCompleted: boolean
lastChatSessionId?: string
logs?: ContinueChatResponse['logs']
visitedEdges?: VisitedEdge[]
setVariableHistory?: SetVariableHistoryItem[]
}
export const upsertResult = ({
resultId,
typebot,
hasStarted,
isCompleted,
lastChatSessionId,
logs,
visitedEdges,
setVariableHistory,
}: Props): Prisma.PrismaPromise<any> => {
const variablesWithValue = filterNonSessionVariablesWithValues(
typebot.variables
)
const logsToCreate =
logs && logs.length > 0
? {
createMany: {
data: logs.map((log) => ({
...log,
details: formatLogDetails(log.details),
})),
},
}
: undefined
const setVariableHistoryToCreate =
setVariableHistory && setVariableHistory.length > 0
? ({
createMany: {
data: setVariableHistory.map((item) => ({
...item,
value: item.value === null ? Prisma.JsonNull : item.value,
resultId: undefined,
})),
},
} as Prisma.SetVariableHistoryItemUpdateManyWithoutResultNestedInput)
: undefined
const visitedEdgesToCreate =
visitedEdges && visitedEdges.length > 0
? {
createMany: {
data: visitedEdges.map((edge) => ({
...edge,
resultId: undefined,
})),
},
}
: undefined
return prisma.result.upsert({
where: { id: resultId },
update: {
isCompleted: isCompleted ? true : undefined,
hasStarted,
variables: variablesWithValue,
lastChatSessionId,
logs: logsToCreate,
setVariableHistory: setVariableHistoryToCreate,
edges: visitedEdgesToCreate,
},
create: {
id: resultId,
@ -31,6 +81,10 @@ export const upsertResult = ({
isCompleted: isCompleted ? true : false,
hasStarted,
variables: variablesWithValue,
lastChatSessionId,
logs: logsToCreate,
setVariableHistory: setVariableHistoryToCreate,
edges: visitedEdgesToCreate,
},
select: { id: true },
})