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

Added bulkWrite to temp/deleteaccount API #126

Merged
merged 1 commit into from
Sep 10, 2023
Merged
Changes from all commits
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
43 changes: 38 additions & 5 deletions controllers/Temp.js
Original file line number Diff line number Diff line change
Expand Up @@ -5414,6 +5414,21 @@ class TempController {

mongoose.startSession().then(session => {
session.startTransaction()

const dbUpdates = [
{
updateOne: {
filter: {_id: {$eq: userGettingFollowed._id}},
update: {$pull : {followers: userFollowingFound.secondId}}
}
},
{
updateOne: {
filter: {_id: {$eq: userId}},
update: { $pull : {following: userGettingFollowed.secondId}}
}
}
]

Promise.all([
popularPosts.length !== newPopularPosts.length ? PopularPosts.findOneAndUpdate({}, {popularPosts: newPopularPosts}) : Promise.resolve('Popular posts do not need to be updated'),
Expand All @@ -5424,17 +5439,35 @@ class TempController {
...threadImageKeys.map(key => fs.promises.unlink(path.resolve(process.env.UPLOADED_PATH, key))),
Thread.deleteMany({creatorId: {$eq: userId}}),
Message.deleteMany({senderId: {$eq: userId}}),
User.updateMany({followers: userFound.secondId}, {$pull: {followers: userFound.secondId}}),
User.updateMany({following: userFound.secondId}, {$pull: {following: userFound.secondId}}),
User.updateMany({blockedAccounts: userFound.secondId}, {$pull: {blockedAccounts: userFound.secondId}}),
User.bulkWrite([
{
updateMany: {
filter: {followers: userFound.secondId},
update: {$pull: {followers: userFound.secondId}}
},
updateMany: {
filter: {following: userFound.secondId},
update: {$pull: {following: userFound.secondId}}
},
updateMany: {
filter: {blockedAccounts: userFound.secondId},
update: {$pull: {blockedAccounts: userFound.secondId}}
},
updateMany: {
filter: {accountFollowRequests: userFound.secondId},
update: {$pull: {accountFollowRequests: userFound.secondId}}
},
deleteOne: {
filter: {_id: {$eq: userId}},
}
}
]),
Downvote.deleteMany({userPublicId: userFound.secondId}),
Upvote.deleteMany({userPublicId: userFound.secondId}),
User.updateMany({accountFollowRequests: userFound.secondId}, {$pull: {accountFollowRequests: userFound.secondId}}),
AccountReports.deleteMany({reporterId: {$eq: userId}}),
PostReports.deleteMany({reporterId: {$eq: userId}}),
RefreshToken.deleteMany({userId: {$eq: userId}}),
Category.updateMany({}, {$pull: {members: userId}}),
User.deleteOne({_id: {$eq: userId}})
]).then(() => session.commitTransaction()).then(() => {
console.log('User with id:', userId, 'has been successfully deleted along with all associated data.')
session.endSession().catch(error => {
Expand Down