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; }