Skip to content

Commit

Permalink
4.8.0 phone reg (#183)
Browse files Browse the repository at this point in the history
* Added 'phone' as mandatory attribute in user registration.
  • Loading branch information
karthik-tarento authored Mar 28, 2023
1 parent 31aea0b commit 1574fda
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 5 deletions.
2 changes: 2 additions & 0 deletions src/main/java/org/sunbird/common/util/Constants.java
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public class UserRegistrationInfo {
private String mapId;
private String sbRootOrgId;
private String sbOrgId;
private String phone;

public String getRegistrationCode() {
return registrationCode;
Expand Down Expand Up @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, Object>() {
Expand Down Expand Up @@ -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("]");
}
Expand All @@ -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<String, Object> mustMatch) throws Exception {
Expand Down Expand Up @@ -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() + "-"
Expand Down Expand Up @@ -352,13 +361,13 @@ private SearchSourceBuilder queryBuilder(Map<String, Object> 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<String, Object> reqMap = new HashMap<>();
reqMap.put(Constants.FILTERS, new HashMap<String, Object>() {
{
put(Constants.EMAIL, email);
put(key, value);
}
});
requestObj.setRequest(reqMap);
Expand Down Expand Up @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, Object> readData = (Map<String, Object>) outboundRequestHandlerService.fetchResultUsingPost(
Expand Down Expand Up @@ -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<String, Object> professionDetailObj = new HashMap<String, Object>();
professionDetailObj.put(Constants.ORGANIZATION_TYPE, Constants.GOVERNMENT);
if (StringUtils.isNotEmpty(userRegistration.getPosition())) {
Expand Down

0 comments on commit 1574fda

Please sign in to comment.