Skip to content

Commit

Permalink
Return all name fields of a user even if they are empty. Partial fix …
Browse files Browse the repository at this point in the history
…for issue 436.
  • Loading branch information
cristina479 committed Jun 10, 2021
1 parent 7b22845 commit 2605129
Showing 1 changed file with 27 additions and 2 deletions.
29 changes: 27 additions & 2 deletions src/controller/org.controller/org.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ async function updateOrg (req, res, next) {
const addRoles = []
const orgRepo = req.ctx.repositories.getOrgRepository()
const org = await orgRepo.findOneByShortName(shortName)
let agt = setAggregateUserObj({ short_name: shortName })
let agt = setAggregateOrgObj({ short_name: shortName })

// org doesn't exist
if (!org) {
Expand Down Expand Up @@ -322,7 +322,7 @@ async function updateOrg (req, res, next) {
// updating the org's shortname (the shortname field is never an empty string due to the stripping of the quotes and double quotes)
if (shortname) {
newOrg.short_name = shortname
agt = setAggregateUserObj({ short_name: newOrg.short_name })
agt = setAggregateOrgObj({ short_name: newOrg.short_name })
const result = await orgRepo.findOneByShortName(newOrg.short_name)

if (result) {
Expand Down Expand Up @@ -407,6 +407,9 @@ async function createUser (req, res, next) {
return res.status(400).json(error.userExists(newUser.username))
}

// Parsing all user name fields
newUser.name = parseUserName(newUser)

await userRepo.updateByUserNameAndOrgUUID(newUser.username, newUser.org_UUID, newUser, { upsert: true }) // Create user in MongoDB if it doesn't exist
const agt = setAggregateUserObj({ username: newUser.username, org_UUID: newUser.org_UUID })
result = await userRepo.aggregate(agt)
Expand Down Expand Up @@ -733,6 +736,28 @@ function setAggregateUserObj (query) {
]
}

function parseUserName (newUser) {
if (newUser.name) {
if (!newUser.name.first) {
newUser.name.first = ''
}
if (!newUser.name.last) {
newUser.name.last = ''
}
if (!newUser.name.middle) {
newUser.name.middle = ''
}
if (!newUser.name.surname) {
newUser.name.surname = ''
}
if (!newUser.name.suffix) {
newUser.name.suffix = ''
}
}

return newUser.name
}

module.exports = {
ORG_ALL: getOrgs,
ORG_SINGLE: getOrg,
Expand Down

0 comments on commit 2605129

Please sign in to comment.