♻️ (usage) Remove limit until temporarily
This commit is contained in:
committed by
Baptiste Arnaud
parent
3c803b1345
commit
3bec24a8cc
@ -3,7 +3,7 @@ import { CollaborationType } from 'db'
|
||||
import prisma from 'libs/prisma'
|
||||
import { NextApiRequest, NextApiResponse } from 'next'
|
||||
import { canReadTypebot, canWriteTypebot } from 'services/api/dbRules'
|
||||
import { getAuthenticatedUser } from 'services/api/utils'
|
||||
import { archiveResults, getAuthenticatedUser } from 'services/api/utils'
|
||||
import { methodNotAllowed, notAuthenticated } from 'utils/api'
|
||||
|
||||
const handler = async (req: NextApiRequest, res: NextApiResponse) => {
|
||||
@ -38,9 +38,10 @@ const handler = async (req: NextApiRequest, res: NextApiResponse) => {
|
||||
const typebots = await prisma.typebot.deleteMany({
|
||||
where: canWriteTypebot(typebotId, user),
|
||||
})
|
||||
await prisma.result.updateMany({
|
||||
where: { typebot: canWriteTypebot(typebotId, user) },
|
||||
data: { isArchived: true },
|
||||
await archiveResults(res)({
|
||||
typebotId,
|
||||
user,
|
||||
resultsFilter: { typebotId },
|
||||
})
|
||||
return res.send({ typebots })
|
||||
}
|
||||
|
@ -1,10 +1,8 @@
|
||||
import { withSentry } from '@sentry/nextjs'
|
||||
import prisma from 'libs/prisma'
|
||||
import { InputBlockType, Typebot } from 'models'
|
||||
import { NextApiRequest, NextApiResponse } from 'next'
|
||||
import { canReadTypebot, canWriteTypebot } from 'services/api/dbRules'
|
||||
import { deleteFiles } from 'services/api/storage'
|
||||
import { getAuthenticatedUser } from 'services/api/utils'
|
||||
import { archiveResults, getAuthenticatedUser } from 'services/api/utils'
|
||||
import {
|
||||
badRequest,
|
||||
forbidden,
|
||||
@ -49,43 +47,12 @@ const handler = async (req: NextApiRequest, res: NextApiResponse) => {
|
||||
const typebotId = req.query.typebotId as string
|
||||
const data = req.body as { ids: string[] }
|
||||
const ids = data.ids
|
||||
const resultsFilter = {
|
||||
await archiveResults(res)({
|
||||
typebotId,
|
||||
user,
|
||||
resultsFilter: {
|
||||
id: ids.length > 0 ? { in: ids } : undefined,
|
||||
typebot: canWriteTypebot(typebotId, user),
|
||||
}
|
||||
const typebot = await prisma.typebot.findFirst({
|
||||
where: canWriteTypebot(typebotId, user),
|
||||
select: { groups: true },
|
||||
})
|
||||
if (!typebot) return forbidden(res)
|
||||
const fileUploadBlockIds = (typebot as Typebot).groups
|
||||
.flatMap((g) => g.blocks)
|
||||
.filter((b) => b.type === InputBlockType.FILE)
|
||||
.map((b) => b.id)
|
||||
if (fileUploadBlockIds.length > 0) {
|
||||
const filesToDelete = await prisma.answer.findMany({
|
||||
where: { result: resultsFilter, blockId: { in: fileUploadBlockIds } },
|
||||
})
|
||||
if (filesToDelete.length > 0)
|
||||
await deleteFiles({
|
||||
urls: filesToDelete.flatMap((a) => a.content.split(', ')),
|
||||
})
|
||||
}
|
||||
await prisma.log.deleteMany({
|
||||
where: {
|
||||
result: resultsFilter,
|
||||
},
|
||||
})
|
||||
await prisma.answer.deleteMany({
|
||||
where: {
|
||||
result: resultsFilter,
|
||||
},
|
||||
})
|
||||
await prisma.result.updateMany({
|
||||
where: resultsFilter,
|
||||
data: {
|
||||
isArchived: true,
|
||||
variables: [],
|
||||
},
|
||||
})
|
||||
return res.status(200).send({ message: 'done' })
|
||||
|
@ -1,8 +1,8 @@
|
||||
import { withSentry } from '@sentry/nextjs'
|
||||
import { Workspace, WorkspaceRole } from 'db'
|
||||
import { Prisma, Workspace, WorkspaceRole } from 'db'
|
||||
import prisma from 'libs/prisma'
|
||||
import { NextApiRequest, NextApiResponse } from 'next'
|
||||
import { getAuthenticatedUser } from 'services/api/utils'
|
||||
import { archiveResults, getAuthenticatedUser } from 'services/api/utils'
|
||||
import { methodNotAllowed, notAuthenticated } from 'utils/api'
|
||||
|
||||
const handler = async (req: NextApiRequest, res: NextApiResponse) => {
|
||||
@ -24,23 +24,27 @@ const handler = async (req: NextApiRequest, res: NextApiResponse) => {
|
||||
}
|
||||
if (req.method === 'DELETE') {
|
||||
const id = req.query.workspaceId as string
|
||||
const workspaceFilter: Prisma.WorkspaceWhereInput = {
|
||||
id,
|
||||
members: { some: { userId: user.id, role: WorkspaceRole.ADMIN } },
|
||||
}
|
||||
const deletedTypebots = await prisma.typebot.findMany({
|
||||
where: {
|
||||
workspace: workspaceFilter,
|
||||
},
|
||||
})
|
||||
await prisma.workspace.deleteMany({
|
||||
where: {
|
||||
id,
|
||||
members: { some: { userId: user.id, role: WorkspaceRole.ADMIN } },
|
||||
},
|
||||
where: workspaceFilter,
|
||||
})
|
||||
await prisma.result.updateMany({
|
||||
where: {
|
||||
typebot: {
|
||||
workspace: {
|
||||
id,
|
||||
members: { some: { userId: user.id, role: WorkspaceRole.ADMIN } },
|
||||
},
|
||||
},
|
||||
},
|
||||
data: { isArchived: true },
|
||||
await Promise.all(
|
||||
deletedTypebots.map((typebot) =>
|
||||
archiveResults(res)({
|
||||
typebotId: typebot.id,
|
||||
user,
|
||||
resultsFilter: { typebotId: typebot.id },
|
||||
})
|
||||
)
|
||||
)
|
||||
return res.status(200).json({
|
||||
message: 'success',
|
||||
})
|
||||
|
@ -1,7 +1,12 @@
|
||||
import { setUser } from '@sentry/nextjs'
|
||||
import { User } from 'db'
|
||||
import { NextApiRequest } from 'next'
|
||||
import { User, Prisma } from 'db'
|
||||
import prisma from 'libs/prisma'
|
||||
import { InputBlockType, Typebot } from 'models'
|
||||
import { NextApiRequest, NextApiResponse } from 'next'
|
||||
import { getSession } from 'next-auth/react'
|
||||
import { forbidden } from 'utils/api'
|
||||
import { canWriteTypebot } from './dbRules'
|
||||
import { deleteFiles } from './storage'
|
||||
|
||||
export const mockedUser: User = {
|
||||
id: 'userId',
|
||||
@ -26,3 +31,51 @@ export const getAuthenticatedUser = async (
|
||||
setUser({ id: user.id, email: user.email ?? undefined })
|
||||
return session?.user as User
|
||||
}
|
||||
|
||||
export const archiveResults =
|
||||
(res: NextApiResponse) =>
|
||||
async ({
|
||||
typebotId,
|
||||
user,
|
||||
resultsFilter,
|
||||
}: {
|
||||
typebotId: string
|
||||
user: User
|
||||
resultsFilter?: Prisma.ResultWhereInput
|
||||
}) => {
|
||||
const typebot = await prisma.typebot.findFirst({
|
||||
where: canWriteTypebot(typebotId, user),
|
||||
select: { groups: true },
|
||||
})
|
||||
if (!typebot) return forbidden(res)
|
||||
const fileUploadBlockIds = (typebot as Typebot).groups
|
||||
.flatMap((g) => g.blocks)
|
||||
.filter((b) => b.type === InputBlockType.FILE)
|
||||
.map((b) => b.id)
|
||||
if (fileUploadBlockIds.length > 0) {
|
||||
const filesToDelete = await prisma.answer.findMany({
|
||||
where: { result: resultsFilter, blockId: { in: fileUploadBlockIds } },
|
||||
})
|
||||
if (filesToDelete.length > 0)
|
||||
await deleteFiles({
|
||||
urls: filesToDelete.flatMap((a) => a.content.split(', ')),
|
||||
})
|
||||
}
|
||||
await prisma.log.deleteMany({
|
||||
where: {
|
||||
result: resultsFilter,
|
||||
},
|
||||
})
|
||||
await prisma.answer.deleteMany({
|
||||
where: {
|
||||
result: resultsFilter,
|
||||
},
|
||||
})
|
||||
await prisma.result.updateMany({
|
||||
where: resultsFilter,
|
||||
data: {
|
||||
isArchived: true,
|
||||
variables: [],
|
||||
},
|
||||
})
|
||||
}
|
||||
|
@ -35,7 +35,7 @@ const handler = async (
|
||||
const typebotId = req.query.typebotId as string
|
||||
const blockId = req.query.blockId as string
|
||||
if (!filePath) return badRequest(res, 'Missing filePath or fileType')
|
||||
const hasReachedStorageLimit = await checkStorageLimit(typebotId)
|
||||
// const hasReachedStorageLimit = await checkStorageLimit(typebotId)
|
||||
const typebot = (await prisma.publicTypebot.findFirst({
|
||||
where: { typebotId },
|
||||
})) as unknown as PublicTypebot
|
||||
@ -54,7 +54,8 @@ const handler = async (
|
||||
sizeLimit: sizeLimit * 1024 * 1024,
|
||||
})
|
||||
|
||||
return res.status(200).send({ presignedUrl, hasReachedStorageLimit })
|
||||
// TODO: enable storage limit on 1st of November 2022
|
||||
return res.status(200).send({ presignedUrl, hasReachedStorageLimit: false })
|
||||
}
|
||||
return methodNotAllowed(res)
|
||||
}
|
||||
|
@ -52,8 +52,9 @@ const handler = async (req: NextApiRequest, res: NextApiResponse) => {
|
||||
},
|
||||
},
|
||||
})
|
||||
const hasReachedLimit = await checkChatsUsage(result.typebot.workspace)
|
||||
res.send({ result, hasReachedLimit })
|
||||
// TODO: enable storage limit on 1st of November 2022
|
||||
// const hasReachedLimit = await checkChatsUsage(result.typebot.workspace)
|
||||
res.send({ result, hasReachedLimit: false })
|
||||
return
|
||||
}
|
||||
methodNotAllowed(res)
|
||||
|
Reference in New Issue
Block a user