Skip to content

Conversation

@m-amaya
Copy link
Contributor

@m-amaya m-amaya commented May 11, 2016

No description provided.

@m-amaya m-amaya added the RFR label May 11, 2016
@m-amaya m-amaya self-assigned this May 11, 2016

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);
});

@jrf0110
Copy link
Member

jrf0110 commented May 26, 2016

connnnnnnnnnflict

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants