2
0

🧑‍💻 (s3) Correctly delete the files when deleting resources

This commit is contained in:
Baptiste Arnaud
2024-09-02 11:23:01 +02:00
parent f53705268c
commit 041b817aa0
16 changed files with 225 additions and 206 deletions

View File

@ -1,11 +1,13 @@
import { Prisma, PrismaClient } from '@typebot.io/prisma'
import { Block, Typebot } from '@typebot.io/schemas'
import { deleteFilesFromBucket } from '@typebot.io/lib/s3/deleteFilesFromBucket'
import { InputBlockType } from '@typebot.io/schemas/features/blocks/inputs/constants'
import { Typebot } from '@typebot.io/schemas'
import { isDefined } from '@typebot.io/lib'
import {
removeAllObjectsFromResult,
removeObjectsFromResult,
} from '@typebot.io/lib/s3/removeObjectsRecursively'
type ArchiveResultsProps = {
typebot: Pick<Typebot, 'groups'>
typebot: Pick<Typebot, 'groups' | 'workspaceId' | 'id'>
resultsFilter?: Omit<Prisma.ResultWhereInput, 'typebotId'> & {
typebotId: string
}
@ -15,10 +17,6 @@ export const archiveResults =
(prisma: PrismaClient) =>
async ({ typebot, resultsFilter }: ArchiveResultsProps) => {
const batchSize = 100
const fileUploadBlockIds = typebot.groups
.flatMap<Block>((group) => group.blocks)
.filter((block) => block.type === InputBlockType.FILE)
.map((block) => block.id)
let currentTotalResults = 0
@ -33,6 +31,8 @@ export const archiveResults =
let progress = 0
const isDeletingAllResults = resultsFilter?.id === undefined
do {
progress += batchSize
console.log(`Archiving ${progress} / ${resultsCount} results...`)
@ -54,19 +54,6 @@ export const archiveResults =
const resultIds = resultsToDelete.map((result) => result.id)
if (fileUploadBlockIds.length > 0) {
const filesToDelete = await prisma.answer.findMany({
where: {
resultId: { in: resultIds },
blockId: { in: fileUploadBlockIds },
},
})
if (filesToDelete.length > 0)
await deleteFilesFromBucket({
urls: filesToDelete.flatMap((a) => a.content.split(', ')),
})
}
await prisma.$transaction([
prisma.log.deleteMany({
where: {
@ -112,7 +99,21 @@ export const archiveResults =
},
}),
])
if (!isDeletingAllResults) {
await removeObjectsFromResult({
workspaceId: typebot.workspaceId,
resultIds: resultIds,
typebotId: typebot.id,
})
}
} while (currentTotalResults >= batchSize)
if (isDeletingAllResults) {
await removeAllObjectsFromResult({
workspaceId: typebot.workspaceId,
typebotId: typebot.id,
})
}
return { success: true }
}