postMembership.ctrl.js
12.2 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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
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')
var env = process.env.NODE_ENV || 'development';
var cfg = require('../../config/config.js').get(env);
const getCmd = "CustomerMembership";
const sendCustomerCmd = "Customer";
const sendMemberCardCmd = "MemberCard";
const d01 = "D01";
const postMethod = constant.METHOD.POST;
const putMethod = constant.METHOD.PUT;
const getMethod = constant.METHOD.GET;
exports.postMembership = async function (req, res, next){
var customerId;
if(req.params.customerId){
customerId = req.params.customerId
} else if(req.params.userData && req.params.userType){
customerId = req.params.userData+"@"+req.params.userType
} else if(req.body.clientName && req.body.commandId){
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 objDataMembership = {
POST : {
req : req,
reqMembership : {
cardId : req.body.cardId,
cardOwnerIdList : [customerId]
},
cmd : sendCustomerCmd,
method : postMethod,
resultData : result.response.resultData
},
PUT : {
req : req,
reqMembership : {
// {"add":{"@cardOwnerIdList":"testAddedCard"}}
add : {
cardOwnerIdList : customerId
}
},
cmd : sendCustomerCmd,
method : putMethod,
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);
log.addSuccessSummary(d01,getMethod+"_"+sendMemberCardCmd,resultObj.resultCode,resultObj.resultDescription);
var todo = await checkOwnerCard(resultObj.resultData,objectData);
// console.log(todo);
if(todo){
var getCustomer = await getCustomerD01(objDataGetCustomer);
var response = await getCustomerHandler(req, getCustomer, resultObj, objDataMembership);
} else {
stats.receiveRestResponse(d01,getMethod,sendMemberCardCmd,constant.SUCCESS);
var response = responseMsg.direct(req,getCmd,constant.RESPONSECONN.MESSAGE.EXCEED);
log.addSuccessSummary(d01, getMethod+"_"+sendMemberCardCmd, response.resultCode, response.developerMessage);
}
} else { //data not found
stats.receiveRestResponse(d01,getMethod,sendMemberCardCmd,constant.RESPONSERESULT.DATA_NOT_FOUND.developerMessage);
log.addSuccessSummary(d01,getMethod+"_"+sendMemberCardCmd,constant.RESPONSERESULT.DATA_NOT_FOUND.resultCode,constant.RESPONSERESULT.DATA_NOT_FOUND.developerMessage);
//get customer
var getCustomer = await getCustomerD01(objDataGetCustomer);
var response = await getCustomerHandler(req, getCustomer, resultObj, objDataMembership);
}
} else if(resultObj.resultCode.startsWith("404")){ //data not found
//get customer
var getCustomer = await getCustomerD01(objDataGetCustomer);
var response = await getCustomerHandler(req, getCustomer, resultObj, objDataMembership);
} else if(resultObj.resultCode.startsWith("5")){
stats.receiveRestResponse(d01, getMethod, sendMemberCardCmd, constant.ERROR);
var response = responseMsg.direct(req, getCmd, resultObj);
log.addErrorSummary(d01, getMethod+"_"+sendMemberCardCmd, resultObj.resultCode, resultObj.developerMessage);
} else {
stats.receiveRestResponse(d01, getMethod, sendMemberCardCmd, constant.ERROR);
var response = responseMsg.error(req, getCmd, 50000);
}
} else {
stats.receiveRestResponse(d01,method,sendCusCmd,constant.ERROR);
var response = responseMsg.error(req,getCmd,50000);
}
}
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;
}
async function getCustomerD01(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;
}
async function postMemberCardD01(data){
var responseData = {
isErr : false,
isDataNotFound : false,
isDirect : false
};
const resultCustomer = await connection.requestJsonToD01(data.reqMembership, 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;
}
async function getCustomerHandler(req, getCustomer, getMemberCard, objDataMembership){
// console.log(getCustomer.response)
if(!getCustomer.isErr){
//if get data exits post membercard
if(!getCustomer.isDataNotFound){
//post membercard
stats.receiveRestResponse(d01, getMethod, sendCustomerCmd, constant.SUCCESS);
log.addSuccessSummary(d01, getMethod+"_"+sendCustomerCmd, getCustomer.response.resultCode, getCustomer.response.resultDescription);
//check post or put
var postMemberCard = await postMemberCardD01(objDataMembership.POST);
var response = await postMemberCardHandler(req, postMemberCard)
} else {
stats.receiveRestResponse(d01, getMethod, sendCustomerCmd, constant.RESPONSERESULT.DATA_NOT_FOUND.resultDescription);
var response = responseMsg.error(req, getCmd, 40401);
log.addErrorSummary(d01, getMethod+"_"+sendCustomerCmd, response.resultCode, response.resultDescription);
}
} else if(getCustomer.isDirect){
stats.receiveRestResponse(d01, getMethod, sendCustomerCmd, constant.ERROR);
var response = responseMsg.direct(req, getCmd, getCustomer.response);
log.addErrorSummary(d01, getMethod+"_"+sendCustomerCmd, getCustomer.response.resultCode, getCustomer.response.resultDescription);
} else {
stats.receiveRestResponse(d01, getMethod, sendCustomerCmd, constant.ERROR);
var response = responseMsg.error(req, getCmd, 50000);
}
//else end flow
return response;
};
async function postMemberCardHandler(req, memberCard){
if(!memberCard.isErr){
stats.receiveRestResponse(d01, postMethod, sendMemberCardCmd, constant.SUCCESS);
var response = responseMsg.direct(req, getCmd, memberCard.response);
log.addSuccessSummary(d01, postMethod+"_"+sendMemberCardCmd, response.resultCode, response.developerMessage);
} else if(getCustomer.isDirect){
stats.receiveRestResponse(d01, postMethod, sendMemberCardCmd, constant.ERROR);
var response = responseMsg.direct(req, getCmd, memberCard.response);
log.addErrorSummary(d01, postMethod+"_"+sendMemberCardCmd, memberCard.response.resultCode, memberCard.response.developerMessage);
} else {
stats.receiveRestResponse(d01, postMethod, sendMemberCardCmd, constant.ERROR);
var response = responseMsg.error(req, getCmd, 50000);
}
return response;
};
async function checkOwnerCard(resultData,objectData){
var userOwnerCardLimit = cfg.service.D01.userOwnerCardLimit
var userMembershipCardLimit = cfg.service.D01.userMembershipCardLimit
var cardId = objectData.cardId;
var customerId = objectData.cardOwnerIdList;
var countf = async function (resultData){
var dataCount = {
userOwnCardCount : 0,
cardOwnUserCount : 0
}
for(var i = 0 ; i < resultData.length ; i++){
// console.log(resultData[i])
if(resultData[i].cardOwnerIdList.includes(customerId)){
dataCount.userOwnCardCount = dataCount.userOwnCardCount++;
}
if(resultData[i].cardId === cardId){
dataCount.cardOwnUserCount = resultData[i].cardOwnerIdList.length;
}
}
return dataCount;
};
var count = await countf(resultData);
// console.log(count);
if(count.userOwnCardCount < userMembershipCardLimit
&& count.cardOwnUserCount < userOwnerCardLimit){
return true
} else {
return false;
}
};