Skip to content

Commit 7b7e9d1

Browse files
Merge pull request #1909 from OneCommunityGlobal/venkataramanan_improve_performance_of_update_user_info
Venkataramanan fix: performance of update user information
2 parents 76866da + a3c7cfa commit 7b7e9d1

1 file changed

Lines changed: 17 additions & 7 deletions

File tree

src/controllers/userProfileController.js

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2150,14 +2150,24 @@ const userProfileController = function (UserProfile, Project) {
21502150
const updateUserInformation = async function (req, res) {
21512151
try {
21522152
const data = req.body;
2153-
data.map(async (e) => {
2154-
const result = await UserProfile.findById(e.user_id);
2155-
result[e.item] = e.value;
2156-
await result.save();
2157-
});
2158-
res.status(200).send({ message: 'Update successful' });
2153+
2154+
if (!Array.isArray(data) || data.length === 0) {
2155+
return res.status(400).send({ error: 'No updates provided' });
2156+
}
2157+
2158+
const ops = data.map(({ user_id, item, value }) => ({
2159+
updateOne: {
2160+
filter: { _id: user_id },
2161+
update: { $set: { [item]: value } },
2162+
},
2163+
}));
2164+
2165+
await UserProfile.bulkWrite(ops);
2166+
2167+
return res.status(200).send({ message: 'Update successful' });
21592168
} catch (error) {
2160-
return res.status(500);
2169+
console.error('Error updating user information:', error);
2170+
return res.status(500).send({ error: 'Internal server error' });
21612171
}
21622172
};
21632173

0 commit comments

Comments
 (0)