From 3bda249c6840260b8f72dd715f4203f8177c11fe Mon Sep 17 00:00:00 2001 From: Mark Moffat Date: Wed, 8 Jan 2020 19:10:45 +1030 Subject: [PATCH] Adding in a maxQuantity value when adding to cart --- bin/testdata.json | 2 +- config/baseSchema.json | 3 +++ config/settings.json | 1 + routes/index.js | 7 +++++++ test/specs/products.js | 16 ++++++++++++++-- 5 files changed, 26 insertions(+), 3 deletions(-) diff --git a/bin/testdata.json b/bin/testdata.json index 53491a4..ba05143 100644 --- a/bin/testdata.json +++ b/bin/testdata.json @@ -121,7 +121,7 @@ ] } }, - "productStock": 10 + "productStock": 100 }, { "productPermalink": "hudderton-backpack", diff --git a/config/baseSchema.json b/config/baseSchema.json index d4efe44..5933f92 100644 --- a/config/baseSchema.json +++ b/config/baseSchema.json @@ -110,6 +110,9 @@ "type": "boolean", "default": true }, + "maxQuantity": { + "type": "number" + }, "modules": { "type": "object", "properties": { diff --git a/config/settings.json b/config/settings.json index c3fac43..61dc69c 100644 --- a/config/settings.json +++ b/config/settings.json @@ -25,6 +25,7 @@ "orderHook": "", "availableLanguages": ["en", "it"], "defaultLocale": "en", + "maxQuantity": 25, "modules": { "enabled": { "shipping": "shipping-basic" diff --git a/routes/index.js b/routes/index.js index 5c25b92..6270652 100644 --- a/routes/index.js +++ b/routes/index.js @@ -365,6 +365,13 @@ router.post('/product/addtocart', async (req, res, next) => { let productQuantity = req.body.productQuantity ? parseInt(req.body.productQuantity) : 1; const productComment = req.body.productComment ? req.body.productComment : null; + // If maxQuantity set, ensure the quantity doesn't exceed that value + if(config.maxQuantity && productQuantity > config.maxQuantity){ + return res.status(400).json({ + message: 'The quantity exceeds the max amount. Please contact us for larger orders.' + }); + } + // Don't allow negative quantity if(productQuantity < 1){ productQuantity = 1; diff --git a/test/specs/products.js b/test/specs/products.js index a1bf2c0..43e801d 100644 --- a/test/specs/products.js +++ b/test/specs/products.js @@ -40,13 +40,25 @@ test('[Fail] Add product to cart when subscription already added', async t => { .post('/product/addtocart') .send({ productId: g.products[1]._id, - productQuantity: 100, + productQuantity: 1, productOptions: JSON.stringify(g.products[1].productOptions) }) .expect(400); t.deepEqual(res.body.message, 'Subscription already existing in cart. You cannot add more.'); }); +test('[Fail] Add quantity which exceeds the maxQuantity', async t => { + const res = await g.request + .post('/product/addtocart') + .send({ + productId: g.products[4]._id, + productQuantity: 75, + productOptions: {} + }) + .expect(400); + t.deepEqual(res.body.message, 'The quantity exceeds the max amount. Please contact us for larger orders.'); +}); + test('[Success] Empty cart', async t => { const res = await g.request .post('/product/emptycart') @@ -113,7 +125,7 @@ test('[Fail] Add product to cart with not enough stock', async t => { .post('/product/addtocart') .send({ productId: g.products[0]._id, - productQuantity: 100, + productQuantity: 20, productOptions: JSON.stringify(g.products[0].productOptions) }) .expect(400);