2
0

🧑‍💻 (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,
})

View File

@@ -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",