fix: update name of user when firstName or lastName is updated

This commit is contained in:
2025-10-11 15:02:59 +05:30
parent f2adfc10b6
commit 06781bd998
2 changed files with 14 additions and 2 deletions

View File

@@ -103,7 +103,8 @@ export async function updateUserHandler(
try { try {
const updatedUser = await updateUser(userId, input, req.user); const updatedUser = await updateUser(userId, input, req.user);
if (!updateUser) return res.code(404).send({ error: "resource not found" }); if (!updatedUser)
return res.code(404).send({ error: "resource not found" });
return res.code(200).send(updatedUser); return res.code(200).send(updatedUser);
} catch (err) { } catch (err) {

View File

@@ -182,7 +182,8 @@ export async function updateUser(
) { ) {
throw ErrOpNotValid; throw ErrOpNotValid;
} }
return await userModel
const userInDb = await userModel
.findOneAndUpdate({ pid: userId }, input, { .findOneAndUpdate({ pid: userId }, input, {
new: true, new: true,
}) })
@@ -190,6 +191,16 @@ export async function updateUser(
"_id pid orgId firstName lastName name email role avatar status createdAt createdBy lastLogin" "_id pid orgId firstName lastName name email role avatar status createdAt createdBy lastLogin"
) )
.populate({ path: "orgId", select: "_id pid name avatar" }); .populate({ path: "orgId", select: "_id pid name avatar" });
if (!userInDb) return null;
const name = userInDb.firstName + " " + userInDb.lastName;
if (name != userInDb.name) {
userInDb.name = name;
await userInDb.save();
}
return userInDb;
} }
export async function updateUserInternal( export async function updateUserInternal(