♻️ (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 prisma from 'libs/prisma'
|
||||||
import { NextApiRequest, NextApiResponse } from 'next'
|
import { NextApiRequest, NextApiResponse } from 'next'
|
||||||
import { canReadTypebot, canWriteTypebot } from 'services/api/dbRules'
|
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'
|
import { methodNotAllowed, notAuthenticated } from 'utils/api'
|
||||||
|
|
||||||
const handler = async (req: NextApiRequest, res: NextApiResponse) => {
|
const handler = async (req: NextApiRequest, res: NextApiResponse) => {
|
||||||
@ -38,9 +38,10 @@ const handler = async (req: NextApiRequest, res: NextApiResponse) => {
|
|||||||
const typebots = await prisma.typebot.deleteMany({
|
const typebots = await prisma.typebot.deleteMany({
|
||||||
where: canWriteTypebot(typebotId, user),
|
where: canWriteTypebot(typebotId, user),
|
||||||
})
|
})
|
||||||
await prisma.result.updateMany({
|
await archiveResults(res)({
|
||||||
where: { typebot: canWriteTypebot(typebotId, user) },
|
typebotId,
|
||||||
data: { isArchived: true },
|
user,
|
||||||
|
resultsFilter: { typebotId },
|
||||||
})
|
})
|
||||||
return res.send({ typebots })
|
return res.send({ typebots })
|
||||||
}
|
}
|
||||||
|
@ -1,10 +1,8 @@
|
|||||||
import { withSentry } from '@sentry/nextjs'
|
import { withSentry } from '@sentry/nextjs'
|
||||||
import prisma from 'libs/prisma'
|
import prisma from 'libs/prisma'
|
||||||
import { InputBlockType, Typebot } from 'models'
|
|
||||||
import { NextApiRequest, NextApiResponse } from 'next'
|
import { NextApiRequest, NextApiResponse } from 'next'
|
||||||
import { canReadTypebot, canWriteTypebot } from 'services/api/dbRules'
|
import { canReadTypebot, canWriteTypebot } from 'services/api/dbRules'
|
||||||
import { deleteFiles } from 'services/api/storage'
|
import { archiveResults, getAuthenticatedUser } from 'services/api/utils'
|
||||||
import { getAuthenticatedUser } from 'services/api/utils'
|
|
||||||
import {
|
import {
|
||||||
badRequest,
|
badRequest,
|
||||||
forbidden,
|
forbidden,
|
||||||
@ -49,43 +47,12 @@ const handler = async (req: NextApiRequest, res: NextApiResponse) => {
|
|||||||
const typebotId = req.query.typebotId as string
|
const typebotId = req.query.typebotId as string
|
||||||
const data = req.body as { ids: string[] }
|
const data = req.body as { ids: string[] }
|
||||||
const ids = data.ids
|
const ids = data.ids
|
||||||
const resultsFilter = {
|
await archiveResults(res)({
|
||||||
id: ids.length > 0 ? { in: ids } : undefined,
|
typebotId,
|
||||||
typebot: canWriteTypebot(typebotId, user),
|
user,
|
||||||
}
|
resultsFilter: {
|
||||||
const typebot = await prisma.typebot.findFirst({
|
id: ids.length > 0 ? { in: ids } : undefined,
|
||||||
where: canWriteTypebot(typebotId, user),
|
typebot: 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' })
|
return res.status(200).send({ message: 'done' })
|
||||||
|
@ -1,8 +1,8 @@
|
|||||||
import { withSentry } from '@sentry/nextjs'
|
import { withSentry } from '@sentry/nextjs'
|
||||||
import { Workspace, WorkspaceRole } from 'db'
|
import { Prisma, Workspace, WorkspaceRole } from 'db'
|
||||||
import prisma from 'libs/prisma'
|
import prisma from 'libs/prisma'
|
||||||
import { NextApiRequest, NextApiResponse } from 'next'
|
import { NextApiRequest, NextApiResponse } from 'next'
|
||||||
import { getAuthenticatedUser } from 'services/api/utils'
|
import { archiveResults, getAuthenticatedUser } from 'services/api/utils'
|
||||||
import { methodNotAllowed, notAuthenticated } from 'utils/api'
|
import { methodNotAllowed, notAuthenticated } from 'utils/api'
|
||||||
|
|
||||||
const handler = async (req: NextApiRequest, res: NextApiResponse) => {
|
const handler = async (req: NextApiRequest, res: NextApiResponse) => {
|
||||||
@ -24,23 +24,27 @@ const handler = async (req: NextApiRequest, res: NextApiResponse) => {
|
|||||||
}
|
}
|
||||||
if (req.method === 'DELETE') {
|
if (req.method === 'DELETE') {
|
||||||
const id = req.query.workspaceId as string
|
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({
|
await prisma.workspace.deleteMany({
|
||||||
where: {
|
where: workspaceFilter,
|
||||||
id,
|
|
||||||
members: { some: { userId: user.id, role: WorkspaceRole.ADMIN } },
|
|
||||||
},
|
|
||||||
})
|
|
||||||
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({
|
return res.status(200).json({
|
||||||
message: 'success',
|
message: 'success',
|
||||||
})
|
})
|
||||||
|
@ -1,7 +1,12 @@
|
|||||||
import { setUser } from '@sentry/nextjs'
|
import { setUser } from '@sentry/nextjs'
|
||||||
import { User } from 'db'
|
import { User, Prisma } from 'db'
|
||||||
import { NextApiRequest } from 'next'
|
import prisma from 'libs/prisma'
|
||||||
|
import { InputBlockType, Typebot } from 'models'
|
||||||
|
import { NextApiRequest, NextApiResponse } from 'next'
|
||||||
import { getSession } from 'next-auth/react'
|
import { getSession } from 'next-auth/react'
|
||||||
|
import { forbidden } from 'utils/api'
|
||||||
|
import { canWriteTypebot } from './dbRules'
|
||||||
|
import { deleteFiles } from './storage'
|
||||||
|
|
||||||
export const mockedUser: User = {
|
export const mockedUser: User = {
|
||||||
id: 'userId',
|
id: 'userId',
|
||||||
@ -26,3 +31,51 @@ export const getAuthenticatedUser = async (
|
|||||||
setUser({ id: user.id, email: user.email ?? undefined })
|
setUser({ id: user.id, email: user.email ?? undefined })
|
||||||
return session?.user as User
|
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 typebotId = req.query.typebotId as string
|
||||||
const blockId = req.query.blockId as string
|
const blockId = req.query.blockId as string
|
||||||
if (!filePath) return badRequest(res, 'Missing filePath or fileType')
|
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({
|
const typebot = (await prisma.publicTypebot.findFirst({
|
||||||
where: { typebotId },
|
where: { typebotId },
|
||||||
})) as unknown as PublicTypebot
|
})) as unknown as PublicTypebot
|
||||||
@ -54,7 +54,8 @@ const handler = async (
|
|||||||
sizeLimit: sizeLimit * 1024 * 1024,
|
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)
|
return methodNotAllowed(res)
|
||||||
}
|
}
|
||||||
|
@ -52,8 +52,9 @@ const handler = async (req: NextApiRequest, res: NextApiResponse) => {
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
const hasReachedLimit = await checkChatsUsage(result.typebot.workspace)
|
// TODO: enable storage limit on 1st of November 2022
|
||||||
res.send({ result, hasReachedLimit })
|
// const hasReachedLimit = await checkChatsUsage(result.typebot.workspace)
|
||||||
|
res.send({ result, hasReachedLimit: false })
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
methodNotAllowed(res)
|
methodNotAllowed(res)
|
||||||
|
Reference in New Issue
Block a user