From 1d537fa2542e4995d17d4c8736d216773dfe4342 Mon Sep 17 00:00:00 2001
From: wayne <wayne8692wayne8692@gmail.com>
Date: 星期一, 24 一月 2022 12:45:42 +0800
Subject: [PATCH] [update] 新增email可以在設定檔中可以用gmail來發送

---
 pamapi/src/main/resources/config/application-pollex.yml               |    4 +
 pamapi/src/main/resources/config/application-dev.yml                  |   18 ++++-
 pamapi/src/main/resources/config/application-sit.yml                  |    4 +
 pamapi/src/main/java/com/pollex/pam/service/AppointmentService.java   |    3 
 pamapi/src/main/java/com/pollex/pam/service/SendMsgService.java       |   52 +++++++++++------
 pamapi/src/main/resources/config/application-uat.yml                  |    4 +
 pamapi/src/main/java/com/pollex/pam/config/ApplicationProperties.java |   37 +++++++++---
 pamapi/src/main/java/com/pollex/pam/enums/SendEmailMsgMethod.java     |    6 ++
 pamapi/src/main/resources/config/application-prod.yml                 |    4 +
 9 files changed, 95 insertions(+), 37 deletions(-)

diff --git a/pamapi/src/main/java/com/pollex/pam/config/ApplicationProperties.java b/pamapi/src/main/java/com/pollex/pam/config/ApplicationProperties.java
index d4b2b36..60b33ca 100644
--- a/pamapi/src/main/java/com/pollex/pam/config/ApplicationProperties.java
+++ b/pamapi/src/main/java/com/pollex/pam/config/ApplicationProperties.java
@@ -1,5 +1,6 @@
 package com.pollex.pam.config;
 
