diff --git a/apps/cron/src/commands/appendix1.helpers.ts b/apps/cron/src/commands/appendix1.helpers.ts index ffdd060a17..465e1162a3 100644 --- a/apps/cron/src/commands/appendix1.helpers.ts +++ b/apps/cron/src/commands/appendix1.helpers.ts @@ -3,6 +3,10 @@ import { subDays, startOfDay } from "date-fns"; import { deleteBsd } from "back"; import { prisma } from "@td/prisma"; +(async function () { + await cleanOrphanAppendix1(); +})(); + export async function cleanAppendix1() { await cleanUnsignedAppendix1(); await cleanOrphanAppendix1(); @@ -88,10 +92,15 @@ async function cleanOrphanAppendix1() { }); cursor = orphansChunk[orphansChunk.length - 1].id; + const orphansChunkIds = orphansChunk.map(form => form.id); await prisma.form.updateMany({ - where: { id: { in: orphansChunk.map(form => form.id) } }, + where: { id: { in: orphansChunkIds } }, data: { isDeleted: true } }); + + for (const id of orphansChunkIds) { + await deleteBsd({ id }, { user: { auth: AuthType.BEARER } } as any); + } } } diff --git a/back/package.json b/back/package.json index 518875f118..748421b953 100644 --- a/back/package.json +++ b/back/package.json @@ -11,7 +11,8 @@ "reindex-bsd": "tsx --tsconfig tsconfig.lib.json ./src/scripts/bin/reindexBsd.ts $bsdId", "purge-pdf-token": "tsx --tsconfig tsconfig.lib.json ./src/scripts/bin/purgePdfAccessToken.ts", "storeAllWebhookSettings.ts": "tsx --tsconfig tsconfig.lib.json ./src/webhooks/commands/storeAllWebhookSettings.ts", - "generate-bsds-templates": "tsx --tsconfig tsconfig.lib.json ./src/scripts/bin/bsdTemplates/generateBsdTemplates.ts" + "generate-bsds-templates": "tsx --tsconfig tsconfig.lib.json ./src/scripts/bin/bsdTemplates/generateBsdTemplates.ts", + "reindex-deleted-orphan-appendix1": "tsx --tsconfig tsconfig.lib.json ./src/scripts/bin/reindexDeletedOrphanAppendix1.ts" }, "engines": { "node": "^20" diff --git a/back/src/scripts/bin/reindexDeletedOrphanAppendix1.ts b/back/src/scripts/bin/reindexDeletedOrphanAppendix1.ts new file mode 100644 index 0000000000..a9a4fc17e1 --- /dev/null +++ b/back/src/scripts/bin/reindexDeletedOrphanAppendix1.ts @@ -0,0 +1,49 @@ +import { prisma } from "@td/prisma"; +import { AuthType, EmitterType, Prisma } from "@prisma/client"; +import { logger } from "@td/logger"; +import { deleteBsd } from "../../common/elastic"; +import { closeQueues } from "../../queue/producers"; + +async function exitScript() { + logger.info("Done reindexAllInBulk script, exiting"); + await prisma.$disconnect(); + await closeQueues(); + process.exit(0); +} + +// ensure deleted orphans appendix1 are removed from ES +(async function () { + const CHUNK_SIZE = 200; + + const where: Prisma.FormWhereInput = { + emitterType: EmitterType.APPENDIX1_PRODUCER, + isDeleted: true, + groupedIn: { none: {} } + }; + const orphansCount = await prisma.form.count({ where }); + + let cursor: string | null = null; + + for (let i = 0; i < orphansCount; i += CHUNK_SIZE) { + console.log(`Chunk ${i + 1}`); + + const orphans = await prisma.form.findMany({ + where, + orderBy: { id: "asc" }, + take: CHUNK_SIZE, + select: { id: true }, + ...(cursor ? { skip: 1, cursor: { id: cursor } } : {}) + }); + + cursor = orphans[orphans.length - 1].id; + + const orphansIds = orphans.map(form => form.id); + + for (const id of orphansIds) { + console.log(`Removing ${id}`); + await deleteBsd({ id }, { user: { auth: AuthType.BEARER } } as any); + } + } + + await exitScript(); +})();