lib/db/db.js
/**
 * Updates a user from the database
 *
 * @param {Object} bundle The ID or the username must be specified, it will otherwise throw an error.
 * @param {number} [bundle.id]
 * @param {string} [bundle.username]
 * @param {Object} params
 * @param {string} [params.username]
 * @param {string} [params.email]
 * @param {string} [params.password]
 * @throws {TypeError} This error is thrown when the bundle object is not correctly passed.
 * @returns {Promise<boolean>}
 * @throws {DatabaseError} This error is thrown when the user does not exist.
 * @example // Updates the user with id=1 to have username 'newUsername'
 * const user = await updateUserFromDB({id: 1}, {username: 'newUsername'});
 * 
 */
Updates a user from the database
@param {Object} bundle The ID or the username must be specified, it will otherwise throw an error.
Updates a user from the database @param { Object } bundle The ID or the username must be specified it will otherwise throw an error .
async function UpdateUserFromDB(bundle, params){
    const VALID_PROPERTIES = ['username', 'password', 'email'];
    const toUpdate = VALID_PROPERTIES.filter((value) => Object.keys(params).includes(value));
    if(!(toUpdate.length)){
        throw new TypeError('Params object is not correct');
    }
    if(!(await UserExistsInDB(bundle))) throw new DatabaseError('User does not exist');
    let updater = {};
    for (const key of toUpdate) {
        if(params[key]) {
            if (key === 'password') {
                const hash = bcrypt.hashSync(params[key], credentials.bcryptConfig.saltRounds);
                updater[key] = hash;
            } else {
                updater[key] = params[key];
            }
        }
    }
    if(bundle.id){
        return knex('users').where('id', bundle.id).update(updater);
    }else if(bundle.username){
        return knex('users').where('username', bundle.username).update(updater);
    }
}
async function UpdateUserFromDB ( bundle , params ) { const VALID_PROPERTIES = [ 'username' , 'password' , 'email' ] ; const toUpdate = VALID_PROPERTIES . filter ( ( value ) => Object . keys ( params ) . includes ( value ) ) ; if ( ! ( toUpdate . length ) ) { throw new TypeError ( 'Params object is not correct' ) ; } if ( ! ( await UserExistsInDB ( bundle ) ) ) throw new DatabaseError ( 'User does not exist' ) ; let updater = { } ; for ( const key of toUpdate ) { if ( params [ key ] ) { if ( key === 'password' ) { const hash = bcrypt . hashSync ( params [ key ] , credentials . bcryptConfig . saltRounds ) ; updater [ key ] = hash ; } else { updater [ key ] = params [ key ] ; } } } if ( bundle . id ) { return knex ( 'users' ) . where ( 'id' , bundle . id ) . update ( updater ) ; } else if ( bundle . username ) { return knex ( 'users' ) . where ( 'username' , bundle . username ) . update ( updater ) ; } }
Updates a user from the database
 @param {Object} bundle The ID or the username must be specified, it will otherwise throw an error.
Updates a user from the database @param { Object } bundle The ID or the username must be specified it will otherwise throw an error .
//If the params object has none of the VALID_PROPERTIES defined, then send out an error // If the user does not exist throw error //Build a new object that will be used to update the user properties