From 3e9c86dc1404f758794c006a92dea248e4856a6c Mon Sep 17 00:00:00 2001 From: nancymuyeh Date: Wed, 12 Jun 2024 08:54:45 +0100 Subject: [PATCH 1/9] feat(backend): configure web security to disable cross sight request forgery --- .../configuration/WebSecurityConfig.java | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 power-pay-backend/src/main/java/com/adorsys/gis/powerpay/powerpaybackend/configuration/WebSecurityConfig.java diff --git a/power-pay-backend/src/main/java/com/adorsys/gis/powerpay/powerpaybackend/configuration/WebSecurityConfig.java b/power-pay-backend/src/main/java/com/adorsys/gis/powerpay/powerpaybackend/configuration/WebSecurityConfig.java new file mode 100644 index 0000000..fe65f5b --- /dev/null +++ b/power-pay-backend/src/main/java/com/adorsys/gis/powerpay/powerpaybackend/configuration/WebSecurityConfig.java @@ -0,0 +1,27 @@ +package com.adorsys.gis.powerpay.powerpaybackend.configuration; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.http.HttpMethod; +import org.springframework.security.config.annotation.method.configuration.EnableMethodSecurity; +import org.springframework.security.config.annotation.web.builders.HttpSecurity; +import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; +import org.springframework.security.web.SecurityFilterChain; + +@EnableWebSecurity +@EnableMethodSecurity(prePostEnabled = true) +@Configuration +public class WebSecurityConfig { + + + @Bean + public SecurityFilterChain configure(HttpSecurity http) throws Exception { + http + .csrf((csrf) -> csrf.disable()) + .authorizeHttpRequests((authorize) -> authorize.requestMatchers(HttpMethod.POST, "/**").permitAll() + .requestMatchers("/**").permitAll() + .anyRequest().authenticated()); + + return http.build(); +} +} From 604891d92b1e5432a9b5be89971022cab25dd0ba Mon Sep 17 00:00:00 2001 From: nancymuyeh Date: Wed, 12 Jun 2024 10:47:32 +0100 Subject: [PATCH 2/9] feat(backend): Implement RESTAPI for user registration --- .../services/userRegistrationController.java | 52 +++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 power-pay-backend/src/main/java/com/adorsys/gis/powerpay/powerpaybackend/services/userRegistrationController.java diff --git a/power-pay-backend/src/main/java/com/adorsys/gis/powerpay/powerpaybackend/services/userRegistrationController.java b/power-pay-backend/src/main/java/com/adorsys/gis/powerpay/powerpaybackend/services/userRegistrationController.java new file mode 100644 index 0000000..4f01eee --- /dev/null +++ b/power-pay-backend/src/main/java/com/adorsys/gis/powerpay/powerpaybackend/services/userRegistrationController.java @@ -0,0 +1,52 @@ +package com.adorsys.gis.powerpay.powerpaybackend.services; + +import com.adorsys.gis.powerpay.powerpaybackend.domain.User; +import com.adorsys.gis.powerpay.powerpaybackend.domain.UserRegistration; +import jakarta.validation.Valid; +import lombok.Getter; +import lombok.Setter; + +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.*; + +@RestController +@RequestMapping("/api/registration") +public class userRegistrationController { + private final UserRegistrationService userRegistrationService; + + public userRegistrationController(UserRegistrationService userRegistrationService) { + this.userRegistrationService = userRegistrationService; + } + + @PostMapping + public ResponseEntity registerUser(@RequestBody @Valid RegistrationRequest registrationRequest) { + UserRegistration userRegistration = userRegistrationService.createProcedure(registrationRequest.getPhoneNumber(), registrationRequest.getUserName()); + return new ResponseEntity<>(userRegistration, HttpStatus.CREATED); + } + + @PostMapping("/{registrationId}") + public ResponseEntity completeRegistration(@PathVariable("registrationId") Integer registrationId, @RequestBody @Valid CompletionRequest completionRequest) { + User user = userRegistrationService.registerUser(registrationId, completionRequest.getPin(), completionRequest.getOtp()); + return new ResponseEntity<>(user, HttpStatus.CREATED); + } + + // Define request bodies for endpoints + @Getter + @Setter + public + static class RegistrationRequest { + private String phoneNumber; + private String userName; + + + } + @Getter + @Setter + public + static class CompletionRequest { + private String pin; + private String otp; + + } +} From 51a0b382a5c779604671e1e995c1e3776a129d20 Mon Sep 17 00:00:00 2001 From: mbunwe-victor Date: Mon, 17 Jun 2024 14:26:17 +0100 Subject: [PATCH 3/9] feat(backend): Implemented integration test for user registration API --- .../UserRegistrationIntegrationTest.java | 70 +++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100644 power-pay-backend/src/test/java/com/adorsys/gis/powerpay/powerpaybackend/services_tests/UserRegistrationIntegrationTest.java diff --git a/power-pay-backend/src/test/java/com/adorsys/gis/powerpay/powerpaybackend/services_tests/UserRegistrationIntegrationTest.java b/power-pay-backend/src/test/java/com/adorsys/gis/powerpay/powerpaybackend/services_tests/UserRegistrationIntegrationTest.java new file mode 100644 index 0000000..c9ad518 --- /dev/null +++ b/power-pay-backend/src/test/java/com/adorsys/gis/powerpay/powerpaybackend/services_tests/UserRegistrationIntegrationTest.java @@ -0,0 +1,70 @@ +package com.adorsys.gis.powerpay.powerpaybackend.services_tests; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; + +import com.adorsys.gis.powerpay.powerpaybackend.services.userRegistrationController.CompletionRequest; +import com.adorsys.gis.powerpay.powerpaybackend.services.userRegistrationController.RegistrationRequest; +import com.fasterxml.jackson.databind.ObjectMapper; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.http.MediaType; +import org.springframework.test.web.servlet.MockMvc; + +@SpringBootTest +@AutoConfigureMockMvc +public class UserRegistrationIntegrationTest { + + @Autowired + private MockMvc mockMvc; + + @Autowired + private ObjectMapper objectMapper; + + @Test + public void testRegisterUser() throws Exception { + RegistrationRequest request = new RegistrationRequest(); + request.setPhoneNumber("1234567890"); + request.setUserName("testuser"); + + mockMvc.perform(post("/api/registration") + .contentType(MediaType.APPLICATION_JSON) + .content(objectMapper.writeValueAsString(request))) + .andExpect(status().isCreated()) + .andExpect(jsonPath("$.id").exists()) + .andExpect(jsonPath("$.phoneNumber").value("1234567890")) + .andExpect(jsonPath("$.userName").value("testuser")); + } + + @Test + public void testCompleteRegistration() throws Exception { + // Register a user + RegistrationRequest registrationRequest = new RegistrationRequest(); + registrationRequest.setPhoneNumber("1234567890"); + registrationRequest.setUserName("testuser"); + + mockMvc.perform(post("/api/registration") + .contentType(MediaType.APPLICATION_JSON) + .content(objectMapper.writeValueAsString(registrationRequest))) + .andExpect(status().isCreated()) + .andReturn() + .getResponse() + .getContentAsString(); + + // Complete the registration + CompletionRequest completionRequest = new CompletionRequest(); + completionRequest.setPin("1234"); + completionRequest.setOtp("5678"); + + mockMvc.perform(post("/api/registration/{registrationId}", 1) // Assuming registrationId is 1 + .contentType(MediaType.APPLICATION_JSON) + .content(objectMapper.writeValueAsString(completionRequest))) + .andExpect(status().isCreated()) + .andExpect(jsonPath("$.id").exists()) + .andExpect(jsonPath("$.phoneNumber").value("1234567890")) + .andExpect(jsonPath("$.userName").value("testuser")); + } +} + From a65f3b0c41de44710b752404fdd300e888d00a81 Mon Sep 17 00:00:00 2001 From: nancymuyeh Date: Fri, 21 Jun 2024 15:28:48 +0100 Subject: [PATCH 4/9] fix(backend): added annotation to make procedure table be created in the database --- .../adorsys/gis/powerpay/powerpaybackend/domain/Procedure.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/power-pay-backend/src/main/java/com/adorsys/gis/powerpay/powerpaybackend/domain/Procedure.java b/power-pay-backend/src/main/java/com/adorsys/gis/powerpay/powerpaybackend/domain/Procedure.java index 5f6553f..5ecc513 100644 --- a/power-pay-backend/src/main/java/com/adorsys/gis/powerpay/powerpaybackend/domain/Procedure.java +++ b/power-pay-backend/src/main/java/com/adorsys/gis/powerpay/powerpaybackend/domain/Procedure.java @@ -2,6 +2,8 @@ import jakarta.persistence.*; @Entity +@Table(name = "procedure") + @Inheritance(strategy = InheritanceType.JOINED) public abstract class Procedure { @Id From 62f49267c7c3818f11470abd614fcba00766e0dc Mon Sep 17 00:00:00 2001 From: nancymuyeh Date: Fri, 21 Jun 2024 15:31:57 +0100 Subject: [PATCH 5/9] refactor(backend): refactored user registration-related files --- .../configuration/WebSecurityConfig.java | 27 ---------- .../UserRegistrationController.java | 35 +++++++++++++ .../domain/FirstRegistrationRequest.java | 12 +++++ .../domain/SecondRegistrationRequest.java | 13 +++++ .../services/userRegistrationController.java | 52 ------------------- .../UserRegistrationIntegrationTest.java | 12 ++--- 6 files changed, 66 insertions(+), 85 deletions(-) delete mode 100644 power-pay-backend/src/main/java/com/adorsys/gis/powerpay/powerpaybackend/configuration/WebSecurityConfig.java create mode 100644 power-pay-backend/src/main/java/com/adorsys/gis/powerpay/powerpaybackend/controller/UserRegistrationController.java create mode 100644 power-pay-backend/src/main/java/com/adorsys/gis/powerpay/powerpaybackend/domain/FirstRegistrationRequest.java create mode 100644 power-pay-backend/src/main/java/com/adorsys/gis/powerpay/powerpaybackend/domain/SecondRegistrationRequest.java delete mode 100644 power-pay-backend/src/main/java/com/adorsys/gis/powerpay/powerpaybackend/services/userRegistrationController.java rename power-pay-backend/src/test/java/com/adorsys/gis/powerpay/powerpaybackend/{services_tests => controllers_tests}/UserRegistrationIntegrationTest.java (84%) diff --git a/power-pay-backend/src/main/java/com/adorsys/gis/powerpay/powerpaybackend/configuration/WebSecurityConfig.java b/power-pay-backend/src/main/java/com/adorsys/gis/powerpay/powerpaybackend/configuration/WebSecurityConfig.java deleted file mode 100644 index fe65f5b..0000000 --- a/power-pay-backend/src/main/java/com/adorsys/gis/powerpay/powerpaybackend/configuration/WebSecurityConfig.java +++ /dev/null @@ -1,27 +0,0 @@ -package com.adorsys.gis.powerpay.powerpaybackend.configuration; - -import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.Configuration; -import org.springframework.http.HttpMethod; -import org.springframework.security.config.annotation.method.configuration.EnableMethodSecurity; -import org.springframework.security.config.annotation.web.builders.HttpSecurity; -import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; -import org.springframework.security.web.SecurityFilterChain; - -@EnableWebSecurity -@EnableMethodSecurity(prePostEnabled = true) -@Configuration -public class WebSecurityConfig { - - - @Bean - public SecurityFilterChain configure(HttpSecurity http) throws Exception { - http - .csrf((csrf) -> csrf.disable()) - .authorizeHttpRequests((authorize) -> authorize.requestMatchers(HttpMethod.POST, "/**").permitAll() - .requestMatchers("/**").permitAll() - .anyRequest().authenticated()); - - return http.build(); -} -} diff --git a/power-pay-backend/src/main/java/com/adorsys/gis/powerpay/powerpaybackend/controller/UserRegistrationController.java b/power-pay-backend/src/main/java/com/adorsys/gis/powerpay/powerpaybackend/controller/UserRegistrationController.java new file mode 100644 index 0000000..6ce9dd2 --- /dev/null +++ b/power-pay-backend/src/main/java/com/adorsys/gis/powerpay/powerpaybackend/controller/UserRegistrationController.java @@ -0,0 +1,35 @@ +package com.adorsys.gis.powerpay.powerpaybackend.controller; + +import com.adorsys.gis.powerpay.powerpaybackend.domain.FirstRegistrationRequest; +import com.adorsys.gis.powerpay.powerpaybackend.domain.SecondRegistrationRequest; +import com.adorsys.gis.powerpay.powerpaybackend.services.UserRegistrationService; +import jakarta.validation.Valid; +import lombok.RequiredArgsConstructor; + + +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.*; + +@RestController +@RequiredArgsConstructor +@RequestMapping("/api/registration") +public class UserRegistrationController { + + private final UserRegistrationService userRegistrationService; + + + @PostMapping + public ResponseEntity registerUser(@RequestBody @Valid FirstRegistrationRequest registrationRequest) { + userRegistrationService.createProcedure(registrationRequest.getPhoneNumber(), registrationRequest.getUserName()); + return ResponseEntity.ok(HttpStatus.OK); + } + + @PostMapping("/{registrationId}") + public ResponseEntity completeRegistration(@PathVariable("registrationId") Integer registrationId, @RequestBody @Valid SecondRegistrationRequest completionRequest) { + userRegistrationService.registerUser(registrationId, completionRequest.getPin(), completionRequest.getOtp()); + return ResponseEntity.ok(HttpStatus.CREATED); + } + + +} diff --git a/power-pay-backend/src/main/java/com/adorsys/gis/powerpay/powerpaybackend/domain/FirstRegistrationRequest.java b/power-pay-backend/src/main/java/com/adorsys/gis/powerpay/powerpaybackend/domain/FirstRegistrationRequest.java new file mode 100644 index 0000000..c4b2387 --- /dev/null +++ b/power-pay-backend/src/main/java/com/adorsys/gis/powerpay/powerpaybackend/domain/FirstRegistrationRequest.java @@ -0,0 +1,12 @@ +package com.adorsys.gis.powerpay.powerpaybackend.domain; +import lombok.Getter; +import lombok.Setter; + + + +@Getter +@Setter +public class FirstRegistrationRequest { + String phoneNumber; + String userName; +} \ No newline at end of file diff --git a/power-pay-backend/src/main/java/com/adorsys/gis/powerpay/powerpaybackend/domain/SecondRegistrationRequest.java b/power-pay-backend/src/main/java/com/adorsys/gis/powerpay/powerpaybackend/domain/SecondRegistrationRequest.java new file mode 100644 index 0000000..df10c90 --- /dev/null +++ b/power-pay-backend/src/main/java/com/adorsys/gis/powerpay/powerpaybackend/domain/SecondRegistrationRequest.java @@ -0,0 +1,13 @@ +package com.adorsys.gis.powerpay.powerpaybackend.domain; +import lombok.Getter; +import lombok.Setter; + + + +@Getter +@Setter +public class SecondRegistrationRequest { + String pin; + String otp; + +} \ No newline at end of file diff --git a/power-pay-backend/src/main/java/com/adorsys/gis/powerpay/powerpaybackend/services/userRegistrationController.java b/power-pay-backend/src/main/java/com/adorsys/gis/powerpay/powerpaybackend/services/userRegistrationController.java deleted file mode 100644 index 4f01eee..0000000 --- a/power-pay-backend/src/main/java/com/adorsys/gis/powerpay/powerpaybackend/services/userRegistrationController.java +++ /dev/null @@ -1,52 +0,0 @@ -package com.adorsys.gis.powerpay.powerpaybackend.services; - -import com.adorsys.gis.powerpay.powerpaybackend.domain.User; -import com.adorsys.gis.powerpay.powerpaybackend.domain.UserRegistration; -import jakarta.validation.Valid; -import lombok.Getter; -import lombok.Setter; - -import org.springframework.http.HttpStatus; -import org.springframework.http.ResponseEntity; -import org.springframework.web.bind.annotation.*; - -@RestController -@RequestMapping("/api/registration") -public class userRegistrationController { - private final UserRegistrationService userRegistrationService; - - public userRegistrationController(UserRegistrationService userRegistrationService) { - this.userRegistrationService = userRegistrationService; - } - - @PostMapping - public ResponseEntity registerUser(@RequestBody @Valid RegistrationRequest registrationRequest) { - UserRegistration userRegistration = userRegistrationService.createProcedure(registrationRequest.getPhoneNumber(), registrationRequest.getUserName()); - return new ResponseEntity<>(userRegistration, HttpStatus.CREATED); - } - - @PostMapping("/{registrationId}") - public ResponseEntity completeRegistration(@PathVariable("registrationId") Integer registrationId, @RequestBody @Valid CompletionRequest completionRequest) { - User user = userRegistrationService.registerUser(registrationId, completionRequest.getPin(), completionRequest.getOtp()); - return new ResponseEntity<>(user, HttpStatus.CREATED); - } - - // Define request bodies for endpoints - @Getter - @Setter - public - static class RegistrationRequest { - private String phoneNumber; - private String userName; - - - } - @Getter - @Setter - public - static class CompletionRequest { - private String pin; - private String otp; - - } -} diff --git a/power-pay-backend/src/test/java/com/adorsys/gis/powerpay/powerpaybackend/services_tests/UserRegistrationIntegrationTest.java b/power-pay-backend/src/test/java/com/adorsys/gis/powerpay/powerpaybackend/controllers_tests/UserRegistrationIntegrationTest.java similarity index 84% rename from power-pay-backend/src/test/java/com/adorsys/gis/powerpay/powerpaybackend/services_tests/UserRegistrationIntegrationTest.java rename to power-pay-backend/src/test/java/com/adorsys/gis/powerpay/powerpaybackend/controllers_tests/UserRegistrationIntegrationTest.java index c9ad518..b33e1b4 100644 --- a/power-pay-backend/src/test/java/com/adorsys/gis/powerpay/powerpaybackend/services_tests/UserRegistrationIntegrationTest.java +++ b/power-pay-backend/src/test/java/com/adorsys/gis/powerpay/powerpaybackend/controllers_tests/UserRegistrationIntegrationTest.java @@ -1,10 +1,10 @@ -package com.adorsys.gis.powerpay.powerpaybackend.services_tests; +package com.adorsys.gis.powerpay.powerpaybackend.controllers_tests; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; -import com.adorsys.gis.powerpay.powerpaybackend.services.userRegistrationController.CompletionRequest; -import com.adorsys.gis.powerpay.powerpaybackend.services.userRegistrationController.RegistrationRequest; +import com.adorsys.gis.powerpay.powerpaybackend.domain.FirstRegistrationRequest; +import com.adorsys.gis.powerpay.powerpaybackend.domain.SecondRegistrationRequest; import com.fasterxml.jackson.databind.ObjectMapper; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; @@ -25,7 +25,7 @@ public class UserRegistrationIntegrationTest { @Test public void testRegisterUser() throws Exception { - RegistrationRequest request = new RegistrationRequest(); + FirstRegistrationRequest request = new FirstRegistrationRequest(); request.setPhoneNumber("1234567890"); request.setUserName("testuser"); @@ -41,7 +41,7 @@ public void testRegisterUser() throws Exception { @Test public void testCompleteRegistration() throws Exception { // Register a user - RegistrationRequest registrationRequest = new RegistrationRequest(); + FirstRegistrationRequest registrationRequest = new FirstRegistrationRequest(); registrationRequest.setPhoneNumber("1234567890"); registrationRequest.setUserName("testuser"); @@ -54,7 +54,7 @@ public void testCompleteRegistration() throws Exception { .getContentAsString(); // Complete the registration - CompletionRequest completionRequest = new CompletionRequest(); + SecondRegistrationRequest completionRequest = new SecondRegistrationRequest(); completionRequest.setPin("1234"); completionRequest.setOtp("5678"); From b74d9f2274c6ae6de10c68d55f0e1736f037d811 Mon Sep 17 00:00:00 2001 From: mbunwe-victor Date: Sat, 22 Jun 2024 08:13:03 +0100 Subject: [PATCH 6/9] Update: updated the UserRegistrationIntegrationTest file --- .../services_tests/UserRegistrationIntegrationTest.java | 1 + 1 file changed, 1 insertion(+) diff --git a/power-pay-backend/src/test/java/com/adorsys/gis/powerpay/powerpaybackend/services_tests/UserRegistrationIntegrationTest.java b/power-pay-backend/src/test/java/com/adorsys/gis/powerpay/powerpaybackend/services_tests/UserRegistrationIntegrationTest.java index c9ad518..14c13fd 100644 --- a/power-pay-backend/src/test/java/com/adorsys/gis/powerpay/powerpaybackend/services_tests/UserRegistrationIntegrationTest.java +++ b/power-pay-backend/src/test/java/com/adorsys/gis/powerpay/powerpaybackend/services_tests/UserRegistrationIntegrationTest.java @@ -8,6 +8,7 @@ import com.fasterxml.jackson.databind.ObjectMapper; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; + import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.http.MediaType; From 3e52f4fe6fbd35d84c9f695ded5e3ba40835544a Mon Sep 17 00:00:00 2001 From: nancymuyeh Date: Sat, 22 Jun 2024 20:32:27 +0100 Subject: [PATCH 7/9] refactor(backend): migrated from http ResponseEntity to springs ResponseBody --- .../domain/UserRegistrationResponseBody.java | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 power-pay-backend/src/main/java/com/adorsys/gis/powerpay/powerpaybackend/domain/UserRegistrationResponseBody.java diff --git a/power-pay-backend/src/main/java/com/adorsys/gis/powerpay/powerpaybackend/domain/UserRegistrationResponseBody.java b/power-pay-backend/src/main/java/com/adorsys/gis/powerpay/powerpaybackend/domain/UserRegistrationResponseBody.java new file mode 100644 index 0000000..0d512b6 --- /dev/null +++ b/power-pay-backend/src/main/java/com/adorsys/gis/powerpay/powerpaybackend/domain/UserRegistrationResponseBody.java @@ -0,0 +1,15 @@ +package com.adorsys.gis.powerpay.powerpaybackend.domain; + +import lombok.Getter; +import lombok.Setter; + +@Getter +@Setter +public class UserRegistrationResponseBody { + private String text; + +public UserRegistrationResponseBody(String text) { + this.text = text; +} + +} \ No newline at end of file From 9b6f7db1c5069478ac0499449cd62c78b043eec5 Mon Sep 17 00:00:00 2001 From: nancymuyeh Date: Sat, 22 Jun 2024 20:44:14 +0100 Subject: [PATCH 8/9] feat(User registration) : catch user exist error to make sure no two users can have the same phone number --- .../UserRegistrationController.java | 28 ++++++++++++------- .../domain/FirstRegistrationRequest.java | 11 ++++++-- .../powerpaybackend/domain/Procedure.java | 1 + .../domain/SecondRegistrationRequest.java | 4 +++ .../UserRegistrationRepository.java | 4 +++ .../services/UserRegistrationService.java | 3 ++ .../services/UserRegistrationServiceImpl.java | 14 ++++++++++ 7 files changed, 53 insertions(+), 12 deletions(-) diff --git a/power-pay-backend/src/main/java/com/adorsys/gis/powerpay/powerpaybackend/controller/UserRegistrationController.java b/power-pay-backend/src/main/java/com/adorsys/gis/powerpay/powerpaybackend/controller/UserRegistrationController.java index 6ce9dd2..760068f 100644 --- a/power-pay-backend/src/main/java/com/adorsys/gis/powerpay/powerpaybackend/controller/UserRegistrationController.java +++ b/power-pay-backend/src/main/java/com/adorsys/gis/powerpay/powerpaybackend/controller/UserRegistrationController.java @@ -2,13 +2,12 @@ import com.adorsys.gis.powerpay.powerpaybackend.domain.FirstRegistrationRequest; import com.adorsys.gis.powerpay.powerpaybackend.domain.SecondRegistrationRequest; -import com.adorsys.gis.powerpay.powerpaybackend.services.UserRegistrationService; +import com.adorsys.gis.powerpay.powerpaybackend.domain.UserRegistrationResponseBody; +import com.adorsys.gis.powerpay.powerpaybackend.services.UserRegistrationServiceImpl; import jakarta.validation.Valid; import lombok.RequiredArgsConstructor; -import org.springframework.http.HttpStatus; -import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.*; @RestController @@ -16,19 +15,28 @@ @RequestMapping("/api/registration") public class UserRegistrationController { - private final UserRegistrationService userRegistrationService; - + private final UserRegistrationServiceImpl userRegistrationService; @PostMapping - public ResponseEntity registerUser(@RequestBody @Valid FirstRegistrationRequest registrationRequest) { - userRegistrationService.createProcedure(registrationRequest.getPhoneNumber(), registrationRequest.getUserName()); - return ResponseEntity.ok(HttpStatus.OK); + @ResponseBody + public UserRegistrationResponseBody postResponse(@RequestBody @Valid FirstRegistrationRequest registrationRequest) { + + boolean userExist = userRegistrationService.findByPhoneNumber(registrationRequest.getPhoneNumber()); + if (userExist) { + return new UserRegistrationResponseBody("Pnone number has already been taken"); + }else { + userRegistrationService.createProcedure(registrationRequest.getPhoneNumber(), registrationRequest.getUserName()); + + return new UserRegistrationResponseBody("User name and phone number has been succesfull recieved now send the otp you recived and your pin code to complete registration"); } + +} @PostMapping("/{registrationId}") - public ResponseEntity completeRegistration(@PathVariable("registrationId") Integer registrationId, @RequestBody @Valid SecondRegistrationRequest completionRequest) { + @ResponseBody + public UserRegistrationResponseBody completeRegistration(@PathVariable("registrationId") Integer registrationId, @RequestBody @Valid SecondRegistrationRequest completionRequest) { userRegistrationService.registerUser(registrationId, completionRequest.getPin(), completionRequest.getOtp()); - return ResponseEntity.ok(HttpStatus.CREATED); + return new UserRegistrationResponseBody("You have succesfully been registered to the powerpay application"); } diff --git a/power-pay-backend/src/main/java/com/adorsys/gis/powerpay/powerpaybackend/domain/FirstRegistrationRequest.java b/power-pay-backend/src/main/java/com/adorsys/gis/powerpay/powerpaybackend/domain/FirstRegistrationRequest.java index c4b2387..cc1c3f3 100644 --- a/power-pay-backend/src/main/java/com/adorsys/gis/powerpay/powerpaybackend/domain/FirstRegistrationRequest.java +++ b/power-pay-backend/src/main/java/com/adorsys/gis/powerpay/powerpaybackend/domain/FirstRegistrationRequest.java @@ -1,4 +1,7 @@ package com.adorsys.gis.powerpay.powerpaybackend.domain; +import jakarta.validation.constraints.Digits; +import jakarta.validation.constraints.NotEmpty; +import jakarta.validation.constraints.Size; import lombok.Getter; import lombok.Setter; @@ -7,6 +10,10 @@ @Getter @Setter public class FirstRegistrationRequest { - String phoneNumber; - String userName; +@NotEmpty(message = "Name may not be empty") +@Size(min = 2, max = 32) + private String userName; + + @Digits(fraction = 0, integer = 12) + private String phoneNumber; } \ No newline at end of file diff --git a/power-pay-backend/src/main/java/com/adorsys/gis/powerpay/powerpaybackend/domain/Procedure.java b/power-pay-backend/src/main/java/com/adorsys/gis/powerpay/powerpaybackend/domain/Procedure.java index 5ecc513..67de6e2 100644 --- a/power-pay-backend/src/main/java/com/adorsys/gis/powerpay/powerpaybackend/domain/Procedure.java +++ b/power-pay-backend/src/main/java/com/adorsys/gis/powerpay/powerpaybackend/domain/Procedure.java @@ -10,6 +10,7 @@ public abstract class Procedure { @GeneratedValue(strategy = GenerationType.IDENTITY) private int id; public ProcedureStatus status = ProcedureStatus.WAITING; + @Column(nullable = false,unique = true) private String phoneNumber; diff --git a/power-pay-backend/src/main/java/com/adorsys/gis/powerpay/powerpaybackend/domain/SecondRegistrationRequest.java b/power-pay-backend/src/main/java/com/adorsys/gis/powerpay/powerpaybackend/domain/SecondRegistrationRequest.java index df10c90..ef1a038 100644 --- a/power-pay-backend/src/main/java/com/adorsys/gis/powerpay/powerpaybackend/domain/SecondRegistrationRequest.java +++ b/power-pay-backend/src/main/java/com/adorsys/gis/powerpay/powerpaybackend/domain/SecondRegistrationRequest.java @@ -1,4 +1,5 @@ package com.adorsys.gis.powerpay.powerpaybackend.domain; +import jakarta.validation.constraints.Digits; import lombok.Getter; import lombok.Setter; @@ -7,7 +8,10 @@ @Getter @Setter public class SecondRegistrationRequest { + @Digits(fraction = 0, integer = 5) String pin; + + @Digits(fraction = 0, integer = 0) String otp; } \ No newline at end of file diff --git a/power-pay-backend/src/main/java/com/adorsys/gis/powerpay/powerpaybackend/repository/UserRegistrationRepository.java b/power-pay-backend/src/main/java/com/adorsys/gis/powerpay/powerpaybackend/repository/UserRegistrationRepository.java index f57155c..06fb40e 100644 --- a/power-pay-backend/src/main/java/com/adorsys/gis/powerpay/powerpaybackend/repository/UserRegistrationRepository.java +++ b/power-pay-backend/src/main/java/com/adorsys/gis/powerpay/powerpaybackend/repository/UserRegistrationRepository.java @@ -3,6 +3,10 @@ import org.springframework.data.jpa.repository.JpaRepository; import com.adorsys.gis.powerpay.powerpaybackend.domain.UserRegistration; + + + public interface UserRegistrationRepository extends JpaRepository { + UserRegistration findByPhoneNumber(String phoneNumber); } diff --git a/power-pay-backend/src/main/java/com/adorsys/gis/powerpay/powerpaybackend/services/UserRegistrationService.java b/power-pay-backend/src/main/java/com/adorsys/gis/powerpay/powerpaybackend/services/UserRegistrationService.java index 9fce7db..4abfc30 100644 --- a/power-pay-backend/src/main/java/com/adorsys/gis/powerpay/powerpaybackend/services/UserRegistrationService.java +++ b/power-pay-backend/src/main/java/com/adorsys/gis/powerpay/powerpaybackend/services/UserRegistrationService.java @@ -10,4 +10,7 @@ public interface UserRegistrationService { UserRegistration createProcedure(String phoneNumber, String userName); + boolean findByPhoneNumber(String phoneNumber); + // UserRegistration findByOpt(String otp); + } diff --git a/power-pay-backend/src/main/java/com/adorsys/gis/powerpay/powerpaybackend/services/UserRegistrationServiceImpl.java b/power-pay-backend/src/main/java/com/adorsys/gis/powerpay/powerpaybackend/services/UserRegistrationServiceImpl.java index d006d4a..9fc819a 100644 --- a/power-pay-backend/src/main/java/com/adorsys/gis/powerpay/powerpaybackend/services/UserRegistrationServiceImpl.java +++ b/power-pay-backend/src/main/java/com/adorsys/gis/powerpay/powerpaybackend/services/UserRegistrationServiceImpl.java @@ -66,4 +66,18 @@ public UserRegistration createProcedure(String phoneNumber, String userName) { return userRegistration; } + + @Override + public boolean findByPhoneNumber(String phoneNumber) { + UserRegistration userRegistration = userRegistrationRepository.findByPhoneNumber(phoneNumber); + if(userRegistration != null) { + return Boolean.TRUE; + }else{ return Boolean.FALSE;} + } + + // @Override + // public UserRegistration findByOpt(String otp) { + // UserRegistration userOtp = userRegistrationRepository.findByOpt(otp); + // return userOtp; + // } } From 9be66f2e815b575b7fd3d4a3bbab6dd7c970d7cb Mon Sep 17 00:00:00 2001 From: nancymuyeh Date: Sun, 30 Jun 2024 04:05:38 +0100 Subject: [PATCH 9/9] feat(User registration) : Implement otp check for phone number validation --- .../controller/UserRegistrationController.java | 10 ++++++++-- .../repository/UserRegistrationRepository.java | 3 +++ .../services/UserRegistrationService.java | 2 +- .../services/UserRegistrationServiceImpl.java | 10 +++++----- 4 files changed, 17 insertions(+), 8 deletions(-) diff --git a/power-pay-backend/src/main/java/com/adorsys/gis/powerpay/powerpaybackend/controller/UserRegistrationController.java b/power-pay-backend/src/main/java/com/adorsys/gis/powerpay/powerpaybackend/controller/UserRegistrationController.java index 760068f..12c271c 100644 --- a/power-pay-backend/src/main/java/com/adorsys/gis/powerpay/powerpaybackend/controller/UserRegistrationController.java +++ b/power-pay-backend/src/main/java/com/adorsys/gis/powerpay/powerpaybackend/controller/UserRegistrationController.java @@ -3,6 +3,7 @@ import com.adorsys.gis.powerpay.powerpaybackend.domain.FirstRegistrationRequest; import com.adorsys.gis.powerpay.powerpaybackend.domain.SecondRegistrationRequest; import com.adorsys.gis.powerpay.powerpaybackend.domain.UserRegistrationResponseBody; +import com.adorsys.gis.powerpay.powerpaybackend.repository.UserRegistrationRepository; import com.adorsys.gis.powerpay.powerpaybackend.services.UserRegistrationServiceImpl; import jakarta.validation.Valid; import lombok.RequiredArgsConstructor; @@ -23,11 +24,11 @@ public UserRegistrationResponseBody postResponse(@RequestBody @Valid FirstRegist boolean userExist = userRegistrationService.findByPhoneNumber(registrationRequest.getPhoneNumber()); if (userExist) { - return new UserRegistrationResponseBody("Pnone number has already been taken"); + return new UserRegistrationResponseBody("This phone number has already been taken"); }else { userRegistrationService.createProcedure(registrationRequest.getPhoneNumber(), registrationRequest.getUserName()); - return new UserRegistrationResponseBody("User name and phone number has been succesfull recieved now send the otp you recived and your pin code to complete registration"); + return new UserRegistrationResponseBody("An otp has been sent to the phone number you entered, Enter the otp and your your pin code to complete registration"); } } @@ -35,9 +36,14 @@ public UserRegistrationResponseBody postResponse(@RequestBody @Valid FirstRegist @PostMapping("/{registrationId}") @ResponseBody public UserRegistrationResponseBody completeRegistration(@PathVariable("registrationId") Integer registrationId, @RequestBody @Valid SecondRegistrationRequest completionRequest) { + String userOtp = userRegistrationService.findByOpt(completionRequest.getOtp()); + if(userOtp != completionRequest.getOtp()) { + return new UserRegistrationResponseBody("wrong otp! Check you messages fot the correct otp to validate your phone number"); + } else { userRegistrationService.registerUser(registrationId, completionRequest.getPin(), completionRequest.getOtp()); return new UserRegistrationResponseBody("You have succesfully been registered to the powerpay application"); } +} } diff --git a/power-pay-backend/src/main/java/com/adorsys/gis/powerpay/powerpaybackend/repository/UserRegistrationRepository.java b/power-pay-backend/src/main/java/com/adorsys/gis/powerpay/powerpaybackend/repository/UserRegistrationRepository.java index 06fb40e..987df4e 100644 --- a/power-pay-backend/src/main/java/com/adorsys/gis/powerpay/powerpaybackend/repository/UserRegistrationRepository.java +++ b/power-pay-backend/src/main/java/com/adorsys/gis/powerpay/powerpaybackend/repository/UserRegistrationRepository.java @@ -1,5 +1,7 @@ package com.adorsys.gis.powerpay.powerpaybackend.repository; +import java.util.Optional; + import org.springframework.data.jpa.repository.JpaRepository; import com.adorsys.gis.powerpay.powerpaybackend.domain.UserRegistration; @@ -8,5 +10,6 @@ public interface UserRegistrationRepository extends JpaRepository { UserRegistration findByPhoneNumber(String phoneNumber); + String findByOpt(String opt); } diff --git a/power-pay-backend/src/main/java/com/adorsys/gis/powerpay/powerpaybackend/services/UserRegistrationService.java b/power-pay-backend/src/main/java/com/adorsys/gis/powerpay/powerpaybackend/services/UserRegistrationService.java index 4abfc30..00bdabd 100644 --- a/power-pay-backend/src/main/java/com/adorsys/gis/powerpay/powerpaybackend/services/UserRegistrationService.java +++ b/power-pay-backend/src/main/java/com/adorsys/gis/powerpay/powerpaybackend/services/UserRegistrationService.java @@ -11,6 +11,6 @@ public interface UserRegistrationService { UserRegistration createProcedure(String phoneNumber, String userName); boolean findByPhoneNumber(String phoneNumber); - // UserRegistration findByOpt(String otp); + String findByOpt(String otp); } diff --git a/power-pay-backend/src/main/java/com/adorsys/gis/powerpay/powerpaybackend/services/UserRegistrationServiceImpl.java b/power-pay-backend/src/main/java/com/adorsys/gis/powerpay/powerpaybackend/services/UserRegistrationServiceImpl.java index 9fc819a..101cbbb 100644 --- a/power-pay-backend/src/main/java/com/adorsys/gis/powerpay/powerpaybackend/services/UserRegistrationServiceImpl.java +++ b/power-pay-backend/src/main/java/com/adorsys/gis/powerpay/powerpaybackend/services/UserRegistrationServiceImpl.java @@ -75,9 +75,9 @@ public boolean findByPhoneNumber(String phoneNumber) { }else{ return Boolean.FALSE;} } - // @Override - // public UserRegistration findByOpt(String otp) { - // UserRegistration userOtp = userRegistrationRepository.findByOpt(otp); - // return userOtp; - // } + @Override + public String findByOpt(String otp) { + String userOtp = userRegistrationRepository.findByOpt(otp); + return userOtp; + } }