🗃️ 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 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,
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
|
|
||||||
|
@ -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,
|
||||||
|
@ -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 } },
|
||||||
},
|
},
|
||||||
|
Reference in New Issue
Block a user