Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add PROMO server-side validation route #2318

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion controllers/orders/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
module.exports = require('./orders');
module.exports.orderItems = require('./order-items');
module.exports.notifications = require('./notifications');
module.exports.deliveryServices = require('./delivery-services');
module.exports.deliveryServices = require('./delivery-services');
module.exports.promos = require('./promos');
30 changes: 30 additions & 0 deletions controllers/orders/promos.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
var db = require('../../db');
var errors = require('../../errors');

module.exports.validate = function(req, res) {
const promo_code = req.body.promo_code;
const order_id = req.params.oid;

var where = { promo_code };

db.promos.findOne( where, (err, result) => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This works nicely, but I think we can make it even better by consolidating the two database calls into one by adding a join:

var where = { promo_code, 'promos_applied.order_id': order_id };

var options = {
  joins:  [ { type: 'left'
            , target: 'promos_applied'
            , on: { promo_id: '$promos.id$' }
            }
          ]
};

db.promos.findOne( where, options, (err, result) => {
  if( !result ) {
    res.error( errors.promos.DOES_NOT_EXIST );
    return;
  }

  if( result.order_id ) {
    res.error( errors.promos.ALREADY_APPLIED );
    return;
  }

  res.sendStatus(204);
});


if( !result ) {
res.error( errors.promos.DOES_NOT_EXIST );
return;
}

var where = { promo_id: promo_code, order_id };

db.promos_applied.findOne( where, (err, result) => {

if( result ) {
res.error( errors.promos.ALREADY_APPLIED );
return;
}

res.sendStatus(200);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the request is successful, and but there's no content to return, use the status code 204 - no content

});

});
};
22 changes: 22 additions & 0 deletions errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -319,3 +319,25 @@ errors.notifications.NOT_AVAILABLE = {
, name: 'NOT_AVAILABLE'
, message: 'The requested notification is not available.'
};

/**
* Promo Errors
*/

errors.promos = { };

errors.promos.DOES_NOT_EXIST = {
type: 'promos'
, code: '0901'
, httpCode: '400'
, name: 'DOES_NOT_EXIST'
, message: 'The requested promo code does not exist.'
};

errors.promos.ALREADY_APPLIED = {
type: 'promos'
, code: '0902'
, httpCode: '400'
, name: 'ALREADY_APPLIED'
, message: 'The requested promo code has already been applied to the order.'
};
4 changes: 4 additions & 0 deletions routes/orders/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -316,3 +316,7 @@ route.get('/:oid/payment', m.getOrder2({
deliveryService: true,
restaurantDbModelFind: true
}), m.restrict(['admin', 'order-owner']), m.view('order-payment', {}));

route.post('/:oid/promo/validate'
, m.restrict(['admin', 'order-owner'])
, controllers.orders.promos.validate);