@ -1,27 +1,28 @@
|
||||
import { z } from '../zod'
|
||||
import { Answer as AnswerPrisma, Prisma } from '@typebot.io/prisma'
|
||||
import { Answer as AnswerV1Prisma, Prisma } from '@typebot.io/prisma'
|
||||
|
||||
export const answerSchema = z.object({
|
||||
const answerV1Schema = z.object({
|
||||
createdAt: z.date(),
|
||||
resultId: z.string(),
|
||||
blockId: z.string(),
|
||||
groupId: z.string(),
|
||||
variableId: z.string().nullable(),
|
||||
content: z.string(),
|
||||
storageUsed: z.number().nullable(),
|
||||
// TO-DO: remove once itemId is removed from database schema
|
||||
}) satisfies z.ZodType<Omit<AnswerPrisma, 'itemId'>>
|
||||
}) satisfies z.ZodType<AnswerV1Prisma>
|
||||
|
||||
export const answerInputSchema = answerSchema
|
||||
export const answerSchema = z.object({
|
||||
blockId: z.string(),
|
||||
content: z.string(),
|
||||
})
|
||||
|
||||
export const answerInputSchema = answerV1Schema
|
||||
.omit({
|
||||
createdAt: true,
|
||||
resultId: true,
|
||||
variableId: true,
|
||||
storageUsed: true,
|
||||
})
|
||||
.extend({
|
||||
variableId: z.string().nullish(),
|
||||
storageUsed: z.number().nullish(),
|
||||
}) satisfies z.ZodType<Prisma.AnswerUncheckedUpdateInput>
|
||||
|
||||
export const statsSchema = z.object({
|
||||
|
@ -5,6 +5,7 @@ export const valueTypes = [
|
||||
'Empty',
|
||||
'Append value(s)',
|
||||
'Environment name',
|
||||
'Transcript',
|
||||
'User ID',
|
||||
'Result ID',
|
||||
'Now',
|
||||
@ -20,6 +21,8 @@ export const valueTypes = [
|
||||
|
||||
export const hiddenTypes = ['Today', 'User ID'] as const
|
||||
|
||||
export const sessionOnlySetVariableOptions = ['Transcript'] as const
|
||||
|
||||
export const defaultSetVariableOptions = {
|
||||
type: 'Custom',
|
||||
isExecutedOnClient: false,
|
||||
|
@ -21,6 +21,7 @@ const basicSetVariableOptionsSchema = baseOptions.extend({
|
||||
'Random ID',
|
||||
'Phone number',
|
||||
'Contact name',
|
||||
'Transcript',
|
||||
]),
|
||||
})
|
||||
|
||||
|
@ -260,6 +260,12 @@ export const startPreviewChatInputSchema = z.object({
|
||||
Email: 'john@gmail.com',
|
||||
},
|
||||
}),
|
||||
sessionId: z
|
||||
.string()
|
||||
.optional()
|
||||
.describe(
|
||||
'If provided, will be used as the session ID and will overwrite any existing session with the same ID.'
|
||||
),
|
||||
})
|
||||
export type StartPreviewChatInput = z.infer<typeof startPreviewChatInputSchema>
|
||||
|
||||
|
@ -1,14 +1,9 @@
|
||||
import { z } from '../../zod'
|
||||
import { answerSchema } from '../answer'
|
||||
import { resultSchema } from '../result'
|
||||
import { resultSchema, setVariableHistoryItemSchema } from '../result'
|
||||
import { typebotInSessionStateSchema, dynamicThemeSchema } from './shared'
|
||||
import { settingsSchema } from '../typebot/settings'
|
||||
|
||||
const answerInSessionStateSchema = answerSchema.pick({
|
||||
content: true,
|
||||
blockId: true,
|
||||
variableId: true,
|
||||
})
|
||||
import { isInputBlock } from '../../helpers'
|
||||
|
||||
const answerInSessionStateSchemaV2 = z.object({
|
||||
key: z.string(),
|
||||
@ -23,7 +18,7 @@ const resultInSessionStateSchema = resultSchema
|
||||
})
|
||||
.merge(
|
||||
z.object({
|
||||
answers: z.array(answerInSessionStateSchema),
|
||||
answers: z.array(answerSchema),
|
||||
id: z.string().optional(),
|
||||
})
|
||||
)
|
||||
@ -94,6 +89,23 @@ const sessionStateSchemaV3 = sessionStateSchemaV2
|
||||
version: z.literal('3'),
|
||||
currentBlockId: z.string().optional(),
|
||||
allowedOrigins: z.array(z.string()).optional(),
|
||||
setVariableIdsForHistory: z.array(z.string()).optional(),
|
||||
currentSetVariableHistoryIndex: z.number().optional(),
|
||||
previewMetadata: z
|
||||
.object({
|
||||
answers: z.array(answerSchema).optional(),
|
||||
visitedEdges: z.array(z.string()).optional(),
|
||||
setVariableHistory: z
|
||||
.array(
|
||||
setVariableHistoryItemSchema.pick({
|
||||
blockId: true,
|
||||
variableId: true,
|
||||
value: true,
|
||||
})
|
||||
)
|
||||
.optional(),
|
||||
})
|
||||
.optional(),
|
||||
})
|
||||
|
||||
export type SessionState = z.infer<typeof sessionStateSchemaV3>
|
||||
@ -119,17 +131,27 @@ const migrateFromV1ToV2 = (
|
||||
{
|
||||
typebot: state.typebot,
|
||||
resultId: state.result.id,
|
||||
answers: state.result.answers.map((answer) => ({
|
||||
key:
|
||||
(answer.variableId
|
||||
? state.typebot.variables.find(
|
||||
(variable) => variable.id === answer.variableId
|
||||
)?.name
|
||||
: state.typebot.groups.find((group) =>
|
||||
group.blocks.find((block) => block.id === answer.blockId)
|
||||
)?.title) ?? '',
|
||||
value: answer.content,
|
||||
})),
|
||||
answers: state.result.answers.map((answer) => {
|
||||
let answerVariableId: string | undefined
|
||||
state.typebot.groups.forEach((group) => {
|
||||
group.blocks.forEach((block) => {
|
||||
if (isInputBlock(block) && block.id === answer.blockId) {
|
||||
answerVariableId = block.options?.variableId
|
||||
}
|
||||
})
|
||||
})
|
||||
return {
|
||||
key:
|
||||
(answerVariableId
|
||||
? state.typebot.variables.find(
|
||||
(variable) => variable.id === answerVariableId
|
||||
)?.name
|
||||
: state.typebot.groups.find((group) =>
|
||||
group.blocks.find((block) => block.id === answer.blockId)
|
||||
)?.title) ?? '',
|
||||
value: answer.content,
|
||||
}
|
||||
}),
|
||||
isMergingWithParent: true,
|
||||
edgeIdToTriggerWhenDone:
|
||||
state.linkedTypebots.queue.length > 0
|
||||
@ -141,17 +163,27 @@ const migrateFromV1ToV2 = (
|
||||
({
|
||||
typebot,
|
||||
resultId: state.result.id,
|
||||
answers: state.result.answers.map((answer) => ({
|
||||
key:
|
||||
(answer.variableId
|
||||
? state.typebot.variables.find(
|
||||
(variable) => variable.id === answer.variableId
|
||||
)?.name
|
||||
: state.typebot.groups.find((group) =>
|
||||
group.blocks.find((block) => block.id === answer.blockId)
|
||||
)?.title) ?? '',
|
||||
value: answer.content,
|
||||
})),
|
||||
answers: state.result.answers.map((answer) => {
|
||||
let answerVariableId: string | undefined
|
||||
typebot.groups.forEach((group) => {
|
||||
group.blocks.forEach((block) => {
|
||||
if (isInputBlock(block) && block.id === answer.blockId) {
|
||||
answerVariableId = block.options?.variableId
|
||||
}
|
||||
})
|
||||
})
|
||||
return {
|
||||
key:
|
||||
(answerVariableId
|
||||
? state.typebot.variables.find(
|
||||
(variable) => variable.id === answerVariableId
|
||||
)?.name
|
||||
: state.typebot.groups.find((group) =>
|
||||
group.blocks.find((block) => block.id === answer.blockId)
|
||||
)?.title) ?? '',
|
||||
value: answer.content,
|
||||
}
|
||||
}),
|
||||
edgeIdToTriggerWhenDone: state.linkedTypebots.queue.at(index + 1)
|
||||
?.edgeId,
|
||||
} satisfies SessionState['typebotsQueue'][number])
|
||||
|
@ -1,9 +1,10 @@
|
||||
import { z } from '../zod'
|
||||
import { answerInputSchema, answerSchema } from './answer'
|
||||
import { variableWithValueSchema } from './typebot/variable'
|
||||
import { listVariableValue, variableWithValueSchema } from './typebot/variable'
|
||||
import {
|
||||
Result as ResultPrisma,
|
||||
Log as LogPrisma,
|
||||
SetVariableHistoryItem as SetVariableHistoryItemPrisma,
|
||||
VisitedEdge,
|
||||
} from '@typebot.io/prisma'
|
||||
import { InputBlockType } from './blocks/inputs/constants'
|
||||
@ -16,6 +17,7 @@ export const resultSchema = z.object({
|
||||
isCompleted: z.boolean(),
|
||||
hasStarted: z.boolean().nullable(),
|
||||
isArchived: z.boolean().nullable(),
|
||||
lastChatSessionId: z.string().nullable(),
|
||||
}) satisfies z.ZodType<ResultPrisma>
|
||||
|
||||
export const resultWithAnswersSchema = resultSchema.merge(
|
||||
@ -78,3 +80,14 @@ export type CellValueType = { element?: JSX.Element; plainText: string }
|
||||
export type TableData = {
|
||||
id: Pick<CellValueType, 'plainText'>
|
||||
} & Record<string, CellValueType>
|
||||
|
||||
export const setVariableHistoryItemSchema = z.object({
|
||||
resultId: z.string(),
|
||||
index: z.number(),
|
||||
blockId: z.string(),
|
||||
variableId: z.string(),
|
||||
value: z.string().or(listVariableValue).nullable(),
|
||||
}) satisfies z.ZodType<SetVariableHistoryItemPrisma>
|
||||
export type SetVariableHistoryItem = z.infer<
|
||||
typeof setVariableHistoryItemSchema
|
||||
>
|
||||
|
Reference in New Issue
Block a user