🗃️ Improve result logs query
This commit is contained in:
@ -1,5 +1,5 @@
|
||||
import { getTypebot } from '@/features/typebot/api/utils/getTypebot'
|
||||
import prisma from '@/lib/prisma'
|
||||
import { canReadTypebots } from '@/utils/api/dbRules'
|
||||
import { authenticatedProcedure } from '@/utils/server/trpc'
|
||||
import { logSchema } from 'models'
|
||||
import { z } from 'zod'
|
||||
@ -22,14 +22,15 @@ export const getResultLogsProcedure = authenticatedProcedure
|
||||
)
|
||||
.output(z.object({ logs: z.array(logSchema) }))
|
||||
.query(async ({ input: { typebotId, resultId }, ctx: { user } }) => {
|
||||
const typebot = await prisma.typebot.findFirst({
|
||||
where: canReadTypebots(typebotId, user),
|
||||
select: { id: true },
|
||||
const typebot = await getTypebot({
|
||||
accessLevel: 'read',
|
||||
user,
|
||||
typebotId,
|
||||
})
|
||||
if (!typebot) throw new Error('Typebot not found')
|
||||
const logs = await prisma.log.findMany({
|
||||
where: {
|
||||
result: { id: resultId, typebotId: typebot.id },
|
||||
resultId,
|
||||
},
|
||||
})
|
||||
|
||||
|
@ -1,5 +1,5 @@
|
||||
import { getTypebot } from '@/features/typebot/api/utils/getTypebot'
|
||||
import prisma from '@/lib/prisma'
|
||||
import { canReadTypebots } from '@/utils/api/dbRules'
|
||||
import { authenticatedProcedure } from '@/utils/server/trpc'
|
||||
import { TRPCError } from '@trpc/server'
|
||||
import { ResultWithAnswers, resultWithAnswersSchema } from 'models'
|
||||
@ -38,11 +38,12 @@ export const getResultsProcedure = authenticatedProcedure
|
||||
message: 'limit must be between 1 and 200',
|
||||
})
|
||||
const { cursor } = input
|
||||
const typebot = await prisma.typebot.findFirst({
|
||||
where: canReadTypebots(input.typebotId, user),
|
||||
select: { id: true },
|
||||
const typebot = await getTypebot({
|
||||
accessLevel: 'read',
|
||||
user,
|
||||
typebotId: input.typebotId,
|
||||
})
|
||||
if (!typebot)
|
||||
if (!typebot?.id)
|
||||
throw new TRPCError({ code: 'NOT_FOUND', message: 'Typebot not found' })
|
||||
const results = (await prisma.result.findMany({
|
||||
take: limit + 1,
|
||||
|
@ -22,6 +22,7 @@ export const getTypebot = async <T extends Prisma.TypebotSelect>({
|
||||
},
|
||||
select: {
|
||||
...select,
|
||||
id: true,
|
||||
workspaceId: true,
|
||||
collaborators: { select: { userId: true, type: true } },
|
||||
},
|
||||
|
Reference in New Issue
Block a user