Skip to content

Commit

Permalink
Fix deleted appendix1 orphans indexation
Browse files Browse the repository at this point in the history
  • Loading branch information
providenz committed Oct 7, 2024
1 parent 2249b23 commit 81790f3
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 2 deletions.
11 changes: 10 additions & 1 deletion apps/cron/src/commands/appendix1.helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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);
}
}
}
3 changes: 2 additions & 1 deletion back/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
49 changes: 49 additions & 0 deletions back/src/scripts/bin/reindexDeletedOrphanAppendix1.ts
Original file line number Diff line number Diff line change
@@ -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();
})();

0 comments on commit 81790f3

Please sign in to comment.