build: Archive results to still be able to check usage
This commit is contained in:
@@ -1,7 +1,9 @@
|
||||
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 { isFreePlan } from 'services/workspace'
|
||||
import {
|
||||
@@ -49,13 +51,47 @@ 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 results = await prisma.result.deleteMany({
|
||||
const resultsFilter = {
|
||||
id: ids.length > 0 ? { in: ids } : undefined,
|
||||
typebot: canWriteTypebot(typebotId, user),
|
||||
}
|
||||
// Weird bug waiting for https://github.com/aws/aws-sdk-js/issues/4137
|
||||
// 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: {
|
||||
id: ids.length > 0 ? { in: ids } : undefined,
|
||||
typebot: canWriteTypebot(typebotId, user),
|
||||
result: resultsFilter,
|
||||
},
|
||||
})
|
||||
return res.status(200).send({ results })
|
||||
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 methodNotAllowed(res)
|
||||
}
|
||||
|
||||
44
apps/builder/services/api/storage.ts
Normal file
44
apps/builder/services/api/storage.ts
Normal file
@@ -0,0 +1,44 @@
|
||||
import { AWSError, config, Endpoint, S3 } from 'aws-sdk'
|
||||
import { PromiseResult } from 'aws-sdk/lib/request'
|
||||
|
||||
export const deleteFiles = async ({
|
||||
urls,
|
||||
}: {
|
||||
urls: string[]
|
||||
}): Promise<PromiseResult<S3.DeleteObjectsOutput, AWSError>> => {
|
||||
if (
|
||||
!process.env.S3_ENDPOINT ||
|
||||
!process.env.S3_ACCESS_KEY ||
|
||||
!process.env.S3_SECRET_KEY
|
||||
)
|
||||
throw new Error(
|
||||
'S3 not properly configured. Missing one of those variables: S3_ENDPOINT, S3_ACCESS_KEY, S3_SECRET_KEY'
|
||||
)
|
||||
|
||||
const sslEnabled =
|
||||
process.env.S3_SSL && process.env.S3_SSL === 'false' ? false : true
|
||||
config.update({
|
||||
accessKeyId: process.env.S3_ACCESS_KEY,
|
||||
secretAccessKey: process.env.S3_SECRET_KEY,
|
||||
region: process.env.S3_REGION,
|
||||
sslEnabled,
|
||||
})
|
||||
const protocol = sslEnabled ? 'https' : 'http'
|
||||
const s3 = new S3({
|
||||
endpoint: new Endpoint(
|
||||
`${protocol}://${process.env.S3_ENDPOINT}${
|
||||
process.env.S3_PORT ? `:${process.env.S3_PORT}` : ''
|
||||
}`
|
||||
),
|
||||
})
|
||||
|
||||
const Bucket = process.env.S3_BUCKET ?? 'typebot'
|
||||
return s3
|
||||
.deleteObjects({
|
||||
Bucket,
|
||||
Delete: {
|
||||
Objects: urls.map((url) => ({ Key: url.split(`/${Bucket}/`)[1] })),
|
||||
},
|
||||
})
|
||||
.promise()
|
||||
}
|
||||
Reference in New Issue
Block a user