🧑‍💻 (results) Add get result by id API endpoint

This commit is contained in:
Baptiste Arnaud
2023-07-24 07:42:30 +02:00
parent 1ebd528753
commit 3283d7e261
4 changed files with 227 additions and 2 deletions

View File

@@ -0,0 +1,52 @@
import { getTypebot } from '@/features/typebot/helpers/getTypebot'
import prisma from '@/lib/prisma'
import { authenticatedProcedure } from '@/helpers/server/trpc'
import { TRPCError } from '@trpc/server'
import { ResultWithAnswers, resultWithAnswersSchema } from '@typebot.io/schemas'
import { z } from 'zod'
export const getResult = authenticatedProcedure
.meta({
openapi: {
method: 'GET',
path: '/typebots/{typebotId}/results/{resultId}',
protect: true,
summary: 'Get result by id',
tags: ['Results'],
},
})
.input(
z.object({
typebotId: z.string(),
resultId: z.string(),
})
)
.output(
z.object({
result: resultWithAnswersSchema,
})
)
.query(async ({ input, ctx: { user } }) => {
const typebot = await getTypebot({
accessLevel: 'read',
user,
typebotId: input.typebotId,
})
if (!typebot?.id)
throw new TRPCError({ code: 'NOT_FOUND', message: 'Typebot not found' })
const results = (await prisma.result.findMany({
where: {
id: input.resultId,
typebotId: typebot.id,
},
orderBy: {
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] }
})

View File

@@ -13,7 +13,7 @@ export const getResults = authenticatedProcedure
method: 'GET',
path: '/typebots/{typebotId}/results',
protect: true,
summary: 'List results',
summary: 'List results ordered by descending creation date',
tags: ['Results'],
},
})

View File

@@ -2,9 +2,11 @@ import { router } from '@/helpers/server/trpc'
import { deleteResults } from './deleteResults'
import { getResultLogs } from './getResultLogs'
import { getResults } from './getResults'
import { getResult } from './getResult'
export const resultsRouter = router({
getResults,
getResult,
deleteResults,
getResultLogs,
})