import { isEmpty, pick } from 'lodash'
import scrypt from 'scrypt-for-humans'

import User from 'model/user'
import { ValidationError } from 'util/error'

const user = {
  read(options) {
    const { id } = options.params

    return User.findById(id)
  },

  browse(options) {
    const attrs = ['id', 'email']
    const filter = pick(options.query, attrs)

    return User.find(filter)
  },

  async add(user, options) {
    const { email } = user

    if (!email) throw new ValidationError('User has no email')

    const saved = await User.find({ email })

    if (!isEmpty(saved)) throw new ValidationError('User already exists')

    // Hashes user password
    user.password = await scrypt.hash(user.password)

    return User.insert(user)
  },

  edit(user, options) {
    user.id = options.params.id

    return User.update(user)
  },

  destroy(options) {
    const { id } = options.params

    return User.remove(id)
  }
}

export default user

@@ -167,96 +167,8 @@
 = %7B%0A
-  read(options) %7B%0A    const %7B id %7D = options.params%0A%0A    return User.findById(id)%0A  %7D,%0A%0A
   br
@@ -299,16 +299,196 @@
 )%0A  %7D,%0A%0A
+  read(options) %7B%0A    const %7B id %7D = options.params%0A%0A    return User.findById(id)%0A  %7D,%0A%0A  edit(user, options) %7B%0A    user.id = options.params.id%0A%0A    return User.update(user)%0A  %7D,%0A%0A
   async 
@@ -842,100 +842,8 @@
 %7D,%0A%0A
-  edit(user, options) %7B%0A    user.id = options.params.id%0A%0A    return User.update(user)%0A  %7D,%0A%0A
   de

rearrange order