Closes #1039 <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit - **New Features** - Workspaces now include additional status indicator: `isPastDue`. - New pages for handling workspaces that are past due. Meaning, an invoice is unpaid. - **Bug Fixes** - Fixed issues with workspace status checks and redirections for suspended workspaces. - **Refactor** - Refactored workspace-related API functions to accommodate new status fields. - Improved permission checks for reading and writing typebots based on workspace status. - **Chores** - Database schema updated to include `isPastDue` field for workspaces. - Implemented new webhook event handling for subscription and invoice updates. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
74 lines
1.8 KiB
TypeScript
74 lines
1.8 KiB
TypeScript
import prisma from '@typebot.io/lib/prisma'
|
|
import { authenticatedProcedure } from '@/helpers/server/trpc'
|
|
import { TRPCError } from '@trpc/server'
|
|
import { resultWithAnswersSchema } from '@typebot.io/schemas'
|
|
import { z } from 'zod'
|
|
import { isReadTypebotForbidden } from '@/features/typebot/helpers/isReadTypebotForbidden'
|
|
|
|
export const getResult = authenticatedProcedure
|
|
.meta({
|
|
openapi: {
|
|
method: 'GET',
|
|
path: '/typebots/{typebotId}/results/{resultId}',
|
|
protect: true,
|
|
summary: 'Get result by id',
|
|
tags: ['Results'],
|
|
},
|
|
})
|
|
.input(
|
|
z.object({
|
|
typebotId: z.string(),
|
|
resultId: z.string(),
|
|
})
|
|
)
|
|
.output(
|
|
z.object({
|
|
result: resultWithAnswersSchema,
|
|
})
|
|
)
|
|
.query(async ({ input, ctx: { user } }) => {
|
|
const typebot = await prisma.typebot.findUnique({
|
|
where: {
|
|
id: input.typebotId,
|
|
},
|
|
select: {
|
|
id: true,
|
|
groups: true,
|
|
workspace: {
|
|
select: {
|
|
isQuarantined: true,
|
|
isPastDue: true,
|
|
members: {
|
|
select: {
|
|
userId: true,
|
|
},
|
|
},
|
|
},
|
|
},
|
|
collaborators: {
|
|
select: {
|
|
userId: true,
|
|
type: true,
|
|
},
|
|
},
|
|
},
|
|
})
|
|
if (!typebot || (await isReadTypebotForbidden(typebot, user)))
|
|
throw new TRPCError({ code: 'NOT_FOUND', message: 'Typebot not found' })
|
|
const results = await prisma.result.findMany({
|
|
where: {
|
|
id: input.resultId,
|
|
typebotId: typebot.id,
|
|
},
|
|
orderBy: {
|
|
createdAt: 'desc',
|
|
},
|
|
include: { answers: true },
|
|
})
|
|
|
|
if (results.length === 0)
|
|
throw new TRPCError({ code: 'NOT_FOUND', message: 'Result not found' })
|
|
|
|
return { result: resultWithAnswersSchema.parse(results[0]) }
|
|
})
|