This commit is contained in:
Mohamed Chedli Ben Yaghlane 2025-06-12 15:17:41 +01:00 committed by GitHub
commit 0a37959377
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 57 additions and 24 deletions

View File

@ -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);
}
}
@ -178,6 +178,7 @@ export class BlockRepository extends BaseRepository<
});
for (const { id, nextBlocks, attachedBlock } of blocks) {
try {
const updatedNextBlocks = nextBlocks.filter((nextBlock) =>
ids.includes(nextBlock),
);
@ -186,10 +187,18 @@ export class BlockRepository extends BaseRepository<
? attachedBlock
: null;
await this.updateOne(id, {
const updates: Partial<Block> = {
nextBlocks: updatedNextBlocks,
attachedBlock: updatedAttachedBlock,
});
};
await this.updateOne(id, updates);
} catch (error) {
this.logger?.error(
`Failed to update block ${id} during in-category scope update.`,
error,
);
}
}
}
@ -206,16 +215,33 @@ export class BlockRepository extends BaseRepository<
ids: string[],
): Promise<void> {
for (const block of otherBlocks) {
if (block.attachedBlock && ids.includes(block.attachedBlock)) {
await this.updateOne(block.id, { attachedBlock: null });
}
try {
const updates: Partial<Block> = {};
const nextBlocks = block.nextBlocks?.filter(
// Check if the block has an attachedBlock
if (block.attachedBlock) {
if (ids.includes(block.attachedBlock)) {
updates.attachedBlock = null;
}
} else {
// Only check nextBlocks if there is no attachedBlock
const filteredNextBlocks = block.nextBlocks?.filter(
(nextBlock) => !ids.includes(nextBlock),
);
if (nextBlocks?.length) {
await this.updateOne(block.id, { nextBlocks });
if (filteredNextBlocks?.length !== block.nextBlocks?.length) {
updates.nextBlocks = filteredNextBlocks || [];
}
}
if (Object.keys(updates).length > 0) {
await this.updateOne(block.id, updates);
}
} catch (error) {
this.logger?.error(
`Failed to update block ${block.id} during out-of-category scope update.`,
error,
);
}
}
}

View File

@ -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",

View File

@ -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",

View File

@ -559,9 +559,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"));
},
},
);