1
+ import { catchAsyncError } from '../../utils/catch_async_error' ;
2
+ import { AppError } from '../../utils/app_error' ;
3
+ import { deleteOne } from '../../handler/factor' ;
4
+ import { ApiFeatures } from '../../utils/api_feature' ;
5
+ import { userModel } from '../../models/user_model' ;
6
+ import bcrypt from "bcrypt" ;
7
+
8
+ const addUser = catchAsyncError ( async ( req , res , next ) => {
9
+ const addUser = new userModel ( req . body ) ;
10
+ await addUser . save ( ) ;
11
+
12
+ res . status ( 201 ) . json ( { message : "success" , addUser } ) ;
13
+ } ) ;
14
+
15
+ const getAllUsers = catchAsyncError ( async ( req , res , next ) => {
16
+ let apiFeature = new ApiFeatures ( userModel . find ( ) , req . query )
17
+ . pagination ( )
18
+ . fields ( )
19
+ . filteration ( )
20
+ . search ( )
21
+ . sort ( ) ;
22
+ const PAGE_NUMBER = apiFeature . queryString . page * 1 || 1 ;
23
+ const getAllUsers = await apiFeature . mongooseQuery ;
24
+
25
+ res . status ( 201 ) . json ( { page : PAGE_NUMBER , message : "success" , getAllUsers } ) ;
26
+ } ) ;
27
+
28
+ const updateUser = catchAsyncError ( async ( req , res , next ) => {
29
+ const { id } = req . params ;
30
+ const updateUser = await userModel . findByIdAndUpdate ( id , req . body , {
31
+ new : true ,
32
+ } ) ;
33
+
34
+ updateUser && res . status ( 201 ) . json ( { message : "success" , updateUser } ) ;
35
+
36
+ ! updateUser && next ( new AppError ( "User was not found" , 404 ) ) ;
37
+ } ) ;
38
+
39
+ const changeUserPassword = catchAsyncError ( async ( req , res , next ) => {
40
+ const { id } = req . params ;
41
+ req . body . passwordChangedAt = Date . now ( ) ;
42
+ console . log ( req . body . passwordChangedAt ) ;
43
+ const changeUserPassword = await userModel . findByIdAndUpdate ( id , req . body , {
44
+ new : true ,
45
+ } ) ;
46
+
47
+ changeUserPassword &&
48
+ res . status ( 201 ) . json ( { message : "success" , changeUserPassword } ) ;
49
+
50
+ ! changeUserPassword && next ( new AppError ( "User was not found" , 404 ) ) ;
51
+ } ) ;
52
+ const deleteUser = deleteOne ( userModel , "user" ) ;
53
+
54
+ export { addUser , getAllUsers , updateUser , deleteUser , changeUserPassword } ;
0 commit comments