Skip to content

Commit

Permalink
Fixes for user registration flow.
Browse files Browse the repository at this point in the history
  • Loading branch information
karthik-tarento committed Feb 2, 2023
1 parent 295daf7 commit 97df821
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 3 deletions.
2 changes: 2 additions & 0 deletions src/main/java/org/sunbird/org/service/ExtendedOrgService.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,6 @@ public interface ExtendedOrgService {
public SBApiResponse orgExtSearch(Map<String, Object> request) throws Exception;

public void getOrgDetailsFromDB(List<String> orgIds, Map<String, String> orgInfoMap);

public SBApiResponse createOrgForUserRegistration(Map<String, Object> requestData);
}
92 changes: 90 additions & 2 deletions src/main/java/org/sunbird/org/service/ExtendedOrgServiceImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -432,7 +432,63 @@ public void getOrgDetailsFromDB(List<String> orgIds, Map<String, String> orgInfo
log.error("Failed to get user details from DB. Exception: ", e);
}
}


public SBApiResponse createOrgForUserRegistration(Map<String, Object> request) {
SBApiResponse response = ProjectUtil.createDefaultResponse(Constants.API_ORG_EXT_CREATE);
try {
String errMsg = validateOrgRequestForRegistration(request);
if (!StringUtils.isEmpty(errMsg)) {
response.getParams().setErrmsg(errMsg);
response.setResponseCode(HttpStatus.BAD_REQUEST);
return response;
}

Map<String, Object> requestData = (Map<String, Object>) request.get(Constants.REQUEST);

String orgId = checkOrgExist((String) requestData.get(Constants.CHANNEL), StringUtils.EMPTY);

if (StringUtils.isEmpty(orgId)) {
orgId = createOrgInSunbird(request, (String) requestData.get(Constants.CHANNEL), StringUtils.EMPTY);
}

if (!StringUtils.isEmpty(orgId)) {
Map<String, Object> updateRequest = new HashMap<String, Object>();
updateRequest.put(Constants.SB_ORG_ID, orgId);
String sbRootOrgId = (String) requestData.get(Constants.SB_ROOT_ORG_ID);

if (StringUtils.isEmpty(sbRootOrgId)) {
sbRootOrgId = findRootOrgId((String) requestData.get(Constants.ORG_NAME),
(String) requestData.get(Constants.MAP_ID));
}

if (!StringUtils.isEmpty(sbRootOrgId)) {
updateRequest.put(Constants.SB_ROOT_ORG_ID, sbRootOrgId);
}

Map<String, Object> compositeKey = new HashMap<String, Object>() {
private static final long serialVersionUID = 1L;
{
put(Constants.ORG_NAME, (String) requestData.get(Constants.ORG_NAME));
put(Constants.MAP_ID, requestData.get(Constants.MAP_ID));
}
};
cassandraOperation.updateRecord(Constants.SUNBIRD_KEY_SPACE_NAME, Constants.TABLE_ORG_HIERARCHY,
updateRequest, compositeKey);
response.getResult().put(Constants.ORGANIZATION_ID, orgId);
response.getResult().put(Constants.RESPONSE, Constants.SUCCESS);
} else {
response.getParams().setErrmsg("Failed to create organisation in Sunbird.");
response.setResponseCode(HttpStatus.INTERNAL_SERVER_ERROR);
}
} catch (Exception e) {
log.error(e);
response.getParams().setErrmsg(e.getMessage());
response.setResponseCode(HttpStatus.INTERNAL_SERVER_ERROR);
}

return response;
}

private String createMapId(Map<String, Object> requestData) {
Map<String, Object> queryRequest = new HashMap<>();
String prefix = StringUtils.EMPTY;
Expand Down Expand Up @@ -490,7 +546,7 @@ private String fetchRootOrgId(Map<String, Object> requestData) {
}
return sbOrgId;
}

private void fetchMapIdFromDB(Map<String, Object> requestData) {
Map<String, Object> queryRequest = new HashMap<>();
queryRequest.put(Constants.ORG_NAME, requestData.get(Constants.ORG_NAME));
Expand All @@ -504,4 +560,36 @@ private void fetchMapIdFromDB(Map<String, Object> requestData) {
requestData.put(Constants.MAP_ID, (String) data.get(Constants.MAP_ID));
}
}

private String validateOrgRequestForRegistration(Map<String, Object> request) {
List<String> params = new ArrayList<String>();
StringBuilder strBuilder = new StringBuilder();
Map<String, Object> requestData = (Map<String, Object>) request.get(Constants.REQUEST);
if (ObjectUtils.isEmpty(requestData)) {
strBuilder.append("Request object is empty.");
return strBuilder.toString();
}

if (StringUtils.isEmpty((String) requestData.get(Constants.ORG_NAME))) {
params.add(Constants.ORG_NAME);
}

if (StringUtils.isEmpty((String) requestData.get(Constants.MAP_ID))) {
params.add(Constants.MAP_ID);
}

if (StringUtils.isEmpty((String) requestData.get(Constants.ORGANIZATION_TYPE))) {
params.add(Constants.ORGANIZATION_TYPE);
}

if (StringUtils.isEmpty((String) requestData.get(Constants.ORGANIZATION_SUB_TYPE))) {
params.add(Constants.ORGANIZATION_SUB_TYPE);
}

if (!params.isEmpty()) {
strBuilder.append("Invalid Request. Missing params - " + params);
}

return strBuilder.toString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ public void initiateCreateUserFlow(String registrationCode) {

// Create the org if it's not already onboarded.
if ("null".equalsIgnoreCase(userReg.getSbOrgId()) || StringUtils.isEmpty(userReg.getSbOrgId())) {
SBApiResponse orgResponse = extOrgService.createOrg(getOrgCreateRequest(userReg), StringUtils.EMPTY);
SBApiResponse orgResponse = extOrgService.createOrgForUserRegistration(getOrgCreateRequest(userReg));
if (orgResponse.getResponseCode() == HttpStatus.OK) {
String orgId = (String) orgResponse.getResult().get(Constants.ORGANIZATION_ID);
userReg.setSbOrgId(orgId);
Expand Down

0 comments on commit 97df821

Please sign in to comment.