🧑💻 (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,
|
||||
})
|
||||
|
||||
@@ -4778,7 +4778,7 @@
|
||||
"/typebots/{typebotId}/results": {
|
||||
"get": {
|
||||
"operationId": "results-getResults",
|
||||
"summary": "List results",
|
||||
"summary": "List results ordered by descending creation date",
|
||||
"tags": [
|
||||
"Results"
|
||||
],
|
||||
@@ -5006,6 +5006,177 @@
|
||||
}
|
||||
}
|
||||
},
|
||||
"/typebots/{typebotId}/results/{resultId}": {
|
||||
"get": {
|
||||
"operationId": "results-getResult",
|
||||
"summary": "Get result by id",
|
||||
"tags": [
|
||||
"Results"
|
||||
],
|
||||
"security": [
|
||||
{
|
||||
"Authorization": []
|
||||
}
|
||||
],
|
||||
"parameters": [
|
||||
{
|
||||
"name": "typebotId",
|
||||
"in": "path",
|
||||
"required": true,
|
||||
"schema": {
|
||||
"type": "string"
|
||||
}
|
||||
},
|
||||
{
|
||||
"name": "resultId",
|
||||
"in": "path",
|
||||
"required": true,
|
||||
"schema": {
|
||||
"type": "string"
|
||||
}
|
||||
}
|
||||
],
|
||||
"responses": {
|
||||
"200": {
|
||||
"description": "Successful response",
|
||||
"content": {
|
||||
"application/json": {
|
||||
"schema": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"result": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"id": {
|
||||
"type": "string"
|
||||
},
|
||||
"createdAt": {
|
||||
"type": "string",
|
||||
"format": "date-time"
|
||||
},
|
||||
"typebotId": {
|
||||
"type": "string"
|
||||
},
|
||||
"variables": {
|
||||
"type": "array",
|
||||
"items": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"id": {
|
||||
"type": "string"
|
||||
},
|
||||
"name": {
|
||||
"type": "string"
|
||||
},
|
||||
"value": {
|
||||
"anyOf": [
|
||||
{
|
||||
"type": "string"
|
||||
},
|
||||
{
|
||||
"type": "array",
|
||||
"items": {
|
||||
"type": "string",
|
||||
"nullable": true
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
},
|
||||
"required": [
|
||||
"id",
|
||||
"name",
|
||||
"value"
|
||||
],
|
||||
"additionalProperties": false
|
||||
}
|
||||
},
|
||||
"isCompleted": {
|
||||
"type": "boolean"
|
||||
},
|
||||
"hasStarted": {
|
||||
"type": "boolean",
|
||||
"nullable": true
|
||||
},
|
||||
"isArchived": {
|
||||
"type": "boolean",
|
||||
"nullable": true
|
||||
},
|
||||
"answers": {
|
||||
"type": "array",
|
||||
"items": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"createdAt": {
|
||||
"type": "string",
|
||||
"format": "date-time"
|
||||
},
|
||||
"resultId": {
|
||||
"type": "string"
|
||||
},
|
||||
"blockId": {
|
||||
"type": "string"
|
||||
},
|
||||
"itemId": {
|
||||
"type": "string",
|
||||
"nullable": true
|
||||
},
|
||||
"groupId": {
|
||||
"type": "string"
|
||||
},
|
||||
"variableId": {
|
||||
"type": "string",
|
||||
"nullable": true
|
||||
},
|
||||
"content": {
|
||||
"type": "string"
|
||||
},
|
||||
"storageUsed": {
|
||||
"type": "number",
|
||||
"nullable": true
|
||||
}
|
||||
},
|
||||
"required": [
|
||||
"createdAt",
|
||||
"resultId",
|
||||
"blockId",
|
||||
"itemId",
|
||||
"groupId",
|
||||
"variableId",
|
||||
"content",
|
||||
"storageUsed"
|
||||
],
|
||||
"additionalProperties": false
|
||||
}
|
||||
}
|
||||
},
|
||||
"required": [
|
||||
"id",
|
||||
"createdAt",
|
||||
"typebotId",
|
||||
"variables",
|
||||
"isCompleted",
|
||||
"hasStarted",
|
||||
"isArchived",
|
||||
"answers"
|
||||
],
|
||||
"additionalProperties": false
|
||||
}
|
||||
},
|
||||
"required": [
|
||||
"result"
|
||||
],
|
||||
"additionalProperties": false
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"default": {
|
||||
"$ref": "#/components/responses/error"
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"/typebots/{typebotId}/results/{resultId}/logs": {
|
||||
"get": {
|
||||
"operationId": "results-getResultLogs",
|
||||
|
||||
Reference in New Issue
Block a user