From e08e0d877a40db15ff667337ee34c2c1ab219d1c Mon Sep 17 00:00:00 2001
From: Mila <Mila@pollex.com.tw>
Date: 星期一, 10 一月 2022 12:06:58 +0800
Subject: [PATCH] update: TODO#133824 企業識別Logo調整

---
 pamapi/src/main/java/com/pollex/pam/service/SendMsgService.java |   59 ++++++++++++++++++++++-------------------------------------
 1 files changed, 22 insertions(+), 37 deletions(-)

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 bbe3e96..2fe2642 100644
--- a/pamapi/src/main/java/com/pollex/pam/service/SendMsgService.java
+++ b/pamapi/src/main/java/com/pollex/pam/service/SendMsgService.java
@@ -3,30 +3,23 @@
 import com.fasterxml.jackson.databind.ObjectMapper;
 import com.pollex.pam.config.ApplicationProperties;
 import com.pollex.pam.config.ApplicationProperties.SMS;
-import com.pollex.pam.domain.Appointment;
 import com.pollex.pam.repository.ConsultantRepository;
 import com.pollex.pam.service.dto.*;
 import com.pollex.pam.service.util.HttpRequestUtil;
 import com.pollex.pam.web.rest.errors.SendEmailFailException;
 import com.pollex.pam.web.rest.errors.SendSMSFailException;
-import io.jsonwebtoken.lang.Assert;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.http.ResponseEntity;
 import org.springframework.stereotype.Service;
-import org.springframework.util.StringUtils;
-import org.thymeleaf.context.Context;
 import org.thymeleaf.spring5.SpringTemplateEngine;
 
 import java.nio.charset.StandardCharsets;
 import java.time.LocalDateTime;
 import java.time.format.DateTimeFormatter;
-import java.util.Base64;
+import java.util.*;
 import java.util.Base64.Encoder;
-import java.util.Collections;
-import java.util.List;
-import java.util.UUID;
 
 @Service
 public class SendMsgService {
@@ -34,7 +27,7 @@
     private static final Logger log = LoggerFactory.getLogger(SendMsgService.class);
     private final Encoder encoder = Base64.getEncoder();
 
-    private static final String EMAIL_SUBJECT = "靽���像�蝟餌絞�嚗���";
+    private static final String SEND_SMS_SUCCESS_CODE = "1";
 
     @Autowired
     ApplicationProperties applicationProperties;
@@ -54,7 +47,7 @@
         sendSMSRequest.setSender(smsProperties.getSender());
         sendSMSRequest.setMsgTypeSet(smsProperties.getSmsType());
         sendSMSRequest.setSendTime(LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:00")));
-        sendSMSRequest.setSubject(smsProperties.getSubject());
+        sendSMSRequest.setSubject(encoder.encodeToString(smsProperties.getSubject().getBytes(StandardCharsets.UTF_8)));
         sendSMSRequest.setActivityId("");
 
         SMSDetail smsDetail = new SMSDetail();
@@ -65,26 +58,22 @@
 
         try {
             ResponseEntity<SendSMSResponse> responseEntity = HttpRequestUtil.postWithJson(smsProperties.getUrl(), sendSMSRequest, SendSMSResponse.class);
+            SendSMSResponse response = responseEntity.getBody();
 
             log.debug("response status code = {}", responseEntity.getStatusCode());
             log.debug("smsResponse = {}", responseEntity.getBody());
 
+            if(!SEND_SMS_SUCCESS_CODE.equals(response.getReturnCode())) {
+                throw new SendSMSFailException("sms service return code = " + response.getReturnCode() + ", error_msg = " + response.getErrorMsg());
+            }
+
             return responseEntity.getBody();
-            // todo ����閬�����隤方������葫��
+        } catch (SendSMSFailException e) {
+            throw e;
+        } catch (Exception e) {
+            log.warn("send sms fail by other reason!", e);
+            throw new SendSMSFailException("send sms fail by other reason!");
         }
-        catch (Exception e) {
-            log.debug("send sms failed!", e);
-            throw new SendSMSFailException();
-        }
-    }
-
-    public String sendMsgByHtmlTestTemplateEmail(String from, String to) {
-        Context context = new Context();
-        context.setVariable("content", "閬芣��“��憟踝����蝑�靽���像������\n");
-        context.setVariable("urlHint", getAppointmentDetailUrl(0L));
-
-        String content = springTemplateEngine.process("mail/appointmentNotifyEmail", context);
-        return sendMsgByEmail(from, to, EMAIL_SUBJECT, content, true);
     }
 
     public String sendMsgByEmail(String from, String to, String subject, String content, boolean htmlFormat) throws SendEmailFailException{
@@ -108,31 +97,27 @@
         return sendMsgByEmail(sendMailRequest);
     }
 
-    private String sendMsgByEmail(SendMailRequest sendMailRequest) throws SendEmailFailException{
+    public String sendMsgByEmail(SendMailRequest sendMailRequest) throws SendEmailFailException{
         try {
             ResponseEntity<String> responseEntity =
                 HttpRequestUtil.postWithJson( applicationProperties.getEmail().getUrl(), sendMailRequest, String.class);
             log.debug("responseEntity = {}", responseEntity);
 
-            SendMailResponse sendMailResponse = new ObjectMapper().readValue(responseEntity.getBody(), SendMailResponse.class);
+            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())) {
-//                throw new SendEmailFailException();
-//            }
+            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) {
             throw e;
+        } catch (Exception e) {
+            log.warn("send email fail by other reason", e);
+            throw new SendEmailFailException("send email failed!");
         }
-        catch (Exception e) {
-            log.debug("send email failed!", e);
-            throw new SendEmailFailException();
-        }
-    }
-
-    private String getAppointmentDetailUrl(Long appointmentId) {
-        return applicationProperties.getFrontEndDomain() + "/myAppointmentList/contactedList?appointmentId=" + appointmentId;
     }
 }

--
Gitblit v1.8.0