wiki/server/models/users.js
/**
   * Update an existing user
   *
   * @param {Object} param0 User ID and fields to update
   */
Update an existing user
@param {Object} param0 User ID and fields to update
Update an existing user @param { Object } param0 User ID and fields to update
static async updateUser ({ id, email, name, newPassword, groups, location, jobTitle, timezone, dateFormat, appearance }) {
    const usr = await WIKI.models.users.query().findById(id)
    if (usr) {
      let usrData = {}
      if (!_.isEmpty(email) && email !== usr.email) {
        const dupUsr = await WIKI.models.users.query().select('id').where({
          email,
          providerKey: usr.providerKey
        }).first()
        if (dupUsr) {
          throw new WIKI.Error.AuthAccountAlreadyExists()
        }
        usrData.email = _.toLower(email)
      }
      if (!_.isEmpty(name) && name !== usr.name) {
        usrData.name = _.trim(name)
      }
      if (!_.isEmpty(newPassword)) {
        if (newPassword.length < 6) {
          throw new WIKI.Error.InputInvalid('Password must be at least 6 characters!')
        }
        usrData.password = newPassword
      }
      if (_.isArray(groups)) {
        const usrGroupsRaw = await usr.$relatedQuery('groups')
        const usrGroups = _.map(usrGroupsRaw, 'id')
        const addUsrGroups = _.difference(groups, usrGroups)
        for (const grp of addUsrGroups) {
          await usr.$relatedQuery('groups').relate(grp)
        }
        const remUsrGroups = _.difference(usrGroups, groups)
        for (const grp of remUsrGroups) {
          await usr.$relatedQuery('groups').unrelate().where('groupId', grp)
        }
      }
      if (!_.isEmpty(location) && location !== usr.location) {
        usrData.location = _.trim(location)
      }
      if (!_.isEmpty(jobTitle) && jobTitle !== usr.jobTitle) {
        usrData.jobTitle = _.trim(jobTitle)
      }
      if (!_.isEmpty(timezone) && timezone !== usr.timezone) {
        usrData.timezone = timezone
      }
      if (!_.isNil(dateFormat) && dateFormat !== usr.dateFormat) {
        usrData.dateFormat = dateFormat
      }
      if (!_.isNil(appearance) && appearance !== usr.appearance) {
        usrData.appearance = appearance
      }
      await WIKI.models.users.query().patch(usrData).findById(id)
    } else {
      throw new WIKI.Error.UserNotFound()
    }
  }
static async updateUser ( { id , email , name , newPassword , groups , location , jobTitle , timezone , dateFormat , appearance } ) { const usr = await WIKI . models . users . query ( ) . findById ( id ) if ( usr ) { let usrData = { } if ( ! _ . isEmpty ( email ) && email !== usr . email ) { const dupUsr = await WIKI . models . users . query ( ) . select ( 'id' ) . where ( { email , providerKey : usr . providerKey } ) . first ( ) if ( dupUsr ) { throw new WIKI . Error . AuthAccountAlreadyExists ( ) } usrData . email = _ . toLower ( email ) } if ( ! _ . isEmpty ( name ) && name !== usr . name ) { usrData . name = _ . trim ( name ) } if ( ! _ . isEmpty ( newPassword ) ) { if ( newPassword . length < 6 ) { throw new WIKI . Error . InputInvalid ( 'Password must be at least 6 characters!' ) } usrData . password = newPassword } if ( _ . isArray ( groups ) ) { const usrGroupsRaw = await usr . $relatedQuery ( 'groups' ) const usrGroups = _ . map ( usrGroupsRaw , 'id' ) const addUsrGroups = _ . difference ( groups , usrGroups ) for ( const grp of addUsrGroups ) { await usr . $relatedQuery ( 'groups' ) . relate ( grp ) } const remUsrGroups = _ . difference ( usrGroups , groups ) for ( const grp of remUsrGroups ) { await usr . $relatedQuery ( 'groups' ) . unrelate ( ) . where ( 'groupId' , grp ) } } if ( ! _ . isEmpty ( location ) && location !== usr . location ) { usrData . location = _ . trim ( location ) } if ( ! _ . isEmpty ( jobTitle ) && jobTitle !== usr . jobTitle ) { usrData . jobTitle = _ . trim ( jobTitle ) } if ( ! _ . isEmpty ( timezone ) && timezone !== usr . timezone ) { usrData . timezone = timezone } if ( ! _ . isNil ( dateFormat ) && dateFormat !== usr . dateFormat ) { usrData . dateFormat = dateFormat } if ( ! _ . isNil ( appearance ) && appearance !== usr . appearance ) { usrData . appearance = appearance } await WIKI . models . users . query ( ) . patch ( usrData ) . findById ( id ) } else { throw new WIKI . Error . UserNotFound ( ) } }
Update an existing user
 @param {Object} param0 User ID and fields to update
Update an existing user @param { Object } param0 User ID and fields to update
// Relate added groups // Unrelate removed groups