Admin Tools: Fix user name

fix user name is not updated

fix first_name/last_name is undefined
This commit is contained in:
yu-i-i
2026-01-27 17:55:34 +01:00
parent 103adeb820
commit 6f5acbd11b
3 changed files with 54 additions and 21 deletions

View File

@@ -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)
}

View File

@@ -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

View File

@@ -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)
}