diff --git a/ais-structure/src/modules/customer/customer.ctrl.js b/ais-structure/src/modules/customer/customer.ctrl.js index 3bee00f..86411fc 100644 --- a/ais-structure/src/modules/customer/customer.ctrl.js +++ b/ais-structure/src/modules/customer/customer.ctrl.js @@ -52,11 +52,11 @@ exports.customer = async function (req, res, next) { }else { stats.receiveRestResponse(d01,method,sendCmd,constant.RESPONSERESULT.DATA_NOT_FOUND.developerMessage); - var response = responseMsg.error(req,getCmd,40300); + var response = responseMsg.error(req,getCmd,40401); log.addErrorSummary(d01,method+"_"+sendCmd,response.resultCode,response.developerMessage); } } else if(resultObj.resultCode.startsWith("404")){ - stats.receiveRestResponse(d01,method,sendCmd,constant.ERROR); + stats.receiveRestResponse(d01,method,sendCmd,constant.RESPONSERESULT.DATA_NOT_FOUND.developerMessage); var response = responseMsg.direct(req,getCmd,resultObj); log.addErrorSummary(d01,method+"_"+sendCmd,resultObj.resultCode,resultObj.developerMessage); } else if(resultObj.resultCode.startsWith("5")){ diff --git a/ais-structure/src/modules/customer/postCustomer.ctrl.js b/ais-structure/src/modules/customer/postCustomer.ctrl.js index aaa2052..cb17ae3 100644 --- a/ais-structure/src/modules/customer/postCustomer.ctrl.js +++ b/ais-structure/src/modules/customer/postCustomer.ctrl.js @@ -44,19 +44,18 @@ exports.postCustomer = async function (req, res, next){ if(typeof result.err === 'undefined'){ // console.log(result.response.body); var resultObj = result.response; - if(resultObj.resultCode.startsWith("2")) - { + if(resultObj.resultCode.startsWith("2")){ stats.receiveRestResponse(d01,method,sendCmd,constant.SUCCESS); var response = responseMsg.direct(req,getCmd,resultObj,{customerId:objectData.customerId}); log.addSuccessSummary(d01,method+"_"+sendCmd,response.resultCode,response.developerMessage); } else if(resultObj.resultCode.startsWith("404")){ - stats.receiveRestResponse(d01,method,sendCmd,constant.ERROR); + stats.receiveRestResponse(d01,method,sendCmd,constant.RESPONSERESULT.DATA_NOT_FOUND.developerMessage); var response = responseMsg.direct(req,getCmd,resultObj); - log.addErrorSummary(d01,method+"_"+sendCmd,response.resultCode,response.developerMessage); + 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,response.resultCode,response.developerMessage); + log.addErrorSummary(d01,method+"_"+sendCmd,resultObj.resultCode,resultObj.developerMessage); } else { stats.receiveRestResponse(d01,method,sendCmd,constant.ERROR); var response = responseMsg.error(req,getCmd,50000); diff --git a/ais-structure/src/modules/helper/connection.js b/ais-structure/src/modules/helper/connection.js index e755c5e..1f16314 100644 --- a/ais-structure/src/modules/helper/connection.js +++ b/ais-structure/src/modules/helper/connection.js @@ -162,8 +162,18 @@ connection.requestJsonToD01 = async function (obj,cmd,myMethod) { } - }else if(params.method == constants.METHOD.POST) - { + } else if(params.method == constants.METHOD.POST){ + params.body = JSON.stringify(params.body); + + var resultRes = await asyncRequest(params,objectData,funStats); + + if(resultRes.response){ + result.response = JSON.parse(resultRes.body); + } else { + result = resultRes; + } + } else if(params.method == constants.METHOD.PUT){ + params.body = JSON.stringify(params.body); var resultRes = await asyncRequest(params,objectData,funStats); @@ -330,8 +340,6 @@ function getUrl(params,limit) careData = filter.multi[i]; } - - } diff --git a/ais-structure/src/modules/membercard/getMembership.ctrl.js b/ais-structure/src/modules/membercard/getMembership.ctrl.js index badde2f..43fe844 100644 --- a/ais-structure/src/modules/membercard/getMembership.ctrl.js +++ b/ais-structure/src/modules/membercard/getMembership.ctrl.js @@ -75,11 +75,11 @@ exports.getMembership = async function (req, res, next) { 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); + var response = responseMsg.error(req,getCmd,40401); log.addErrorSummary(d01,method+"_"+sendCusCmd,response.resultCode,response.developerMessage); } } else if(resultObj.resultCode.startsWith("404")){ - stats.receiveRestResponse(d01,method,sendCmd,constant.ERROR); + stats.receiveRestResponse(d01,method,sendCmd,constant.RESPONSERESULT.DATA_NOT_FOUND.developerMessage); var response = responseMsg.direct(req,getCmd,resultObj); log.addErrorSummary(d01,method+"_"+sendCmd,resultObj.resultCode,resultObj.developerMessage); } else if(resultObj.resultCode.startsWith("5")){ diff --git a/ais-structure/src/modules/membercard/postMembership.ctrl.js b/ais-structure/src/modules/membercard/postMembership.ctrl.js index 0b36693..2e1925e 100644 --- a/ais-structure/src/modules/membercard/postMembership.ctrl.js +++ b/ais-structure/src/modules/membercard/postMembership.ctrl.js @@ -6,57 +6,129 @@ var log = require('../helper/log.js'); var constant = require('../helper/constants.js') exports.postMembership = async function (req, res, next){ - var getCmd = "Customer"; - var sendCmd = "Customer"; - var sacf = "SACF"; + var getCmd = "CustomerMembership"; + var sendCustomerCmd = "Customer"; + var sendMemberCardCmd = "MemberCard"; var d01 = "D01"; - var method = constant.METHOD.POST; + var postMethod = constant.METHOD.POST; + var putMethod = constant.METHOD.PUT; + var getMethod = constant.METHOD.GET; - if(req.params.userData && req.params.userType){ - var customerId = req.body.user.userData+"@"+req.body.user.userType; + 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(method+"_"+getCmd,req.body.commandId,customerId); - log.logDetail.addInput(req.body.clientName,method+"_"+getCmd,constant.REQUEST,req,req.body); + 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.query.clientName,method+"_"+getCmd,"null","Fail"); + log.addErrorSummary(req.body.clientName,postMethod+"_"+getCmd,"null","Fail"); console.log(err); var response = responseMsg.error(req,getCmd,40300); }else { - log.addSuccessSummary(req.query.clientName,method+"_"+getCmd,"null","Success"); + log.addSuccessSummary(req.body.clientName,postMethod+"_"+getCmd,"null","Success"); + var objectData = { - customerId : customerId, - userIdData : req.body.user.userData, - userIdType : req.body.user.userType, - firstName : req.body.firstName, - lastName : req.body.lastName, - mobile : req.body.mobile, - emailAddress : req.body.emailAddress, - dateCreated : req.body.dateCreated - }; - - const result = await connection.requestJsonToD01(objectData,sendCmd,"POST"); - log.logDetail.addInput(d01,method+"_"+sendCmd,constant.RESPONSE,result,result.response); + 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'){ - // console.log(result.response.body); var resultObj = result.response; - if(resultObj.resultCode.startsWith("2")) - { - stats.receiveRestResponse(d01,constant.METHOD.POST,sendCmd,"Success"); - var response = responseMsg.direct(req,getCmd,resultObj,{customerId:objectData.customerId}); - log.addSuccessSummary(d01,method+"_"+sendCmd,response); - } else { - if(resultObj.resultCode != "40401"){ - resultObj.resultCode = "50000"; + 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 + } - stats.receiveRestResponse(d01,constant.METHOD.POST,sendCmd,"Error"); + } 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,response); + 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"); @@ -65,7 +137,7 @@ exports.postMembership = async function (req, res, next){ } } - log.logDetail.addOutput(req.body.clientName,method+"_"+getCmd,constant.RESPONSE,response,response); + log.logDetail.addOutput(req.body.clientName,postMethod+"_"+getCmd,constant.RESPONSE,response,response); res.status(200).json(response); next(); }; @@ -74,14 +146,14 @@ function validator(req,api) { // console.log(req.body); var list = []; - list.push([true,"body","user.userType","string"]); - list.push([true,"body","user.userData","string"]); - list.push([true,"body","commandId","string"]); + list.push([true,"body","commandId","int"]); 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"]); + 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) @@ -90,4 +162,67 @@ function validator(req,api) 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; } \ No newline at end of file -- libgit2 0.21.2