Martin Donnelly 57bd6c8e6a init
2018-06-24 21:15:03 +01:00

99 lines
2.6 KiB
JavaScript

/**
* Controller to manage the merchant status
*/
'use strict';
var _ = require('lodash');
var Q = require('q');
var httpStatus = require('http-status-codes');
var mongodb = require('mongodb');
var debug = require('debug')('webconsole-api:controllers:merchant');
var mainDB = require(global.pathPrefix + 'mainDB.js');
module.exports = {
addMerchantPromoCode: addMerchantPromoCode
};
/**
* Enable the merchant status based on a valid promo code
*
* @param {Object} req - Express request object
* @param {Object} res - Express response object
*/
function addMerchantPromoCode(req, res) {
//
// Check that the client is a merchant
//
if (req.session.data.isMerchant) {
res.status(httpStatus.CONFLICT).json({
code: 31001,
info: 'Already a merchant'
});
return;
}
//
// Get the query params from the request and the session
//
var clientID = req.session.data.clientID;
var promoCode = req.swagger.params.body.value.PromoCode;
//
// Check the promo code is the one we have hardcoded
//
const DEFAULT_PROMO_CODE = 'c4e2cd44f7774ad5847ca6d5';
if (promoCode !== DEFAULT_PROMO_CODE) {
res.status(httpStatus.BAD_REQUEST).json({
code: 31002,
info: 'Invalid Promotion Code'
});
return;
}
//
// Define the query according to the params
//
var query = {
ClientID: clientID
};
var update = {
$set: {
'Merchant.0.MerchantStatus': 1
},
$currentDate: {
LastUpdate: true
},
$inc: {
LastVersion: 1
}
};
var options = {
upsert: false // Don't upsert if not found
};
mainDB.updateObject(mainDB.collectionClient, query, update, options, false,
function(err, results) {
if (err) {
debug('- failed to update Merchant', err);
res.status(httpStatus.BAD_GATEWAY).json({
code: 31003,
info: 'Database offline'
});
} else if (results.result.n === 0) {
//
// Nothing found - perhaps the client has been removed in the interim?
//
res.status(httpStatus.NOT_FOUND).json({
code: 31004,
info: 'Client not found'
});
} else {
// All good - update the session to note that we are now a merchant
req.session.data.isMerchant = true;
res.status(httpStatus.OK).json();
}
});
}