(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

@@ -1,10 +1,10 @@
import { getTypebot } from '@/features/typebot/helpers/getTypebot'
import { authenticatedProcedure } from '@/helpers/server/trpc'
import { TRPCError } from '@trpc/server'
import { Typebot } from '@typebot.io/schemas'
import { Group } from '@typebot.io/schemas'
import { z } from 'zod'
import { archiveResults } from '@typebot.io/lib/api/helpers/archiveResults'
import prisma from '@/lib/prisma'
import { isWriteTypebotForbidden } from '@/features/typebot/helpers/isWriteTypebotForbidden'
export const deleteResults = authenticatedProcedure
.meta({
@@ -31,18 +31,27 @@ export const deleteResults = authenticatedProcedure
.mutation(async ({ input, ctx: { user } }) => {
const idsArray = input.resultIds?.split(',')
const { typebotId } = input
const typebot = (await getTypebot({
accessLevel: 'write',
typebotId,
user,
select: {
groups: true,
const typebot = await prisma.typebot.findUnique({
where: {
id: typebotId,
},
})) as Pick<Typebot, 'groups'> | null
if (!typebot)
select: {
workspaceId: true,
groups: true,
collaborators: {
select: {
userId: true,
type: true,
},
},
},
})
if (!typebot || (await isWriteTypebotForbidden(typebot, user)))
throw new TRPCError({ code: 'NOT_FOUND', message: 'Typebot not found' })
const { success } = await archiveResults(prisma)({
typebot,
typebot: {
groups: typebot.groups as Group[],
},
resultsFilter: {
id: (idsArray?.length ?? 0) > 0 ? { in: idsArray } : undefined,
typebotId,

View File

@@ -1,9 +1,9 @@
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'
import { isReadTypebotForbidden } from '@/features/typebot/helpers/isReadTypebotForbidden'
export const getResult = authenticatedProcedure
.meta({
@@ -27,12 +27,23 @@ export const getResult = authenticatedProcedure
})
)
.query(async ({ input, ctx: { user } }) => {
const typebot = await getTypebot({
accessLevel: 'read',
user,
typebotId: input.typebotId,
const typebot = await prisma.typebot.findUnique({
where: {
id: input.typebotId,
},
select: {
id: true,
workspaceId: true,
groups: true,
collaborators: {
select: {
userId: true,
type: true,
},
},
},
})
if (!typebot?.id)
if (!typebot || (await isReadTypebotForbidden(typebot, user)))
throw new TRPCError({ code: 'NOT_FOUND', message: 'Typebot not found' })
const results = (await prisma.result.findMany({
where: {

View File

@@ -1,8 +1,8 @@
import { getTypebot } from '@/features/typebot/helpers/getTypebot'
import prisma from '@/lib/prisma'
import { authenticatedProcedure } from '@/helpers/server/trpc'
import { logSchema } from '@typebot.io/schemas'
import { z } from 'zod'
import { isReadTypebotForbidden } from '@/features/typebot/helpers/isReadTypebotForbidden'
export const getResultLogs = authenticatedProcedure
.meta({
@@ -22,12 +22,24 @@ export const getResultLogs = authenticatedProcedure
)
.output(z.object({ logs: z.array(logSchema) }))
.query(async ({ input: { typebotId, resultId }, ctx: { user } }) => {
const typebot = await getTypebot({
accessLevel: 'read',
user,
typebotId,
const typebot = await prisma.typebot.findUnique({
where: {
id: typebotId,
},
select: {
id: true,
workspaceId: true,
groups: true,
collaborators: {
select: {
userId: true,
type: true,
},
},
},
})
if (!typebot) throw new Error('Typebot not found')
if (!typebot || (await isReadTypebotForbidden(typebot, user)))
throw new Error('Typebot not found')
const logs = await prisma.log.findMany({
where: {
resultId,

View File

@@ -1,9 +1,9 @@
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'
import { isReadTypebotForbidden } from '@/features/typebot/helpers/isReadTypebotForbidden'
const maxLimit = 200
@@ -38,12 +38,23 @@ export const getResults = authenticatedProcedure
message: 'limit must be between 1 and 200',
})
const { cursor } = input
const typebot = await getTypebot({
accessLevel: 'read',
user,
typebotId: input.typebotId,
const typebot = await prisma.typebot.findUnique({
where: {
id: input.typebotId,
},
select: {
id: true,
workspaceId: true,
groups: true,
collaborators: {
select: {
userId: true,
type: true,
},
},
},
})
if (!typebot?.id)
if (!typebot || (await isReadTypebotForbidden(typebot, user)))
throw new TRPCError({ code: 'NOT_FOUND', message: 'Typebot not found' })
const results = (await prisma.result.findMany({
take: limit + 1,