🗃️ (results) Improve result delete queries
This commit is contained in:
@ -1,24 +1,16 @@
|
||||
import prisma from '@/lib/prisma'
|
||||
import { canWriteTypebots } from '@/utils/api/dbRules'
|
||||
import { deleteFiles } from '@/utils/api/storage'
|
||||
import { User, Prisma } from 'db'
|
||||
import { Prisma } from 'db'
|
||||
import { InputBlockType, Typebot } from 'models'
|
||||
|
||||
export const archiveResults = async ({
|
||||
typebotId,
|
||||
user,
|
||||
typebot,
|
||||
resultsFilter,
|
||||
}: {
|
||||
typebotId: string
|
||||
user: User
|
||||
typebot: Pick<Typebot, 'groups'>
|
||||
resultsFilter?: Prisma.ResultWhereInput
|
||||
}) => {
|
||||
const typebot = await prisma.typebot.findFirst({
|
||||
where: canWriteTypebots(typebotId, user),
|
||||
select: { groups: true },
|
||||
})
|
||||
if (!typebot) return { success: false }
|
||||
const fileUploadBlockIds = (typebot as Typebot).groups
|
||||
const fileUploadBlockIds = typebot.groups
|
||||
.flatMap((g) => g.blocks)
|
||||
.filter((b) => b.type === InputBlockType.FILE)
|
||||
.map((b) => b.id)
|
||||
|
@ -1,6 +1,7 @@
|
||||
import { canWriteTypebots } from '@/utils/api/dbRules'
|
||||
import { getTypebot } from '@/features/typebot/api/utils/getTypebot'
|
||||
import { authenticatedProcedure } from '@/utils/server/trpc'
|
||||
import { TRPCError } from '@trpc/server'
|
||||
import { Typebot } from 'models'
|
||||
import { z } from 'zod'
|
||||
import { archiveResults } from '../archiveResults'
|
||||
|
||||
@ -29,12 +30,20 @@ export const deleteResultsProcedure = authenticatedProcedure
|
||||
.mutation(async ({ input, ctx: { user } }) => {
|
||||
const idsArray = input.resultIds?.split(',')
|
||||
const { typebotId } = input
|
||||
const { success } = await archiveResults({
|
||||
const typebot = (await getTypebot({
|
||||
accessLevel: 'write',
|
||||
typebotId,
|
||||
user,
|
||||
select: {
|
||||
groups: true,
|
||||
},
|
||||
})) as Pick<Typebot, 'groups'> | null
|
||||
if (!typebot)
|
||||
throw new TRPCError({ code: 'NOT_FOUND', message: 'Typebot not found' })
|
||||
const { success } = await archiveResults({
|
||||
typebot,
|
||||
resultsFilter: {
|
||||
id: (idsArray?.length ?? 0) > 0 ? { in: idsArray } : undefined,
|
||||
typebot: canWriteTypebots(typebotId, user),
|
||||
},
|
||||
})
|
||||
|
||||
|
35
apps/builder/src/features/typebot/api/utils/getTypebot.ts
Normal file
35
apps/builder/src/features/typebot/api/utils/getTypebot.ts
Normal file
@ -0,0 +1,35 @@
|
||||
import prisma from '@/lib/prisma'
|
||||
import { Prisma, User } from 'db'
|
||||
import { isReadTypebotForbidden } from './isReadTypebotForbidden'
|
||||
import { isWriteTypebotForbidden } from './isWriteTypebotForbidden'
|
||||
|
||||
type Props<T extends Prisma.TypebotSelect> = {
|
||||
typebotId: string
|
||||
user: Pick<User, 'id' | 'email'>
|
||||
accessLevel: 'read' | 'write'
|
||||
select?: T
|
||||
}
|
||||
|
||||
export const getTypebot = async <T extends Prisma.TypebotSelect>({
|
||||
typebotId,
|
||||
user,
|
||||
accessLevel,
|
||||
select,
|
||||
}: Props<T>) => {
|
||||
const typebot = await prisma.typebot.findFirst({
|
||||
where: {
|
||||
id: typebotId,
|
||||
},
|
||||
select: {
|
||||
...select,
|
||||
workspaceId: true,
|
||||
collaborators: { select: { userId: true, type: true } },
|
||||
},
|
||||
})
|
||||
if (!typebot) return null
|
||||
if (accessLevel === 'read' && (await isReadTypebotForbidden(typebot, user)))
|
||||
return null
|
||||
if (accessLevel === 'write' && (await isWriteTypebotForbidden(typebot, user)))
|
||||
return null
|
||||
return typebot
|
||||
}
|
@ -0,0 +1,26 @@
|
||||
import prisma from '@/lib/prisma'
|
||||
import { CollaboratorsOnTypebots, User } from 'db'
|
||||
import { Typebot } from 'models'
|
||||
import { isNotDefined } from 'utils'
|
||||
|
||||
export const isReadTypebotForbidden = async (
|
||||
typebot: Pick<Typebot, 'workspaceId'> & {
|
||||
collaborators: Pick<CollaboratorsOnTypebots, 'userId' | 'type'>[]
|
||||
},
|
||||
user: Pick<User, 'email' | 'id'>
|
||||
) => {
|
||||
if (
|
||||
process.env.ADMIN_EMAIL === user.email ||
|
||||
typebot.collaborators.find(
|
||||
(collaborator) => collaborator.userId === user.id
|
||||
)
|
||||
)
|
||||
return false
|
||||
const memberInWorkspace = await prisma.memberInWorkspace.findFirst({
|
||||
where: {
|
||||
workspaceId: typebot.workspaceId,
|
||||
userId: user.id,
|
||||
},
|
||||
})
|
||||
return isNotDefined(memberInWorkspace)
|
||||
}
|
@ -0,0 +1,26 @@
|
||||
import prisma from '@/lib/prisma'
|
||||
import { CollaborationType, CollaboratorsOnTypebots, User } from 'db'
|
||||
import { Typebot } from 'models'
|
||||
import { isNotDefined } from 'utils'
|
||||
|
||||
export const isWriteTypebotForbidden = async (
|
||||
typebot: Pick<Typebot, 'workspaceId'> & {
|
||||
collaborators: Pick<CollaboratorsOnTypebots, 'userId' | 'type'>[]
|
||||
},
|
||||
user: Pick<User, 'email' | 'id'>
|
||||
) => {
|
||||
if (
|
||||
process.env.ADMIN_EMAIL === user.email ||
|
||||
typebot.collaborators.find(
|
||||
(collaborator) => collaborator.userId === user.id
|
||||
)?.type === CollaborationType.WRITE
|
||||
)
|
||||
return false
|
||||
const memberInWorkspace = await prisma.memberInWorkspace.findFirst({
|
||||
where: {
|
||||
workspaceId: typebot.workspaceId,
|
||||
userId: user.id,
|
||||
},
|
||||
})
|
||||
return isNotDefined(memberInWorkspace) || memberInWorkspace.role === 'GUEST'
|
||||
}
|
Reference in New Issue
Block a user