fix(api): optimize preUpdateMany

This commit is contained in:
yassinedorbozgithub 2025-04-12 06:31:08 +01:00
parent 3813635ee0
commit 69a994f9e3

View File

@ -172,22 +172,24 @@ export class BlockRepository extends BaseRepository<
category: string, category: string,
ids: string[], ids: string[],
): Promise<void> { ): Promise<void> {
for (const id of ids) { const blocks = await this.find({
const oldState = await this.findOne(id); _id: { $in: ids },
if (oldState && oldState.category !== category) { category: { $ne: category },
const updatedNextBlocks = oldState.nextBlocks?.filter((nextBlock) => });
ids.includes(nextBlock),
);
const updatedAttachedBlock = ids.includes(oldState.attachedBlock || '') for (const { id, nextBlocks, attachedBlock } of blocks) {
? oldState.attachedBlock const updatedNextBlocks = nextBlocks.filter((nextBlock) =>
: null; ids.includes(nextBlock),
);
await this.updateOne(id, { const updatedAttachedBlock = ids.includes(attachedBlock || '')
nextBlocks: updatedNextBlocks, ? attachedBlock
attachedBlock: updatedAttachedBlock, : null;
});
} await this.updateOne(id, {
nextBlocks: updatedNextBlocks,
attachedBlock: updatedAttachedBlock,
});
} }
} }