From e211aa36ba4e6d3411df317a79e983d8415d46fd Mon Sep 17 00:00:00 2001 From: medchedli Date: Thu, 12 Jun 2025 12:20:25 +0100 Subject: [PATCH] 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(