From d5a812af10c4bcb69e13b5ec35f9216acf495992 Mon Sep 17 00:00:00 2001 From: sozyGithub <thinpotatogithub@gmail.com> Date: Thu, 16 Nov 2023 14:45:48 +0700 Subject: [PATCH] add: email functionality --- .env.example | 6 ++- pom.xml | 7 ++- .../AccountVerificationRequestService.java | 53 ++++++++++--------- ...AccountVerificationRequestServiceImpl.java | 30 ++++++++++- .../java/org/soapService/Utils/Email.java | 35 ++++++++++++ .../AccountVerificationRequestValidation.java | 5 +- 6 files changed, 105 insertions(+), 31 deletions(-) create mode 100644 src/main/java/org/soapService/Utils/Email.java diff --git a/.env.example b/.env.example index 60c2490..c483a2b 100644 --- a/.env.example +++ b/.env.example @@ -5,4 +5,8 @@ DB_PASSWORD= DB_USER=root PHP_API_KEY="" -REST_API_KEY="" \ No newline at end of file +REST_API_KEY="" + +# Email +EMAIL_ADDRESS="" +EMAIL_APP_PASSWORD="" # From application password or sandi aplikasi on Google Account \ No newline at end of file diff --git a/pom.xml b/pom.xml index e8cc475..643a0c9 100644 --- a/pom.xml +++ b/pom.xml @@ -49,7 +49,12 @@ <artifactId>gson</artifactId> <version>2.10.1</version> </dependency> - + <!-- https://mvnrepository.com/artifact/com.sun.mail/javax.mail --> + <dependency> + <groupId>com.sun.mail</groupId> + <artifactId>javax.mail</artifactId> + <version>1.6.2</version> + </dependency> </dependencies> <build> diff --git a/src/main/java/org/soapService/Services/AccountVerificationRequestService.java b/src/main/java/org/soapService/Services/AccountVerificationRequestService.java index ea3cbfd..6c98cb9 100644 --- a/src/main/java/org/soapService/Services/AccountVerificationRequestService.java +++ b/src/main/java/org/soapService/Services/AccountVerificationRequestService.java @@ -13,36 +13,37 @@ import javax.xml.ws.RequestWrapper; import javax.xml.ws.soap.SOAPFaultException; @WebService -@XmlSeeAlso({ ServiceResponse.class }) +@XmlSeeAlso({ServiceResponse.class}) @HandlerChain(file = "handler-chain.xml") public interface AccountVerificationRequestService { - @WebMethod(operationName = "GetRequests") - @RequestWrapper(className = "AccountVerificationRequestService.GetRequests") - public ServiceResponse<GetAllResponse<AccountVerificationRequest>> getAccountVerificationRequests( - @WebParam(name = "page") Integer page, @WebParam(name = "pageSize") Integer pageSize) - throws SOAPFaultException; + @WebMethod(operationName = "GetRequests") + @RequestWrapper(className = "AccountVerificationRequestService.GetRequests") + public ServiceResponse<GetAllResponse<AccountVerificationRequest>> getAccountVerificationRequests( + @WebParam(name = "page") Integer page, @WebParam(name = "pageSize") Integer pageSize) + throws SOAPFaultException; - @WebMethod(operationName = "CreateRequest") - @RequestWrapper(className = "AccountVerificationRequestService.AccountVerificationCreateRequest") - public ServiceResponse<AccountVerificationRequest> createAccountVerificationRequest( - @WebParam(name = "userId") String userId) - throws SOAPFaultException; + @WebMethod(operationName = "CreateRequest") + @RequestWrapper(className = "AccountVerificationRequestService.AccountVerificationCreateRequest") + public ServiceResponse<AccountVerificationRequest> createAccountVerificationRequest( + @WebParam(name = "userId") String userId) + throws SOAPFaultException; - @WebMethod(operationName = "AcceptRequest") - @RequestWrapper(className = "AccountVerificationRequestService.AcceptRequest") - public ServiceResponse<AccountVerificationRequest> acceptAccountVerificationRequest( - @WebParam(name = "userId") String userId) - throws SOAPFaultException; + @WebMethod(operationName = "AcceptRequest") + @RequestWrapper(className = "AccountVerificationRequestService.AcceptRequest") + public ServiceResponse<AccountVerificationRequest> acceptAccountVerificationRequest( + @WebParam(name = "userId") String userId, + @WebParam(name = "email") String email) + throws SOAPFaultException; - @WebMethod(operationName = "RejectRequest") - @RequestWrapper(className = "AccountVerificationRequestService.RejectRequest") - public ServiceResponse<AccountVerificationRequest> rejectAccountVerificationRequest( - @WebParam(name = "userId") String userId) - throws SOAPFaultException; + @WebMethod(operationName = "RejectRequest") + @RequestWrapper(className = "AccountVerificationRequestService.RejectRequest") + public ServiceResponse<AccountVerificationRequest> rejectAccountVerificationRequest( + @WebParam(name = "userId") String userId) + throws SOAPFaultException; - @WebMethod(operationName = "DeleteRequest") - @RequestWrapper(className = "AccountVerificationRequestService.DeleteRequest") - public ServiceResponse<AccountVerificationRequest> deleteAccountVerificationRequest( - @WebParam(name = "requestId") int requestId) - throws SOAPFaultException; + @WebMethod(operationName = "DeleteRequest") + @RequestWrapper(className = "AccountVerificationRequestService.DeleteRequest") + public ServiceResponse<AccountVerificationRequest> deleteAccountVerificationRequest( + @WebParam(name = "requestId") int requestId) + throws SOAPFaultException; } diff --git a/src/main/java/org/soapService/Services/AccountVerificationRequestServiceImpl.java b/src/main/java/org/soapService/Services/AccountVerificationRequestServiceImpl.java index 1982faf..45094a7 100644 --- a/src/main/java/org/soapService/Services/AccountVerificationRequestServiceImpl.java +++ b/src/main/java/org/soapService/Services/AccountVerificationRequestServiceImpl.java @@ -1,5 +1,6 @@ package org.soapService.Services; +import io.github.cdimascio.dotenv.Dotenv; import org.soapService.Common.HTTPStatusCode; import org.soapService.Common.ServiceResponse; import org.soapService.Domain.AccountVerificationRequest; @@ -7,13 +8,18 @@ import org.soapService.Domain.GetAllResponse; import org.soapService.Exceptions.RequestException; import org.soapService.Exceptions.ValidationException; import org.soapService.Repository.AccountVerificationRequestRepository; +import org.soapService.Utils.Email; import org.soapService.Validations.AccountVerificationRequestValidation; import javax.jws.WebService; +import javax.mail.Authenticator; +import javax.mail.PasswordAuthentication; +import javax.mail.Session; import javax.xml.ws.soap.SOAPFaultException; import java.sql.SQLException; import java.util.ArrayList; import java.util.List; +import java.util.Properties; @WebService(endpointInterface = "org.soapService.Services.AccountVerificationRequestService") public class AccountVerificationRequestServiceImpl extends BaseService implements AccountVerificationRequestService { @@ -92,11 +98,11 @@ public class AccountVerificationRequestServiceImpl extends BaseService implement return response; } - public ServiceResponse<AccountVerificationRequest> acceptAccountVerificationRequest(String userId) + public ServiceResponse<AccountVerificationRequest> acceptAccountVerificationRequest(String userId, String email) throws SOAPFaultException { List<AccountVerificationRequest> lru = new ArrayList<>(); try { - accountVerificationServiceValidation.validateAcceptVerificationRequest(userId); + accountVerificationServiceValidation.validateAcceptVerificationRequest(userId, email); AccountVerificationRequest accountVerificationRequest = new AccountVerificationRequest(); accountVerificationRequest.setUserId(userId); accountVerificationRequest.setStatus("ACCEPTED"); @@ -106,6 +112,26 @@ public class AccountVerificationRequestServiceImpl extends BaseService implement new RequestException(HTTPStatusCode.BAD_REQUEST.getCodeStr(), "This user doesn't have a request or already verified"); } + + // Send success email + Dotenv dotenv = Dotenv.load(); + + Properties props = new Properties(); + props.put("mail.smtp.host", "smtp.gmail.com"); + props.put("mail.smtp.port", "587"); + props.put("mail.smtp.auth", "true"); + props.put("mail.smtp.starttls.enable", "true"); + + Authenticator auth = new Authenticator() { + @Override + protected PasswordAuthentication getPasswordAuthentication() { + return new PasswordAuthentication(dotenv.get("EMAIL_ADDRESS"), dotenv.get("EMAIL_APP_PASSWORD")); + } + }; + Session session = Session.getInstance(props, auth); + + Email.send(session, "DQ Admin", email, "Drawl Purple", "Congratulation! You're now titled as Drawl Purple User 🎉 <br/> Enjoy the benefits! Visit <b>DQ</b> now! <br/> Best regards, DQ Teams."); + } catch (ValidationException e) { System.out.println(e.getMessage()); new RequestException(HTTPStatusCode.BAD_REQUEST.getCodeStr(), e.getMessage()); diff --git a/src/main/java/org/soapService/Utils/Email.java b/src/main/java/org/soapService/Utils/Email.java new file mode 100644 index 0000000..f22928d --- /dev/null +++ b/src/main/java/org/soapService/Utils/Email.java @@ -0,0 +1,35 @@ +package org.soapService.Utils; + +import io.github.cdimascio.dotenv.Dotenv; + +import javax.mail.Message; +import javax.mail.Session; +import javax.mail.Transport; +import javax.mail.internet.InternetAddress; +import javax.mail.internet.MimeMessage; +import java.util.Date; + +public class Email { + public static void send(Session session, String senderName, String to, String subject, String body) throws Exception { + Dotenv dotenv = Dotenv.load(); + try { + MimeMessage message = new MimeMessage(session); + + message.addHeader("Content-Type", "text/HTML; charset=UTF-8"); + message.addHeader("format", "flowed"); + message.addHeader("Content-Transfer-Encoding", "8bit"); + + message.setFrom(new InternetAddress(dotenv.get("EMAIL_ADDRESS"), senderName)); + message.setReplyTo(InternetAddress.parse(dotenv.get("EMAIL_ADDRESS"), false)); + message.setSubject(subject, "UTF-8"); + message.setText(body, "UTF-8"); + message.setSentDate(new Date()); + message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to, false)); + + Transport.send(message); + } catch (Exception e) { + e.printStackTrace(); + throw e; + } + } +} diff --git a/src/main/java/org/soapService/Validations/AccountVerificationRequestValidation.java b/src/main/java/org/soapService/Validations/AccountVerificationRequestValidation.java index e7e8125..f9b076c 100644 --- a/src/main/java/org/soapService/Validations/AccountVerificationRequestValidation.java +++ b/src/main/java/org/soapService/Validations/AccountVerificationRequestValidation.java @@ -17,12 +17,15 @@ public class AccountVerificationRequestValidation { } } - public void validateAcceptVerificationRequest(String userId) throws ValidationException { + public void validateAcceptVerificationRequest(String userId, String email) throws ValidationException { System.out.println(userId); System.out.println(userId.trim()); if (userId == null || userId.trim().equals("")) { throw new ValidationException(HTTPStatusCode.BAD_REQUEST.getCodeStr(), "User ID is required"); } + if (email == null || email.trim().isEmpty()) { + throw new ValidationException(HTTPStatusCode.BAD_REQUEST.getCodeStr(), "Email is required"); + } } public void validateRejectVerificationRequest(String userId) throws ValidationException { -- GitLab