From 6efce50d56212f30f1747f9db2753e00f1e8f0e1 Mon Sep 17 00:00:00 2001 From: Nattapon Wongpaet Date: Wed, 15 Aug 2018 09:32:01 +0700 Subject: [PATCH] update validate and messageSoap --- ais-structure/src/config/config.js | 1 + ais-structure/src/config/express.js | 5 ++++- ais-structure/src/modules/helper/messageSOAP.js | 84 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ ais-structure/src/modules/helper/stats.js | 33 +++++++++++++++++++++++++++++++++ ais-structure/src/modules/helper/validator.js | 31 +++++++++++++++++++++++++++++++ ais-structure/src/modules/vizcard/postVizCard.ctrl.js | 10 +++++++++- ais-structure/src/modules/vizcard/vizCard.ctrl.js | 138 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++------------------------------------------------- 7 files changed, 251 insertions(+), 51 deletions(-) create mode 100644 ais-structure/src/modules/helper/messageSOAP.js create mode 100644 ais-structure/src/modules/helper/stats.js create mode 100644 ais-structure/src/modules/helper/validator.js diff --git a/ais-structure/src/config/config.js b/ais-structure/src/config/config.js index 00c7b14..de04bb7 100644 --- a/ais-structure/src/config/config.js +++ b/ais-structure/src/config/config.js @@ -1,6 +1,7 @@ /* ------------- [START SERVER CONFIG VARIABLES] ------------ */ var config = { development: { + app_name: "CMF", app_host: "0.0.0.0", app_port: "3000", app_https: false, diff --git a/ais-structure/src/config/express.js b/ais-structure/src/config/express.js index 94fdc4b..56a759d 100644 --- a/ais-structure/src/config/express.js +++ b/ais-structure/src/config/express.js @@ -140,7 +140,7 @@ module.exports = function () { app.all('/api/*', getCurrentUser, function (req, res, next) { res.header("Access-Control-Allow-Origin", "*"); res.header("Access-Control-Allow-Headers", "X-Requested-With"); - // var headerLog = req.currentUser ? + '' + req.currentUser.username + ':'+req.id: req.id; + // var headerLog = req.currentUser ? + '' + req.currentUser.username + ':'+req.id: req.id; next(); }); @@ -158,6 +158,9 @@ module.exports = function () { var headerLog = 'IP|'+remoteIp+'|USER|'+username+'|REQUESTID|'+req.id; logger.setHeader(headerLog); + // console.log("req " +req.headers['x-token']); + // console.log("fullUrl " +req.originalUrl); + // console.log("method " +req.method); next(); }); diff --git a/ais-structure/src/modules/helper/messageSOAP.js b/ais-structure/src/modules/helper/messageSOAP.js new file mode 100644 index 0000000..2df4c25 --- /dev/null +++ b/ais-structure/src/modules/helper/messageSOAP.js @@ -0,0 +1,84 @@ +var soap = []; + +soap.objectToSOAP = function (objectData,tag){ + var header = ''; + var footer = ""; + var body = ""+getBodySOAP(objectData)+""; + + return header+body+footer; + +}; + +soap.soapToArray = function(req,resultSoap) +{ + var returnData = []; + + if(req.query.fields){ + var listFields = req.query.fields.split(","); + console.log(listFields) + } + + var resultSet = []; + if(Array.isArray(resultSoap)) + { + for(var i=0;i"+objectData[keyList[i]]+""; + else + body += ""+getBodySOAP(objectData[keyList[i]])+""; + } + } + return body; +} + +function capitalizeFirstLetter(string) { + return string.charAt(0).toLowerCase() + string.slice(1); +} + +module.exports = soap; \ No newline at end of file diff --git a/ais-structure/src/modules/helper/stats.js b/ais-structure/src/modules/helper/stats.js new file mode 100644 index 0000000..4cb0a4f --- /dev/null +++ b/ais-structure/src/modules/helper/stats.js @@ -0,0 +1,33 @@ +var env = process.env.NODE_ENV || 'development'; +var cfg = require('../../config/config.js').get(env); + +var nodeName = cfg.app_name; +var stat = []; + + + +stat.reciveRequest = function (api,pass){ + if(pass) + writeStats(nodeName+" Recive "+api+" Request"); + else + writeStats(nodeName+" Recive Bad "+api+" Request"); +}; + +stat.reciveResponse = function (fromNode,api,result){ + writeStats(nodeName+" Recive "+fromNode+" "+api+" Response "+result); +}; + +stat.sendRequest = function (toNode,api){ + writeStats(nodeName+" Send "+toNode+" "+api+" Request"); +}; + +stat.sendResponse = function (api,result){ + writeStats(nodeName+" Send "+api+" Response "+result); +}; + +function writeStats(string) { + console.log(string); + // console.log(cfg); +} + +module.exports = stat; \ No newline at end of file diff --git a/ais-structure/src/modules/helper/validator.js b/ais-structure/src/modules/helper/validator.js new file mode 100644 index 0000000..1cb886f --- /dev/null +++ b/ais-structure/src/modules/helper/validator.js @@ -0,0 +1,31 @@ +var stats = require('../helper/stats.js'); + +module.exports = function (req,mandatoryList,api){ + + var err = []; + + for(var i=0;i 0) + stats.reciveRequest(api,false); + else + stats.reciveRequest(api,true); + + // console.log(err); + + return err; +}; + diff --git a/ais-structure/src/modules/vizcard/postVizCard.ctrl.js b/ais-structure/src/modules/vizcard/postVizCard.ctrl.js index f861b83..8fc1cae 100644 --- a/ais-structure/src/modules/vizcard/postVizCard.ctrl.js +++ b/ais-structure/src/modules/vizcard/postVizCard.ctrl.js @@ -4,8 +4,16 @@ var _ = require('lodash'); var env = process.env.NODE_ENV || 'development'; var rp = require('request-promise'); var logger = require('../../logger/logger'); -var cfg = require('../../config/config.js').get(env); +var cfg = require('../../config/config.js').get(env); + +var stats = require('../helper/stats.js'); +var messageSOAP = require('../helper/messageSOAP.js'); exports.postVizcard = function (req, res, next){ + stats.reciveRequest("Post Vizcard"); res.send("post viz card"); + + + + // console.log(messageSOAP.objectToSOAP(req,"SearchCustomer")); }; \ No newline at end of file diff --git a/ais-structure/src/modules/vizcard/vizCard.ctrl.js b/ais-structure/src/modules/vizcard/vizCard.ctrl.js index 810ad95..e74f531 100644 --- a/ais-structure/src/modules/vizcard/vizCard.ctrl.js +++ b/ais-structure/src/modules/vizcard/vizCard.ctrl.js @@ -7,74 +7,114 @@ var logger = require('../../logger/logger'); var cfg = require('../../config/config.js').get(env); var request = require('request'); var parseJson = require('xml-js'); -var _url = `http://localhost:8080` +var _url = `http://localhost:8080`; + +var stats = require('../helper/stats.js'); +var messageSOAP = require('../helper/messageSOAP.js'); +var validatorHelper = require('../helper/validator.js'); exports.vizcard = async function (req, res, next) { - var requestXml = ` - - - - axviz - 1234 - 30 - 1 - ${req.params.id} - - - ` - - // console.log(requestXml); - // console.log(req.query.fields) - - const result = await asyncRequest({ - url : _url, - body : requestXml, - method : 'POST' - }); - // parseString(result.body, function (err, result1) { - // console.log(result1); - // }); + var err = validator(req,"GET VIZCard"); + + if(err.length > 0) + { + var response = { + resultCode : "40300", + developerMessage : "Missing or invalid parameter" + }; - var result1 = parseJson.xml2json(result.body, {compact: true, spaces: 4}) - result1 = JSON.parse(result1) - result1 = result1['soap:Envelope']['soap:Body']['tem:SearchCustomerResponse']['tem:SearchCustomerResult']['tem:CustomerSearchResult']; - delete result1._comment; + res.status(200).json(response); + }else + { + // var requestXml = ` + // + // + // + // axviz + // 1234 + // 30 + // 1 + // ${req.params.id} + // + // + // ` - var response = {}; + var objectData = { + Username : "axviz", + Password : "1234", + SystemName : "30", + QueryType : "1", + CardId : req.params.id + }; - response['resultCode'] = '20000'; - response['developerMessage'] = 'Success'; + var soap = messageSOAP.objectToSOAP(objectData,"SearchCustomer") + // console.log(soap); - if(req.query.fields){ - var listFields = req.query.fields.split(","); - // console.log(listFields) - } + // console.log(requestXml); + // console.log(req.query.fields) - for(var key in result1){ - var keyObj = capitalizeFirstLetter(key.replace('tem:','')) - // console.log(keyObj+' : '+result1[key]['_text']); - if(listFields){ - if(listFields.includes(keyObj)){ - response[keyObj] = result1[key]['_text']; - } + const result = await asyncRequest({ + url : _url, + body : soap, + method : 'POST' + }); + + console.log(result.err) + + // parseString(result.body, function (err, result1) { + // console.log(result1); + // }); + if(typeof result.err === 'undefined'){ + var resultSoap = parseJson.xml2json(result.body, {compact: true, spaces: 4}) + resultSoap = JSON.parse(resultSoap) + resultSoap = resultSoap['soap:Envelope']['soap:Body']['tem:SearchCustomerResponse']['tem:SearchCustomerResult']['tem:CustomerSearchResult']; + + var resultData = messageSOAP.soapToArray(req,resultSoap); + + var response = { + resultCode : "20000", + developerMessage : "Success", + resultData : resultData, + rowCount : resultData.length + }; } else { - response[keyObj] = result1[key]['_text']; + var response = { + resultCode : "50000", + developerMessage : "System error" + }; } + + res.status(200).json(response); } - - res.status(200).json(response); }; -function capitalizeFirstLetter(string) { - return string.charAt(0).toLowerCase() + string.slice(1); + +function validator(req,api) +{ + var list = []; + list.push("commandId"); + list.push("firstName"); + list.push("lastName"); + list.push("emailAddress"); + list.push("moblieNo"); + + return validatorHelper(req,list,api); } + + + + function asyncRequest (params = {}) { return new Promise((resolve, reject) => { request(params, function (error, response, body) { if (error) { - reject(error); + // reject(error); + resolve({ + 'body' : body, + 'err': error + }); } else { resolve({ 'body' : body, -- libgit2 0.21.2