import User from '../models/User'

export const getUserById = (userId) => {
    return User.findById(userId).select('-password').exec()
}

export const getAllUsers = () => {
    return User.find().select('-password').exec()
}

export const getUsersByPage = (page, nPerPage) => {
    return User.find().select('-password')
        .skip(page * nPerPage)
        .limit(nPerPage)
        .exec()
}

export const postNewUser = (body) => {
    const newUser = new User({
        firstName: body.firstName,
        lastName: body.lastName,
        email : body.email,
        password: body.password,
        createdAt: body.createdAt,
    })
    return newUser.save()
}

export const updateUser = (userId, body) => {
    return User.findById(userId).exec()
        .then(function(user) {
            if (user) {
                user.firstName = body.firstName
                user.lastName = body.lastName
                user.password = body.password
                user.email = body.email
                user.createdAt = (body.createdAt || user.createdAt)
                return user.save()
            }
            throw new Error('User not found')
        })
}

export const updateUserInfo = (body) => {
    return User.findById(body._id).exec()
        .then(function(user) {
            if (user) {
                user.firstName = body.firstName
                user.lastName = body.lastName
                return user.save()
            }
            throw new Error('User not found')
        })
}

export const updateUserPassword = (_id, newPassword) => {
    return User.findById(_id).exec()
        .then(function(user) {
            if (user) {
                user.password = newPassword
                return user.save()
            }
            throw new Error('User not found')
        })
}

export const removeUser = (userId) => {
    return User.findByIdAndRemove(userId).exec()
}

@@ -1049,16 +1049,60 @@
 atedAt)%0A
+                user.isAdmin = body.isAdmin%0A
         
@@ -1839,32 +1839,322 @@
 )%0A        %7D)%0A%7D%0A%0A
+export const updateUserRole = (_id, isAdmin) =%3E %7B%0A    return User.findById(_id).exec()%0A        .then(function(user) %7B%0A            if (user) %7B%0A                user.isAdmin = isAdmin%0A                return user.save()%0A            %7D%0A            throw new Error('User not found')%0A        %7D)%0A%7D%0A%0A
 export const rem

change user role when update user