diff --git a/src/main/java/org/sunbird/common/util/Constants.java b/src/main/java/org/sunbird/common/util/Constants.java index 52a2d7f01..3799e6ca3 100644 --- a/src/main/java/org/sunbird/common/util/Constants.java +++ b/src/main/java/org/sunbird/common/util/Constants.java @@ -635,6 +635,8 @@ public class Constants { public static final String CHECKS = "checks"; public static final String CASSANDRA_DB = "cassandra db"; public static final String REDIS_CACHE = "redis cache"; + public static final String PHONE_NUMBER_EXIST_ERROR = "Phone number is already registered."; + public static final String PHONE_VERIFIED = "phoneVerified"; public static final String QUERY = "query"; public static final String SORT_BY_KEYWORD = "sortBy"; private Constants() { diff --git a/src/main/java/org/sunbird/user/registration/model/UserRegistrationInfo.java b/src/main/java/org/sunbird/user/registration/model/UserRegistrationInfo.java index 8849afd25..16e26826b 100644 --- a/src/main/java/org/sunbird/user/registration/model/UserRegistrationInfo.java +++ b/src/main/java/org/sunbird/user/registration/model/UserRegistrationInfo.java @@ -20,6 +20,7 @@ public class UserRegistrationInfo { private String mapId; private String sbRootOrgId; private String sbOrgId; + private String phone; public String getRegistrationCode() { return registrationCode; @@ -124,4 +125,12 @@ public String getSbOrgId() { public void setSbOrgId(String sbOrgId) { this.sbOrgId = sbOrgId; } + + public String getPhone() { + return phone; + } + + public void setPhone(String phone) { + this.phone = phone; + } } diff --git a/src/main/java/org/sunbird/user/registration/service/UserRegistrationServiceImpl.java b/src/main/java/org/sunbird/user/registration/service/UserRegistrationServiceImpl.java index 7a5b696aa..621972c3c 100644 --- a/src/main/java/org/sunbird/user/registration/service/UserRegistrationServiceImpl.java +++ b/src/main/java/org/sunbird/user/registration/service/UserRegistrationServiceImpl.java @@ -90,8 +90,10 @@ public SBApiResponse registerUser(UserRegistrationInfo userRegInfo) { String errMsg = validateRegisterationPayload(userRegInfo); if (StringUtils.isBlank(errMsg)) { try { - if (isUserExist(userRegInfo.getEmail().toLowerCase())) { + if (isUserExist(Constants.EMAIL, userRegInfo.getEmail().toLowerCase())) { errMsg = Constants.EMAIL_EXIST_ERROR; + } if (isUserExist(Constants.PHONE, userRegInfo.getPhone())) { + errMsg = Constants.PHONE_NUMBER_EXIST_ERROR; } else { // verify the given email exist in ES Server UserRegistration regDocument = getUserRegistrationDocument(new HashMap() { @@ -286,6 +288,9 @@ private String validateRegisterationPayload(UserRegistrationInfo userRegInfo) { if (StringUtils.isBlank(userRegInfo.getSource())) { errList.add("Source"); } + if(StringUtils.isBlank(userRegInfo.getPhone())) { + errList.add("Phone"); + } if (!errList.isEmpty()) { str.append("Failed to Register User Details. Missing Params - [").append(errList.toString()).append("]"); } @@ -294,8 +299,11 @@ private String validateRegisterationPayload(UserRegistrationInfo userRegInfo) { str.setLength(0); str.append("Invalid email id"); } + if(StringUtils.isNotBlank(userRegInfo.getPhone()) && !isValidPhoneNumber(userRegInfo.getPhone())) { + str.setLength(0); + str.append("Invalid phone number"); + } return str.toString(); - } private UserRegistration getUserRegistrationDocument(Map mustMatch) throws Exception { @@ -324,6 +332,7 @@ private UserRegistration getRegistrationObject(UserRegistrationInfo userRegInfo) userRegistration.setMapId(userRegInfo.getMapId()); userRegistration.setOrganisationType(userRegInfo.getOrganisationType()); userRegistration.setOrganisationSubType(userRegInfo.getOrganisationSubType()); + userRegistration.setPhone(userRegInfo.getPhone()); if (StringUtils.isBlank(userRegInfo.getRegistrationCode())) { userRegistration.setRegistrationCode(serverProperties.getUserRegCodePrefix() + "-" @@ -352,13 +361,13 @@ private SearchSourceBuilder queryBuilder(Map mustMatch) { return new SearchSourceBuilder().query(boolBuilder); } - private boolean isUserExist(String email) { + private boolean isUserExist(String key, String value) { // request body SunbirdApiRequest requestObj = new SunbirdApiRequest(); Map reqMap = new HashMap<>(); reqMap.put(Constants.FILTERS, new HashMap() { { - put(Constants.EMAIL, email); + put(key, value); } }); requestObj.setRequest(reqMap); @@ -520,4 +529,11 @@ private void updateValues(UserRegistration userReg, UserRegistrationInfo userReg userReg.setSbRootOrgId(userRegInfo.getSbRootOrgId()); userReg.setSbOrgId(userRegInfo.getSbOrgId()); } + + private boolean isValidPhoneNumber(String phone) { + if (phone.matches("\\d{10}")) { + return true; + } else + return false; + } } diff --git a/src/main/java/org/sunbird/user/service/UserUtilityServiceImpl.java b/src/main/java/org/sunbird/user/service/UserUtilityServiceImpl.java index 0616d4a8c..c9631bf3d 100644 --- a/src/main/java/org/sunbird/user/service/UserUtilityServiceImpl.java +++ b/src/main/java/org/sunbird/user/service/UserUtilityServiceImpl.java @@ -223,6 +223,8 @@ public boolean createUser(UserRegistration userRegistration) { requestBody.put(Constants.FIRSTNAME, userRegistration.getFirstName()); requestBody.put(Constants.LASTNAME, userRegistration.getLastName()); requestBody.put(Constants.EMAIL_VERIFIED, true); + requestBody.put(Constants.PHONE, userRegistration.getPhone()); + requestBody.put(Constants.PHONE_VERIFIED, true); request.put(Constants.REQUEST, requestBody); try { Map readData = (Map) outboundRequestHandlerService.fetchResultUsingPost( @@ -260,8 +262,9 @@ public boolean updateUser(UserRegistration userRegistration) { personalDetails.put(Constants.FIRSTNAME.toLowerCase(), userRegistration.getFirstName()); personalDetails.put(Constants.SURNAME, userRegistration.getLastName()); personalDetails.put(Constants.PRIMARY_EMAIL, userRegistration.getEmail()); + personalDetails.put(Constants.MOBILE, userRegistration.getPhone()); + personalDetails.put(Constants.PHONE_VERIFIED, true); profileDetails.put(Constants.PERSONAL_DETAILS, personalDetails); - Map professionDetailObj = new HashMap(); professionDetailObj.put(Constants.ORGANIZATION_TYPE, Constants.GOVERNMENT); if (StringUtils.isNotEmpty(userRegistration.getPosition())) {