1
+ const User = require ( '../models/User' )
2
+ const Note = require ( '../models/Note' )
3
+ const asyncHandler = require ( 'express-async-handler' )
4
+ const bcrypt = require ( 'bcrypt' )
5
+
6
+ // @desc Get all users
7
+ // @route GET /users
8
+ // @access Private
9
+ const getAllUsers = asyncHandler ( async ( req , res ) => {
10
+ // Get all users from MongoDB
11
+ const users = await User . find ( ) . select ( '-password' ) . lean ( )
12
+
13
+ // If no users
14
+ if ( ! users ?. length ) {
15
+ return res . status ( 400 ) . json ( { message : 'No users found' } )
16
+ }
17
+
18
+ res . json ( users )
19
+ } )
20
+
21
+ // @desc Create new user
22
+ // @route POST /users
23
+ // @access Private
24
+ const createNewUser = asyncHandler ( async ( req , res ) => {
25
+ const { username, password, roles } = req . body
26
+
27
+ // Confirm data
28
+ if ( ! username || ! password || ! Array . isArray ( roles ) || ! roles . length ) {
29
+ return res . status ( 400 ) . json ( { message : 'All fields are required' } )
30
+ }
31
+
32
+ // Check for duplicate username
33
+ const duplicate = await User . findOne ( { username } ) . lean ( ) . exec ( )
34
+
35
+ if ( duplicate ) {
36
+ return res . status ( 409 ) . json ( { message : 'Duplicate username' } )
37
+ }
38
+
39
+ // Hash password
40
+ const hashedPwd = await bcrypt . hash ( password , 10 ) // salt rounds
41
+
42
+ const userObject = { username, "password" : hashedPwd , roles }
43
+
44
+ // Create and store new user
45
+ const user = await User . create ( userObject )
46
+
47
+ if ( user ) { //created
48
+ res . status ( 201 ) . json ( { message : `New user ${ username } created` } )
49
+ } else {
50
+ res . status ( 400 ) . json ( { message : 'Invalid user data received' } )
51
+ }
52
+ } )
53
+
54
+ // @desc Update a user
55
+ // @route PATCH /users
56
+ // @access Private
57
+ const updateUser = asyncHandler ( async ( req , res ) => {
58
+ const { id, username, roles, active, password } = req . body
59
+
60
+ // Confirm data
61
+ if ( ! id || ! username || ! Array . isArray ( roles ) || ! roles . length || typeof active !== 'boolean' ) {
62
+ return res . status ( 400 ) . json ( { message : 'All fields except password are required' } )
63
+ }
64
+
65
+ // Does the user exist to update?
66
+ const user = await User . findById ( id ) . exec ( )
67
+
68
+ if ( ! user ) {
69
+ return res . status ( 400 ) . json ( { message : 'User not found' } )
70
+ }
71
+
72
+ // Check for duplicate
73
+ const duplicate = await User . findOne ( { username } ) . lean ( ) . exec ( )
74
+
75
+ // Allow updates to the original user
76
+ if ( duplicate && duplicate ?. _id . toString ( ) !== id ) {
77
+ return res . status ( 409 ) . json ( { message : 'Duplicate username' } )
78
+ }
79
+
80
+ user . username = username
81
+ user . roles = roles
82
+ user . active = active
83
+
84
+ if ( password ) {
85
+ // Hash password
86
+ user . password = await bcrypt . hash ( password , 10 ) // salt rounds
87
+ }
88
+
89
+ const updatedUser = await user . save ( )
90
+
91
+ res . json ( { message : `${ updatedUser . username } updated` } )
92
+ } )
93
+
94
+ // @desc Delete a user
95
+ // @route DELETE /users
96
+ // @access Private
97
+ const deleteUser = asyncHandler ( async ( req , res ) => {
98
+ const { id } = req . body
99
+
100
+ // Confirm data
101
+ if ( ! id ) {
102
+ return res . status ( 400 ) . json ( { message : 'User ID Required' } )
103
+ }
104
+
105
+ // Does the user still have assigned notes?
106
+ const note = await Note . findOne ( { user : id } ) . lean ( ) . exec ( )
107
+ if ( note ) {
108
+ return res . status ( 400 ) . json ( { message : 'User has assigned notes' } )
109
+ }
110
+
111
+ // Does the user exist to delete?
112
+ const user = await User . findById ( id ) . exec ( )
113
+
114
+ if ( ! user ) {
115
+ return res . status ( 400 ) . json ( { message : 'User not found' } )
116
+ }
117
+
118
+ const result = await user . deleteOne ( )
119
+
120
+ const reply = `Username ${ result . username } with ID ${ result . _id } deleted`
121
+
122
+ res . json ( reply )
123
+ } )
124
+
125
+ module . exports = {
126
+ getAllUsers,
127
+ createNewUser,
128
+ updateUser,
129
+ deleteUser
130
+ }
0 commit comments