From 266730ba8161d0260f8815ed1d0a1465449c9f2d Mon Sep 17 00:00:00 2001 From: medchedli Date: Wed, 4 Jun 2025 17:18:38 +0100 Subject: [PATCH] fix: update block repository logic to correctly reference source category and handle block updates --- api/src/chat/repositories/block.repository.ts | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/api/src/chat/repositories/block.repository.ts b/api/src/chat/repositories/block.repository.ts index 78903a3c..55c5c1e2 100644 --- a/api/src/chat/repositories/block.repository.ts +++ b/api/src/chat/repositories/block.repository.ts @@ -140,12 +140,12 @@ export class BlockRepository extends BaseRepository< // Step 1: Map IDs and Category const objIds = ids.map((id) => new Types.ObjectId(id)); - const objCategoryId = new Types.ObjectId(categoryId); + const sourceCategoryId = movedBlocks[0].category; - // Step 2: Find other blocks + // Step 2: Find blocks in source category that reference the moved blocks const otherBlocks = await this.find({ _id: { $nin: objIds }, - category: { $ne: objCategoryId }, + category: sourceCategoryId, $or: [ { attachedBlock: { $in: objIds } }, { nextBlocks: { $in: objIds } }, @@ -154,7 +154,7 @@ export class BlockRepository extends BaseRepository< // Step 3: Update blocks in the provided scope await this.prepareBlocksInCategoryUpdateScope(categoryId, ids); - // Step 4: Update external blocks + // Step 4: Update blocks in source category await this.prepareBlocksOutOfCategoryUpdateScope(otherBlocks, ids); } } @@ -206,16 +206,21 @@ export class BlockRepository extends BaseRepository< ids: string[], ): Promise { for (const block of otherBlocks) { + // Handle attached block references if (block.attachedBlock && ids.includes(block.attachedBlock)) { await this.updateOne(block.id, { attachedBlock: null }); } - const nextBlocks = block.nextBlocks?.filter( + // Handle nextBlocks references + const filteredNextBlocks = block.nextBlocks?.filter( (nextBlock) => !ids.includes(nextBlock), ); - if (nextBlocks?.length) { - await this.updateOne(block.id, { nextBlocks }); + // If the filtered nextBlocks length is different from the original, update the block + if (filteredNextBlocks?.length !== block.nextBlocks?.length) { + await this.updateOne(block.id, { + nextBlocks: filteredNextBlocks || [], + }); } } }