Skip to content

Commit

Permalink
♻️ (usage) Remove limit until temporarily
Browse files Browse the repository at this point in the history
  • Loading branch information
baptisteArno committed Sep 27, 2022
1 parent 3c803b1 commit 3bec24a
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 65 deletions.
9 changes: 5 additions & 4 deletions apps/builder/pages/api/typebots/[typebotId].ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down Expand Up @@ -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 })
}
Expand Down
47 changes: 7 additions & 40 deletions apps/builder/pages/api/typebots/[typebotId]/results.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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 = {
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: [],
await archiveResults(res)({
typebotId,
user,
resultsFilter: {
id: ids.length > 0 ? { in: ids } : undefined,
typebot: canWriteTypebot(typebotId, user),
},
})
return res.status(200).send({ message: 'done' })
Expand Down
34 changes: 19 additions & 15 deletions apps/builder/pages/api/workspaces/[workspaceId].ts
Original file line number Diff line number Diff line change
@@ -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) => {
Expand All @@ -24,23 +24,27 @@ const handler = async (req: NextApiRequest, res: NextApiResponse) => {
}
if (req.method === 'DELETE') {
const id = req.query.workspaceId as string
await prisma.workspace.deleteMany({
const workspaceFilter: Prisma.WorkspaceWhereInput = {
id,
members: { some: { userId: user.id, role: WorkspaceRole.ADMIN } },
}
const deletedTypebots = await prisma.typebot.findMany({
where: {
id,
members: { some: { userId: user.id, role: WorkspaceRole.ADMIN } },
workspace: workspaceFilter,
},
})
await prisma.result.updateMany({
where: {
typebot: {
workspace: {
id,
members: { some: { userId: user.id, role: WorkspaceRole.ADMIN } },
},
},
},
data: { isArchived: true },
await prisma.workspace.deleteMany({
where: workspaceFilter,
})
await Promise.all(
deletedTypebots.map((typebot) =>
archiveResults(res)({
typebotId: typebot.id,
user,
resultsFilter: { typebotId: typebot.id },
})
)
)
return res.status(200).json({
message: 'success',
})
Expand Down
57 changes: 55 additions & 2 deletions apps/builder/services/api/utils.ts
Original file line number Diff line number Diff line change
@@ -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',
Expand All @@ -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: [],
},
})
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
}
Expand Down
5 changes: 3 additions & 2 deletions apps/viewer/pages/api/typebots/[typebotId]/results.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 3bec24a

Please sign in to comment.