Skip to content
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

Minor changes in removeOrganization.ts to enhance the performance and efficiency #1261

Closed
nishant3721 opened this issue Apr 6, 2023 · 2 comments · Fixed by #1263
Closed
Assignees
Labels
bug Something isn't working

Comments

@nishant3721
Copy link
Contributor

nishant3721 commented Apr 6, 2023

Describe the bug
The removeOrganization.ts file has many flaws which can cause slower performance like,

  • The organization.posts.forEach loop uses async/await syntax to delete each post and comments associated with it one by one, which can result in slower performance. A better approach would be to use Promise.all to run all the delete operations concurrently.
  • In the current implementation, when removing an organization, we are fetching each user that has joined the organization and removing the organization ID from their joinedOrganizations field one by one. This is not efficient for large organizations with many members. A better approach would be to use the $pull operator to remove the organization ID from all affected users' joinedOrganizations field in one go.
  • The code has several nested if statements to handle errors and check user roles. This can make the code harder to read and follow. We can use early returns to exit the function early.
  • The code currently calls the lean() method on several MongoDB queries. While this can improve performance, it can also cause issues with accessing related models or fields. Unless you're sure you don't need the functionality provided by the full model object, it's better to avoid calling lean().

To Reproduce
Steps to reproduce the behavior:

  1. run the local server 'using npm run dev'
  2. run the query removeOrganization

Expected behavior
The following changes can result in enhanced performance,

  • Remove unnecessary lean() calls
  • Use $pull operator to remove organization IDs from users' fields.
  • Use Promise.all to delete posts and comments associated with the organization.

Actual behavior
The current implementation can can result in slow performance

Screenshots
image
image

Additional details
Not required

Potential internship candidates
Please read this if you are planning to apply for a Palisadoes Foundation internship PalisadoesFoundation/talawa#359

@nishant3721 nishant3721 added the bug Something isn't working label Apr 6, 2023
@nishant3721
Copy link
Contributor Author

@palisadoes I would like to work on it.

@github-actions github-actions bot added the unapproved Unapproved for Pull Request label Apr 6, 2023
@nishant3721 nishant3721 changed the title Use Promise.all to delete posts and comments associated with the organization Minor changes in removeOrganization.ts to enhance the performance and efficiency Apr 6, 2023
@palisadoes palisadoes removed the unapproved Unapproved for Pull Request label Apr 6, 2023
@palisadoes
Copy link
Contributor

Add @xoldyckk to the PR reviewers for this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants