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

@ -2,6 +2,7 @@ import { Prisma, PrismaClient } from '@typebot.io/prisma'
import { Block, Typebot } from '@typebot.io/schemas'
import { deleteFilesFromBucket } from '@typebot.io/lib/s3/deleteFilesFromBucket'
import { InputBlockType } from '@typebot.io/schemas/features/blocks/inputs/constants'
import { isDefined } from '@typebot.io/lib'
type ArchiveResultsProps = {
typebot: Pick<Typebot, 'groups'>
@ -42,6 +43,7 @@ export const archiveResults =
},
select: {
id: true,
lastChatSessionId: true,
},
take: batchSize,
})
@ -76,6 +78,30 @@ export const archiveResults =
resultId: { in: resultIds },
},
}),
prisma.answerV2.deleteMany({
where: {
resultId: { in: resultIds },
},
}),
prisma.visitedEdge.deleteMany({
where: {
resultId: { in: resultIds },
},
}),
prisma.setVariableHistoryItem.deleteMany({
where: {
resultId: { in: resultIds },
},
}),
prisma.chatSession.deleteMany({
where: {
id: {
in: resultsToDelete
.map((r) => r.lastChatSessionId)
.filter(isDefined),
},
},
}),
prisma.result.updateMany({
where: {
id: { in: resultIds },

View File

@ -24,11 +24,19 @@ const defaultCellParser: CellParser = (content, blockType) => {
: { plainText: content.toString() }
}
export const convertResultsToTableData = (
results: ResultWithAnswers[] | undefined,
headerCells: ResultHeaderCell[],
cellParser: CellParser = defaultCellParser
): TableData[] =>
type Props = {
results: ResultWithAnswers[] | undefined
headerCells: ResultHeaderCell[]
cellParser?: CellParser
blockIdVariableIdMap: Record<string, string>
}
export const convertResultsToTableData = ({
results,
headerCells,
cellParser = defaultCellParser,
blockIdVariableIdMap,
}: Props): TableData[] =>
(results ?? []).map((result) => ({
id: { plainText: result.id },
date: {
@ -37,23 +45,23 @@ export const convertResultsToTableData = (
...[...result.answers, ...result.variables].reduce<{
[key: string]: { element?: JSX.Element; plainText: string }
}>((tableData, answerOrVariable) => {
if ('groupId' in answerOrVariable) {
const answer = answerOrVariable satisfies Answer
const header = answer.variableId
if ('blockId' in answerOrVariable) {
const answer = answerOrVariable satisfies Pick<
Answer,
'blockId' | 'content'
>
const answerVariableId = blockIdVariableIdMap[answer.blockId]
const header = answerVariableId
? headerCells.find((headerCell) =>
headerCell.variableIds?.includes(answer.variableId as string)
headerCell.variableIds?.includes(answerVariableId)
)
: headerCells.find((headerCell) =>
headerCell.blocks?.some((block) => block.id === answer.blockId)
)
if (!header || !header.blocks || !header.blockType) return tableData
const variableValue = result.variables.find(
(variable) => variable.id === answer.variableId
)?.value
const content = variableValue ?? answer.content
return {
...tableData,
[header.id]: cellParser(content, header.blockType),
[header.id]: cellParser(answer.content, header.blockType),
}
}
const variable = answerOrVariable satisfies VariableWithValue

View File

@ -0,0 +1,19 @@
import { PublicTypebotV6 } from '@typebot.io/schemas'
import { isInputBlock } from '@typebot.io/schemas/helpers'
export const parseBlockIdVariableIdMap = (
groups?: PublicTypebotV6['groups']
): {
[key: string]: string
} => {
if (!groups) return {}
const blockIdVariableIdMap: { [key: string]: string } = {}
groups.forEach((group) => {
group.blocks.forEach((block) => {
if (isInputBlock(block) && block.options?.variableId) {
blockIdVariableIdMap[block.id] = block.options.variableId
}
})
})
return blockIdVariableIdMap
}

View File

@ -35,7 +35,11 @@ export const parseResultHeader = (
{ label: 'Submitted at', id: 'date' },
...inputsResultHeader,
...parseVariablesHeaders(parsedVariables, inputsResultHeader),
...parseResultsFromPreviousBotVersions(results ?? [], inputsResultHeader),
...parseResultsFromPreviousBotVersions({
results: results ?? [],
existingInputResultHeaders: inputsResultHeader,
groups: parsedGroups,
}),
]
}
@ -176,19 +180,22 @@ const parseVariablesHeaders = (
return [...existingHeaders, newHeaderCell]
}, [])
const parseResultsFromPreviousBotVersions = (
results: ResultWithAnswers[],
const parseResultsFromPreviousBotVersions = ({
results,
existingInputResultHeaders,
groups,
}: {
results: ResultWithAnswers[]
existingInputResultHeaders: ResultHeaderCell[]
): ResultHeaderCell[] =>
groups: Group[]
}): ResultHeaderCell[] =>
results
.flatMap((result) => result.answers)
.filter(
(answer) =>
!answer.variableId &&
existingInputResultHeaders.every(
(header) => header.id !== answer.blockId
) &&
isNotEmpty(answer.content)
) && isNotEmpty(answer.content)
)
.reduce<ResultHeaderCell[]>((existingHeaders, answer) => {
if (
@ -197,6 +204,10 @@ const parseResultsFromPreviousBotVersions = (
)
)
return existingHeaders
const groupId =
groups.find((group) =>
group.blocks.some((block) => block.id === answer.blockId)
)?.id ?? ''
return [
...existingHeaders,
{
@ -205,7 +216,7 @@ const parseResultsFromPreviousBotVersions = (
blocks: [
{
id: answer.blockId,
groupId: answer.groupId,
groupId,
},
],
blockType: InputBlockType.TEXT,