Commit 6efce50d56212f30f1747f9db2753e00f1e8f0e1

Authored by Nattapon Wongpaet
1 parent ba477a68
Exists in master and in 1 other branch dev

update validate and messageSoap

ais-structure/src/config/config.js
1 /* ------------- [START SERVER CONFIG VARIABLES] ------------ */ 1 /* ------------- [START SERVER CONFIG VARIABLES] ------------ */
2 var config = { 2 var config = {
3 development: { 3 development: {
  4 + app_name: "CMF",
4 app_host: "0.0.0.0", 5 app_host: "0.0.0.0",
5 app_port: "3000", 6 app_port: "3000",
6 app_https: false, 7 app_https: false,
ais-structure/src/config/express.js
@@ -140,7 +140,7 @@ module.exports = function () { @@ -140,7 +140,7 @@ module.exports = function () {
140 app.all('/api/*', getCurrentUser, function (req, res, next) { 140 app.all('/api/*', getCurrentUser, function (req, res, next) {
141 res.header("Access-Control-Allow-Origin", "*"); 141 res.header("Access-Control-Allow-Origin", "*");
142 res.header("Access-Control-Allow-Headers", "X-Requested-With"); 142 res.header("Access-Control-Allow-Headers", "X-Requested-With");
143 - // var headerLog = req.currentUser ? + '' + req.currentUser.username + ':'+req.id: req.id; 143 + // var headerLog = req.currentUser ? + '' + req.currentUser.username + ':'+req.id: req.id;
144 next(); 144 next();
145 }); 145 });
146 146
@@ -158,6 +158,9 @@ module.exports = function () { @@ -158,6 +158,9 @@ module.exports = function () {
158 var headerLog = 'IP|'+remoteIp+'|USER|'+username+'|REQUESTID|'+req.id; 158 var headerLog = 'IP|'+remoteIp+'|USER|'+username+'|REQUESTID|'+req.id;
159 159
160 logger.setHeader(headerLog); 160 logger.setHeader(headerLog);
  161 + // console.log("req " +req.headers['x-token']);
  162 + // console.log("fullUrl " +req.originalUrl);
  163 + // console.log("method " +req.method);
161 next(); 164 next();
162 }); 165 });
163 166
ais-structure/src/modules/helper/messageSOAP.js 0 → 100644
@@ -0,0 +1,84 @@ @@ -0,0 +1,84 @@
  1 +var soap = [];
  2 +
  3 +soap.objectToSOAP = function (objectData,tag){
  4 + var header = '<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:tem="http://tempuri.org/"><soap:Header/><soap:Body>';
  5 + var footer = "</soap:Body></soap:Envelope>";
  6 + var body = "<tem:"+tag+">"+getBodySOAP(objectData)+"</tem:"+tag+">";
  7 +
  8 + return header+body+footer;
  9 +
  10 +};
  11 +
  12 +soap.soapToArray = function(req,resultSoap)
  13 +{
  14 + var returnData = [];
  15 +
  16 + if(req.query.fields){
  17 + var listFields = req.query.fields.split(",");
  18 + console.log(listFields)
  19 + }
  20 +
  21 + var resultSet = [];
  22 + if(Array.isArray(resultSoap))
  23 + {
  24 + for(var i=0;i<resultSoap.length;i++)
  25 + {
  26 + var row = resultSoap[i];
  27 + delete row._comment;
  28 + resultSet.push(row);
  29 +
  30 + }
  31 + }else
  32 + {
  33 + delete resultSoap._comment;
  34 + resultSet.push(resultSoap);
  35 + }
  36 +
  37 +
  38 + // console.log(resultSet);
  39 +
  40 +
  41 +
  42 + for(var i=0;i<resultSet.length;i++)
  43 + {
  44 + var data = {}
  45 + for(var key in resultSet[i]){
  46 + var keyObj = capitalizeFirstLetter(key.replace('tem:',''))
  47 + // console.log(keyObj+' : '+resultSet[i][key]['_text']);
  48 + if(listFields){
  49 + if(listFields.includes(keyObj)){
  50 + data[keyObj] = resultSet[i][key]['_text'];
  51 + }
  52 + } else {
  53 + data[keyObj] = resultSet[i][key]['_text'];
  54 + }
  55 + }
  56 + returnData.push(data);
  57 + }
  58 +
  59 + return returnData;
  60 +
  61 +};
  62 +
  63 +function getBodySOAP(objectData)
  64 +{
  65 + var body = "";
  66 + if(typeof objectData != 'undefined')
  67 + {
  68 + var keyList = Object.keys(objectData);
  69 + for(var i=0;i<keyList.length;i++)
  70 + {
  71 + if(typeof objectData[keyList[i]] != 'object')
  72 + body += "<tem:"+keyList[i]+">"+objectData[keyList[i]]+"</tem:"+keyList[i]+">";
  73 + else
  74 + body += "<tem:"+keyList[i]+">"+getBodySOAP(objectData[keyList[i]])+"</tem:"+keyList[i]+">";
  75 + }
  76 + }
  77 + return body;
  78 +}
  79 +
  80 +function capitalizeFirstLetter(string) {
  81 + return string.charAt(0).toLowerCase() + string.slice(1);
  82 +}
  83 +
  84 +module.exports = soap;
0 \ No newline at end of file 85 \ No newline at end of file
ais-structure/src/modules/helper/stats.js 0 → 100644
@@ -0,0 +1,33 @@ @@ -0,0 +1,33 @@
  1 +var env = process.env.NODE_ENV || 'development';
  2 +var cfg = require('../../config/config.js').get(env);
  3 +
  4 +var nodeName = cfg.app_name;
  5 +var stat = [];
  6 +
  7 +
  8 +
  9 +stat.reciveRequest = function (api,pass){
  10 + if(pass)
  11 + writeStats(nodeName+" Recive "+api+" Request");
  12 + else
  13 + writeStats(nodeName+" Recive Bad "+api+" Request");
  14 +};
  15 +
  16 +stat.reciveResponse = function (fromNode,api,result){
  17 + writeStats(nodeName+" Recive "+fromNode+" "+api+" Response "+result);
  18 +};
  19 +
  20 +stat.sendRequest = function (toNode,api){
  21 + writeStats(nodeName+" Send "+toNode+" "+api+" Request");
  22 +};
  23 +
  24 +stat.sendResponse = function (api,result){
  25 + writeStats(nodeName+" Send "+api+" Response "+result);
  26 +};
  27 +
  28 +function writeStats(string) {
  29 + console.log(string);
  30 + // console.log(cfg);
  31 +}
  32 +
  33 +module.exports = stat;
0 \ No newline at end of file 34 \ No newline at end of file
ais-structure/src/modules/helper/validator.js 0 → 100644
@@ -0,0 +1,31 @@ @@ -0,0 +1,31 @@
  1 +var stats = require('../helper/stats.js');
  2 +
  3 +module.exports = function (req,mandatoryList,api){
  4 +
  5 + var err = [];
  6 +
  7 + for(var i=0;i<mandatoryList.length;i++)
  8 + {
  9 + if(typeof req.query[mandatoryList[i]] === 'undefined')
  10 + {
  11 + var errDes = {
  12 + Param : mandatoryList[i],
  13 + Reason : "Missing"
  14 + }
  15 + err.push(errDes);
  16 + }else
  17 + {
  18 + // invaild
  19 + }
  20 + }
  21 +
  22 + if(err.length > 0)
  23 + stats.reciveRequest(api,false);
  24 + else
  25 + stats.reciveRequest(api,true);
  26 +
  27 + // console.log(err);
  28 +
  29 + return err;
  30 +};
  31 +
ais-structure/src/modules/vizcard/postVizCard.ctrl.js
@@ -4,8 +4,16 @@ var _ = require(&#39;lodash&#39;); @@ -4,8 +4,16 @@ var _ = require(&#39;lodash&#39;);
4 var env = process.env.NODE_ENV || 'development'; 4 var env = process.env.NODE_ENV || 'development';
5 var rp = require('request-promise'); 5 var rp = require('request-promise');
6 var logger = require('../../logger/logger'); 6 var logger = require('../../logger/logger');
7 -var cfg = require('../../config/config.js').get(env); 7 +var cfg = require('../../config/config.js').get(env);
  8 +
  9 +var stats = require('../helper/stats.js');
  10 +var messageSOAP = require('../helper/messageSOAP.js');
8 11
9 exports.postVizcard = function (req, res, next){ 12 exports.postVizcard = function (req, res, next){
  13 + stats.reciveRequest("Post Vizcard");
10 res.send("post viz card"); 14 res.send("post viz card");
  15 +
  16 +
  17 +
  18 + // console.log(messageSOAP.objectToSOAP(req,"SearchCustomer"));
11 }; 19 };
12 \ No newline at end of file 20 \ No newline at end of file
ais-structure/src/modules/vizcard/vizCard.ctrl.js
@@ -7,74 +7,114 @@ var logger = require(&#39;../../logger/logger&#39;); @@ -7,74 +7,114 @@ var logger = require(&#39;../../logger/logger&#39;);
7 var cfg = require('../../config/config.js').get(env); 7 var cfg = require('../../config/config.js').get(env);
8 var request = require('request'); 8 var request = require('request');
9 var parseJson = require('xml-js'); 9 var parseJson = require('xml-js');
10 -var _url = `http://localhost:8080` 10 +var _url = `http://localhost:8080`;
  11 +
  12 +var stats = require('../helper/stats.js');
  13 +var messageSOAP = require('../helper/messageSOAP.js');
  14 +var validatorHelper = require('../helper/validator.js');
11 15
12 exports.vizcard = async function (req, res, next) { 16 exports.vizcard = async function (req, res, next) {
13 - var requestXml = `<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:tem="http://tempuri.org/">  
14 - <soap:Header/>  
15 - <soap:Body>  
16 - <tem:SearchCustomer>  
17 - <tem:username>axviz</tem:username>  
18 - <tem:password>1234</tem:password>  
19 - <tem:systemName>30</tem:systemName>  
20 - <tem:queryType>1</tem:queryType>  
21 - <tem:cardId>${req.params.id}</tem:cardId>  
22 - </tem:SearchCustomer>  
23 - </soap:Body>  
24 - </soap:Envelope>`  
25 -  
26 - // console.log(requestXml);  
27 - // console.log(req.query.fields)  
28 -  
29 - const result = await asyncRequest({  
30 - url : _url,  
31 - body : requestXml,  
32 - method : 'POST'  
33 - });  
34 17
35 - // parseString(result.body, function (err, result1) {  
36 - // console.log(result1);  
37 - // }); 18 + var err = validator(req,"GET VIZCard");
  19 +
  20 + if(err.length > 0)
  21 + {
  22 + var response = {
  23 + resultCode : "40300",
  24 + developerMessage : "Missing or invalid parameter"
  25 + };
38 26
39 - var result1 = parseJson.xml2json(result.body, {compact: true, spaces: 4})  
40 - result1 = JSON.parse(result1)  
41 - result1 = result1['soap:Envelope']['soap:Body']['tem:SearchCustomerResponse']['tem:SearchCustomerResult']['tem:CustomerSearchResult'];  
42 - delete result1._comment; 27 + res.status(200).json(response);
  28 + }else
  29 + {
  30 + // var requestXml = `<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:tem="http://tempuri.org/">
  31 + // <soap:Header/>
  32 + // <soap:Body>
  33 + // <tem:SearchCustomer>
  34 + // <tem:username>axviz</tem:username>
  35 + // <tem:password>1234</tem:password>
  36 + // <tem:systemName>30</tem:systemName>
  37 + // <tem:queryType>1</tem:queryType>
  38 + // <tem:cardId>${req.params.id}</tem:cardId>
  39 + // </tem:SearchCustomer>
  40 + // </soap:Body>
  41 + // </soap:Envelope>`
43 42
44 - var response = {}; 43 + var objectData = {
  44 + Username : "axviz",
  45 + Password : "1234",
  46 + SystemName : "30",
  47 + QueryType : "1",
  48 + CardId : req.params.id
  49 + };
45 50
46 - response['resultCode'] = '20000';  
47 - response['developerMessage'] = 'Success'; 51 + var soap = messageSOAP.objectToSOAP(objectData,"SearchCustomer")
  52 + // console.log(soap);
48 53
49 - if(req.query.fields){  
50 - var listFields = req.query.fields.split(",");  
51 - // console.log(listFields)  
52 - } 54 + // console.log(requestXml);
  55 + // console.log(req.query.fields)
53 56
54 - for(var key in result1){  
55 - var keyObj = capitalizeFirstLetter(key.replace('tem:',''))  
56 - // console.log(keyObj+' : '+result1[key]['_text']);  
57 - if(listFields){  
58 - if(listFields.includes(keyObj)){  
59 - response[keyObj] = result1[key]['_text'];  
60 - } 57 + const result = await asyncRequest({
  58 + url : _url,
  59 + body : soap,
  60 + method : 'POST'
  61 + });
  62 +
  63 + console.log(result.err)
  64 +
  65 + // parseString(result.body, function (err, result1) {
  66 + // console.log(result1);
  67 + // });
  68 + if(typeof result.err === 'undefined'){
  69 + var resultSoap = parseJson.xml2json(result.body, {compact: true, spaces: 4})
  70 + resultSoap = JSON.parse(resultSoap)
  71 + resultSoap = resultSoap['soap:Envelope']['soap:Body']['tem:SearchCustomerResponse']['tem:SearchCustomerResult']['tem:CustomerSearchResult'];
  72 +
  73 + var resultData = messageSOAP.soapToArray(req,resultSoap);
  74 +
  75 + var response = {
  76 + resultCode : "20000",
  77 + developerMessage : "Success",
  78 + resultData : resultData,
  79 + rowCount : resultData.length
  80 + };
61 } else { 81 } else {
62 - response[keyObj] = result1[key]['_text']; 82 + var response = {
  83 + resultCode : "50000",
  84 + developerMessage : "System error"
  85 + };
63 } 86 }
  87 +
  88 + res.status(200).json(response);
64 } 89 }
65 -  
66 - res.status(200).json(response);  
67 }; 90 };
68 91
69 -function capitalizeFirstLetter(string) {  
70 - return string.charAt(0).toLowerCase() + string.slice(1); 92 +
  93 +function validator(req,api)
  94 +{
  95 + var list = [];
  96 + list.push("commandId");
  97 + list.push("firstName");
  98 + list.push("lastName");
  99 + list.push("emailAddress");
  100 + list.push("moblieNo");
  101 +
  102 + return validatorHelper(req,list,api);
71 } 103 }
72 104
  105 +
  106 +
  107 +
  108 +
73 function asyncRequest (params = {}) { 109 function asyncRequest (params = {}) {
74 return new Promise((resolve, reject) => { 110 return new Promise((resolve, reject) => {
75 request(params, function (error, response, body) { 111 request(params, function (error, response, body) {
76 if (error) { 112 if (error) {
77 - reject(error); 113 + // reject(error);
  114 + resolve({
  115 + 'body' : body,
  116 + 'err': error
  117 + });
78 } else { 118 } else {
79 resolve({ 119 resolve({
80 'body' : body, 120 'body' : body,