import User from '../models/user-model';

const getUser = (req, res) => {
  User.get(req.params.id).then((user) => {
    if (!user) {
      res.status(404).json({ status: 'ok', message: 'User not found' });
    } else {
      res.status(200).json({ status: 'ok', user });
    }
  }).catch(error => res.status(500).json({ status: 'error', error }));
};

const list = (req, res) => {
  // const { limit = 50, skip = 0 } = req.query;
  User.list({}).then((users) => {
    res.status(200).json({ status: 'ok', users });
  }).catch(error => res.status(500).json({ status: 'error', error }));
};

const create = (req, res) => {
  const user = new User({
    email: req.body.email,
    password: req.body.password,
    profile: {
      firstName: req.body.firstName || null,
      lastName: req.body.lastName || null
    }
  });

  user.save().then(() => {
    res.status(201).json({ status: 'ok', message: 'User created' });
    return true;
  }).catch(error => res.status(400).json({ status: 'error', error: error.message }));
};

const update = (req, res) => {
  // The password can't be updated by this method for security reasons
  // the user must always ask for a reset email
  if (req.body.password) {
    res.status(400).json({ status: 'error', error: { msg: 'You can´t change the password with this endpoint' } });
    return;
  } else if (req.body.email || (!req.body.firstName && !req.body.lastName)) {
    res.status(400).json({ status: 'error', error: { msg: 'Wrong request' } });
    return;
  }
  // Check if the user exist
  User.get(req.params.id).then((user) => {
    if (!user) {
      res.status(404).json({ status: 'error', error: { msg: 'User not found' } });
    } else {
      const updatedUser = {
        'profile.firstName': req.body.firstName || user.profile.firstName,
        'profile.lastName': req.body.lastName || user.profile.lastName
      };
      User.update({ _id: req.params.id }, { $set: updatedUser })
        .exec()
        .then(() => {
          res.status(200).json({ status: 'ok', message: 'User updated' });
        })
        .catch(() => {
          res.status(500).json({ status: 'error', message: 'Could no fullfill the request' });
        });
    }
  }).catch(error => res.status(500).json({ status: 'error', error }));
};

export default { list, create, getUser, update };

@@ -52,32 +52,38 @@
 User = (req, res
+, next
 ) =%3E %7B%0A  User.ge
@@ -72,36 +72,47 @@
 xt) =%3E %7B%0A  User.
-get(
+findOne(%7B _id: 
 req.params.id).t
@@ -100,32 +100,34 @@
 d: req.params.id
+ %7D
 ).then((user) =%3E
@@ -305,65 +305,12 @@
 tch(
-error =%3E res.status(500).json(%7B status: 'error', error %7D)
+next
 );%0A%7D
@@ -322,16 +322,21 @@
 nst list
+Users
  = (req,
@@ -343,63 +343,20 @@
  res
+, next
 ) =%3E %7B
-%0A  // const %7B limit = 50, skip = 0 %7D = req.query;
 %0A  U
@@ -452,65 +452,12 @@
 tch(
-error =%3E res.status(500).json(%7B status: 'error', error %7D)
+next
 );%0A%7D
@@ -471,16 +471,20 @@
 t create
+User
  = (req,
@@ -479,32 +479,38 @@
 User = (req, res
+, next
 ) =%3E %7B%0A  const u
@@ -829,80 +829,12 @@
 tch(
-error =%3E res.status(400).json(%7B status: 'error', error: error.message %7D)
+next
 );%0A%7D
@@ -848,16 +848,20 @@
 t update
+User
  = (req,
@@ -864,16 +864,22 @@
 req, res
+, next
 ) =%3E %7B%0A 
@@ -1077,18 +1077,22 @@
 ror: %7B m
-sg
+essage
 : 'You c
@@ -1181,19 +1181,159 @@
 dy.email
- %7C%7C
+) %7B%0A    res.status(400).json(%7B status: 'error', error: %7B message: 'You can%C2%B4t change the email with this endpoint' %7D %7D);%0A    return;%0A  %7D else if
  (!req.b
@@ -1368,17 +1368,16 @@
 astName)
-)
  %7B%0A    r
@@ -1425,18 +1425,22 @@
 ror: %7B m
-sg
+essage
 : 'Wrong
@@ -1447,16 +1447,39 @@
  request
+, profile data required
 ' %7D %7D);%0A
@@ -1534,12 +1534,23 @@
 ser.
-get(
+findOne(%7B _id: 
 req.
@@ -1558,16 +1558,18 @@
 arams.id
+ %7D
 ).then((
@@ -1656,10 +1656,14 @@
  %7B m
-sg
+essage
 : 'U
@@ -1869,16 +1869,82 @@
 lastName
+,%0A        'profile.avatar': req.body.avatar %7C%7C user.profile.avatar
 %0A      %7D
@@ -2153,197 +2153,36 @@
 tch(
-() =%3E %7B%0A          res.status(500).json(%7B status: 'error', message: 'Could no fullfill the request' %7D);%0A        %7D);%0A    %7D%0A  %7D).catch(error =%3E res.status(500).json(%7B status: 'error', error %7D)
+next);%0A    %7D%0A  %7D).catch(next
 );%0A%7D
@@ -2209,16 +2209,21 @@
 list
+Users
 , create
 , ge
@@ -2218,16 +2218,20 @@
 , create
+User
 , getUse
@@ -2239,12 +2239,16 @@
 , update
+User
  %7D;%0A

refactor user controller