2
0

🗃️ Improve result logs query

This commit is contained in:
Baptiste Arnaud
2023-02-14 08:13:47 +01:00
parent 2c80e3a1c0
commit 1a3596b15c
3 changed files with 13 additions and 10 deletions

View File

@ -1,5 +1,5 @@
import { getTypebot } from '@/features/typebot/api/utils/getTypebot'
import prisma from '@/lib/prisma' import prisma from '@/lib/prisma'
import { canReadTypebots } from '@/utils/api/dbRules'
import { authenticatedProcedure } from '@/utils/server/trpc' import { authenticatedProcedure } from '@/utils/server/trpc'
import { logSchema } from 'models' import { logSchema } from 'models'
import { z } from 'zod' import { z } from 'zod'
@ -22,14 +22,15 @@ export const getResultLogsProcedure = authenticatedProcedure
) )
.output(z.object({ logs: z.array(logSchema) })) .output(z.object({ logs: z.array(logSchema) }))
.query(async ({ input: { typebotId, resultId }, ctx: { user } }) => { .query(async ({ input: { typebotId, resultId }, ctx: { user } }) => {
const typebot = await prisma.typebot.findFirst({ const typebot = await getTypebot({
where: canReadTypebots(typebotId, user), accessLevel: 'read',
select: { id: true }, user,
typebotId,
}) })
if (!typebot) throw new Error('Typebot not found') if (!typebot) throw new Error('Typebot not found')
const logs = await prisma.log.findMany({ const logs = await prisma.log.findMany({
where: { where: {
result: { id: resultId, typebotId: typebot.id }, resultId,
}, },
}) })

View File

@ -1,5 +1,5 @@
import { getTypebot } from '@/features/typebot/api/utils/getTypebot'
import prisma from '@/lib/prisma' import prisma from '@/lib/prisma'
import { canReadTypebots } from '@/utils/api/dbRules'
import { authenticatedProcedure } from '@/utils/server/trpc' import { authenticatedProcedure } from '@/utils/server/trpc'
import { TRPCError } from '@trpc/server' import { TRPCError } from '@trpc/server'
import { ResultWithAnswers, resultWithAnswersSchema } from 'models' import { ResultWithAnswers, resultWithAnswersSchema } from 'models'
@ -38,11 +38,12 @@ export const getResultsProcedure = authenticatedProcedure
message: 'limit must be between 1 and 200', message: 'limit must be between 1 and 200',
}) })
const { cursor } = input const { cursor } = input
const typebot = await prisma.typebot.findFirst({ const typebot = await getTypebot({
where: canReadTypebots(input.typebotId, user), accessLevel: 'read',
select: { id: true }, user,
typebotId: input.typebotId,
}) })
if (!typebot) if (!typebot?.id)
throw new TRPCError({ code: 'NOT_FOUND', message: 'Typebot not found' }) throw new TRPCError({ code: 'NOT_FOUND', message: 'Typebot not found' })
const results = (await prisma.result.findMany({ const results = (await prisma.result.findMany({
take: limit + 1, take: limit + 1,

View File

@ -22,6 +22,7 @@ export const getTypebot = async <T extends Prisma.TypebotSelect>({
}, },
select: { select: {
...select, ...select,
id: true,
workspaceId: true, workspaceId: true,
collaborators: { select: { userId: true, type: true } }, collaborators: { select: { userId: true, type: true } },
}, },