getMembership.ctrl.js
5.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
var parseJson = require('xml-js');
var stats = require('../helper/stats.js');
var messageSOAP = require('../helper/messageSOAP.js');
var validatorHelper = require('../helper/validator.js');
var connection = require('../helper/connection.js');
var responseMsg = require('../helper/responseMsg.js');
var log = require('../helper/log.js');
var constant = require('../helper/constants.js')
exports.getMembership = async function (req, res, next) {
var getCmd = "CustomerMembership";
var sendCusCmd = "Customer";
var sendMemCmd = "MemberCard";
var d01 = "D01";
var method = constant.METHOD.GET;
if(req.params.customerId){
var customerId = req.params.customerId
} else if(req.params.userData && req.params.userType){
var customerId = req.params.userData+"@"+req.params.userType
} else if(req.query.clientName && req.query.commandId){
var customerId = req.query.clientName+"@"+req.query.commandId;
}
log.startlog(method+"_"+getCmd,req.query.commandId,customerId);
log.logDetail.addInput(req.query.clientName,method+"_"+getCmd,constant.REQUEST,req,req.body);
var err = validator(req,getCmd);
if(err.length > 0)
{
log.addErrorSummary(req.query.clientName,method+"_"+getCmd,"null","Fail");
log.log(err,'error');
var response = responseMsg.error(req,getCmd,40300);
// res.status(200).json(response);
}else
{
log.addSuccessSummary(req.query.clientName,method+"_"+getCmd,"null","Success");
var objectData = Object.assign(req.query,req.params);
const result = await connection.requestJsonToD01(objectData,sendCusCmd,method);
log.logDetail.addInput(d01,method+"_"+sendCusCmd,constant.RESPONSE,result,result.response);
if(typeof result.err === 'undefined'){
var resultObj = result.response;
if(resultObj.resultCode.startsWith("2"))
{
if(resultObj.resultData && resultObj.resultData.length>0)
{
log.addSuccessSummary(d01,method+"_"+sendCusCmd,resultObj.resultCode,resultObj.resultDescription);
var cusIdArr = [];
stats.receiveRestResponse(d01,method,sendCusCmd,constant.SUCCESS);
for(var i = 0; i < resultObj.resultData.length; i++){
cusIdArr.push(resultObj.resultData[i].customerId)
}
objectData = {customerId : cusIdArr}
// console.log(objectData)
const resultMemberCard = await connection.requestJsonToD01(objectData,sendMemCmd,method);
stats.receiveRestResponse(d01,method,sendMemCmd,constant.SUCCESS);
log.logDetail.addInput(d01,method+"_"+sendCusCmd,constant.RESPONSE,resultMemberCard,resultMemberCard.response);
// console.log(JSON.stringify(resultMemberCard));
var resultMemberCardObj = resultMemberCard.response;
console.log(JSON.stringify(resultMemberCardObj))
log.addSuccessSummary(d01,method+"_"+sendMemCmd,resultMemberCardObj.resultCode,resultMemberCardObj.resultDescription);
for(var i = 0; i < resultObj.resultData.length; i++){
for(var j = 0; j < resultMemberCardObj.resultData.length; j++){
if(resultMemberCardObj.resultData[j].cardOwnerIdList.includes(resultObj.resultData[i].customerId)){
resultObj.resultData[i] = Object.assign(resultObj.resultData[i],{cardId : resultMemberCardObj.resultData[j].cardId});
}
}
}
var response = responseMsg.success(req,getCmd,resultObj);
}else
{
stats.receiveRestResponse(d01,method,sendCusCmd,constant.RESPONSERESULT.DATA_NOT_FOUND.developerMessage);
var response = responseMsg.error(req,getCmd,40300);
log.addErrorSummary(d01,method+"_"+sendCusCmd,response.resultCode,response.developerMessage);
}
} else {
stats.receiveRestResponse(d01,method,sendCusCmd,constant.ERROR);
var response = responseMsg.direct(req,getCmd,resultObj);
log.addErrorSummary(d01,method+"_"+sendCusCmd,resultObj.resultCode,resultObj.developerMessage);
}
} else {
stats.receiveRestResponse(d01,method,sendCusCmd,constant.ERROR);
var response = responseMsg.error(req,getCmd,50000);
// log.addErrorSummary(d01,method+"_"+sendCmd,response.resultCode,response.developerMessage);
}
}
// console.log(response);
log.logDetail.addOutput(req.query.clientName,method+"_"+getCmd,constant.RESPONSE,response,response);
res.status(200).json(response);
next();
};
function validator(req,cmd)
{
var list = [];
list.push([true,"queryStr","commandId","int"]);
list.push([true,"queryStr","clientName","string"]);
// list.push([true,"queryStr","lastName","string"]);
// list.push([true,"queryStr","emailAddress","string"]);
// list.push([true,"queryStr","mobileNo","string"]);
// list.push([false,"queryStr","mobileNo","string"]);
var err = validatorHelper(req,list);
if(err.length > 0)
stats.receiveBadRequest(req.method,cmd);
else
stats.receiveRequest(req.method,cmd);
return err;
}