@@ -1,8 +1,8 @@
|
||||
import { useToast } from '@/hooks/useToast'
|
||||
import {
|
||||
LogicBlockType,
|
||||
ResultHeaderCell,
|
||||
ResultWithAnswers,
|
||||
Typebot,
|
||||
} from '@typebot.io/schemas'
|
||||
import { createContext, ReactNode, useContext, useMemo } from 'react'
|
||||
import { parseResultHeader } from '@typebot.io/lib/results'
|
||||
@@ -12,6 +12,7 @@ import { TableData } from './types'
|
||||
import { convertResultsToTableData } from './helpers/convertResultsToTableData'
|
||||
import { trpc } from '@/lib/trpc'
|
||||
import { isDefined } from '@typebot.io/lib/utils'
|
||||
import { LogicBlockType } from '@typebot.io/schemas/features/blocks/logic/constants'
|
||||
|
||||
const resultsContext = createContext<{
|
||||
resultsList: { results: ResultWithAnswers[] }[] | undefined
|
||||
@@ -50,16 +51,15 @@ export const ResultsProvider = ({
|
||||
const linkedTypebotIds =
|
||||
publishedTypebot?.groups
|
||||
.flatMap((group) => group.blocks)
|
||||
.reduce<string[]>(
|
||||
(typebotIds, block) =>
|
||||
block.type === LogicBlockType.TYPEBOT_LINK &&
|
||||
isDefined(block.options.typebotId) &&
|
||||
!typebotIds.includes(block.options.typebotId) &&
|
||||
block.options.mergeResults !== false
|
||||
? [...typebotIds, block.options.typebotId]
|
||||
: typebotIds,
|
||||
[]
|
||||
) ?? []
|
||||
.reduce<string[]>((typebotIds, block) => {
|
||||
if (block.type !== LogicBlockType.TYPEBOT_LINK) return typebotIds
|
||||
const typebotId = block.options?.typebotId
|
||||
return isDefined(typebotId) &&
|
||||
!typebotIds.includes(typebotId) &&
|
||||
block.options?.mergeResults !== false
|
||||
? [...typebotIds, typebotId]
|
||||
: typebotIds
|
||||
}, []) ?? []
|
||||
|
||||
const { data: linkedTypebotsData } = trpc.getLinkedTypebots.useQuery(
|
||||
{
|
||||
@@ -78,7 +78,13 @@ export const ResultsProvider = ({
|
||||
const resultHeader = useMemo(
|
||||
() =>
|
||||
publishedTypebot
|
||||
? parseResultHeader(publishedTypebot, linkedTypebotsData?.typebots)
|
||||
? parseResultHeader(
|
||||
publishedTypebot,
|
||||
linkedTypebotsData?.typebots as Pick<
|
||||
Typebot,
|
||||
'groups' | 'variables'
|
||||
>[]
|
||||
)
|
||||
: [],
|
||||
[linkedTypebotsData?.typebots, publishedTypebot]
|
||||
)
|
||||
|
||||
@@ -1,10 +1,10 @@
|
||||
import { authenticatedProcedure } from '@/helpers/server/trpc'
|
||||
import { TRPCError } from '@trpc/server'
|
||||
import { Group } from '@typebot.io/schemas'
|
||||
import { z } from 'zod'
|
||||
import { archiveResults } from '@typebot.io/lib/api/helpers/archiveResults'
|
||||
import prisma from '@typebot.io/lib/prisma'
|
||||
import { isWriteTypebotForbidden } from '@/features/typebot/helpers/isWriteTypebotForbidden'
|
||||
import { Typebot } from '@typebot.io/schemas'
|
||||
|
||||
export const deleteResults = authenticatedProcedure
|
||||
.meta({
|
||||
@@ -50,8 +50,8 @@ export const deleteResults = authenticatedProcedure
|
||||
throw new TRPCError({ code: 'NOT_FOUND', message: 'Typebot not found' })
|
||||
const { success } = await archiveResults(prisma)({
|
||||
typebot: {
|
||||
groups: typebot.groups as Group[],
|
||||
},
|
||||
groups: typebot.groups,
|
||||
} as Pick<Typebot, 'groups'>,
|
||||
resultsFilter: {
|
||||
id: (idsArray?.length ?? 0) > 0 ? { in: idsArray } : undefined,
|
||||
typebotId,
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import prisma from '@typebot.io/lib/prisma'
|
||||
import { authenticatedProcedure } from '@/helpers/server/trpc'
|
||||
import { TRPCError } from '@trpc/server'
|
||||
import { ResultWithAnswers, resultWithAnswersSchema } from '@typebot.io/schemas'
|
||||
import { resultWithAnswersSchema } from '@typebot.io/schemas'
|
||||
import { z } from 'zod'
|
||||
import { isReadTypebotForbidden } from '@/features/typebot/helpers/isReadTypebotForbidden'
|
||||
|
||||
@@ -45,7 +45,7 @@ export const getResult = authenticatedProcedure
|
||||
})
|
||||
if (!typebot || (await isReadTypebotForbidden(typebot, user)))
|
||||
throw new TRPCError({ code: 'NOT_FOUND', message: 'Typebot not found' })
|
||||
const results = (await prisma.result.findMany({
|
||||
const results = await prisma.result.findMany({
|
||||
where: {
|
||||
id: input.resultId,
|
||||
typebotId: typebot.id,
|
||||
@@ -54,10 +54,10 @@ export const getResult = authenticatedProcedure
|
||||
createdAt: 'desc',
|
||||
},
|
||||
include: { answers: true },
|
||||
})) as ResultWithAnswers[]
|
||||
})
|
||||
|
||||
if (results.length === 0)
|
||||
throw new TRPCError({ code: 'NOT_FOUND', message: 'Result not found' })
|
||||
|
||||
return { result: results[0] }
|
||||
return { result: resultWithAnswersSchema.parse(results[0]) }
|
||||
})
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import prisma from '@typebot.io/lib/prisma'
|
||||
import { authenticatedProcedure } from '@/helpers/server/trpc'
|
||||
import { TRPCError } from '@trpc/server'
|
||||
import { ResultWithAnswers, resultWithAnswersSchema } from '@typebot.io/schemas'
|
||||
import { resultWithAnswersSchema } from '@typebot.io/schemas'
|
||||
import { z } from 'zod'
|
||||
import { isReadTypebotForbidden } from '@/features/typebot/helpers/isReadTypebotForbidden'
|
||||
|
||||
@@ -56,7 +56,7 @@ export const getResults = authenticatedProcedure
|
||||
})
|
||||
if (!typebot || (await isReadTypebotForbidden(typebot, user)))
|
||||
throw new TRPCError({ code: 'NOT_FOUND', message: 'Typebot not found' })
|
||||
const results = (await prisma.result.findMany({
|
||||
const results = await prisma.result.findMany({
|
||||
take: limit + 1,
|
||||
cursor: cursor ? { id: cursor } : undefined,
|
||||
where: {
|
||||
@@ -68,7 +68,7 @@ export const getResults = authenticatedProcedure
|
||||
createdAt: 'desc',
|
||||
},
|
||||
include: { answers: true },
|
||||
})) as ResultWithAnswers[]
|
||||
})
|
||||
|
||||
let nextCursor: typeof cursor | undefined
|
||||
if (results.length > limit) {
|
||||
@@ -76,5 +76,8 @@ export const getResults = authenticatedProcedure
|
||||
nextCursor = nextResult?.id
|
||||
}
|
||||
|
||||
return { results, nextCursor }
|
||||
return {
|
||||
results: z.array(resultWithAnswersSchema).parse(results),
|
||||
nextCursor,
|
||||
}
|
||||
})
|
||||
|
||||
@@ -23,6 +23,7 @@ import { useResults } from '../../ResultsProvider'
|
||||
import { parseColumnOrder } from '../../helpers/parseColumnsOrder'
|
||||
import { convertResultsToTableData } from '../../helpers/convertResultsToTableData'
|
||||
import { byId, isDefined } from '@typebot.io/lib'
|
||||
import { Typebot } from '@typebot.io/schemas'
|
||||
|
||||
type Props = {
|
||||
isOpen: boolean
|
||||
@@ -82,7 +83,10 @@ export const ExportAllResultsModal = ({ isOpen, onClose }: Props) => {
|
||||
const resultHeader = areDeletedBlocksIncluded
|
||||
? parseResultHeader(
|
||||
publishedTypebot,
|
||||
linkedTypebotsData?.typebots,
|
||||
linkedTypebotsData?.typebots as Pick<
|
||||
Typebot,
|
||||
'groups' | 'variables'
|
||||
>[],
|
||||
results
|
||||
)
|
||||
: existingResultHeader
|
||||
|
||||
@@ -4,12 +4,12 @@ import {
|
||||
ResultWithAnswers,
|
||||
ResultHeaderCell,
|
||||
VariableWithValue,
|
||||
InputBlockType,
|
||||
Answer,
|
||||
} from '@typebot.io/schemas'
|
||||
import { FileLinks } from '../components/FileLinks'
|
||||
import { TableData } from '../types'
|
||||
import { convertDateToReadable } from './convertDateToReadable'
|
||||
import { InputBlockType } from '@typebot.io/schemas/features/blocks/inputs/constants'
|
||||
|
||||
export const convertResultsToTableData = (
|
||||
results: ResultWithAnswers[] | undefined,
|
||||
|
||||
Reference in New Issue
Block a user