👷 Improve monthly clean database script
Archive results that were not properly archived
This commit is contained in:
@@ -3,7 +3,8 @@ import { authenticatedProcedure } from '@/helpers/server/trpc'
|
||||
import { TRPCError } from '@trpc/server'
|
||||
import { Typebot } from '@typebot.io/schemas'
|
||||
import { z } from 'zod'
|
||||
import { archiveResults } from '../helpers/archiveResults'
|
||||
import { archiveResults } from '@typebot.io/lib/api/helpers/archiveResults'
|
||||
import prisma from '@/lib/prisma'
|
||||
|
||||
export const deleteResults = authenticatedProcedure
|
||||
.meta({
|
||||
@@ -40,7 +41,7 @@ export const deleteResults = authenticatedProcedure
|
||||
})) as Pick<Typebot, 'groups'> | null
|
||||
if (!typebot)
|
||||
throw new TRPCError({ code: 'NOT_FOUND', message: 'Typebot not found' })
|
||||
const { success } = await archiveResults({
|
||||
const { success } = await archiveResults(prisma)({
|
||||
typebot,
|
||||
resultsFilter: {
|
||||
id: (idsArray?.length ?? 0) > 0 ? { in: idsArray } : undefined,
|
||||
|
||||
@@ -1,78 +0,0 @@
|
||||
import { deleteFilesFromBucket } from '@/helpers/deleteFilesFromBucket'
|
||||
import prisma from '@/lib/prisma'
|
||||
import { Prisma } from '@typebot.io/prisma'
|
||||
import { InputBlockType, Typebot } from '@typebot.io/schemas'
|
||||
|
||||
const batchSize = 100
|
||||
|
||||
type Props = {
|
||||
typebot: Pick<Typebot, 'groups'>
|
||||
resultsFilter?: Omit<Prisma.ResultWhereInput, 'typebotId'> & {
|
||||
typebotId: string
|
||||
}
|
||||
}
|
||||
|
||||
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 deleteFilesFromBucket({
|
||||
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 }
|
||||
}
|
||||
@@ -6,10 +6,10 @@ import { getAuthenticatedUser } from '@/features/auth/helpers/getAuthenticatedUs
|
||||
import { Typebot } from '@typebot.io/schemas'
|
||||
import { omit } from '@typebot.io/lib'
|
||||
import { getTypebot } from '@/features/typebot/helpers/getTypebot'
|
||||
import { archiveResults } from '@/features/results/helpers/archiveResults'
|
||||
import { isReadTypebotForbidden } from '@/features/typebot/helpers/isReadTypebotForbidden'
|
||||
import { removeTypebotOldProperties } from '@/features/typebot/helpers/removeTypebotOldProperties'
|
||||
import { roundGroupsCoordinate } from '@/features/typebot/helpers/roundGroupsCoordinate'
|
||||
import { archiveResults } from '@typebot.io/lib/api/helpers/archiveResults'
|
||||
|
||||
const handler = async (req: NextApiRequest, res: NextApiResponse) => {
|
||||
const user = await getAuthenticatedUser(req, res)
|
||||
@@ -56,7 +56,7 @@ const handler = async (req: NextApiRequest, res: NextApiResponse) => {
|
||||
},
|
||||
})) as Pick<Typebot, 'groups'> | null
|
||||
if (!typebot) return res.status(404).send({ typebot: null })
|
||||
const { success } = await archiveResults({
|
||||
const { success } = await archiveResults(prisma)({
|
||||
typebot,
|
||||
resultsFilter: { typebotId },
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user