(api) Add CRUD typebot endpoints

Closes #320, closes #696
This commit is contained in:
Baptiste Arnaud
2023-08-17 09:39:11 +02:00
parent 019f72ac7e
commit 454d320c6b
78 changed files with 25014 additions and 1073 deletions

View File

@@ -0,0 +1,46 @@
import prisma from '@/lib/prisma'
import { authenticatedProcedure } from '@/helpers/server/trpc'
import { TRPCError } from '@trpc/server'
import { z } from 'zod'
import { collaboratorSchema } from '@typebot.io/schemas/features/collaborators'
import { isReadTypebotForbidden } from '@/features/typebot/helpers/isReadTypebotForbidden'
export const getCollaborators = authenticatedProcedure
.meta({
openapi: {
method: 'GET',
path: '/typebots/{typebotId}/collaborators',
protect: true,
summary: 'Get collaborators',
tags: ['Collaborators'],
},
})
.input(
z.object({
typebotId: z.string(),
})
)
.output(
z.object({
collaborators: z.array(collaboratorSchema),
})
)
.query(async ({ input: { typebotId }, ctx: { user } }) => {
const existingTypebot = await prisma.typebot.findFirst({
where: {
id: typebotId,
},
include: {
collaborators: true,
},
})
if (
!existingTypebot?.id ||
(await isReadTypebotForbidden(existingTypebot, user))
)
throw new TRPCError({ code: 'NOT_FOUND', message: 'Typebot not found' })
return {
collaborators: existingTypebot.collaborators,
}
})

View File

@@ -0,0 +1,6 @@
import { router } from '@/helpers/server/trpc'
import { getCollaborators } from './getCollaborators'
export const collaboratorsRouter = router({
getCollaborators: getCollaborators,
})