2
0

Fix / improve results archive crash when too many

This commit is contained in:
Baptiste Arnaud
2023-02-23 08:37:21 +01:00
parent 671c2cb101
commit cc9817b2e3
2 changed files with 69 additions and 36 deletions

View File

@ -3,43 +3,76 @@ import { deleteFiles } from '@/utils/api/storage'
import { Prisma } from 'db'
import { InputBlockType, Typebot } from 'models'
export const archiveResults = async ({
typebot,
resultsFilter,
}: {
const batchSize = 100
type Props = {
typebot: Pick<Typebot, 'groups'>
resultsFilter?: Prisma.ResultWhereInput
}) => {
resultsFilter?: Omit<Prisma.ResultWhereInput, 'typebotId'> & {
typebotId: string
}
}
export const archiveResults = async ({ typebot, resultsFilter }: Props) => {
const fileUploadBlockIds = typebot.groups
.flatMap((g) => g.blocks)
.filter((b) => b.type === InputBlockType.FILE)
.map((b) => b.id)
.flatMap((group) => group.blocks)
.filter((block) => block.type === InputBlockType.FILE)
.map((block) => block.id)
let currentTotalResults = 0
do {
const resultsToDelete = await prisma.result.findMany({
where: {
...resultsFilter,
isArchived: false,
},
select: {
id: true,
},
take: batchSize,
})
if (resultsToDelete.length === 0) break
currentTotalResults = resultsToDelete.length
const resultIds = resultsToDelete.map((result) => result.id)
if (fileUploadBlockIds.length > 0) {
const filesToDelete = await prisma.answer.findMany({
where: { result: resultsFilter, blockId: { in: fileUploadBlockIds } },
where: {
resultId: { in: resultIds },
blockId: { in: fileUploadBlockIds },
},
})
if (filesToDelete.length > 0)
await deleteFiles({
urls: filesToDelete.flatMap((a) => a.content.split(', ')),
})
}
await prisma.log.deleteMany({
await prisma.$transaction([
prisma.log.deleteMany({
where: {
result: resultsFilter,
resultId: { in: resultIds },
},
})
await prisma.answer.deleteMany({
}),
prisma.answer.deleteMany({
where: {
result: resultsFilter,
resultId: { in: resultIds },
},
}),
prisma.result.updateMany({
where: {
id: { in: resultIds },
},
})
await prisma.result.updateMany({
where: resultsFilter,
data: {
isArchived: true,
variables: [],
},
})
}),
])
} while (currentTotalResults >= batchSize)
return { success: true }
}

View File

@ -57,7 +57,7 @@ const handler = async (req: NextApiRequest, res: NextApiResponse) => {
typebot,
resultsFilter: { typebotId },
})
if (!success) return res.status(500).send({ success: false })
if (!success) return res.status(500).send({ success: false, error: '' })
await prisma.publicTypebot.deleteMany({
where: { typebotId },
})