postMembership.ctrl.js
9.63 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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
var stats = require('../helper/stats.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.postMembership = async function (req, res, next){
var getCmd = "CustomerMembership";
var sendCustomerCmd = "Customer";
var sendMemberCardCmd = "MemberCard";
var d01 = "D01";
var postMethod = constant.METHOD.POST;
var putMethod = constant.METHOD.PUT;
var getMethod = 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.body.clientName && req.body.commandId){
var customerId = req.body.clientName+"@"+req.body.commandId;
}
log.startlog(postMethod+"_"+getCmd,req.body.commandId,customerId);
log.logDetail.addInput(req.body.clientName,postMethod+"_"+getCmd,constant.REQUEST,req,req.body);
var err = validator(req,getCmd);
if(err.length > 0)
{
log.addErrorSummary(req.body.clientName,postMethod+"_"+getCmd,"null","Fail");
console.log(err);
var response = responseMsg.error(req,getCmd,40300);
}else
{
log.addSuccessSummary(req.body.clientName,postMethod+"_"+getCmd,"null","Success");
var objectData = {
cardId : req.body.cardId,
cardOwnerIdList : customerId
// _flag : "or"
}
//GET Customer with cardId and cardOwnerIdList
const result = await connection.requestJsonToD01(objectData,sendMemberCardCmd,getMethod);
log.logDetail.addInput(d01,getMethod+"_"+sendMemberCardCmd,constant.RESPONSE,result,result.response);
var objDataGetCustomer = {
req : req,
reqCustomer : {
customerId : customerId
},
cmd : sendCustomerCmd,
method : getMethod,
resultData : result.response.resultData
}
var objDataPostCustomer = {
req : req,
reqCustomer : {
cardId : req.body.cardId,
cardOwnerIdList:[customerId]
},
cmd : sendCustomerCmd,
method : postMethod,
resultData : result.response.resultData
}
if(typeof result.err === 'undefined'){
var resultObj = result.response;
if(resultObj.resultCode.startsWith("2")){
if(resultObj.resultData && resultObj.resultData.length>0){
stats.receiveRestResponse(d01,getMethod,sendMemberCardCmd,constant.SUCCESS);
var response = responseMsg.success(req,getCmd,resultObj);
log.addSuccessSummary(d01,getMethod+"_"+sendMemberCardCmd,response.resultCode,response.developerMessage);
} else { //data not found
//get customer
var getCustomer = await getCustomerD01(objDataGetCustomer);
//if get data exits post membercard
if(!getCustomer.isErr){
if(!getCustomer.isDataNotFound){
//post membercard
stats.receiveRestResponse(d01,getMethod,sendCustomerCmd,constant.SUCCESS);
log.addSuccessSummary(d01,getMethod+"_"+sendCustomerCmd,getCustomer.response.resultCode,getCustomer.response.developerMessage);
var postCustomer = await postCustomerD01(objDataPostCustomer);
if(!postCustomer.isErr){
stats.receiveRestResponse(d01,postMethod,sendCustomerCmd,constant.SUCCESS);
var response = responseMsg.direct(req,getCmd,postCustomer.response);
log.addSuccessSummary(d01,postMethod+"_"+sendCustomerCmd,response.resultCode,response.developerMessage);
} else if(getCustomer.isDirect){
stats.receiveRestResponse(d01,method,sendCustomerCmd,constant.ERROR);
var response = responseMsg.direct(req,getCmd,getCustomer.response);
log.addErrorSummary(d01,method+"_"+sendCmd,getCustomer.response.resultCode,getCustomer.response.developerMessage);
} else {
stats.receiveRestResponse(d01,method,sendCmd,constant.ERROR);
var response = responseMsg.error(req,getCmd,50000);
}
} else {
stats.receiveRestResponse(d01,method,sendCmd,constant.RESPONSERESULT.DATA_NOT_FOUND.developerMessage);
var response = responseMsg.error(req,getCmd,40401);
log.addErrorSummary(d01,method+"_"+sendCmd,response.resultCode,response.developerMessage);
}
} else if(getCustomer.isDirect){
stats.receiveRestResponse(d01,method,sendCmd,constant.ERROR);
var response = responseMsg.direct(req,getCmd,getCustomer.response);
log.addErrorSummary(d01,method+"_"+sendCmd,getCustomer.response.resultCode,getCustomer.response.developerMessage);
} else {
stats.receiveRestResponse(d01,method,sendCmd,constant.ERROR);
var response = responseMsg.error(req,getCmd,50000);
}
//else end flow
}
} else if(resultObj.resultCode.startsWith("404")){
//get customer
//if get data exits post member card
//else end flow
stats.receiveRestResponse(d01,method,sendCmd,constant.ERROR);
var response = responseMsg.direct(req,getCmd,resultObj);
log.addErrorSummary(d01,method+"_"+sendCmd,resultObj.resultCode,resultObj.developerMessage);
} else if(resultObj.resultCode.startsWith("5")){
stats.receiveRestResponse(d01,method,sendCmd,constant.ERROR);
var response = responseMsg.direct(req,getCmd,resultObj);
log.addErrorSummary(d01,method+"_"+sendCmd,resultObj.resultCode,resultObj.developerMessage);
} else {
stats.receiveRestResponse(d01,method,sendCmd,constant.ERROR);
var response = responseMsg.error(req,getCmd,50000);
}
} else {
stats.receiveRestResponse(d01,constant.METHOD.POST,sendCmd,"Error");
var response = responseMsg.direct(req,getCmd,result.response);
log.addErrorSummary(d01,method+"_"+sendCmd,response);
}
}
log.logDetail.addOutput(req.body.clientName,postMethod+"_"+getCmd,constant.RESPONSE,response,response);
res.status(200).json(response);
next();
};
function validator(req,api)
{
// console.log(req.body);
var list = [];
list.push([true,"body","commandId","int"]);
list.push([true,"body","clientName","string"]);
list.push([true,"body","cardId","string"]);
// list.push([true,"body","clientName","string"]);
// list.push([true,"body","firstName","string"]);
// list.push([true,"body","lastName","string"]);
// list.push([true,"body","mobile","string"]);
// list.push([true,"body","emailAddress","string"]);
var err = validatorHelper(req,list,api)
if(err.length > 0)
stats.receiveBadRequest(req.method,api);
else
stats.receiveRequest(req.method,api);
return err;
}
var getCustomerD01 = async function(data){
var responseData = {
isErr : false,
isDataNotFound : false,
isDirect : false
};
const resultCustomer = await connection.requestJsonToD01(data.reqCustomer,data.cmd,data.method);
var resultObj = resultCustomer.response
if(typeof resultCustomer.err === 'undefined'){
if(resultObj.resultCode.startsWith("2")){
if(resultObj.resultData && resultObj.resultData.length>0){
responseData.response = resultObj;
} else {
responseData.response = resultObj;
responseData.isDataNotFound = true;
}
} else if(resultObj.resultCode.startsWith("404") || resultObj.resultCode.startsWith("5")){
responseData.response = resultObj;
responseData.isDirect = true;
} else {
responseData.response = resultObj;
responseData.isErr = true;
}
} else {
responseData.response = resultObj;
responseData.isErr = true;
}
return responseData;
}
var postCustomerD01 = async function(data){
var responseData = {
isErr : false,
isDataNotFound : false,
isDirect : false
};
const resultCustomer = await connection.requestJsonToD01(data.reqCustomer,data.cmd,data.method);
var resultObj = resultCustomer.response
if(typeof resultCustomer.err === 'undefined'){
if(resultObj.resultCode.startsWith("2")){
responseData.response = resultObj;
} else if(resultObj.resultCode.startsWith("404") || resultObj.resultCode.startsWith("5")){
responseData.response = resultObj;
responseData.isDirect = true;
} else {
responseData.response = resultObj;
responseData.isErr = true;
}
} else {
responseData.response = resultObj;
responseData.isErr = true;
}
return responseData;
}