Fixed updating of cart + added retrieval of cart & specs
							parent
							
								
									ddf78c1618
								
							
						
					
					
						commit
						588fdddb75
					
				|  | @ -443,12 +443,10 @@ function updateCart(){ | |||
|     // gather items of cart
 | ||||
|     var cartItems = []; | ||||
|     $('.cart-product-quantity').each(function(){ | ||||
|         var item = { | ||||
|             cartIndex: $(this).attr('id'), | ||||
|             itemQuantity: $(this).val(), | ||||
|             productId: $(this).attr('data-id') | ||||
|         }; | ||||
|         cartItems.push(item); | ||||
|         cartItems.push({ | ||||
|             productId: $(this).attr('data-id'), | ||||
|             quantity: $(this).val() | ||||
|         }); | ||||
|     }); | ||||
| 
 | ||||
|     // 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
 | ||||
| router.post('/product/updatecart', (req, res, next) => { | ||||
|     const db = req.app.db; | ||||
|  | @ -201,12 +211,20 @@ router.post('/product/updatecart', (req, res, next) => { | |||
|     let stockError = false; | ||||
| 
 | ||||
|     async.eachSeries(cartItems, async (cartItem, callback) => { | ||||
|         const productQuantity = cartItem.itemQuantity ? cartItem.itemQuantity : 1; | ||||
|         if(cartItem.itemQuantity === 0){ | ||||
|         // Find index in cart
 | ||||
|         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
 | ||||
|             req.session.cart.splice(cartItem.cartIndex, 1); | ||||
|             req.session.cart.splice(cartIndex, 1); | ||||
|             callback(null); | ||||
|         }else{ | ||||
|             return; | ||||
|         } | ||||
|         const product = await db.products.findOne({ _id: getId(cartItem.productId) }); | ||||
|         if(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); | ||||
|                 if(req.session.cart[cartItem.cartIndex]){ | ||||
|                     req.session.cart[cartItem.cartIndex].quantity = productQuantity; | ||||
|                     req.session.cart[cartItem.cartIndex].totalItemPrice = productPrice * productQuantity; | ||||
|             if(req.session.cart[cartIndex]){ | ||||
|                 req.session.cart[cartIndex].quantity = productQuantity; | ||||
|                 req.session.cart[cartIndex].totalItemPrice = productPrice * productQuantity; | ||||
|                 callback(null); | ||||
|             } | ||||
|         }else{ | ||||
|             hasError = true; | ||||
|             callback(null); | ||||
|         } | ||||
|         } | ||||
|     }, async () => { | ||||
|         // update total cart amount
 | ||||
|         updateTotalCartAmount(req, res); | ||||
|  |  | |||
|  | @ -70,6 +70,32 @@ test('[Success] Add product to cart', async t => { | |||
|     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 => { | ||||
|     const res = await g.request | ||||
|         .post('/product/addtocart') | ||||
|  |  | |||
		Loading…
	
		Reference in New Issue