fix: update block repository logic to correctly reference source category and handle block updates

This commit is contained in:
medchedli 2025-06-04 17:18:38 +01:00
parent 9cde4f85bc
commit 266730ba81

View File

@ -140,12 +140,12 @@ export class BlockRepository extends BaseRepository<
// Step 1: Map IDs and Category // Step 1: Map IDs and Category
const objIds = ids.map((id) => new Types.ObjectId(id)); 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({ const otherBlocks = await this.find({
_id: { $nin: objIds }, _id: { $nin: objIds },
category: { $ne: objCategoryId }, category: sourceCategoryId,
$or: [ $or: [
{ attachedBlock: { $in: objIds } }, { attachedBlock: { $in: objIds } },
{ nextBlocks: { $in: objIds } }, { nextBlocks: { $in: objIds } },
@ -154,7 +154,7 @@ export class BlockRepository extends BaseRepository<
// Step 3: Update blocks in the provided scope // Step 3: Update blocks in the provided scope
await this.prepareBlocksInCategoryUpdateScope(categoryId, ids); await this.prepareBlocksInCategoryUpdateScope(categoryId, ids);
// Step 4: Update external blocks // Step 4: Update blocks in source category
await this.prepareBlocksOutOfCategoryUpdateScope(otherBlocks, ids); await this.prepareBlocksOutOfCategoryUpdateScope(otherBlocks, ids);
} }
} }
@ -206,16 +206,21 @@ export class BlockRepository extends BaseRepository<
ids: string[], ids: string[],
): Promise<void> { ): Promise<void> {
for (const block of otherBlocks) { for (const block of otherBlocks) {
// Handle attached block references
if (block.attachedBlock && ids.includes(block.attachedBlock)) { if (block.attachedBlock && ids.includes(block.attachedBlock)) {
await this.updateOne(block.id, { attachedBlock: null }); await this.updateOne(block.id, { attachedBlock: null });
} }
const nextBlocks = block.nextBlocks?.filter( // Handle nextBlocks references
const filteredNextBlocks = block.nextBlocks?.filter(
(nextBlock) => !ids.includes(nextBlock), (nextBlock) => !ids.includes(nextBlock),
); );
if (nextBlocks?.length) { // If the filtered nextBlocks length is different from the original, update the block
await this.updateOne(block.id, { nextBlocks }); if (filteredNextBlocks?.length !== block.nextBlocks?.length) {
await this.updateOne(block.id, {
nextBlocks: filteredNextBlocks || [],
});
} }
} }
} }