+import com.pollex.pam.enums.SendEmailMsgMethod;
 import org.springframework.boot.context.properties.ConfigurationProperties;
 
 /**
@@ -19,7 +20,6 @@
     private String eServiceLoginFunc;
     private String eServiceLoginSys;
     private String frontEndDomain;
-    private boolean sendNotifyMsg;
     private SMS sms;
     private Email email;
     private String fileFolderPath;
@@ -88,14 +88,6 @@
         this.frontEndDomain = frontEndDomain;
     }
 
-    public boolean isSendNotifyMsg() {
-        return sendNotifyMsg;
-    }
-
-    public void setSendNotifyMsg(boolean sendNotifyMsg) {
-        this.sendNotifyMsg = sendNotifyMsg;
-    }
-
     public SMS getSms() {
         return sms;
     }
@@ -118,6 +110,7 @@
         private String sender;
         private String smsType;
         private String subject;
+        private boolean sendNotifyMsg;
 
         public String getUrl() {
             return url;
@@ -158,12 +151,22 @@
         public void setSubject(String subject) {
             this.subject = subject;
         }
+
+        public boolean isSendNotifyMsg() {
+            return sendNotifyMsg;
+        }
+
+        public void setSendNotifyMsg(boolean sendNotifyMsg) {
+            this.sendNotifyMsg = sendNotifyMsg;
+        }
     }
 
     public static class Email {
         private String url;
         private String functionId;
         private String senderEmail;
+        private boolean sendNotifyMsg;
+        private SendEmailMsgMethod method;
 
         public String getUrl() {
             return url;
@@ -188,6 +191,22 @@
         public void setSenderEmail(String senderEmail) {
             this.senderEmail = senderEmail;
         }
+
+        public boolean isSendNotifyMsg() {
+            return sendNotifyMsg;
+        }
+
+        public void setSendNotifyMsg(boolean sendNotifyMsg) {
+            this.sendNotifyMsg = sendNotifyMsg;
+        }
+
+        public SendEmailMsgMethod getMethod() {
+            return method;
+        }
+
+        public void setMethod(SendEmailMsgMethod method) {
+            this.method = method;
+        }
     }
 	public String getFileFolderPath() {
 		return fileFolderPath;
diff --git a/pamapi/src/main/java/com/pollex/pam/enums/SendEmailMsgMethod.java b/pamapi/src/main/java/com/pollex/pam/enums/SendEmailMsgMethod.java
new file mode 100644
index 0000000..42a10ce
--- /dev/null
+++ b/pamapi/src/main/java/com/pollex/pam/enums/SendEmailMsgMethod.java
@@ -0,0 +1,6 @@
+package com.pollex.pam.enums;
+
+public enum SendEmailMsgMethod {
+    PAM_EMAIL_SERVICE,
+    POLLEX_GMAIL
+}
diff --git a/pamapi/src/main/java/com/pollex/pam/service/AppointmentService.java b/pamapi/src/main/java/com/pollex/pam/service/AppointmentService.java
index 6d5ded6..3ae99c4 100644
--- a/pamapi/src/main/java/com/pollex/pam/service/AppointmentService.java
+++ b/pamapi/src/main/java/com/pollex/pam/service/AppointmentService.java
@@ -200,7 +200,8 @@
     public void sendAppointmentNotify(Appointment appointment) {
         Assert.notNull(appointment, "appointment entity cannot be null");
 
-        log.debug("is need send appointment notify msg? = {}", applicationProperties.isSendNotifyMsg());
+        log.debug("is need send appointment notify msg? sms = {}, email = {}",
+            applicationProperties.getSms().isSendNotifyMsg(), applicationProperties.getEmail().isSendNotifyMsg());
 
         log.debug("sending appointment notify, appointmentId = {}", appointment.getId());
         sendAppointmentNotifyBySMS(appointment);
diff --git a/pamapi/src/main/java/com/pollex/pam/service/SendMsgService.java b/pamapi/src/main/java/com/pollex/pam/service/SendMsgService.java
index 15a72fc..a5be71e 100644
--- a/pamapi/src/main/java/com/pollex/pam/service/SendMsgService.java
+++ b/pamapi/src/main/java/com/pollex/pam/service/SendMsgService.java
@@ -2,8 +2,10 @@
 
 import com.fasterxml.jackson.databind.ObjectMapper;
 import com.pollex.pam.config.ApplicationProperties;
+import com.pollex.pam.config.ApplicationProperties.Email;
 import com.pollex.pam.config.ApplicationProperties.SMS;
 import com.pollex.pam.config.Constants;
+import com.pollex.pam.enums.SendEmailMsgMethod;
 import com.pollex.pam.repository.ConsultantRepository;
 import com.pollex.pam.service.dto.*;
 import com.pollex.pam.service.util.HttpRequestUtil;
@@ -49,12 +51,12 @@
     MailService mailService;
 
     public SendSMSResponse sendMsgBySMS(String toMobile, String content) throws SendSMSFailException {
-    	if(!applicationProperties.isSendNotifyMsg()) {
-//    		return getMockSMSResponse();
-    		return null;
-    	}
 
-    	SMS smsProperties = applicationProperties.getSms();
+        SMS smsProperties = applicationProperties.getSms();
+        if(!smsProperties.isSendNotifyMsg()) {
+//    		return getMockSMSResponse();
+            return null;
+        }
 
         SendSMSRequest sendSMSRequest = new SendSMSRequest();
         sendSMSRequest.setpKey(UUID.randomUUID().toString());
@@ -119,34 +121,48 @@
     }
 
     public String sendMsgByEmail(SendMailRequest sendMailRequest) throws SendEmailFailException{
-        if(!applicationProperties.isSendNotifyMsg()) {
-            if(environment.acceptsProfiles(Profiles.of(Constants.SPRING_PROFILE_POLLEX_DEVELOPMENT))) {
-                String subject = sendMailRequest.getSubject();
-                String content = sendMailRequest.getContent();
-                boolean isHtml = sendMailRequest.isHtmlFormat();
+        final Email emailProperties = applicationProperties.getEmail();
 
-                sendMailRequest.getSendMailAddresses().forEach(receiver -> mailService.sendEmail(receiver, subject, content, false, isHtml));
-            }
-            
+        if(!emailProperties.isSendNotifyMsg()) {
             return null;
         }
 
-    	try {
+        if(emailProperties.getMethod() == SendEmailMsgMethod.POLLEX_GMAIL) {
+            return sendMsgByPollexGmail(sendMailRequest);
+        }
+        else if(emailProperties.getMethod() == SendEmailMsgMethod.PAM_EMAIL_SERVICE) {
+            return sendMsgByPamEmailService(sendMailRequest);
+        }
+
+        return null;
+    }
+
+    private String sendMsgByPollexGmail(SendMailRequest sendMailRequest) {
+        String subject = sendMailRequest.getSubject();
+        String content = sendMailRequest.getContent();
+        boolean isHtml = sendMailRequest.isHtmlFormat();
+        sendMailRequest.getSendMailAddresses().forEach(receiver -> mailService.sendEmail(receiver, subject, content, false, isHtml));
+
+        return null;
+    }
+
+    private String sendMsgByPamEmailService(SendMailRequest sendMailRequest) {
+        final Email emailProperties = applicationProperties.getEmail();
+        try {
             ResponseEntity<String> responseEntity =
-                HttpRequestUtil.postWithJson( applicationProperties.getEmail().getUrl(), sendMailRequest, String.class);
+                HttpRequestUtil.postWithJson(emailProperties.getUrl(), sendMailRequest, String.class);
             log.debug("responseEntity = {}", responseEntity);
 
             String rawResponseString = responseEntity.getBody();
             SendMailResponse sendMailResponse = new ObjectMapper().readValue(rawResponseString, SendMailResponse.class);
             log.debug("sendMailResponse = {}", sendMailResponse);
 
-            if(sendMailResponse == null || sendMailResponse.getData() == null || !"ADDED".equalsIgnoreCase(sendMailResponse.getData().getMessageStatus())) {
+            if (sendMailResponse == null || sendMailResponse.getData() == null || !"ADDED".equalsIgnoreCase(sendMailResponse.getData().getMessageStatus())) {
                 throw new SendEmailFailException("send email service return error msg! raw response string= " + rawResponseString);
             }
 
             return responseEntity.getBody();
-        }
-        catch (SendEmailFailException e) {
+        } catch (SendEmailFailException e) {
             throw e;
         } catch (Exception e) {
             log.warn("send email fail by other reason", e);
diff --git a/pamapi/src/main/resources/config/application-dev.yml b/pamapi/src/main/resources/config/application-dev.yml
index ced80df..e612913 100644
--- a/pamapi/src/main/resources/config/application-dev.yml
+++ b/pamapi/src/main/resources/config/application-dev.yml
@@ -45,10 +45,16 @@
     # Remove 'faker' if you do not want the sample data to be loaded automatically
     contexts: dev, faker
   mail:
-    host: localhost
-    port: 25
-    username:
-    password:
+    host: smtp.gmail.com
+    port: 587
+    username: pollex.testing@gmail.com
+    password: ilismmmhtscppxft
+    properties:
+      mail:
+        smtp:
+          auth: true
+          starttls:
+            enable: true
   messages:
     cache-duration: PT1S # 1 second, see the ISO 8601 standard
   thymeleaf:
@@ -120,15 +126,17 @@
   e-service-login-func: ValidateUsrLogin
   e-service-login-sys: epos
   front-end-domain: http://localhost:3000
-  send-notify-msg: false
   sms:
+    send-notify-msg: false
     url: https://localhost:8081/testSMS
     source-code: ePos
     sender: POS
     sms-type: '0017'
     subject: '慦�像��'
   email:
+    send-notify-msg: false
     url: https://localhost:8081/testEmail
     function-id: epos
     sender-email: noreply@pcalife.com.tw
+    method: 'POLLEX_GMAIL'
   file-folder-path: C://pam_file
diff --git a/pamapi/src/main/resources/config/application-pollex.yml b/pamapi/src/main/resources/config/application-pollex.yml
index d584a36..e6a2ae8 100644
--- a/pamapi/src/main/resources/config/application-pollex.yml
+++ b/pamapi/src/main/resources/config/application-pollex.yml
@@ -124,15 +124,17 @@
   e-service-login-func: ValidateUsrLogin
   e-service-login-sys: epos
   front-end-domain: http://dev.pollex.com.tw:5566/pam
-  send-notify-msg: false
   sms:
+    send-notify-msg: false
     url: https://localhost:8081/testSMS
     source-code: ePos
     sender: POS
     sms-type: '0017'
     subject: '慦�像��'
   email:
+    send-notify-msg: true
     url: https://localhost:8081/testEmail
     function-id: epos
     sender-email: noreply@pcalife.com.tw
+    method: 'POLLEX_GMAIL'
   file-folder-path: C://pam_file
diff --git a/pamapi/src/main/resources/config/application-prod.yml b/pamapi/src/main/resources/config/application-prod.yml
index 71773a9..90ff8b7 100644
--- a/pamapi/src/main/resources/config/application-prod.yml
+++ b/pamapi/src/main/resources/config/application-prod.yml
@@ -140,15 +140,17 @@
   e-service-login-func: ValidateUsrLogin
   e-service-login-sys: epos
   front-end-domain: https://vtwlifeopensysuat.pru.intranet.asia/pam
-  send-notify-msg: true
   sms:
+    send-notify-msg: true
     url: https://vtwlifeopensysuat.pru.intranet.asia/MesgQueueMgmnt/rest/smsSendMsgResource
     source-code: ePos
     sender: POS
     sms-type: '0017'
     subject: '慦�像��'
   email:
+    send-notify-msg: true
     url: https://vtwlifeopensysuat.pru.intranet.asia/tsgw/mq/mqSendMail
     function-id: epos
     sender-email: noreply@pcalife.com.tw
+    method: 'PAM_EMAIL_SERVICE'
   file-folder-path: /sfs_omo/vtwlifewpsfs01/SensitiveData4AP$/OMO
diff --git a/pamapi/src/main/resources/config/application-sit.yml b/pamapi/src/main/resources/config/application-sit.yml
index 4f4c25c..402dcef 100644
--- a/pamapi/src/main/resources/config/application-sit.yml
+++ b/pamapi/src/main/resources/config/application-sit.yml
@@ -118,15 +118,17 @@
   e-service-login-func: ValidateUsrLogin
   e-service-login-sys: epos
   front-end-domain: https://vtwlifeopensyssit.pru.intranet.asia/pam
-  send-notify-msg: true
   sms:
+    send-notify-msg: true
     url: https://vtwlifeopensysuat.pru.intranet.asia/MesgQueueMgmnt/rest/smsSendMsgResource
     source-code: ePos
     sender: POS
     sms-type: '0017'
     subject: '慦�像��'
   email:
+    send-notify-msg: true
     url: https://vtwlifeopensysuat.pru.intranet.asia/tsgw/mq/mqSendMail
     function-id: epos
     sender-email: noreply@pcalife.com.tw
+    method: 'PAM_EMAIL_SERVICE'
   file-folder-path: /sfs_omo/vtwlifewuftp66/sensitivedata4ap$/OMOSIT
diff --git a/pamapi/src/main/resources/config/application-uat.yml b/pamapi/src/main/resources/config/application-uat.yml
index 15f5390..28d8c16 100644
--- a/pamapi/src/main/resources/config/application-uat.yml
+++ b/pamapi/src/main/resources/config/application-uat.yml
@@ -118,15 +118,17 @@
   e-service-login-func: ValidateUsrLogin
   e-service-login-sys: epos
   front-end-domain: https://vtwlifeopensysuat.pru.intranet.asia/pam
-  send-notify-msg: true
   sms:
+    send-notify-msg: true
     url: https://vtwlifeopensysuat.pru.intranet.asia/MesgQueueMgmnt/rest/smsSendMsgResource
     source-code: ePos
     sender: POS
     sms-type: '0017'
     subject: '慦�像��'
   email:
+    send-notify-msg: true
     url: https://vtwlifeopensysuat.pru.intranet.asia/tsgw/mq/mqSendMail
     function-id: epos
     sender-email: noreply@pcalife.com.tw
+    method: 'PAM_EMAIL_SERVICE'
   file-folder-path: /sfs_omo/vtwlifewuftp66/sensitivedata4ap$/OMO

--
Gitblit v1.8.0