Skip to content

Instantly share code, notes, and snippets.

@legndery
Created October 1, 2018 15:52
Show Gist options
  • Save legndery/88c5d6fec3698329ae2ad1a024865cd5 to your computer and use it in GitHub Desktop.
Save legndery/88c5d6fec3698329ae2ad1a024865cd5 to your computer and use it in GitHub Desktop.
var http = require('http');
var request = require('request');
var env = process.env.NODE_ENV;
var bunyan = require('bunyan');
const uuidV1 = require('uuid/v1');
var logenv = process.env.LOG_ENV;
var fileName = 'Utils';
var externalApiParams;
if(env){
fileName += '_'+env;
}
if(logenv){
fileName += '_'+logenv;
}
if(logenv == 'debug'){
var log = bunyan.createLogger({
name: fileName,
streams: [
{
stream: process.stdout,
level: 'debug'
},
{
stream: process.stdout,
level: 'info'
},
{
stream: process.stdout,
level: 'error'
}
]
});
}
else{
var log = bunyan.createLogger({
name: fileName,
streams: [
{
stream: process.stdout,
level: 'info'
},
{
stream: process.stdout,
level: 'error'
}
]
});
}
var querystring = require('querystring');
var _ = require('lodash');
// if(env == 'production'){
// try{
// externalApiParams = require('..//..//server/config/externalApiParams.production.json');
// }
// catch(error){
// log.error('Error in finding externalApiParams for production',error);
// }
// try{
// logisticsKey = require('../../server/config/logisticsConfig.production');
// }
// catch(error){
// log.error('Error in finding logisticsKey for production',error);
// }
// }
if (env === 'testing') {
externalApiParams = require('..//..//server/externalApiParams.json');
} else if (env === 'production') {
externalApiParams = require('..//..//server/externalApiParams.production.json');
} else if (env == 'demo') {
externalApiParams = require('..//..//server/externalApiParams.json');
} else {
externalApiParams = require('..//..//server/externalApiParams.json');
}
var paymentUtilsRequest = request.defaults({
headers: {
'content-type': 'application/json',
'accept': 'application/json',
'module': 'core'
}
})
function hasSufficientParameters(data, requiredKeysArray, specificValues, specificChecks, callback) {
var missing = [];
var cnt = 0;
var checkFunctions = {
inArray: function(value, checkIn) {
var boo = false;
if (checkIn.indexOf(value) > -1) {
boo = true;
}
return boo;
},
arrayLength: function(value, minLength) {
var boo = false;
if (value && value.length > minLength) {
boo = true;
}
return boo;
},
exists: function(value) {
var boo = false;
if (value != null) {
boo = true;
}
return boo;
},
fieldsInArray: function(value) {
log.trace(value);
var boo = false;
for (var i = specificValues.length - 1; i >= 0; i--) {
if (!data[specificValues[i]]) {
cnt = parseInt(cnt) + 1;
}
//console.log(cnt);
}
if (cnt != specificValues.length) {
boo = true;
}
return boo;
}
};
var i;
if (requiredKeysArray.length > 0) {
for (i = requiredKeysArray.length - 1; i >= 0; i--) {
if (typeof data[requiredKeysArray[i]] !== 'boolean') {
if (!data[requiredKeysArray[i]]) {
missing.push(requiredKeysArray[i]);
}
}
}
}
if (specificValues.length > 0) {
//console.log(specificValues.length);
for (i = specificValues.length - 1; i >= 0; i--) {
checkObj = specificChecks[i];
currentValue = specificValues[i];
if (checkObj.checkType == 'exists') {
if (!(checkFunctions[checkObj.checkType](data[currentValue]))) {
missing.push(currentValue);
}
} else if (checkObj.checkType == 'fieldsInArray') {
if (!(checkFunctions[checkObj.checkType](data[currentValue]))) {
missing = missing.concat(specificValues);
//console.log(missing);
}
break;
} else {
if (!(checkFunctions[checkObj.checkType](data[currentValue], checkObj.checkAgainstValue))) {
missing.push(currentValue);
}
}
}
}
if (missing.length > 0) {
return callback({
status: 400,
msg: "Insufficient parameter",
data: missing
});
} else {
return callback(null, {
success: true,
msg: 'All required keys are present'
});
}
}
function externalRequest(requestData, body, cb) {
paymentUtilsRequest.post({
url: externalApiParams.payment.url + externalApiParams.payment.urlList.externalRequest,
json: {
requestData: requestData,
body: body
}
}, function(error, response, body){
return cb(error, body);
});
// hasSufficientParameters(requestData, ['host', 'method', 'authToken', 'contentType'], [], [], function (error, sufficientParamObj) {
// if (sufficientParamObj.success) {
// log.info('requestData', requestData);
// var host = requestData.host;
// var method = requestData.method;
// var authToken = requestData.authToken;
// var contentType = requestData['contentType'];
// var sendObj;
// if (method === 'GET') {
// var sendObj = {
// url: host,
// method: method,
// headers: {
// 'content-type': contentType,
// 'Authorization': authToken
// }
// };
// } else {
// sendObj = {
// url: host,
// method: method,
// json: true,
// body: body,
// headers: {
// 'content-type': contentType,
// 'Authorization': authToken
// }
// };
// }
// if (contentType != 'application/json' && sendObj.json) {
// delete sendObj['json'];
// }
// if(authToken == 'N/A'){
// delete sendObj['headers']['Authorization'];
// }
// var uid = uuidV1();
// sendObj.uid = uid;
// log.info('Request Body ',sendObj)
// request(sendObj, function (error, res, body) {
// if (error) {
// log.info('Response Error ', error,uid);
// return cb(null, {
// success: false,
// msg: "Error from host",
// data: {},
// statusCode: 400,
// statusType: "ERROR_FROM_HOST"
// });
// } else if (res.statusCode !== 200) {
// log.info('Response Body Status Code is Not 200 ', res.body,uid);
// return cb(null, {
// success: false,
// msg: "Error from host",
// data: res.body,
// statusCode: 400,
// statusType: "ERROR_FROM_HOST"
// });
// } else {
// log.info('Response Body ', res.body,uid);
// return cb(null, {
// success: true,
// msg: "Success from host",
// data: res.body,
// statusCode: 200,
// statusType: "SUCCESS_FROM_HOST"
// });
// }
// });
// } else {
// return cb(null, sufficientParamObj);
// }
// });
};
function postTokenRequest(data, cb){
log.info("input in postTokenRequest");
log.info(data);
paymentUtilsRequest.post({
url: externalApiParams.payment.url + externalApiParams.urlList.postTokenRequest,
json: {
url: data
}
}, function(error, response, body){
return cb(error, body);
})
// request.post({
// url: data
// }, function(error, response, body){
// try{
// body = JSON.parse(body);
// if(error){
// log.error("Error occured");
// log.error(error);
// cb()
// }
// // else if(!body.success){
// // log.error("failed");
// // cb(null, {success:false, message:body.message, data:body});
// // }
// else{
// log.info("success");
// log.info(body);
// cb(null, {success:true, message:body.StatusMessage, data:body});
// }
// }
// catch (err){
// log.info("error occured in parsing ", err);
// cb();
// }
// });
}
exports.externalRequest = externalRequest;
exports.postTokenRequest = postTokenRequest;
exports.hasSufficientParameters = hasSufficientParameters;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment