From 6f5acbd11ba1b354be81c1a5a83ac30f7b888223 Mon Sep 17 00:00:00 2001 From: yu-i-i Date: Tue, 27 Jan 2026 17:55:34 +0100 Subject: [PATCH] Admin Tools: Fix user name fix user name is not updated fix first_name/last_name is undefined --- .../app/src/UserListController.mjs | 48 ++++++++++++++----- .../js/project-list/util/sort-projects.ts | 17 +++++-- .../frontend/js/user-list/util/sort-users.ts | 10 ++-- 3 files changed, 54 insertions(+), 21 deletions(-) diff --git a/services/web/modules/admin-tools/app/src/UserListController.mjs b/services/web/modules/admin-tools/app/src/UserListController.mjs index c5087cdb63..7976ad63e5 100644 --- a/services/web/modules/admin-tools/app/src/UserListController.mjs +++ b/services/web/modules/admin-tools/app/src/UserListController.mjs @@ -467,15 +467,25 @@ async function updateUser(req, res, next) { req.logger.addFields({ actorUserId }) const { body } = req - const projection = Object.fromEntries(Object.keys(body).map(k => [k, 1])) + const updatesInput = { ...body } + if ('firstName' in updatesInput) { + updatesInput.first_name = updatesInput.firstName + delete updatesInput.firstName + } + if ('lastName' in updatesInput) { + updatesInput.last_name = updatesInput.lastName + delete updatesInput.lastName + } + + const projection = Object.fromEntries(Object.keys(updatesInput).map(k => [k, 1])) const user = await User.findById(userId, projection).exec() - if (user == null) { + if (!user) { throw new OError('problem updating user settings', { userId }) } let emailIsUpdated = false - const newEmail = body.email?.trim().toLowerCase() + const newEmail = updatesInput.email?.trim().toLowerCase() if (newEmail != null && newEmail !== user.email) { // email is updated if (newEmail.indexOf('@') === -1) { const message = req.i18n.translate('email_address_is_invalid') @@ -502,31 +512,45 @@ async function updateUser(req, res, next) { } } if (userId == actorUserId) { - SessionManager.setInSessionUser(req.session, { - email: newEmail, - }) + SessionManager.setInSessionUser(req.session, { email: newEmail }) } } const update = {} - for (const [key, value] of Object.entries(body)) { - if (key === "email") continue - if (value === user[key]) continue - update[key] = typeof value === "string" ? value.trim() : value + + for (let [key, value] of Object.entries(updatesInput)) { + if (key === 'email') continue + + const newValue = typeof value === 'string' ? value.trim() : value + if (newValue === user[key]) continue + + update[key] = newValue } + Object.assign(user, update) try { await user.save() - } catch (err) { + } catch { throw new OError('problem updating user settings', { userId }) } + if (userId == actorUserId) { const sessionUpdate = {} if (update.first_name != null) sessionUpdate.first_name = update.first_name if (update.last_name != null) sessionUpdate.last_name = update.last_name SessionManager.setInSessionUser(req.session, sessionUpdate) } - if (emailIsUpdated) update["email"] = newEmail + + if (emailIsUpdated) update.email = newEmail + + if (update.first_name != null) { + update.firstName = update.first_name + delete update.first_name + } + if (update.last_name != null) { + update.lastName = update.last_name + delete update.last_name + } return res.json(update) } diff --git a/services/web/modules/admin-tools/frontend/js/project-list/util/sort-projects.ts b/services/web/modules/admin-tools/frontend/js/project-list/util/sort-projects.ts index 0ebfb95407..b1ff081a8d 100644 --- a/services/web/modules/admin-tools/frontend/js/project-list/util/sort-projects.ts +++ b/services/web/modules/admin-tools/frontend/js/project-list/util/sort-projects.ts @@ -7,6 +7,15 @@ const order = (order: SortingOrder, projects: Project[]) => { return order === 'asc' ? [...projects] : projects.reverse() } +function cmp(a, b) { + const aEmpty = a == null || a === "" + const bEmpty = b == null || b === "" + if (aEmpty && bEmpty) return Compare.SORT_KEEP_ORDER + if (aEmpty) return Compare.SORT_A_AFTER_B + if (bEmpty) return Compare.SORT_A_BEFORE_B + return a.localeCompare(b) +} + export const ownerNameComparator = (getUserById: (userId: string) => User | null) => (v1: Project, v2: Project) => { @@ -26,11 +35,11 @@ export const ownerNameComparator = return Compare.SORT_A_BEFORE_B } - const lastNameCmp = user1.lastName.localeCompare(user2.lastName) - if (lastNameCmp !== 0) return lastNameCmp + const lastNameCmp = cmp(user1.lastName, user2.lastName) + if (lastNameCmp !== Compare.SORT_KEEP_ORDER) return lastNameCmp - const firstNameCmp = user1.firstName.localeCompare(user2.firstName) - if (firstNameCmp !== 0) return firstNameCmp + const firstNameCmp = cmp(user1.firstName, user2.firstName) + if (firstNameCmp !== Compare.SORT_KEEP_ORDER) return firstNameCmp return v1.lastUpdated < v2.lastUpdated ? Compare.SORT_A_BEFORE_B diff --git a/services/web/modules/admin-tools/frontend/js/user-list/util/sort-users.ts b/services/web/modules/admin-tools/frontend/js/user-list/util/sort-users.ts index ca4aee0a5b..deabb5cdfd 100644 --- a/services/web/modules/admin-tools/frontend/js/user-list/util/sort-users.ts +++ b/services/web/modules/admin-tools/frontend/js/user-list/util/sort-users.ts @@ -6,18 +6,18 @@ const order = (order: SortingOrder, users: User[]) => { return order === 'asc' ? [...users] : users.reverse() } - function cmp(a, b) { - const aEmpty = (a === "") - const bEmpty = (b === "") + const aEmpty = a == null || a === "" + const bEmpty = b == null || b === "" if (aEmpty && bEmpty) return Compare.SORT_KEEP_ORDER - if (aEmpty !== bEmpty) return aEmpty ? Compare.SORT_A_AFTER_B : Compare.SORT_A_BEFORE_B + if (aEmpty) return Compare.SORT_A_AFTER_B + if (bEmpty) return Compare.SORT_A_BEFORE_B return a.localeCompare(b) } export const userNameComparator = (v1: User, v2: User) => { const res = cmp(v1.lastName, v2.lastName) - if (res !== 0) return res + if (res !== Compare.SORT_KEEP_ORDER) return res return cmp(v1.firstName, v2.firstName) }