1
- import { apiStatus , apiError } from '../lib/util' ;
1
+ import { apiStatus , apiError , getToken } from '../lib/util' ;
2
2
import { Router } from 'express' ;
3
3
import PlatformFactory from '../platform/factory' ;
4
4
@@ -13,11 +13,12 @@ export default ({ config, db }) => {
13
13
14
14
/**
15
15
* POST create a cart
16
- * req.query.token - user token
16
+ * req.query.token | req.headers.authorization - user token
17
17
*/
18
18
cartApi . post ( '/create' , ( req , res ) => {
19
19
const cartProxy = _getProxy ( req )
20
- cartProxy . create ( req . query . token ) . then ( ( result ) => {
20
+ const token = getToken ( req )
21
+ cartProxy . create ( token ) . then ( ( result ) => {
21
22
apiStatus ( res , result , 200 ) ;
22
23
} ) . catch ( err => {
23
24
apiError ( res , err ) ;
@@ -26,18 +27,19 @@ export default ({ config, db }) => {
26
27
27
28
/**
28
29
* POST update or add the cart item
29
- * req.query.token - user token
30
+ * req.query.token | req.headers.authorization - user token
30
31
* body.cartItem: {
31
32
* sku: orderItem.sku,
32
33
* qty: orderItem.qty,
33
34
* quoteId: cartKey}
34
35
*/
35
36
cartApi . post ( '/update' , ( req , res ) => {
36
37
const cartProxy = _getProxy ( req )
38
+ const token = getToken ( req )
37
39
if ( ! req . body . cartItem ) {
38
40
return apiStatus ( res , 'No cartItem element provided within the request body' , 500 )
39
41
}
40
- cartProxy . update ( req . query . token , req . query . cartId ? req . query . cartId : null , req . body . cartItem ) . then ( ( result ) => {
42
+ cartProxy . update ( token , req . query . cartId ? req . query . cartId : null , req . body . cartItem ) . then ( ( result ) => {
41
43
apiStatus ( res , result , 200 ) ;
42
44
} ) . catch ( err => {
43
45
apiError ( res , err ) ;
@@ -46,16 +48,17 @@ export default ({ config, db }) => {
46
48
47
49
/**
48
50
* POST apply the coupon code
49
- * req.query.token - user token
51
+ * req.query.token | req.headers.authorization - user token
50
52
* req.query.cartId - cart Ids
51
53
* req.query.coupon - coupon
52
54
*/
53
55
cartApi . post ( '/apply-coupon' , ( req , res ) => {
54
56
const cartProxy = _getProxy ( req )
57
+ const token = getToken ( req )
55
58
if ( ! req . query . coupon ) {
56
59
return apiStatus ( res , 'No coupon code provided' , 500 )
57
60
}
58
- cartProxy . applyCoupon ( req . query . token , req . query . cartId ? req . query . cartId : null , req . query . coupon ) . then ( ( result ) => {
61
+ cartProxy . applyCoupon ( token , req . query . cartId ? req . query . cartId : null , req . query . coupon ) . then ( ( result ) => {
59
62
apiStatus ( res , result , 200 ) ;
60
63
} ) . catch ( err => {
61
64
apiError ( res , err ) ;
@@ -64,12 +67,13 @@ export default ({ config, db }) => {
64
67
65
68
/**
66
69
* POST remove the coupon code
67
- * req.query.token - user token
70
+ * req.query.token | req.headers.authorization - user token
68
71
* req.query.cartId - cart Ids
69
72
*/
70
73
cartApi . post ( '/delete-coupon' , ( req , res ) => {
71
74
const cartProxy = _getProxy ( req )
72
- cartProxy . deleteCoupon ( req . query . token , req . query . cartId ? req . query . cartId : null ) . then ( ( result ) => {
75
+ const token = getToken ( req )
76
+ cartProxy . deleteCoupon ( token , req . query . cartId ? req . query . cartId : null ) . then ( ( result ) => {
73
77
apiStatus ( res , result , 200 ) ;
74
78
} ) . catch ( err => {
75
79
apiError ( res , err ) ;
@@ -78,12 +82,13 @@ export default ({ config, db }) => {
78
82
79
83
/**
80
84
* GET get the applied coupon code
81
- * req.query.token - user token
85
+ * req.query.token | req.headers.authorization - user token
82
86
* req.query.cartId - cart Ids
83
87
*/
84
88
cartApi . get ( '/coupon' , ( req , res ) => {
85
89
const cartProxy = _getProxy ( req )
86
- cartProxy . getCoupon ( req . query . token , req . query . cartId ? req . query . cartId : null ) . then ( ( result ) => {
90
+ const token = getToken ( req )
91
+ cartProxy . getCoupon ( token , req . query . cartId ? req . query . cartId : null ) . then ( ( result ) => {
87
92
apiStatus ( res , result , 200 ) ;
88
93
} ) . catch ( err => {
89
94
apiError ( res , err ) ;
@@ -92,18 +97,19 @@ export default ({ config, db }) => {
92
97
93
98
/**
94
99
* POST delete the cart item
95
- * req.query.token - user token
100
+ * req.query.token | req.headers.authorization - user token
96
101
* body.cartItem: {
97
102
* sku: orderItem.sku,
98
103
* qty: orderItem.qty,
99
104
* quoteId: cartKey}
100
105
*/
101
106
cartApi . post ( '/delete' , ( req , res ) => {
102
107
const cartProxy = _getProxy ( req )
108
+ const token = getToken ( req )
103
109
if ( ! req . body . cartItem ) {
104
110
return apiStatus ( res , 'No cartItem element provided within the request body' , 500 )
105
111
}
106
- cartProxy . delete ( req . query . token , req . query . cartId ? req . query . cartId : null , req . body . cartItem ) . then ( ( result ) => {
112
+ cartProxy . delete ( token , req . query . cartId ? req . query . cartId : null , req . body . cartItem ) . then ( ( result ) => {
107
113
apiStatus ( res , result , 200 ) ;
108
114
} ) . catch ( err => {
109
115
apiError ( res , err ) ;
@@ -112,13 +118,14 @@ export default ({ config, db }) => {
112
118
113
119
/**
114
120
* GET pull the whole cart as it's currently se server side
115
- * req.query.token - user token
121
+ * req.query.token | req.headers.authorization - user token
116
122
* req.query.cartId - cartId
117
123
*/
118
124
cartApi . get ( '/pull' , ( req , res ) => {
119
125
const cartProxy = _getProxy ( req )
126
+ const token = getToken ( req )
120
127
res . setHeader ( 'Cache-Control' , 'no-cache, no-store' ) ;
121
- cartProxy . pull ( req . query . token , req . query . cartId ? req . query . cartId : null , req . body ) . then ( ( result ) => {
128
+ cartProxy . pull ( token , req . query . cartId ? req . query . cartId : null , req . body ) . then ( ( result ) => {
122
129
apiStatus ( res , result , 200 ) ;
123
130
} ) . catch ( err => {
124
131
apiError ( res , err ) ;
@@ -127,13 +134,14 @@ export default ({ config, db }) => {
127
134
128
135
/**
129
136
* GET totals the cart totals
130
- * req.query.token - user token
137
+ * req.query.token | req.headers.authorization - user token
131
138
* req.query.cartId - cartId
132
139
*/
133
140
cartApi . get ( '/totals' , ( req , res ) => {
134
141
const cartProxy = _getProxy ( req )
142
+ const token = getToken ( req )
135
143
res . setHeader ( 'Cache-Control' , 'no-cache, no-store' ) ;
136
- cartProxy . totals ( req . query . token , req . query . cartId ? req . query . cartId : null , req . body ) . then ( ( result ) => {
144
+ cartProxy . totals ( token , req . query . cartId ? req . query . cartId : null , req . body ) . then ( ( result ) => {
137
145
apiStatus ( res , result , 200 ) ;
138
146
} ) . catch ( err => {
139
147
apiError ( res , err ) ;
@@ -142,17 +150,18 @@ export default ({ config, db }) => {
142
150
143
151
/**
144
152
* POST /shipping-methods - available shipping methods for a given address
145
- * req.query.token - user token
153
+ * req.query.token | req.headers.authorization - user token
146
154
* req.query.cartId - cart ID if user is logged in, cart token if not
147
155
* req.body.address - shipping address object
148
156
*/
149
157
cartApi . post ( '/shipping-methods' , ( req , res ) => {
150
158
const cartProxy = _getProxy ( req )
159
+ const token = getToken ( req )
151
160
res . setHeader ( 'Cache-Control' , 'no-cache, no-store' ) ;
152
161
if ( ! req . body . address ) {
153
162
return apiStatus ( res , 'No address element provided within the request body' , 500 )
154
163
}
155
- cartProxy . getShippingMethods ( req . query . token , req . query . cartId ? req . query . cartId : null , req . body . address ) . then ( ( result ) => {
164
+ cartProxy . getShippingMethods ( token , req . query . cartId ? req . query . cartId : null , req . body . address ) . then ( ( result ) => {
156
165
apiStatus ( res , result , 200 ) ;
157
166
} ) . catch ( err => {
158
167
apiError ( res , err ) ;
@@ -161,13 +170,14 @@ export default ({ config, db }) => {
161
170
162
171
/**
163
172
* GET /payment-methods - available payment methods
164
- * req.query.token - user token
173
+ * req.query.token | req.headers.authorization - user token
165
174
* req.query.cartId - cart ID if user is logged in, cart token if not
166
175
*/
167
176
cartApi . get ( '/payment-methods' , ( req , res ) => {
168
177
const cartProxy = _getProxy ( req )
178
+ const token = getToken ( req )
169
179
res . setHeader ( 'Cache-Control' , 'no-cache, no-store' ) ;
170
- cartProxy . getPaymentMethods ( req . query . token , req . query . cartId ? req . query . cartId : null ) . then ( ( result ) => {
180
+ cartProxy . getPaymentMethods ( token , req . query . cartId ? req . query . cartId : null ) . then ( ( result ) => {
171
181
apiStatus ( res , result , 200 ) ;
172
182
} ) . catch ( err => {
173
183
apiError ( res , err ) ;
@@ -176,17 +186,18 @@ export default ({ config, db }) => {
176
186
177
187
/**
178
188
* POST /shipping-information - set shipping information for collecting cart totals after address changed
179
- * req.query.token - user token
189
+ * req.query.token | req.headers.authorization - user token
180
190
* req.query.cartId - cart ID if user is logged in, cart token if not
181
191
* req.body.addressInformation - shipping address object
182
192
*/
183
193
cartApi . post ( '/shipping-information' , ( req , res ) => {
184
194
const cartProxy = _getProxy ( req )
195
+ const token = getToken ( req )
185
196
res . setHeader ( 'Cache-Control' , 'no-cache, no-store' ) ;
186
197
if ( ! req . body . addressInformation ) {
187
198
return apiStatus ( res , 'No address element provided within the request body' , 500 )
188
199
}
189
- cartProxy . setShippingInformation ( req . query . token , req . query . cartId ? req . query . cartId : null , req . body ) . then ( ( result ) => {
200
+ cartProxy . setShippingInformation ( token , req . query . cartId ? req . query . cartId : null , req . body ) . then ( ( result ) => {
190
201
apiStatus ( res , result , 200 ) ;
191
202
} ) . catch ( err => {
192
203
apiError ( res , err ) ;
@@ -195,17 +206,18 @@ export default ({ config, db }) => {
195
206
196
207
/**
197
208
* POST /collect-totals - collect cart totals after shipping address changed
198
- * req.query.token - user token
209
+ * req.query.token | req.headers.authorization - user token
199
210
* req.query.cartId - cart ID if user is logged in, cart token if not
200
211
* req.body.shippingMethod - shipping and payment methods object
201
212
*/
202
213
cartApi . post ( '/collect-totals' , ( req , res ) => {
203
214
const cartProxy = _getProxy ( req )
215
+ const token = getToken ( req )
204
216
res . setHeader ( 'Cache-Control' , 'no-cache, no-store' ) ;
205
217
if ( ! req . body . methods ) {
206
218
return apiStatus ( res , 'No shipping and payment methods element provided within the request body' , 500 )
207
219
}
208
- cartProxy . collectTotals ( req . query . token , req . query . cartId ? req . query . cartId : null , req . body . methods ) . then ( ( result ) => {
220
+ cartProxy . collectTotals ( token , req . query . cartId ? req . query . cartId : null , req . body . methods ) . then ( ( result ) => {
209
221
apiStatus ( res , result , 200 ) ;
210
222
} ) . catch ( err => {
211
223
apiError ( res , err ) ;
0 commit comments