1
1
const bcrypt = require ( 'bcrypt' )
2
2
const jwt = require ( 'jsonwebtoken' )
3
+ const { isValidEmail } = require ( '../../utils' )
3
4
4
5
const { employeeModel } = require ( '../models/employees.model' )
5
6
@@ -11,29 +12,33 @@ exports.addEmployee = (req, res) => {
11
12
if ( user ) {
12
13
res . status ( 409 ) . json ( { err : 'Email already exists. Try another one' } )
13
14
} else {
14
- employeeModel
15
- . create ( {
16
- firstName : req . body . firstName ,
17
- lastName : req . body . lastName ,
18
- specialty : req . body . specialty ,
19
- rol : req . body . rol ,
20
- email : req . body . email ,
21
- password : hashed_pwd
22
- } )
23
- . then ( user => {
24
- const user_data = { rol : user . rol , email : user . email }
15
+ if ( isValidEmail ( req . body . email ) ) {
16
+ employeeModel
17
+ . create ( {
18
+ firstName : req . body . firstName ,
19
+ lastName : req . body . lastName ,
20
+ specialty : req . body . specialty ,
21
+ rol : req . body . rol ,
22
+ email : req . body . email ,
23
+ password : hashed_pwd
24
+ } )
25
+ . then ( user => {
26
+ const user_data = { rol : user . rol , email : user . email }
25
27
26
- const token = jwt . sign (
27
- user_data ,
28
- process . env . SECRET , // TODO SECRET MORE SECRET PLEASE
29
- { expiresIn : '1h' }
30
- )
31
- return res . json ( { token : token , ...user_data } )
32
- } )
33
- . catch ( err => {
34
- console . log ( err )
35
- res . status ( 500 ) . json ( { msg : 'Error' } )
36
- } )
28
+ const token = jwt . sign (
29
+ user_data ,
30
+ process . env . SECRET , // TODO SECRET MORE SECRET PLEASE
31
+ { expiresIn : '1h' }
32
+ )
33
+ return res . json ( { token : token , ...user_data } )
34
+ } )
35
+ . catch ( err => {
36
+ console . log ( err )
37
+ res . status ( 500 ) . json ( { msg : 'Error' } )
38
+ } )
39
+ } else {
40
+ res . status ( 409 ) . json ( { err : 'Wrong email format' } )
41
+ }
37
42
}
38
43
} )
39
44
. catch ( err => {
@@ -57,30 +62,27 @@ exports.updateEmployee = (req, res) => {
57
62
. findById ( req . params . idEmployee )
58
63
. then ( user => {
59
64
if ( req . body . employee . email ) {
60
- employeeModel
61
- . findOne ( { email : req . body . employee . email } )
62
- . then ( user => {
63
- if ( user ) res . status ( 403 ) . json ( { msg : 'The email already exists!' } )
64
- } )
65
- . catch ( err => {
66
- console . log ( err )
67
- res . status ( 500 ) . json ( { msg : 'Error' } )
68
- } )
65
+ if ( ! isValidEmail ( req . body . employee . email ) ) {
66
+ return res . status ( 409 ) . json ( { err : 'Wrong email format' } )
67
+ }
69
68
}
70
69
if ( req . body . employee . password ) {
71
- req . body . employee . password = bcrypt . hashSync ( req . body . employee . password , 10 )
70
+ user . password = bcrypt . hashSync ( req . body . employee . password , 10 )
72
71
}
73
72
74
- console . log ( result )
73
+ user . firstName = req . body . employee . firstName ?? user . firstName
74
+ user . lastName = req . body . employee . lastName ?? user . lastName
75
+ user . specialty = req . body . employee . specialty ?? user . specialty
76
+ user . email = req . body . employee . email ?? user . email
77
+ user . rol = req . body . employee . rol ?? user . rol
78
+
75
79
user . save ( function ( err , result ) {
76
80
if ( err ) {
77
- console . log ( err ) ;
78
- }
79
- else {
80
- console . log ( result )
81
+ res . status ( 500 ) . json ( { msg : 'Error' } )
82
+ } else {
83
+ res . status ( 200 ) . json ( { msg : 'Update successful!' } )
81
84
}
82
85
} )
83
- res . status ( 200 ) . json ( { msg : 'Update successful!' } )
84
86
} )
85
87
. catch ( err => {
86
88
console . log ( err )
0 commit comments