1
1
const fs = require ( "fs" ) ;
2
2
const path = require ( 'path' ) ;
3
- const jwt = require ( 'jsonwebtoken' )
4
- const connection = require ( "../db" ) ;
3
+ const jwt = require ( 'jsonwebtoken' ) ;
4
+ const poolPromise = require ( "../db" ) ;
5
5
const config = JSON . parse ( fs . readFileSync ( path . resolve ( __dirname , "../env.config.json" ) , 'utf8' ) ) ;
6
- const json_response_model = JSON . parse ( fs . readFileSync ( path . resolve ( __dirname , "../response_format.json" ) , 'utf8' ) ) ;
7
-
6
+ // const json_response_model = JSON.parse(fs.readFileSync(path.resolve(__dirname, "../response_format.json"), 'utf8'));
7
+ let json_response_model = require ( '../json_response' ) ;
8
8
module . exports = {
9
9
10
- verifyUser : ( req , res , next ) => {
11
- let json_response = Object . create ( json_response_model )
12
-
13
- let token = req . headers [ 'x-access-token' ]
10
+ verifyUser : async ( req , res , next ) => {
11
+ // let json_response = Object.create(json_response_model)
12
+ let json_response = json_response_model ( ) ;
13
+ let token = req . headers [ 'x-access-token' ] ;
14
14
if ( ! token ) {
15
15
json_response [ 'success' ] = false ;
16
16
json_response [ 'message' ] = "Login to proceed" ;
17
- json_response [ 'data' ] = [ ]
18
- json_response [ 'token' ] = ''
17
+ json_response [ 'data' ] = [ ] ;
18
+ json_response [ 'token' ] = '' ;
19
19
return res . status ( 403 ) . json ( json_response ) ;
20
20
}
21
21
22
22
jwt . verify ( token , config . secret , ( error , decoded ) => {
23
23
if ( error ) {
24
24
json_response [ 'success' ] = false ;
25
25
json_response [ 'message' ] = "Cannot verify user" ;
26
- json_response [ 'data' ] = [ ]
27
- json_response [ 'token' ] = ''
26
+ json_response [ 'data' ] = [ ] ;
27
+ json_response [ 'token' ] = '' ;
28
28
return res . status ( 403 ) . json ( json_response ) ;
29
29
}
30
30
31
- if ( typeof ( decoded . customerId ) !== 'undefined' ) {
32
- req . userId = decoded . customerId
33
- req . userType = 1
34
- }
35
- else if ( typeof ( decoded . shopId ) !== 'undefined' ) {
36
- req . userId = decoded . shopId
37
- req . userType = 2
31
+ if ( typeof ( decoded . user_id ) !== 'undefined' ) {
32
+ req . userId = decoded . user_id ;
33
+ req . userType = 'REG_USER' ;
34
+ } else if ( typeof ( decoded . admin_id ) !== 'undefined' ) {
35
+ req . userId = decoded . admin_id ;
36
+ req . userType = 'ADMIN' ;
38
37
}
39
38
next ( )
40
39
} )
41
40
} ,
42
41
43
- isCustomer : ( req , res , next ) => {
44
- let json_response = Object . create ( json_response_model )
45
-
46
- if ( req . userType === 1 ) {
42
+ isAdmin : async ( req , res , next ) => {
43
+ // let json_response = Object.create(json_response_model)
44
+ let json_response = json_response_model ( ) ;
45
+ if ( req . userType === 'ADMIN' ) {
47
46
next ( )
48
- }
49
- else {
47
+ } else {
50
48
json_response [ 'success' ] = false ;
51
49
json_response [ 'message' ] = "Access not authorized" ;
52
- json_response [ 'data' ] = [ ]
53
- json_response [ 'token' ] = req . headers [ 'x-access-token' ]
50
+ json_response [ 'data' ] = [ ] ;
51
+ json_response [ 'token' ] = req . headers [ 'x-access-token' ] ;
54
52
return res . status ( 401 ) . json ( json_response ) ;
55
53
}
56
54
} ,
57
55
58
- isShop : ( req , res , next ) => {
59
- let json_response = Object . create ( json_response_model )
60
-
61
- if ( req . userType === 2 ) {
56
+ isRegUser : ( req , res , next ) => {
57
+ // let json_response = Object.create(json_response_model)
58
+ let json_response = json_response_model ( ) ;
59
+ if ( req . userType === 'REG_USER' ) {
62
60
next ( )
63
- }
64
- else {
61
+ } else {
65
62
json_response [ 'success' ] = false ;
66
63
json_response [ 'message' ] = "Access not authorized" ;
67
- json_response [ 'data' ] = [ ]
68
- json_response [ 'token' ] = req . headers [ 'x-access-token' ]
64
+ json_response [ 'data' ] = [ ] ;
65
+ json_response [ 'token' ] = req . headers [ 'x-access-token' ] ;
69
66
return res . status ( 401 ) . json ( json_response ) ;
70
67
}
71
68
} ,
72
69
73
- isSameUser : ( req , res , next ) => {
74
- let json_response = Object . create ( json_response_model )
75
-
76
- if ( req . userId == req . params . id ) {
70
+ isSameUser : async ( req , res , next ) => {
71
+ // let json_response = Object.create(json_response_model)
72
+ let json_response = json_response_model ( ) ;
73
+ if ( req . userId === req . params . id ) {
77
74
next ( )
78
- }
79
- else {
75
+ } else {
80
76
json_response [ 'success' ] = false ;
81
77
json_response [ 'message' ] = "Access not authorized" ;
82
- json_response [ 'data' ] = [ ]
83
- json_response [ 'token' ] = req . headers [ 'x-access-token' ]
78
+ json_response [ 'data' ] = [ ] ;
79
+ json_response [ 'token' ] = req . headers [ 'x-access-token' ] ;
84
80
return res . status ( 401 ) . json ( json_response ) ;
85
81
}
86
82
} ,
87
83
88
- checkAccessToOrder : ( req , res , next ) => {
89
- let json_response = Object . create ( json_response_model )
90
-
91
- let orderId = req . params . id
92
- let userId = req . userId
93
- let userType = req . userType
84
+ checkAccessToOrder : async ( req , res , next ) => {
85
+ let json_response = Object . create ( json_response_model ) ;
86
+
87
+ let orderId = req . params . id ;
88
+ let userId = req . userId ;
89
+ let userType = req . userType ;
94
90
95
91
if ( userType === 1 ) {
96
92
connection . query ( "select customer_id from orders where id=?" , orderId , ( error , results ) => {
97
93
if ( error ) {
98
94
json_response [ 'success' ] = false ;
99
95
json_response [ 'message' ] = "Access failed" ;
100
- json_response [ 'data' ] = [ ]
101
- json_response [ 'token' ] = req . headers [ 'x-access-token' ]
96
+ json_response [ 'data' ] = [ ] ;
97
+ json_response [ 'token' ] = req . headers [ 'x-access-token' ] ;
102
98
return res . status ( 503 ) . json ( json_response ) ;
103
- }
104
- else if ( results . length === 0 ) {
99
+ } else if ( results . length === 0 ) {
105
100
json_response [ 'success' ] = false ;
106
101
json_response [ 'message' ] = "Order does not exist" ;
107
- json_response [ 'data' ] = [ ]
108
- json_response [ 'token' ] = req . headers [ 'x-access-token' ]
102
+ json_response [ 'data' ] = [ ] ;
103
+ json_response [ 'token' ] = req . headers [ 'x-access-token' ] ;
109
104
return res . status ( 400 ) . json ( json_response ) ;
110
105
}
111
- let customerId = results [ 0 ] . customer_id
112
- if ( customerId == userId ) {
106
+ let customerId = results [ 0 ] . customer_id ;
107
+ if ( customerId === userId ) {
113
108
return next ( )
114
109
}
115
110
json_response [ 'success' ] = false ;
116
111
json_response [ 'message' ] = "Access not authorized" ;
117
- json_response [ 'data' ] = [ ]
118
- json_response [ 'token' ] = req . headers [ 'x-access-token' ]
112
+ json_response [ 'data' ] = [ ] ;
113
+ json_response [ 'token' ] = req . headers [ 'x-access-token' ] ;
119
114
return res . status ( 401 ) . json ( json_response ) ;
120
115
} )
121
- }
122
- else if ( userType === 2 ) {
116
+ } else if ( userType === 2 ) {
123
117
next ( )
124
118
}
125
119
}
126
- }
120
+ } ;
0 commit comments