@@ -395,7 +395,7 @@ async function updateOrg (req, res, next) {
395395
396396 // update org
397397 let result = await orgRepo . updateByOrgUUID ( org . UUID , newOrg )
398- if ( result . n === 0 ) {
398+ if ( result . matchedCount === 0 ) {
399399 logger . info ( { uuid : req . ctx . uuid , message : shortName + ' organization could not be updated in MongoDB because it does not exist.' } )
400400 return res . status ( 404 ) . json ( error . orgDnePathParam ( shortName ) )
401401 }
@@ -450,34 +450,42 @@ async function createUser (req, res, next) {
450450 return res . status ( 400 ) . json ( error . userLimitReached ( ) )
451451 }
452452
453- Object . keys ( req . ctx . body ) . forEach ( k => {
454- const key = k . toLowerCase ( )
453+ const body = req . ctx . body
454+ const keys = Object . keys ( body )
455455
456- if ( key === 'username' ) {
457- newUser . username = req . ctx . body . username
458- } else if ( key === 'authority' ) {
459- if ( req . ctx . body . authority . active_roles ) {
460- newUser . authority . active_roles = [ ...new Set ( req . ctx . body . authority . active_roles ) ] // Removes any duplicate strings from array
461- }
462- } else if ( key === 'name' ) {
463- if ( req . ctx . body . name . first ) {
464- newUser . name . first = req . ctx . body . name . first
465- }
466- if ( req . ctx . body . name . last ) {
467- newUser . name . last = req . ctx . body . name . last
468- }
469- if ( req . ctx . body . name . middle ) {
470- newUser . name . middle = req . ctx . body . name . middle
471- }
472- if ( req . ctx . body . name . suffix ) {
473- newUser . name . suffix = req . ctx . body . name . suffix
474- }
475- } else if ( key === 'org_uuid' ) {
476- return res . status ( 400 ) . json ( error . uuidProvided ( 'org' ) )
477- } else if ( key === 'uuid' ) {
456+ for ( const keyRaw of keys ) {
457+ const key = keyRaw . toLowerCase ( )
458+
459+ if ( key === 'uuid' ) {
478460 return res . status ( 400 ) . json ( error . uuidProvided ( 'user' ) )
479461 }
480- } )
462+
463+ if ( key === 'org_uuid' ) {
464+ return res . status ( 400 ) . json ( error . uuidProvided ( 'org' ) )
465+ }
466+
467+ const handlers = {
468+ username : ( ) => {
469+ newUser . username = body . username
470+ } ,
471+ authority : ( ) => {
472+ if ( body . authority ?. active_roles ) {
473+ newUser . authority . active_roles = [ ...new Set ( body . authority . active_roles ) ]
474+ }
475+ } ,
476+ name : ( ) => {
477+ const name = body . name || { }
478+ if ( name . first ) newUser . name . first = name . first
479+ if ( name . last ) newUser . name . last = name . last
480+ if ( name . middle ) newUser . name . middle = name . middle
481+ if ( name . suffix ) newUser . name . suffix = name . suffix
482+ }
483+ }
484+
485+ if ( handlers [ key ] ) {
486+ handlers [ key ] ( ) // execute the appropriate handler
487+ }
488+ }
481489
482490 const requesterOrgUUID = await orgRepo . getOrgUUID ( requesterShortName )
483491 const isSecretariat = await orgRepo . isSecretariatUUID ( requesterOrgUUID )
@@ -711,7 +719,7 @@ async function updateUser (req, res, next) {
711719 newUser . authority . active_roles = duplicateCheckedRoles
712720
713721 let result = await userRepo . updateByUserNameAndOrgUUID ( username , orgUUID , newUser )
714- if ( result . n === 0 ) {
722+ if ( result . matchedCount === 0 ) {
715723 logger . info ( { uuid : req . ctx . uuid , message : 'The user could not be updated because ' + username + ' does not exist for ' + shortName + ' organization.' } )
716724 return res . status ( 404 ) . json ( error . userDne ( username ) )
717725 }
@@ -786,7 +794,7 @@ async function resetSecret (req, res, next) {
786794 const randomKey = cryptoRandomString ( { length : getConstants ( ) . CRYPTO_RANDOM_STRING_LENGTH } )
787795 oldUser . secret = await argon2 . hash ( randomKey ) // store in db
788796 const user = await userRepo . updateByUserNameAndOrgUUID ( oldUser . username , orgUUID , oldUser )
789- if ( user . n === 0 ) {
797+ if ( user . matchedCount === 0 ) {
790798 logger . info ( { uuid : req . ctx . uuid , message : 'The user could not be updated because ' + username + ' does not exist for ' + orgShortName + ' organization.' } )
791799 return res . status ( 404 ) . json ( error . userDne ( username ) )
792800 }
0 commit comments