Commit cf54110357166f487fb9eca73d9eefbd7039bca9

Authored by Nattapon Wongpaet
1 parent 9a512329
Exists in master and in 1 other branch dev

update post customer membership

ais-structure/src/modules/customer/customer.ctrl.js
@@ -52,11 +52,11 @@ exports.customer = async function (req, res, next) { @@ -52,11 +52,11 @@ exports.customer = async function (req, res, next) {
52 }else 52 }else
53 { 53 {
54 stats.receiveRestResponse(d01,method,sendCmd,constant.RESPONSERESULT.DATA_NOT_FOUND.developerMessage); 54 stats.receiveRestResponse(d01,method,sendCmd,constant.RESPONSERESULT.DATA_NOT_FOUND.developerMessage);
55 - var response = responseMsg.error(req,getCmd,40300); 55 + var response = responseMsg.error(req,getCmd,40401);
56 log.addErrorSummary(d01,method+"_"+sendCmd,response.resultCode,response.developerMessage); 56 log.addErrorSummary(d01,method+"_"+sendCmd,response.resultCode,response.developerMessage);
57 } 57 }
58 } else if(resultObj.resultCode.startsWith("404")){ 58 } else if(resultObj.resultCode.startsWith("404")){
59 - stats.receiveRestResponse(d01,method,sendCmd,constant.ERROR); 59 + stats.receiveRestResponse(d01,method,sendCmd,constant.RESPONSERESULT.DATA_NOT_FOUND.developerMessage);
60 var response = responseMsg.direct(req,getCmd,resultObj); 60 var response = responseMsg.direct(req,getCmd,resultObj);
61 log.addErrorSummary(d01,method+"_"+sendCmd,resultObj.resultCode,resultObj.developerMessage); 61 log.addErrorSummary(d01,method+"_"+sendCmd,resultObj.resultCode,resultObj.developerMessage);
62 } else if(resultObj.resultCode.startsWith("5")){ 62 } else if(resultObj.resultCode.startsWith("5")){
ais-structure/src/modules/customer/postCustomer.ctrl.js
@@ -44,19 +44,18 @@ exports.postCustomer = async function (req, res, next){ @@ -44,19 +44,18 @@ exports.postCustomer = async function (req, res, next){
44 if(typeof result.err === 'undefined'){ 44 if(typeof result.err === 'undefined'){
45 // console.log(result.response.body); 45 // console.log(result.response.body);
46 var resultObj = result.response; 46 var resultObj = result.response;
47 - if(resultObj.resultCode.startsWith("2"))  
48 - { 47 + if(resultObj.resultCode.startsWith("2")){
49 stats.receiveRestResponse(d01,method,sendCmd,constant.SUCCESS); 48 stats.receiveRestResponse(d01,method,sendCmd,constant.SUCCESS);
50 var response = responseMsg.direct(req,getCmd,resultObj,{customerId:objectData.customerId}); 49 var response = responseMsg.direct(req,getCmd,resultObj,{customerId:objectData.customerId});
51 log.addSuccessSummary(d01,method+"_"+sendCmd,response.resultCode,response.developerMessage); 50 log.addSuccessSummary(d01,method+"_"+sendCmd,response.resultCode,response.developerMessage);
52 } else if(resultObj.resultCode.startsWith("404")){ 51 } else if(resultObj.resultCode.startsWith("404")){
53 - stats.receiveRestResponse(d01,method,sendCmd,constant.ERROR); 52 + stats.receiveRestResponse(d01,method,sendCmd,constant.RESPONSERESULT.DATA_NOT_FOUND.developerMessage);
54 var response = responseMsg.direct(req,getCmd,resultObj); 53 var response = responseMsg.direct(req,getCmd,resultObj);
55 - log.addErrorSummary(d01,method+"_"+sendCmd,response.resultCode,response.developerMessage); 54 + log.addErrorSummary(d01,method+"_"+sendCmd,resultObj.resultCode,resultObj.developerMessage);
56 } else if(resultObj.resultCode.startsWith("5")){ 55 } else if(resultObj.resultCode.startsWith("5")){
57 stats.receiveRestResponse(d01,method,sendCmd,constant.ERROR); 56 stats.receiveRestResponse(d01,method,sendCmd,constant.ERROR);
58 var response = responseMsg.direct(req,getCmd,resultObj); 57 var response = responseMsg.direct(req,getCmd,resultObj);
59 - log.addErrorSummary(d01,method+"_"+sendCmd,response.resultCode,response.developerMessage); 58 + log.addErrorSummary(d01,method+"_"+sendCmd,resultObj.resultCode,resultObj.developerMessage);
60 } else { 59 } else {
61 stats.receiveRestResponse(d01,method,sendCmd,constant.ERROR); 60 stats.receiveRestResponse(d01,method,sendCmd,constant.ERROR);
62 var response = responseMsg.error(req,getCmd,50000); 61 var response = responseMsg.error(req,getCmd,50000);
ais-structure/src/modules/helper/connection.js
@@ -162,8 +162,18 @@ connection.requestJsonToD01 = async function (obj,cmd,myMethod) { @@ -162,8 +162,18 @@ connection.requestJsonToD01 = async function (obj,cmd,myMethod) {
162 } 162 }
163 163
164 164
165 - }else if(params.method == constants.METHOD.POST)  
166 - { 165 + } else if(params.method == constants.METHOD.POST){
  166 + params.body = JSON.stringify(params.body);
  167 +
  168 + var resultRes = await asyncRequest(params,objectData,funStats);
  169 +
  170 + if(resultRes.response){
  171 + result.response = JSON.parse(resultRes.body);
  172 + } else {
  173 + result = resultRes;
  174 + }
  175 + } else if(params.method == constants.METHOD.PUT){
  176 +
167 params.body = JSON.stringify(params.body); 177 params.body = JSON.stringify(params.body);
168 178
169 var resultRes = await asyncRequest(params,objectData,funStats); 179 var resultRes = await asyncRequest(params,objectData,funStats);
@@ -330,8 +340,6 @@ function getUrl(params,limit) @@ -330,8 +340,6 @@ function getUrl(params,limit)
330 careData = filter.multi[i]; 340 careData = filter.multi[i];
331 341
332 } 342 }
333 -  
334 -  
335 } 343 }
336 344
337 345
ais-structure/src/modules/membercard/getMembership.ctrl.js
@@ -75,11 +75,11 @@ exports.getMembership = async function (req, res, next) { @@ -75,11 +75,11 @@ exports.getMembership = async function (req, res, next) {
75 var response = responseMsg.success(req,getCmd,resultObj); 75 var response = responseMsg.success(req,getCmd,resultObj);
76 } else { 76 } else {
77 stats.receiveRestResponse(d01,method,sendCusCmd,constant.RESPONSERESULT.DATA_NOT_FOUND.developerMessage); 77 stats.receiveRestResponse(d01,method,sendCusCmd,constant.RESPONSERESULT.DATA_NOT_FOUND.developerMessage);
78 - var response = responseMsg.error(req,getCmd,40300); 78 + var response = responseMsg.error(req,getCmd,40401);
79 log.addErrorSummary(d01,method+"_"+sendCusCmd,response.resultCode,response.developerMessage); 79 log.addErrorSummary(d01,method+"_"+sendCusCmd,response.resultCode,response.developerMessage);
80 } 80 }
81 } else if(resultObj.resultCode.startsWith("404")){ 81 } else if(resultObj.resultCode.startsWith("404")){
82 - stats.receiveRestResponse(d01,method,sendCmd,constant.ERROR); 82 + stats.receiveRestResponse(d01,method,sendCmd,constant.RESPONSERESULT.DATA_NOT_FOUND.developerMessage);
83 var response = responseMsg.direct(req,getCmd,resultObj); 83 var response = responseMsg.direct(req,getCmd,resultObj);
84 log.addErrorSummary(d01,method+"_"+sendCmd,resultObj.resultCode,resultObj.developerMessage); 84 log.addErrorSummary(d01,method+"_"+sendCmd,resultObj.resultCode,resultObj.developerMessage);
85 } else if(resultObj.resultCode.startsWith("5")){ 85 } else if(resultObj.resultCode.startsWith("5")){
ais-structure/src/modules/membercard/postMembership.ctrl.js
@@ -6,57 +6,129 @@ var log = require('../helper/log.js'); @@ -6,57 +6,129 @@ var log = require('../helper/log.js');
6 var constant = require('../helper/constants.js') 6 var constant = require('../helper/constants.js')
7 7
8 exports.postMembership = async function (req, res, next){ 8 exports.postMembership = async function (req, res, next){
9 - var getCmd = "Customer";  
10 - var sendCmd = "Customer";  
11 - var sacf = "SACF"; 9 + var getCmd = "CustomerMembership";
  10 + var sendCustomerCmd = "Customer";
  11 + var sendMemberCardCmd = "MemberCard";
12 var d01 = "D01"; 12 var d01 = "D01";
13 - var method = constant.METHOD.POST; 13 + var postMethod = constant.METHOD.POST;
  14 + var putMethod = constant.METHOD.PUT;
  15 + var getMethod = constant.METHOD.GET;
14 16
15 - if(req.params.userData && req.params.userType){  
16 - var customerId = req.body.user.userData+"@"+req.body.user.userType; 17 + if(req.params.customerId){
  18 + var customerId = req.params.customerId
  19 + } else if(req.params.userData && req.params.userType){
  20 + var customerId = req.params.userData+"@"+req.params.userType
  21 + } else if(req.body.clientName && req.body.commandId){
  22 + var customerId = req.body.clientName+"@"+req.body.commandId;
17 } 23 }
18 24
19 - log.startlog(method+"_"+getCmd,req.body.commandId,customerId);  
20 - log.logDetail.addInput(req.body.clientName,method+"_"+getCmd,constant.REQUEST,req,req.body); 25 + log.startlog(postMethod+"_"+getCmd,req.body.commandId,customerId);
  26 + log.logDetail.addInput(req.body.clientName,postMethod+"_"+getCmd,constant.REQUEST,req,req.body);
21 27
22 var err = validator(req,getCmd); 28 var err = validator(req,getCmd);
23 29
24 if(err.length > 0) 30 if(err.length > 0)
25 { 31 {
26 - log.addErrorSummary(req.query.clientName,method+"_"+getCmd,"null","Fail"); 32 + log.addErrorSummary(req.body.clientName,postMethod+"_"+getCmd,"null","Fail");
27 console.log(err); 33 console.log(err);
28 var response = responseMsg.error(req,getCmd,40300); 34 var response = responseMsg.error(req,getCmd,40300);
29 }else 35 }else
30 { 36 {
31 - log.addSuccessSummary(req.query.clientName,method+"_"+getCmd,"null","Success"); 37 + log.addSuccessSummary(req.body.clientName,postMethod+"_"+getCmd,"null","Success");
  38 +
32 var objectData = { 39 var objectData = {
33 - customerId : customerId,  
34 - userIdData : req.body.user.userData,  
35 - userIdType : req.body.user.userType,  
36 - firstName : req.body.firstName,  
37 - lastName : req.body.lastName,  
38 - mobile : req.body.mobile,  
39 - emailAddress : req.body.emailAddress,  
40 - dateCreated : req.body.dateCreated  
41 - };  
42 -  
43 - const result = await connection.requestJsonToD01(objectData,sendCmd,"POST");  
44 - log.logDetail.addInput(d01,method+"_"+sendCmd,constant.RESPONSE,result,result.response); 40 + cardId : req.body.cardId,
  41 + cardOwnerIdList : customerId
  42 + // _flag : "or"
  43 + }
  44 +
  45 + //GET Customer with cardId and cardOwnerIdList
  46 + const result = await connection.requestJsonToD01(objectData,sendMemberCardCmd,getMethod);
  47 + log.logDetail.addInput(d01,getMethod+"_"+sendMemberCardCmd,constant.RESPONSE,result,result.response);
  48 +
  49 + var objDataGetCustomer = {
  50 + req : req,
  51 + reqCustomer : {
  52 + customerId : customerId
  53 + },
  54 + cmd : sendCustomerCmd,
  55 + method : getMethod,
  56 + resultData : result.response.resultData
  57 + }
  58 +
  59 + var objDataPostCustomer = {
  60 + req : req,
  61 + reqCustomer : {
  62 + cardId : req.body.cardId,
  63 + cardOwnerIdList:[customerId]
  64 + },
  65 + cmd : sendCustomerCmd,
  66 + method : postMethod,
  67 + resultData : result.response.resultData
  68 + }
  69 +
45 if(typeof result.err === 'undefined'){ 70 if(typeof result.err === 'undefined'){
46 - // console.log(result.response.body);  
47 var resultObj = result.response; 71 var resultObj = result.response;
48 - if(resultObj.resultCode.startsWith("2"))  
49 - {  
50 - stats.receiveRestResponse(d01,constant.METHOD.POST,sendCmd,"Success");  
51 - var response = responseMsg.direct(req,getCmd,resultObj,{customerId:objectData.customerId});  
52 - log.addSuccessSummary(d01,method+"_"+sendCmd,response);  
53 - } else {  
54 - if(resultObj.resultCode != "40401"){  
55 - resultObj.resultCode = "50000"; 72 + if(resultObj.resultCode.startsWith("2")){
  73 + if(resultObj.resultData && resultObj.resultData.length>0){
  74 + stats.receiveRestResponse(d01,getMethod,sendMemberCardCmd,constant.SUCCESS);
  75 + var response = responseMsg.success(req,getCmd,resultObj);
  76 + log.addSuccessSummary(d01,getMethod+"_"+sendMemberCardCmd,response.resultCode,response.developerMessage);
  77 + } else { //data not found
  78 + //get customer
  79 + var getCustomer = await getCustomerD01(objDataGetCustomer);
  80 + //if get data exits post membercard
  81 + if(!getCustomer.isErr){
  82 + if(!getCustomer.isDataNotFound){
  83 + //post membercard
  84 + stats.receiveRestResponse(d01,getMethod,sendCustomerCmd,constant.SUCCESS);
  85 + log.addSuccessSummary(d01,getMethod+"_"+sendCustomerCmd,getCustomer.response.resultCode,getCustomer.response.developerMessage);
  86 +
  87 + var postCustomer = await postCustomerD01(objDataPostCustomer);
  88 +
  89 + if(!postCustomer.isErr){
  90 + stats.receiveRestResponse(d01,postMethod,sendCustomerCmd,constant.SUCCESS);
  91 + var response = responseMsg.direct(req,getCmd,postCustomer.response);
  92 + log.addSuccessSummary(d01,postMethod+"_"+sendCustomerCmd,response.resultCode,response.developerMessage);
  93 + } else if(getCustomer.isDirect){
  94 + stats.receiveRestResponse(d01,method,sendCustomerCmd,constant.ERROR);
  95 + var response = responseMsg.direct(req,getCmd,getCustomer.response);
  96 + log.addErrorSummary(d01,method+"_"+sendCmd,getCustomer.response.resultCode,getCustomer.response.developerMessage);
  97 + } else {
  98 + stats.receiveRestResponse(d01,method,sendCmd,constant.ERROR);
  99 + var response = responseMsg.error(req,getCmd,50000);
  100 + }
  101 +
  102 + } else {
  103 + stats.receiveRestResponse(d01,method,sendCmd,constant.RESPONSERESULT.DATA_NOT_FOUND.developerMessage);
  104 + var response = responseMsg.error(req,getCmd,40401);
  105 + log.addErrorSummary(d01,method+"_"+sendCmd,response.resultCode,response.developerMessage);
  106 + }
  107 + } else if(getCustomer.isDirect){
  108 + stats.receiveRestResponse(d01,method,sendCmd,constant.ERROR);
  109 + var response = responseMsg.direct(req,getCmd,getCustomer.response);
  110 + log.addErrorSummary(d01,method+"_"+sendCmd,getCustomer.response.resultCode,getCustomer.response.developerMessage);
  111 + } else {
  112 + stats.receiveRestResponse(d01,method,sendCmd,constant.ERROR);
  113 + var response = responseMsg.error(req,getCmd,50000);
  114 + }
  115 + //else end flow
  116 +
56 } 117 }
57 - stats.receiveRestResponse(d01,constant.METHOD.POST,sendCmd,"Error"); 118 + } else if(resultObj.resultCode.startsWith("404")){
  119 + //get customer
  120 + //if get data exits post member card
  121 + //else end flow
  122 + stats.receiveRestResponse(d01,method,sendCmd,constant.ERROR);
58 var response = responseMsg.direct(req,getCmd,resultObj); 123 var response = responseMsg.direct(req,getCmd,resultObj);
59 - log.addErrorSummary(d01,method+"_"+sendCmd,response); 124 + log.addErrorSummary(d01,method+"_"+sendCmd,resultObj.resultCode,resultObj.developerMessage);
  125 + } else if(resultObj.resultCode.startsWith("5")){
  126 + stats.receiveRestResponse(d01,method,sendCmd,constant.ERROR);
  127 + var response = responseMsg.direct(req,getCmd,resultObj);
  128 + log.addErrorSummary(d01,method+"_"+sendCmd,resultObj.resultCode,resultObj.developerMessage);
  129 + } else {
  130 + stats.receiveRestResponse(d01,method,sendCmd,constant.ERROR);
  131 + var response = responseMsg.error(req,getCmd,50000);
60 } 132 }
61 } else { 133 } else {
62 stats.receiveRestResponse(d01,constant.METHOD.POST,sendCmd,"Error"); 134 stats.receiveRestResponse(d01,constant.METHOD.POST,sendCmd,"Error");
@@ -65,7 +137,7 @@ exports.postMembership = async function (req, res, next){ @@ -65,7 +137,7 @@ exports.postMembership = async function (req, res, next){
65 } 137 }
66 } 138 }
67 139
68 - log.logDetail.addOutput(req.body.clientName,method+"_"+getCmd,constant.RESPONSE,response,response); 140 + log.logDetail.addOutput(req.body.clientName,postMethod+"_"+getCmd,constant.RESPONSE,response,response);
69 res.status(200).json(response); 141 res.status(200).json(response);
70 next(); 142 next();
71 }; 143 };
@@ -74,14 +146,14 @@ function validator(req,api) @@ -74,14 +146,14 @@ function validator(req,api)
74 { 146 {
75 // console.log(req.body); 147 // console.log(req.body);
76 var list = []; 148 var list = [];
77 - list.push([true,"body","user.userType","string"]);  
78 - list.push([true,"body","user.userData","string"]);  
79 - list.push([true,"body","commandId","string"]); 149 + list.push([true,"body","commandId","int"]);
80 list.push([true,"body","clientName","string"]); 150 list.push([true,"body","clientName","string"]);
81 - list.push([true,"body","firstName","string"]);  
82 - list.push([true,"body","lastName","string"]);  
83 - list.push([true,"body","mobile","string"]);  
84 - list.push([true,"body","emailAddress","string"]); 151 + list.push([true,"body","cardId","string"]);
  152 + // list.push([true,"body","clientName","string"]);
  153 + // list.push([true,"body","firstName","string"]);
  154 + // list.push([true,"body","lastName","string"]);
  155 + // list.push([true,"body","mobile","string"]);
  156 + // list.push([true,"body","emailAddress","string"]);
85 var err = validatorHelper(req,list,api) 157 var err = validatorHelper(req,list,api)
86 158
87 if(err.length > 0) 159 if(err.length > 0)
@@ -90,4 +162,67 @@ function validator(req,api) @@ -90,4 +162,67 @@ function validator(req,api)
90 stats.receiveRequest(req.method,api); 162 stats.receiveRequest(req.method,api);
91 163
92 return err; 164 return err;
  165 +}
  166 +
  167 +var getCustomerD01 = async function(data){
  168 +
  169 + var responseData = {
  170 + isErr : false,
  171 + isDataNotFound : false,
  172 + isDirect : false
  173 + };
  174 +
  175 + const resultCustomer = await connection.requestJsonToD01(data.reqCustomer,data.cmd,data.method);
  176 + var resultObj = resultCustomer.response
  177 +
  178 + if(typeof resultCustomer.err === 'undefined'){
  179 + if(resultObj.resultCode.startsWith("2")){
  180 + if(resultObj.resultData && resultObj.resultData.length>0){
  181 + responseData.response = resultObj;
  182 + } else {
  183 + responseData.response = resultObj;
  184 + responseData.isDataNotFound = true;
  185 + }
  186 + } else if(resultObj.resultCode.startsWith("404") || resultObj.resultCode.startsWith("5")){
  187 + responseData.response = resultObj;
  188 + responseData.isDirect = true;
  189 + } else {
  190 + responseData.response = resultObj;
  191 + responseData.isErr = true;
  192 + }
  193 + } else {
  194 + responseData.response = resultObj;
  195 + responseData.isErr = true;
  196 + }
  197 +
  198 + return responseData;
  199 +}
  200 +
  201 +var postCustomerD01 = async function(data){
  202 +
  203 + var responseData = {
  204 + isErr : false,
  205 + isDataNotFound : false,
  206 + isDirect : false
  207 + };
  208 +
  209 + const resultCustomer = await connection.requestJsonToD01(data.reqCustomer,data.cmd,data.method);
  210 + var resultObj = resultCustomer.response
  211 +
  212 + if(typeof resultCustomer.err === 'undefined'){
  213 + if(resultObj.resultCode.startsWith("2")){
  214 + responseData.response = resultObj;
  215 + } else if(resultObj.resultCode.startsWith("404") || resultObj.resultCode.startsWith("5")){
  216 + responseData.response = resultObj;
  217 + responseData.isDirect = true;
  218 + } else {
  219 + responseData.response = resultObj;
  220 + responseData.isErr = true;
  221 + }
  222 + } else {
  223 + responseData.response = resultObj;
  224 + responseData.isErr = true;
  225 + }
  226 +
  227 + return responseData;
93 } 228 }
94 \ No newline at end of file 229 \ No newline at end of file