Fixed updating of cart + added retrieval of cart & specs
							parent
							
								
									ddf78c1618
								
							
						
					
					
						commit
						588fdddb75
					
				|  | @ -443,12 +443,10 @@ function updateCart(){ | ||||||
|     // gather items of cart
 |     // gather items of cart
 | ||||||
|     var cartItems = []; |     var cartItems = []; | ||||||
|     $('.cart-product-quantity').each(function(){ |     $('.cart-product-quantity').each(function(){ | ||||||
|         var item = { |         cartItems.push({ | ||||||
|             cartIndex: $(this).attr('id'), |             productId: $(this).attr('data-id'), | ||||||
|             itemQuantity: $(this).val(), |             quantity: $(this).val() | ||||||
|             productId: $(this).attr('data-id') |         }); | ||||||
|         }; |  | ||||||
|         cartItems.push(item); |  | ||||||
|     }); |     }); | ||||||
| 
 | 
 | ||||||
|     // update cart on server
 |     // update cart on server
 | ||||||
|  |  | ||||||
										
											
												File diff suppressed because one or more lines are too long
											
										
									
								
							|  | @ -192,6 +192,16 @@ router.get('/product/:id', async (req, res) => { | ||||||
|     }); |     }); | ||||||
| }); | }); | ||||||
| 
 | 
 | ||||||
|  | // Gets the current cart
 | ||||||
|  | router.get('/cart/retrieve', async (req, res, next) => { | ||||||
|  |     const db = req.app.db; | ||||||
|  | 
 | ||||||
|  |     // Get the cart from the DB using the session id
 | ||||||
|  |     const cart = await db.cart.findOne({ sessionId: getId(req.session.id) }); | ||||||
|  | 
 | ||||||
|  |     res.status(200).json({ cart: cart.cart }); | ||||||
|  | }); | ||||||
|  | 
 | ||||||
| // Updates a single product quantity
 | // Updates a single product quantity
 | ||||||
| router.post('/product/updatecart', (req, res, next) => { | router.post('/product/updatecart', (req, res, next) => { | ||||||
|     const db = req.app.db; |     const db = req.app.db; | ||||||
|  | @ -201,12 +211,20 @@ router.post('/product/updatecart', (req, res, next) => { | ||||||
|     let stockError = false; |     let stockError = false; | ||||||
| 
 | 
 | ||||||
|     async.eachSeries(cartItems, async (cartItem, callback) => { |     async.eachSeries(cartItems, async (cartItem, callback) => { | ||||||
|         const productQuantity = cartItem.itemQuantity ? cartItem.itemQuantity : 1; |         // Find index in cart
 | ||||||
|         if(cartItem.itemQuantity === 0){ |         const cartIndex = _.findIndex(req.session.cart, { productId: cartItem.productId }); | ||||||
|  | 
 | ||||||
|  |         // Calculate the quantity to update
 | ||||||
|  |         let productQuantity = cartItem.quantity ? cartItem.quantity : 1; | ||||||
|  |         if(typeof productQuantity === 'string'){ | ||||||
|  |             productQuantity = parseInt(productQuantity); | ||||||
|  |         } | ||||||
|  |         if(productQuantity === 0){ | ||||||
|             // quantity equals zero so we remove the item
 |             // quantity equals zero so we remove the item
 | ||||||
|             req.session.cart.splice(cartItem.cartIndex, 1); |             req.session.cart.splice(cartIndex, 1); | ||||||
|             callback(null); |             callback(null); | ||||||
|         }else{ |             return; | ||||||
|  |         } | ||||||
|         const product = await db.products.findOne({ _id: getId(cartItem.productId) }); |         const product = await db.products.findOne({ _id: getId(cartItem.productId) }); | ||||||
|         if(product){ |         if(product){ | ||||||
|             // If stock management on check there is sufficient stock for this product
 |             // If stock management on check there is sufficient stock for this product
 | ||||||
|  | @ -220,16 +238,15 @@ router.post('/product/updatecart', (req, res, next) => { | ||||||
|             } |             } | ||||||
| 
 | 
 | ||||||
|             const productPrice = parseFloat(product.productPrice).toFixed(2); |             const productPrice = parseFloat(product.productPrice).toFixed(2); | ||||||
|                 if(req.session.cart[cartItem.cartIndex]){ |             if(req.session.cart[cartIndex]){ | ||||||
|                     req.session.cart[cartItem.cartIndex].quantity = productQuantity; |                 req.session.cart[cartIndex].quantity = productQuantity; | ||||||
|                     req.session.cart[cartItem.cartIndex].totalItemPrice = productPrice * productQuantity; |                 req.session.cart[cartIndex].totalItemPrice = productPrice * productQuantity; | ||||||
|                 callback(null); |                 callback(null); | ||||||
|             } |             } | ||||||
|         }else{ |         }else{ | ||||||
|             hasError = true; |             hasError = true; | ||||||
|             callback(null); |             callback(null); | ||||||
|         } |         } | ||||||
|         } |  | ||||||
|     }, async () => { |     }, async () => { | ||||||
|         // update total cart amount
 |         // update total cart amount
 | ||||||
|         updateTotalCartAmount(req, res); |         updateTotalCartAmount(req, res); | ||||||
|  |  | ||||||
|  | @ -70,6 +70,32 @@ test('[Success] Add product to cart', async t => { | ||||||
|     t.deepEqual(res.body.message, 'Cart successfully updated'); |     t.deepEqual(res.body.message, 'Cart successfully updated'); | ||||||
| }); | }); | ||||||
| 
 | 
 | ||||||
|  | test('[Success] Update cart', async t => { | ||||||
|  |     const cart = await g.request | ||||||
|  |         .get('/cart/retrieve') | ||||||
|  |         .expect(200); | ||||||
|  | 
 | ||||||
|  |     // Adjust the quantity of an item
 | ||||||
|  |     cart.body.cart[0].quantity = 10; | ||||||
|  | 
 | ||||||
|  |     const res = await g.request | ||||||
|  |         .post('/product/updatecart') | ||||||
|  |         .send({ | ||||||
|  |             items: JSON.stringify(cart.body.cart) | ||||||
|  |         }) | ||||||
|  |         .expect(200); | ||||||
|  | 
 | ||||||
|  |     t.deepEqual(res.body.message, 'Cart successfully updated'); | ||||||
|  | 
 | ||||||
|  |     const checkCart = await g.request | ||||||
|  |         .get('/cart/retrieve') | ||||||
|  |         .expect(200); | ||||||
|  | 
 | ||||||
|  |     // Check new quantity and total price has been updated
 | ||||||
|  |     t.deepEqual(checkCart.body.cart[0].quantity, 10); | ||||||
|  |     t.deepEqual(checkCart.body.cart[0].totalItemPrice, cart.body.cart[0].totalItemPrice * 10); | ||||||
|  | }); | ||||||
|  | 
 | ||||||
| test('[Fail] Cannot add subscripton when other product in cart', async t => { | test('[Fail] Cannot add subscripton when other product in cart', async t => { | ||||||
|     const res = await g.request |     const res = await g.request | ||||||
|         .post('/product/addtocart') |         .post('/product/addtocart') | ||||||
|  |  | ||||||
		Loading…
	
		Reference in New Issue