🔥 Remove disable response saving option

Doesn't work properly when it comes to keep tracking storage usage
This commit is contained in:
Baptiste Arnaud
2023-03-07 14:41:57 +01:00
parent 0c19ea20f8
commit b77e2c8d2c
26 changed files with 194 additions and 182 deletions

View File

@@ -13,7 +13,7 @@ import {
ChatReply,
chatReplySchema,
ChatSession,
Result,
ResultInSession,
sendMessageInputSchema,
SessionState,
StartParams,
@@ -85,7 +85,11 @@ export const sendMessageProcedure = publicProcedure
},
})
if (!input && session.state.result?.hasStarted)
if (
!input &&
session.state.result.answers.length > 0 &&
session.state.result.id
)
await setResultAsCompleted(session.state.result.id)
return {
@@ -106,9 +110,6 @@ const startSession = async (startParams?: StartParams, userId?: string) => {
message: 'No typebot provided in startParams',
})
const isPreview =
startParams?.isPreview || typeof startParams?.typebot !== 'string'
const typebot = await getTypebot(startParams, userId)
const prefilledVariables = startParams.prefilledVariables
@@ -117,8 +118,8 @@ const startSession = async (startParams?: StartParams, userId?: string) => {
const result = await getResult({
...startParams,
isPreview,
typebot: typebot.id,
isPreview: startParams.isPreview || typeof startParams.typebot !== 'string',
typebotId: typebot.id,
prefilledVariables,
isNewResultOnRefreshEnabled:
typebot.settings.general.isNewResultOnRefreshEnabled ?? false,
@@ -140,10 +141,11 @@ const startSession = async (startParams?: StartParams, userId?: string) => {
typebots: [],
queue: [],
},
result: result
? { id: result.id, variables: result.variables, hasStarted: false }
: undefined,
isPreview,
result: {
id: result?.id,
variables: result?.variables ?? [],
answers: result?.answers ?? [],
},
currentTypebotId: typebot.id,
dynamicTheme: parseDynamicThemeInState(typebot.theme),
}
@@ -297,20 +299,21 @@ const getTypebot = async (
}
const getResult = async ({
typebot,
typebotId,
isPreview,
resultId,
prefilledVariables,
isNewResultOnRefreshEnabled,
}: Pick<StartParams, 'isPreview' | 'resultId' | 'typebot'> & {
}: Pick<StartParams, 'isPreview' | 'resultId'> & {
typebotId: string
prefilledVariables: Variable[]
isNewResultOnRefreshEnabled: boolean
}) => {
if (isPreview || typeof typebot !== 'string') return
if (isPreview) return
const select = {
id: true,
variables: true,
hasStarted: true,
answers: { select: { blockId: true, variableId: true, content: true } },
} satisfies Prisma.ResultSelect
const existingResult =
@@ -318,7 +321,7 @@ const getResult = async ({
? ((await prisma.result.findFirst({
where: { id: resultId },
select,
})) as Pick<Result, 'id' | 'variables' | 'hasStarted'>)
})) as ResultInSession)
: undefined
if (existingResult) {
@@ -344,19 +347,19 @@ const getResult = async ({
return {
id: existingResult.id,
variables: updatedResult.variables,
hasStarted: existingResult.hasStarted,
answers: existingResult.answers,
}
} else {
return (await prisma.result.create({
data: {
isCompleted: false,
typebotId: typebot,
typebotId,
variables: prefilledVariables.filter((variable) =>
isDefined(variable.value)
),
},
select,
})) as Pick<Result, 'id' | 'variables' | 'hasStarted'>
})) as ResultInSession
}
}

View File

