@@ -7,6 +7,29 @@ api.init(serverSettings.apiBaseUrl, serverSettings.security.token);
77const DEFAULT_CACHE_CONTROL = 'public, max-age=600' ;
88const PRODUCTS_CACHE_CONTROL = 'public, max-age=60' ;
99
10+ const fillCartItemWithProductData = ( products , cartItem ) => {
11+ const product = products . find ( p => p . id === cartItem . product_id ) ;
12+ if ( product ) {
13+ cartItem . image_url = product . images . length > 0 ? product . images [ 0 ] . url : null ;
14+ cartItem . stock_quantity = product . stock_quantity ;
15+ }
16+ return cartItem ;
17+ }
18+
19+ const fillCartItems = ( cartResponse ) => {
20+ let cart = cartResponse . json ;
21+ if ( cart && cart . items && cart . items . length > 0 ) {
22+ const productIds = cart . items . map ( item => item . product_id ) ;
23+ return api . products . list ( { ids : productIds , fields : 'images,enabled,stock_quantity' } ) . then ( ( { status, json} ) => {
24+ const newCartItem = cart . items . map ( cartItem => fillCartItemWithProductData ( json , cartItem ) )
25+ cartResponse . json . items = newCartItem ;
26+ return cartResponse ;
27+ } )
28+ } else {
29+ return Promise . resolve ( cartResponse )
30+ }
31+ }
32+
1033ajaxRouter . get ( '/products' , ( req , res , next ) => {
1134 const filter = req . query ;
1235 filter . enabled = true ;
@@ -24,7 +47,7 @@ ajaxRouter.get('/products/:id', (req, res, next) => {
2447ajaxRouter . get ( '/cart' , ( req , res , next ) => {
2548 const order_id = req . signedCookies . order_id ;
2649 if ( order_id ) {
27- api . orders . retrieve ( order_id ) . then ( ( { status, json} ) => {
50+ api . orders . retrieve ( order_id ) . then ( cartResponse => fillCartItems ( cartResponse ) ) . then ( ( { status, json} ) => {
2851 res . status ( status ) . send ( json ) ;
2952 } )
3053 } else {
@@ -36,7 +59,7 @@ ajaxRouter.post('/cart/items', (req, res, next) => {
3659 const order_id = req . signedCookies . order_id ;
3760 const item = req . body ;
3861 if ( order_id ) {
39- api . orders . addItem ( order_id , item ) . then ( ( { status, json} ) => {
62+ api . orders . addItem ( order_id , item ) . then ( cartResponse => fillCartItems ( cartResponse ) ) . then ( ( { status, json} ) => {
4063 res . status ( status ) . send ( json ) ;
4164 } )
4265 } else {
@@ -61,7 +84,7 @@ ajaxRouter.post('/cart/items', (req, res, next) => {
6184 }
6285 } ) . then ( ( { status, json} ) => {
6386 res . cookie ( 'order_id' , json . id , serverSettings . cartCookieOptions ) ;
64- api . orders . addItem ( json . id , item ) . then ( ( { status, json} ) => {
87+ api . orders . addItem ( json . id , item ) . then ( cartResponse => fillCartItems ( cartResponse ) ) . then ( ( { status, json} ) => {
6588 res . status ( status ) . send ( json ) ;
6689 } )
6790 } )
@@ -72,7 +95,7 @@ ajaxRouter.delete('/cart/items/:item_id', (req, res, next) => {
7295 const order_id = req . signedCookies . order_id ;
7396 const item_id = req . params . item_id ;
7497 if ( order_id && item_id ) {
75- api . orders . deleteItem ( order_id , item_id ) . then ( ( { status, json} ) => {
98+ api . orders . deleteItem ( order_id , item_id ) . then ( cartResponse => fillCartItems ( cartResponse ) ) . then ( ( { status, json} ) => {
7699 res . status ( status ) . send ( json ) ;
77100 } )
78101 } else {
@@ -85,7 +108,7 @@ ajaxRouter.put('/cart/items/:item_id', (req, res, next) => {
85108 const item_id = req . params . item_id ;
86109 const item = req . body ;
87110 if ( order_id && item_id ) {
88- api . orders . updateItem ( order_id , item_id , item ) . then ( ( { status, json} ) => {
111+ api . orders . updateItem ( order_id , item_id , item ) . then ( cartResponse => fillCartItems ( cartResponse ) ) . then ( ( { status, json} ) => {
89112 res . status ( status ) . send ( json ) ;
90113 } )
91114 } else {
@@ -96,7 +119,7 @@ ajaxRouter.put('/cart/items/:item_id', (req, res, next) => {
96119ajaxRouter . put ( '/cart/checkout' , ( req , res , next ) => {
97120 const order_id = req . signedCookies . order_id ;
98121 if ( order_id ) {
99- api . orders . checkout ( order_id ) . then ( ( { status, json} ) => {
122+ api . orders . checkout ( order_id ) . then ( cartResponse => fillCartItems ( cartResponse ) ) . then ( ( { status, json} ) => {
100123 res . clearCookie ( 'order_id' ) ;
101124 res . status ( status ) . send ( json ) ;
102125 } )
@@ -108,7 +131,7 @@ ajaxRouter.put('/cart/checkout', (req, res, next) => {
108131ajaxRouter . put ( '/cart' , ( req , res , next ) => {
109132 const order_id = req . signedCookies . order_id ;
110133 if ( order_id ) {
111- api . orders . update ( order_id , req . body ) . then ( ( { status, json} ) => {
134+ api . orders . update ( order_id , req . body ) . then ( cartResponse => fillCartItems ( cartResponse ) ) . then ( ( { status, json} ) => {
112135 res . status ( status ) . send ( json ) ;
113136 } )
114137 } else {
@@ -119,7 +142,7 @@ ajaxRouter.put('/cart', (req, res, next) => {
119142ajaxRouter . put ( '/cart/shipping_address' , ( req , res , next ) => {
120143 const order_id = req . signedCookies . order_id ;
121144 if ( order_id ) {
122- api . orders . updateShippingAddress ( order_id , req . body ) . then ( ( { status, json} ) => {
145+ api . orders . updateShippingAddress ( order_id , req . body ) . then ( cartResponse => fillCartItems ( cartResponse ) ) . then ( ( { status, json} ) => {
123146 res . status ( status ) . send ( json ) ;
124147 } )
125148 } else {
@@ -130,7 +153,7 @@ ajaxRouter.put('/cart/shipping_address', (req, res, next) => {
130153ajaxRouter . put ( '/cart/billing_address' , ( req , res , next ) => {
131154 const order_id = req . signedCookies . order_id ;
132155 if ( order_id ) {
133- api . orders . updateBillingAddress ( order_id , req . body ) . then ( ( { status, json} ) => {
156+ api . orders . updateBillingAddress ( order_id , req . body ) . then ( cartResponse => fillCartItems ( cartResponse ) ) . then ( ( { status, json} ) => {
134157 res . status ( status ) . send ( json ) ;
135158 } )
136159 } else {
@@ -144,11 +167,11 @@ ajaxRouter.get('/product_categories', (req, res, next) => {
144167 } )
145168} )
146169
147- ajaxRouter . get ( '/product_categories/:id' , ( req , res , next ) => {
148- api . product_categories . retrieve ( req . params . id ) . then ( ( { status, json} ) => {
149- res . status ( status ) . header ( 'Cache-Control' , DEFAULT_CACHE_CONTROL ) . send ( json ) ;
150- } )
151- } )
170+ // ajaxRouter.get('/product_categories/:id', (req, res, next) => {
171+ // api.product_categories.retrieve(req.params.id).then(({status, json}) => {
172+ // res.status(status).header('Cache-Control', DEFAULT_CACHE_CONTROL).send(json);
173+ // })
174+ // })
152175
153176ajaxRouter . get ( '/pages/:id' , ( req , res , next ) => {
154177 api . pages . retrieve ( req . params . id ) . then ( ( { status, json} ) => {
@@ -182,11 +205,11 @@ ajaxRouter.get('/shipping_methods', (req, res, next) => {
182205 } )
183206} )
184207
185- ajaxRouter . get ( '/countries' , ( req , res , next ) => {
186- api . countries . list ( ) . then ( ( { status, json} ) => {
187- res . status ( status ) . header ( 'Cache-Control' , DEFAULT_CACHE_CONTROL ) . send ( json ) ;
188- } )
189- } )
208+ // ajaxRouter.get('/countries', (req, res, next) => {
209+ // api.countries.list().then(({status, json}) => {
210+ // res.status(status).header('Cache-Control', DEFAULT_CACHE_CONTROL).send(json);
211+ // })
212+ // })
190213
191214ajaxRouter . all ( '*' , ( req , res , next ) => {
192215 res . status ( 405 ) . send ( { 'error' : 'Method Not Allowed' } ) ;
0 commit comments