From cc9817b2e36474adbcb5201ce4e7f99d0dd2599d Mon Sep 17 00:00:00 2001 From: Baptiste Arnaud Date: Thu, 23 Feb 2023 08:37:21 +0100 Subject: [PATCH] :zap: Fix / improve results archive crash when too many --- .../features/results/api/archiveResults.ts | 103 ++++++++++++------ .../src/pages/api/typebots/[typebotId].ts | 2 +- 2 files changed, 69 insertions(+), 36 deletions(-) diff --git a/apps/builder/src/features/results/api/archiveResults.ts b/apps/builder/src/features/results/api/archiveResults.ts index 9af1d9559..264ed46f9 100644 --- a/apps/builder/src/features/results/api/archiveResults.ts +++ b/apps/builder/src/features/results/api/archiveResults.ts @@ -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 - resultsFilter?: Prisma.ResultWhereInput -}) => { - const fileUploadBlockIds = 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(', ')), - }) + resultsFilter?: Omit & { + typebotId: string } - await prisma.log.deleteMany({ - where: { - result: resultsFilter, - }, - }) - await prisma.answer.deleteMany({ - where: { - result: resultsFilter, - }, - }) - await prisma.result.updateMany({ - where: resultsFilter, - data: { - isArchived: true, - variables: [], - }, - }) +} + +export const archiveResults = async ({ typebot, resultsFilter }: Props) => { + const fileUploadBlockIds = typebot.groups + .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: { + resultId: { in: resultIds }, + blockId: { in: fileUploadBlockIds }, + }, + }) + if (filesToDelete.length > 0) + await deleteFiles({ + urls: filesToDelete.flatMap((a) => a.content.split(', ')), + }) + } + + await prisma.$transaction([ + prisma.log.deleteMany({ + where: { + resultId: { in: resultIds }, + }, + }), + prisma.answer.deleteMany({ + where: { + resultId: { in: resultIds }, + }, + }), + prisma.result.updateMany({ + where: { + id: { in: resultIds }, + }, + data: { + isArchived: true, + variables: [], + }, + }), + ]) + } while (currentTotalResults >= batchSize) return { success: true } } diff --git a/apps/builder/src/pages/api/typebots/[typebotId].ts b/apps/builder/src/pages/api/typebots/[typebotId].ts index c0dcbf7bb..b89ee91b6 100644 --- a/apps/builder/src/pages/api/typebots/[typebotId].ts +++ b/apps/builder/src/pages/api/typebots/[typebotId].ts @@ -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 }, })