diff --git a/api/cart/index.js b/api/cart/index.js index 9abf2a635..a1f3b6341 100644 --- a/api/cart/index.js +++ b/api/cart/index.js @@ -61,32 +61,6 @@ }); }); - // Update item - app.put("/cart/:id", function (req, res, next) { - if (req.params.id == null) { - return next(new Error("Must pass id of item to update"), 400); - } - - console.log("Updating item: " + req.params.id + " quantity: " + req.body.quantity); - - var custId = helpers.getCustomerId(req, app.get("env")); - var options = { - uri: endpoints.cartsUrl + "/" + custId + "/items", - method: 'PATCH', - json: true, - body: {itemId: req.params.id, quantity: req.body.quantity} - }; - console.log(options.uri) - request(options, function (error, response, body) { - if (error) { - return next(error); - } - console.log('Item updated with status: ' + response.statusCode); - helpers.respondStatus(res, response.statusCode); - }); - - }); - // Add new item to cart app.post("/cart", function (req, res, next) { console.log("Attempting to add to cart: " + JSON.stringify(req.body)); @@ -132,5 +106,53 @@ }); }); +// Update cart item + app.post("/cart/update", function (req, res, next) { + console.log("Attempting to update cart item: " + JSON.stringify(req.body)); + + if (req.body.id == null) { + next(new Error("Must pass id of item to update"), 400); + return; + } + if (req.body.quantity == null) { + next(new Error("Must pass quantity to update"), 400); + return; + } + var custId = helpers.getCustomerId(req, app.get("env")); + + async.waterfall([ + function (callback) { + request(endpoints.catalogueUrl + "/catalogue/" + req.body.id.toString(), function (error, response, body) { + console.log(body); + callback(error, JSON.parse(body)); + }); + }, + function (item, callback) { + var options = { + uri: endpoints.cartsUrl + "/" + custId + "/items", + method: 'PATCH', + json: true, + body: {itemId: item.id, quantity: parseInt(req.body.quantity), unitPrice: item.price} + }; + console.log("PATCH to carts: " + options.uri + " body: " + JSON.stringify(options.body)); + request(options, function (error, response, body) { + if (error) { + callback(error) + return; + } + callback(null, response.statusCode); + }); + } + ], function (err, statusCode) { + if (err) { + return next(err); + } + if (statusCode != 202) { + return next(new Error("Unable to add to cart. Status code: " + statusCode)) + } + helpers.respondStatus(res, statusCode); + }); + }); + module.exports = app; }()); diff --git a/public/basket.html b/public/basket.html index 91521add6..b18572082 100644 --- a/public/basket.html +++ b/public/basket.html @@ -334,26 +334,27 @@