@@ -7,6 +7,7 @@ import { validateUrl } from '@/features/blocks/inputs/url/api'
import { parseVariables, updateVariables } from '@/features/variables'
import prisma from '@/lib/prisma'
import { TRPCError } from '@trpc/server'
import { Prisma } from 'db'
import got from 'got'
import {
Block,
@@ -15,6 +16,7 @@ import {
ChatReply,
InputBlock,
InputBlockType,
ResultInSession,
SessionState,
} from 'models'
import { isInputBlock, isNotDefined } from 'utils'
@@ -86,17 +88,9 @@ const processAndSaveAnswer =
(state: SessionState, block: InputBlock) =>
async (reply: string | null): Promise<SessionState> => {
if (!reply) return state
if (!state.isPreview && state.result) {
await saveAnswer(state.result.id, block)(reply)
if (!state.result.hasStarted) await setResultAsStarted(state.result.id)
}
const newState = await saveVariableValueIfAny(state, block)(reply)
return {
...newState,
result: newState.result
? { ...newState.result, hasStarted: true }
: undefined,
}
let newState = await saveAnswer(state, block)(reply)
newState = await saveVariableValueIfAny(newState, block)(reply)
return newState
}
const saveVariableValueIfAny =
@@ -115,13 +109,6 @@ const saveVariableValueIfAny =
return newSessionState
}
const setResultAsStarted = async (resultId: string) => {
await prisma.result.update({
where: { id: resultId },
data: { hasStarted: true },
})
}
export const setResultAsCompleted = async (resultId: string) => {
await prisma.result.update({
where: { id: resultId },
@@ -152,31 +139,65 @@ const parseRetryMessage = (
}
const saveAnswer =
(resultId: string, block: InputBlock) => async (reply: string) => {
(state: SessionState, block: InputBlock) =>
async (reply: string): Promise<SessionState> => {
const resultId = state.result?.id
const answer = {
resultId: resultId,
resultId,
blockId: block.id,
groupId: block.groupId,
content: reply,
variableId: block.options.variableId,
storageUsed: 0,
}
if (state.result.answers.length === 0 && state.result.id)
await setResultAsStarted(state.result.id)
const newSessionState = setNewAnswerInState(state)({
blockId: block.id,
variableId: block.options.variableId ?? null,
content: reply,
})
if (reply.includes('http') && block.type === InputBlockType.FILE) {
answer.storageUsed = await computeStorageUsed(reply)
}
await prisma.answer.upsert({
where: {
resultId_blockId_groupId: {
resultId,
groupId: block.groupId,
blockId: block.id,
if (resultId)
await prisma.answer.upsert({
where: {
resultId_blockId_groupId: {
resultId,
groupId: block.groupId,
blockId: block.id,
},
},
create: answer as Prisma.AnswerUncheckedCreateInput,
update: answer,
})
return newSessionState
}
const setResultAsStarted = async (resultId: string) => {
await prisma.result.update({
where: { id: resultId },
data: { hasStarted: true },
})
}
const setNewAnswerInState =
(state: SessionState) => (newAnswer: ResultInSession['answers'][number]) => {
const newAnswers = state.result.answers
.filter((answer) => answer.blockId !== newAnswer.blockId)
.concat(newAnswer)
return {
...state,
result: {
...state.result,
answers: newAnswers,
},
create: answer,
update: answer,
})
} satisfies SessionState
}
const computeStorageUsed = async (reply: string) => {

View File

@@ -113,7 +113,7 @@ export const executeGroup =
}
const computeRuntimeOptions =
(state: Pick<SessionState, 'isPreview' | 'typebot'>) =>
(state: Pick<SessionState, 'result' | 'typebot'>) =>
(block: InputBlock): Promise<RuntimeOptions> | undefined => {
switch (block.type) {
case InputBlockType.PAYMENT: {
@@ -158,7 +158,7 @@ const parseBubbleBlock =
}
const injectVariablesValueInBlock =
(state: Pick<SessionState, 'isPreview' | 'typebot'>) =>
(state: Pick<SessionState, 'result' | 'typebot'>) =>
async (block: InputBlock): Promise<ChatReply['input']> => {
switch (block.type) {
case InputBlockType.CHOICE: {