diff --git a/pom.xml b/pom.xml index afe601a..08a2359 100644 --- a/pom.xml +++ b/pom.xml @@ -164,6 +164,11 @@ libphonenumber 8.13.36 + + org.springframework.boot + spring-boot-starter-mail + 3.1.5 + diff --git a/src/main/java/zw/qantra/tm/configs/SecurityConfig.java b/src/main/java/zw/qantra/tm/configs/SecurityConfig.java index a403342..3afa522 100644 --- a/src/main/java/zw/qantra/tm/configs/SecurityConfig.java +++ b/src/main/java/zw/qantra/tm/configs/SecurityConfig.java @@ -29,7 +29,8 @@ public class SecurityConfig { http .csrf(AbstractHttpConfigurer::disable) .authorizeHttpRequests(auth -> auth - .requestMatchers("/explorer/**").permitAll() + .requestMatchers("/test/**").permitAll() + .requestMatchers("/explorer/**").permitAll() .requestMatchers("/nflow/**").permitAll() .requestMatchers("/auth/**").permitAll() .requestMatchers("/public/**").permitAll() diff --git a/src/main/java/zw/qantra/tm/domain/controllers/TestController.java b/src/main/java/zw/qantra/tm/domain/controllers/TestController.java index 140e6af..feb067f 100644 --- a/src/main/java/zw/qantra/tm/domain/controllers/TestController.java +++ b/src/main/java/zw/qantra/tm/domain/controllers/TestController.java @@ -1,17 +1,24 @@ package zw.qantra.tm.domain.controllers; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.ResponseEntity; import org.springframework.security.core.Authentication; import org.springframework.security.core.context.SecurityContextHolder; -import org.springframework.web.bind.annotation.CrossOrigin; -import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.bind.annotation.*; +import zw.qantra.tm.domain.services.EmailService; @RestController @RequestMapping("/test") @CrossOrigin(origins = "*") public class TestController { + @Autowired + private EmailService emailService; + + @GetMapping("/hello/{email}") + public ResponseEntity helloEndpoint(@PathVariable String email) { + emailService.sendSimpleMessage(email, "Hello", "Hello World!"); + return ResponseEntity.ok("Hello World!"); + } @GetMapping("/protected") public ResponseEntity protectedEndpoint() { diff --git a/src/main/java/zw/qantra/tm/domain/services/EmailService.java b/src/main/java/zw/qantra/tm/domain/services/EmailService.java new file mode 100644 index 0000000..aa47ba8 --- /dev/null +++ b/src/main/java/zw/qantra/tm/domain/services/EmailService.java @@ -0,0 +1,23 @@ +package zw.qantra.tm.domain.services; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.mail.SimpleMailMessage; +import org.springframework.mail.javamail.JavaMailSender; +import org.springframework.stereotype.Component; + +@Component +public class EmailService { + @Autowired + private JavaMailSender emailSender; + + public void sendSimpleMessage( + String to, String subject, String text) { + SimpleMailMessage message = new SimpleMailMessage(); + message.setFrom("noreply@qantra.co.zw"); + message.setTo(to); + message.setSubject(subject); + message.setText(text); + emailSender.send(message); + } + +} diff --git a/src/main/java/zw/qantra/tm/domain/workflows/RegistrationWorkflow.java b/src/main/java/zw/qantra/tm/domain/workflows/RegistrationWorkflow.java index ba36cba..efef450 100644 --- a/src/main/java/zw/qantra/tm/domain/workflows/RegistrationWorkflow.java +++ b/src/main/java/zw/qantra/tm/domain/workflows/RegistrationWorkflow.java @@ -18,6 +18,7 @@ import zw.qantra.tm.domain.dtos.*; import zw.qantra.tm.domain.dtos.erp.CustomerDto; import zw.qantra.tm.domain.models.Otp; import zw.qantra.tm.domain.models.User; +import zw.qantra.tm.domain.services.EmailService; import zw.qantra.tm.domain.services.OtpService; import zw.qantra.tm.domain.services.UserService; import io.nflow.engine.workflow.curated.State; @@ -46,6 +47,8 @@ public class RegistrationWorkflow extends WorkflowDefinition { private OtpService otpService; @Autowired private RestService restService; + @Autowired + private EmailService emailService; @Value("${infobip.url}") private String infobipUrl; @@ -136,11 +139,8 @@ public class RegistrationWorkflow extends WorkflowDefinition { // Generate OTP Otp otp = otpService.generateOtp(username, "REGISTRATION"); - String string = getSMSString(registerRequest.getPhone(), otp.getOtp()); - HttpHeaders headers = new HttpHeaders(); - headers.add("Authorization", "App " + infobipApiKey); - headers.add("Content-Type", "application/json"); - headers.add("Accept", "application/json"); + String string = String.format("Your Peak verification code is: %s", otp.getOtp()); + emailService.sendSimpleMessage(email, "Peak verification code", string); // LinkedHashMap response = restService.postAs(infobipUrl, string, headers, LinkedHashMap.class); log.info("otp generated successfully: {}", otp.getOtp()); @@ -164,7 +164,8 @@ public class RegistrationWorkflow extends WorkflowDefinition { public void resendOtp(StateExecution execution) { OtpRequest otpRequest = execution.getVariable("body", OtpRequest.class); Otp otp = otpService.generateOtp(otpRequest.getUsername(), "REGISTRATION"); - String string = getSMSString(otpRequest.getPhone(), otp.getOtp()); + String string = String.format("Your Peak verification code is: %s", otp.getOtp()); + emailService.sendSimpleMessage(otpRequest.getUsername(), "Peak verification code", string); log.info("otp generated successfully: {}", otp.getOtp()); } @@ -249,20 +250,4 @@ public class RegistrationWorkflow extends WorkflowDefinition { return NextAction.stopInState(DONE, "ERP updated successfully for: " + registerRequest.getUsername()); } - - String getSMSString(String to, String otp) { - return String.format("{\n" + - " \"messages\": [\n" + - " {\n" + - " \"destinations\": [\n" + - " {\n" + - " \"to\": \"%s\"\n" + - " }\n" + - " ],\n" + - " \"from\": \"447491163443\",\n" + - " \"text\": \"Your verification code is: %s\"\n" + - " }\n" + - " ]\n" + - "}", to, otp); - } } diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index 8e61d8e..e5c4b26 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -59,3 +59,9 @@ nflow.db.postgresql.password=zdDZMzq6F4B4L1IUl infobip.url=https://z3m696.api.infobip.com/sms/2/text/advanced infobip.apikey=0ede918b7c1110465a0518021521638c-ebc4fdb7-547d-413b-bbca-781949ac6597 +spring.mail.host=mail.zimscape.com +spring.mail.port=587 +spring.mail.username=notifications@qantra.co.zw +spring.mail.password=QdZucpJMJIZ +spring.mail.properties.mail.smtp.auth=true +spring.mail.properties.mail.smtp.starttls.enable=true