🧑💻 (results) Add get result by id API endpoint
This commit is contained in:
52
apps/builder/src/features/results/api/getResult.ts
Normal file
52
apps/builder/src/features/results/api/getResult.ts
Normal 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] }
|
||||
})
|
||||
@@ -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'],
|
||||
},
|
||||
})
|
||||
|
||||
@@ -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,
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user