mirror of
https://github.com/hexastack/hexabot
synced 2025-06-26 18:27:28 +00:00
fix: remove links refering to moved blocks
This commit is contained in:
@@ -205,17 +205,30 @@ export class BlockRepository extends BaseRepository<
|
||||
otherBlocks: Block[],
|
||||
ids: string[],
|
||||
): Promise<void> {
|
||||
// Fetch all moved blocks with their nextBlocks
|
||||
const movedBlocks = await this.find({ _id: { $in: ids } });
|
||||
|
||||
// Build a set of all blocks the moved blocks are pointing to
|
||||
const movedBlockLinkTargets = new Set<string>();
|
||||
for (const block of movedBlocks) {
|
||||
block.nextBlocks?.forEach((id) => movedBlockLinkTargets.add(id));
|
||||
}
|
||||
|
||||
for (const block of otherBlocks) {
|
||||
if (block.attachedBlock && ids.includes(block.attachedBlock)) {
|
||||
await this.updateOne(block.id, { attachedBlock: null });
|
||||
}
|
||||
|
||||
const nextBlocks = block.nextBlocks?.filter(
|
||||
(nextBlock) => !ids.includes(nextBlock),
|
||||
);
|
||||
if (block.nextBlocks?.length) {
|
||||
const updatedNextBlocks = block.nextBlocks.filter((nextBlockId) => {
|
||||
return (
|
||||
!ids.includes(nextBlockId) || movedBlockLinkTargets.has(block.id)
|
||||
);
|
||||
});
|
||||
|
||||
if (nextBlocks?.length) {
|
||||
await this.updateOne(block.id, { nextBlocks });
|
||||
if (updatedNextBlocks.length !== block.nextBlocks.length) {
|
||||
await this.updateOne(block.id, { nextBlocks: updatedNextBlocks });
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user