From 266730ba8161d0260f8815ed1d0a1465449c9f2d Mon Sep 17 00:00:00 2001 From: medchedli Date: Wed, 4 Jun 2025 17:18:38 +0100 Subject: [PATCH 1/5] 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 || [], + }); } } } From 00820307703dfd5a64e113fd00a35bb4cd3f41b8 Mon Sep 17 00:00:00 2001 From: medchedli Date: Wed, 4 Jun 2025 17:20:30 +0100 Subject: [PATCH 2/5] fix: add error message for moving blocks in English and French translations --- frontend/public/locales/en/translation.json | 1 + frontend/public/locales/fr/translation.json | 1 + 2 files changed, 2 insertions(+) diff --git a/frontend/public/locales/en/translation.json b/frontend/public/locales/en/translation.json index fd7b1ec1..55114e04 100644 --- a/frontend/public/locales/en/translation.json +++ b/frontend/public/locales/en/translation.json @@ -118,6 +118,7 @@ "logout_failed": "Something went wrong during logout", "duplicate_labels_not_allowed": "Duplicate labels are not allowed", "duplicate_block_error": "Something went wrong while duplicating block", + "move_block_error": "Something went wrong. Unable to move block", "image_error": "Image not found", "file_error": "File not found", "audio_error": "Audio not found", diff --git a/frontend/public/locales/fr/translation.json b/frontend/public/locales/fr/translation.json index 0e746c98..8f701c3c 100644 --- a/frontend/public/locales/fr/translation.json +++ b/frontend/public/locales/fr/translation.json @@ -118,6 +118,7 @@ "logout_failed": "Une erreur s'est produite lors de la déconnexion", "duplicate_labels_not_allowed": "Les étiquettes en double ne sont pas autorisées", "duplicate_block_error": "Une erreur est survenue lors de la duplication du bloc", + "move_block_error": "Une erreur est survenue. Impossible de déplacer le bloc", "image_error": "Image introuvable", "file_error": "Fichier introuvable", "audio_error": "Audio introuvable", From 79f866e4060d8174973076e1bb4f7a7deffaff01 Mon Sep 17 00:00:00 2001 From: medchedli Date: Wed, 4 Jun 2025 17:22:58 +0100 Subject: [PATCH 3/5] fix: add error handling to updateBlocks --- frontend/src/components/visual-editor/v2/Diagrams.tsx | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/frontend/src/components/visual-editor/v2/Diagrams.tsx b/frontend/src/components/visual-editor/v2/Diagrams.tsx index d9060e41..9fa04982 100644 --- a/frontend/src/components/visual-editor/v2/Diagrams.tsx +++ b/frontend/src/components/visual-editor/v2/Diagrams.tsx @@ -548,9 +548,14 @@ const Diagrams = () => { }, }); - onCategoryChange( - categories.findIndex(({ id }) => id === targetCategoryId), + const targetCategoryIndex = categories.findIndex( + ({ id }) => id === targetCategoryId, ); + + onCategoryChange(targetCategoryIndex); + }, + onError: () => { + toast.error(t("message.move_block_error")); }, }, ); From 6a128541df033c348cadd8f6135c018fe3aa2982 Mon Sep 17 00:00:00 2001 From: medchedli Date: Wed, 11 Jun 2025 11:58:09 +0100 Subject: [PATCH 4/5] fix: simplify prepareBlocksOutOfCategoryUpdateScope logic and add error handling --- api/src/chat/repositories/block.repository.ts | 62 ++++++++++++------- 1 file changed, 39 insertions(+), 23 deletions(-) diff --git a/api/src/chat/repositories/block.repository.ts b/api/src/chat/repositories/block.repository.ts index 55c5c1e2..7be589bf 100644 --- a/api/src/chat/repositories/block.repository.ts +++ b/api/src/chat/repositories/block.repository.ts @@ -178,18 +178,25 @@ export class BlockRepository extends BaseRepository< }); for (const { id, nextBlocks, attachedBlock } of blocks) { - const updatedNextBlocks = nextBlocks.filter((nextBlock) => - ids.includes(nextBlock), - ); + try { + const updatedNextBlocks = nextBlocks.filter((nextBlock) => + ids.includes(nextBlock), + ); - const updatedAttachedBlock = ids.includes(attachedBlock || '') - ? attachedBlock - : null; + const updatedAttachedBlock = ids.includes(attachedBlock || '') + ? attachedBlock + : null; - await this.updateOne(id, { - nextBlocks: updatedNextBlocks, - attachedBlock: updatedAttachedBlock, - }); + await this.updateOne(id, { + nextBlocks: updatedNextBlocks, + attachedBlock: updatedAttachedBlock, + }); + } catch (error) { + this.logger?.error( + `Failed to update block ${id} during in-category scope update.`, + error, + ); + } } } @@ -206,21 +213,30 @@ 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 }); - } + try { + // If block has an attachedBlock, we only need to check that + if (block.attachedBlock) { + if (ids.includes(block.attachedBlock)) { + await this.updateOne(block.id, { attachedBlock: null }); + } + continue; // Skip nextBlocks check since it can't have both + } - // Handle nextBlocks references - const filteredNextBlocks = block.nextBlocks?.filter( - (nextBlock) => !ids.includes(nextBlock), - ); + // Only check nextBlocks if there is no attachedBlock + const filteredNextBlocks = block.nextBlocks?.filter( + (nextBlock) => !ids.includes(nextBlock), + ); - // 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 || [], - }); + if (filteredNextBlocks?.length !== block.nextBlocks?.length) { + await this.updateOne(block.id, { + nextBlocks: filteredNextBlocks || [], + }); + } + } catch (error) { + this.logger?.error( + `Failed to update block ${block.id} during out-of-category scope update.`, + error, + ); } } } From e211aa36ba4e6d3411df317a79e983d8415d46fd Mon Sep 17 00:00:00 2001 From: medchedli Date: Thu, 12 Jun 2025 12:20:25 +0100 Subject: [PATCH 5/5] refactor: consolidate block updates operations --- api/src/chat/repositories/block.repository.ts | 33 +++++++++++-------- 1 file changed, 19 insertions(+), 14 deletions(-) diff --git a/api/src/chat/repositories/block.repository.ts b/api/src/chat/repositories/block.repository.ts index 7be589bf..ec2a3f64 100644 --- a/api/src/chat/repositories/block.repository.ts +++ b/api/src/chat/repositories/block.repository.ts @@ -187,10 +187,12 @@ export class BlockRepository extends BaseRepository< ? attachedBlock : null; - await this.updateOne(id, { + const updates: Partial = { nextBlocks: updatedNextBlocks, attachedBlock: updatedAttachedBlock, - }); + }; + + await this.updateOne(id, updates); } catch (error) { this.logger?.error( `Failed to update block ${id} during in-category scope update.`, @@ -214,23 +216,26 @@ export class BlockRepository extends BaseRepository< ): Promise { for (const block of otherBlocks) { try { - // If block has an attachedBlock, we only need to check that + const updates: Partial = {}; + + // Check if the block has an attachedBlock if (block.attachedBlock) { if (ids.includes(block.attachedBlock)) { - await this.updateOne(block.id, { attachedBlock: null }); + updates.attachedBlock = null; + } + } else { + // Only check nextBlocks if there is no attachedBlock + const filteredNextBlocks = block.nextBlocks?.filter( + (nextBlock) => !ids.includes(nextBlock), + ); + + if (filteredNextBlocks?.length !== block.nextBlocks?.length) { + updates.nextBlocks = filteredNextBlocks || []; } - continue; // Skip nextBlocks check since it can't have both } - // Only check nextBlocks if there is no attachedBlock - const filteredNextBlocks = block.nextBlocks?.filter( - (nextBlock) => !ids.includes(nextBlock), - ); - - if (filteredNextBlocks?.length !== block.nextBlocks?.length) { - await this.updateOne(block.id, { - nextBlocks: filteredNextBlocks || [], - }); + if (Object.keys(updates).length > 0) { + await this.updateOne(block.id, updates); } } catch (error) { this.logger?.error(