♻️ (builder) Remove barrel export and flatten folder arch
This commit is contained in:
@@ -1,78 +0,0 @@
|
||||
import prisma from '@/lib/prisma'
|
||||
import { deleteFiles } from '@/utils/api/storage'
|
||||
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 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 }
|
||||
}
|
||||
@@ -1,11 +1,11 @@
|
||||
import { getTypebot } from '@/features/typebot/api/utils/getTypebot'
|
||||
import { authenticatedProcedure } from '@/utils/server/trpc'
|
||||
import { getTypebot } from '@/features/typebot/helpers/getTypebot'
|
||||
import { authenticatedProcedure } from '@/helpers/server/trpc'
|
||||
import { TRPCError } from '@trpc/server'
|
||||
import { Typebot } from '@typebot.io/schemas'
|
||||
import { z } from 'zod'
|
||||
import { archiveResults } from '../archiveResults'
|
||||
import { archiveResults } from '../helpers/archiveResults'
|
||||
|
||||
export const deleteResultsProcedure = authenticatedProcedure
|
||||
export const deleteResults = authenticatedProcedure
|
||||
.meta({
|
||||
openapi: {
|
||||
method: 'DELETE',
|
||||
@@ -1,10 +1,10 @@
|
||||
import { getTypebot } from '@/features/typebot/api/utils/getTypebot'
|
||||
import { getTypebot } from '@/features/typebot/helpers/getTypebot'
|
||||
import prisma from '@/lib/prisma'
|
||||
import { authenticatedProcedure } from '@/utils/server/trpc'
|
||||
import { authenticatedProcedure } from '@/helpers/server/trpc'
|
||||
import { logSchema } from '@typebot.io/schemas'
|
||||
import { z } from 'zod'
|
||||
|
||||
export const getResultLogsProcedure = authenticatedProcedure
|
||||
export const getResultLogs = authenticatedProcedure
|
||||
.meta({
|
||||
openapi: {
|
||||
method: 'GET',
|
||||
@@ -1,13 +1,13 @@
|
||||
import { getTypebot } from '@/features/typebot/api/utils/getTypebot'
|
||||
import { getTypebot } from '@/features/typebot/helpers/getTypebot'
|
||||
import prisma from '@/lib/prisma'
|
||||
import { authenticatedProcedure } from '@/utils/server/trpc'
|
||||
import { authenticatedProcedure } from '@/helpers/server/trpc'
|
||||
import { TRPCError } from '@trpc/server'
|
||||
import { ResultWithAnswers, resultWithAnswersSchema } from '@typebot.io/schemas'
|
||||
import { z } from 'zod'
|
||||
|
||||
const maxLimit = 200
|
||||
|
||||
export const getResultsProcedure = authenticatedProcedure
|
||||
export const getResults = authenticatedProcedure
|
||||
.meta({
|
||||
openapi: {
|
||||
method: 'GET',
|
||||
@@ -1,2 +0,0 @@
|
||||
export * from './archiveResults'
|
||||
export * from './router'
|
||||
@@ -1,3 +0,0 @@
|
||||
export * from './deleteResultsProcedure'
|
||||
export * from './getResultLogsProcedure'
|
||||
export * from './getResultsProcedure'
|
||||
@@ -1,12 +1,10 @@
|
||||
import { router } from '@/utils/server/trpc'
|
||||
import {
|
||||
deleteResultsProcedure,
|
||||
getResultLogsProcedure,
|
||||
getResultsProcedure,
|
||||
} from './procedures'
|
||||
import { router } from '@/helpers/server/trpc'
|
||||
import { deleteResults } from './deleteResults'
|
||||
import { getResultLogs } from './getResultLogs'
|
||||
import { getResults } from './getResults'
|
||||
|
||||
export const resultsRouter = router({
|
||||
getResults: getResultsProcedure,
|
||||
deleteResults: deleteResultsProcedure,
|
||||
getResultLogs: getResultLogsProcedure,
|
||||
getResults,
|
||||
deleteResults,
|
||||
getResultLogs,
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user