-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #107 from Awambeng/feat/12-implement-user-registra…
…tion-logic Implement the user registration logic
- Loading branch information
Showing
6 changed files
with
103 additions
and
0 deletions.
There are no files selected for viewing
8 changes: 8 additions & 0 deletions
8
.../java/com/adorsys/gis/powerpay/powerpaybackend/repository/UserRegistrationRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package com.adorsys.gis.powerpay.powerpaybackend.repository; | ||
|
||
import org.springframework.data.jpa.repository.JpaRepository; | ||
import com.adorsys.gis.powerpay.powerpaybackend.domain.UserRegistration; | ||
|
||
public interface UserRegistrationRepository extends JpaRepository<UserRegistration, Integer> { | ||
|
||
} |
13 changes: 13 additions & 0 deletions
13
.../main/java/com/adorsys/gis/powerpay/powerpaybackend/services/UserRegistrationService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
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.constraints.NotNull; | ||
|
||
public interface UserRegistrationService { | ||
|
||
User registerUser(@NotNull Integer registrationId, String pin, String otp); | ||
|
||
UserRegistration createProcedure(String phoneNumber, String userName); | ||
|
||
} |
69 changes: 69 additions & 0 deletions
69
...n/java/com/adorsys/gis/powerpay/powerpaybackend/services/UserRegistrationServiceImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package com.adorsys.gis.powerpay.powerpaybackend.services; | ||
|
||
import java.security.SecureRandom; | ||
|
||
import org.springframework.stereotype.Service; | ||
|
||
import com.adorsys.gis.powerpay.powerpaybackend.domain.User; | ||
import com.adorsys.gis.powerpay.powerpaybackend.domain.UserRegistration; | ||
import com.adorsys.gis.powerpay.powerpaybackend.repository.UserRegistrationRepository; | ||
import com.adorsys.gis.powerpay.powerpaybackend.repository.UserRepository; | ||
import com.adorsys.gis.powerpay.powerpaybackend.utils.DataSecurityService; | ||
import com.adorsys.gis.powerpay.powerpaybackend.utils.UserRegistrationException; | ||
|
||
import jakarta.validation.constraints.NotNull; | ||
import lombok.AllArgsConstructor; | ||
|
||
@Service | ||
@AllArgsConstructor | ||
public class UserRegistrationServiceImpl implements UserRegistrationService { | ||
|
||
private final UserRegistrationRepository userRegistrationRepository; | ||
private final UserRepository userRepository; | ||
private final DataSecurityService dataSecurityService; | ||
|
||
private static final String CHARACTERS = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"; | ||
private static final int OTP_LENGTH = 8; | ||
|
||
@Override | ||
public User registerUser(@NotNull Integer registrationId, String pin, String otp) { | ||
UserRegistration userRegistration = userRegistrationRepository.findById(registrationId) | ||
.orElseThrow(() -> new UserRegistrationException("User registration not found")); | ||
|
||
User newUser = new User(); | ||
newUser.setUserName(userRegistration.getUserName()); | ||
newUser.setPin(dataSecurityService.hashData(pin)); | ||
|
||
try { | ||
userRepository.save(newUser); | ||
} catch (Exception e) { | ||
throw new UserRegistrationException("User registration failed: " + e.getMessage()); | ||
} | ||
|
||
return newUser; | ||
} | ||
|
||
@Override | ||
public UserRegistration createProcedure(String phoneNumber, String userName) { | ||
UserRegistration userRegistration = new UserRegistration(); | ||
|
||
// generating an otp | ||
StringBuilder otp = new StringBuilder(); | ||
SecureRandom secureRandom = new SecureRandom(); | ||
|
||
for (int i = 0; i < OTP_LENGTH; i++) { | ||
int index = secureRandom.nextInt(CHARACTERS.length()); | ||
char character = CHARACTERS.charAt(index); | ||
otp.append(character); | ||
} | ||
|
||
var otpValue = otp.toString(); | ||
|
||
userRegistration.setPhoneNumber(phoneNumber); | ||
userRegistration.setUserName(userName); | ||
userRegistration.setOpt(dataSecurityService.hashData(otpValue)); | ||
userRegistrationRepository.save(userRegistration); | ||
|
||
return userRegistration; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 11 additions & 0 deletions
11
...c/main/java/com/adorsys/gis/powerpay/powerpaybackend/utils/UserRegistrationException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package com.adorsys.gis.powerpay.powerpaybackend.utils; | ||
|
||
public class UserRegistrationException extends RuntimeException { | ||
public UserRegistrationException(String message, Exception exception) { | ||
super(message, exception); | ||
} | ||
|
||
public UserRegistrationException(String userRegistrationNotFound) { | ||
super(userRegistrationNotFound); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters