import express from 'express';
import bcrypt from 'bcryptjs';
import PostIt from './../src/postit';

const userRoutes = express.Router();

const PostItInstance = new PostIt();

/**
 * Registers a new user
 * Rejects users that have already registered
 */
userRoutes.post('/user/signup', (request, response) => {
  const [
    firstName,
    lastName,
    email,
    phone,
    password
  ] = Object.keys(request.body).map(key =>
    request.body[key]);

  bcrypt.hash(password, 10).then((hash) => {
    PostItInstance.registerUser(
      firstName,
      lastName,
      email,
      phone,
      hash)
      .then(() => {
        response.status(200).json({
          message: 'Registration successful',
          status: 200
        });
      }).catch((error) => {
        if (error.name === 'SequelizeUniqueConstraintError') {
          response.status(406).json({
            message: `${error.errors[0].value} is already registered`,
            status: 406
          });
        } else if (error.name === 'SequelizeValidationError') {
          const errMessage = {};

          error.errors.forEach((err) => {
            errMessage[err.path] = 'Invalid input';
          });

          response.status(406).json({
            message: 'Please check your submission',
            errors: errMessage,
            status: 406
          });
        } else {
          response.status(500).json({
            message: 'Oops! Something broke',
            status: 500
          });
        }
      });
  });
});

/**
 * Logs a new user in
 * Rejects signing in if credentials are wrong
 */
userRoutes.post('/user/signin', (request, response) => {
  const [
    email,
    password
  ] = Object.keys(request.body).map(key =>
    request.body[key]);

  PostItInstance.findUser(email, password)
    .then((user) => {
      bcrypt.compare(password, user.password)
        .then((matched) => {
          if (matched) {
            request.session.user = user;

            response.status(200).json({
              message: `Welcome ${request.session.user.firstName}`,
              status: 200
            });
          } else {
            response.status(401).json({
              message: 'You have entered a wrong password',
              status: 401
            });
          }
        });
    }).catch((error) => {
      if (!Object.keys(error).length) {
        response.status(401).json({
          message: 'Email is not registered',
          status: 401
        });
      } else {
        response.status(500).json({
          message: 'Oops! Something broke',
          status: 500
        });
      }
    });
});

export default userRoutes;

@@ -447,16 +447,166 @@
 key%5D);%0A%0A
+  if (password.length !== 8) %7B%0A    response.status(406).json(%7B%0A      message: 'Please enter a valid password',%0A      status: 406%0A    %7D);%0A  %7D else %7B%0A  
   bcrypt
@@ -646,16 +646,18 @@
 %3E %7B%0A    
+  
 PostItIn
@@ -677,16 +677,18 @@
 erUser(%0A
+  
       fi
@@ -698,24 +698,26 @@
 Name,%0A      
+  
 lastName,%0A  
@@ -710,24 +710,26 @@
   lastName,%0A
+  
       email,
@@ -731,24 +731,26 @@
 mail,%0A      
+  
 phone,%0A     
@@ -754,14 +754,18 @@
     
+ 
+ 
 hash)%0A
+  
     
@@ -776,24 +776,26 @@
 hen(() =%3E %7B%0A
+  
         resp
@@ -810,32 +810,34 @@
 tus(200).json(%7B%0A
+  
           messag
@@ -868,32 +868,34 @@
 ful',%0A          
+  
 status: 200%0A    
@@ -882,32 +882,34 @@
     status: 200%0A
+  
         %7D);%0A    
@@ -906,24 +906,26 @@
   %7D);%0A      
+  
 %7D).catch((er
@@ -938,24 +938,26 @@
 %3E %7B%0A        
+ 
+ 
 if (error.na
@@ -1001,32 +1001,34 @@
 r') %7B%0A          
+  
 response.status(
@@ -1043,32 +1043,34 @@
 n(%7B%0A            
+ 
+ 
 message: %60$%7Berro
@@ -1112,16 +1112,18 @@
 tered%60,%0A
+  
         
@@ -1140,36 +1140,40 @@
 : 406%0A          
+  
 %7D);%0A
+  
         %7D else i
@@ -1230,16 +1230,18 @@
         
+  
 const er
@@ -1262,24 +1262,26 @@
 %0A%0A          
+ 
+ 
 error.errors
@@ -1292,32 +1292,34 @@
 Each((err) =%3E %7B%0A
+  
             errM
@@ -1364,21 +1364,25 @@
         
+  
 %7D);%0A%0A
+  
         
@@ -1415,32 +1415,34 @@
 n(%7B%0A            
+  
 message: 'Please
@@ -1474,24 +1474,26 @@
             
+  
 errors: errM
@@ -1504,32 +1504,34 @@
 ge,%0A            
+  
 status: 406%0A    
@@ -1528,36 +1528,40 @@
 : 406%0A          
+ 
+ 
 %7D);%0A
+  
         %7D else %7B
@@ -1553,32 +1553,34 @@
        %7D else %7B%0A
+  
           respon
@@ -1593,32 +1593,34 @@
 tus(500).json(%7B%0A
+  
             mess
@@ -1653,32 +1653,34 @@
 e',%0A            
+  
 status: 500%0A    
@@ -1667,32 +1667,34 @@
     status: 500%0A
+  
           %7D);%0A  
@@ -1699,24 +1699,26 @@
         
+  
 %7D%0A      
 %7D);%0A  %7D)
@@ -1713,18 +1713,26 @@
     
+ 
+ 
 %7D);%0A  
+ 
+ 
 %7D);%0A
+  %7D%0A
 %7D);%0A

Add default value for priority in message model