2
0

♻️ (usage) Remove limit until temporarily

This commit is contained in:
Baptiste Arnaud
2022-09-26 08:07:49 +02:00
committed by Baptiste Arnaud
parent 3c803b1345
commit 3bec24a8cc
6 changed files with 94 additions and 67 deletions

View File

@ -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: [],
},
})
}