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,
ids: string[],
): Promise<void> {
for (const id of ids) {
const oldState = await this.findOne(id);
if (oldState && oldState.category !== category) {
const updatedNextBlocks = oldState.nextBlocks?.filter((nextBlock) =>
ids.includes(nextBlock),
);
const blocks = await this.find({
_id: { $in: ids },
category: { $ne: category },
});
const updatedAttachedBlock = ids.includes(oldState.attachedBlock || '')
? oldState.attachedBlock
: null;
for (const { id, nextBlocks, attachedBlock } of blocks) {
const updatedNextBlocks = nextBlocks.filter((nextBlock) =>
ids.includes(nextBlock),
);
await this.updateOne(id, {
nextBlocks: updatedNextBlocks,
attachedBlock: updatedAttachedBlock,
});
}
const updatedAttachedBlock = ids.includes(attachedBlock || '')
? attachedBlock
: null;
await this.updateOne(id, {
nextBlocks: updatedNextBlocks,
attachedBlock: updatedAttachedBlock,
});
}
}