Skip to content

fix: remove links refering to moved blocks #952

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 18 additions & 5 deletions api/src/chat/repositories/block.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 });
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wouldn't it better to use an array of promises inside the loop and then resolving them all in one single call at the end when we exit the loop using Promise.all instead of redundant calls ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes @MohamedAliBouhaouala you're right I'll apply your suggestion thanks 🙏

}

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 });
}
}
}
}
Expand Down