Skip to content

Commit

Permalink
Added check for null recipients before generating events
Browse files Browse the repository at this point in the history
  • Loading branch information
karthik-tarento committed Feb 21, 2024
1 parent 3d6aa23 commit da6bb92
Showing 1 changed file with 44 additions and 38 deletions.
82 changes: 44 additions & 38 deletions src/main/java/org/sunbird/cbp/service/CbplanContentConsumer.java
Original file line number Diff line number Diff line change
Expand Up @@ -73,58 +73,64 @@ private void processKafkaMessage(Map<String, Object> cbplanContentRequest) {
}
Set<String> providerRootOrgIds = new HashSet<>((List<String>) cbplanContentRequest.get(Constants.PROVIDER_ORG_ID));
String mdoAdminId = (String) cbplanContentRequest.get(Constants.CREATED_BY);
String mdoAdminEmail = userService.getUserDetails(Collections.singletonList(mdoAdminId), new ArrayList<>()).get(mdoAdminId).get(Constants.EMAIL);

Map<String, Object> mailNotificationDetails = new HashMap<>();
mailNotificationDetails.put(Constants.PROVIDER_EMAIL_ID_LIST, getCBPAdminDetails(providerRootOrgIds));
mailNotificationDetails.put(Constants.MDO_NAME, cbplanContentRequest.get(Constants.MDO_NAME));
mailNotificationDetails.put(Constants.COMPETENCY_AREA, competencyInfoMap.get(Constants.NAME));
mailNotificationDetails.put(Constants.COMPETENCY_THEMES, allThemes.replace(allThemes.length()-2, allThemes.length() - 1, "."));
mailNotificationDetails.put(Constants.COMPETENCY_SUB_THEMES, allSubThemes.replace(allSubThemes.length()-2, allSubThemes.length()-1, "."));
mailNotificationDetails.put(Constants.DESCRIPTION , cbplanContentRequest.get(Constants.DESCRIPTION));
mailNotificationDetails.put(Constants.COPY_EMAIL, mdoAdminEmail);
mailNotificationDetails.put(Constants.CREATED_BY, mdoAdminId);
sendNotificationToProvidersAsync(mailNotificationDetails);
Map<String, Map<String, String>> userMap = userService.getUserDetails(Collections.singletonList(mdoAdminId), new ArrayList<>());
if (!CollectionUtils.isEmpty(userMap)) {
String mdoAdminEmail = userMap.get(mdoAdminId).get(Constants.EMAIL);
Map<String, Object> mailNotificationDetails = new HashMap<>();
mailNotificationDetails.put(Constants.PROVIDER_EMAIL_ID_LIST, getCBPAdminDetails(providerRootOrgIds));
mailNotificationDetails.put(Constants.MDO_NAME, cbplanContentRequest.get(Constants.MDO_NAME));
mailNotificationDetails.put(Constants.COMPETENCY_AREA, competencyInfoMap.get(Constants.NAME));
mailNotificationDetails.put(Constants.COMPETENCY_THEMES, allThemes.replace(allThemes.length()-2, allThemes.length() - 1, "."));
mailNotificationDetails.put(Constants.COMPETENCY_SUB_THEMES, allSubThemes.replace(allSubThemes.length()-2, allSubThemes.length()-1, "."));
mailNotificationDetails.put(Constants.DESCRIPTION , cbplanContentRequest.get(Constants.DESCRIPTION));
mailNotificationDetails.put(Constants.COPY_EMAIL, mdoAdminEmail);
mailNotificationDetails.put(Constants.CREATED_BY, mdoAdminId);
sendNotificationToProvidersAsync(mailNotificationDetails);
} else {
logger.error("User search returns empty result for Id: " + mdoAdminId, new Exception());
}
logger.info(String.format("Completed request for content. Time taken: ", (System.currentTimeMillis() - startTime)));
} catch (Exception e) {
logger.error("Exception occurred while sending email : " + e.getMessage(), e);
}
}

public List<String> getCBPAdminDetails(Set<String> rootOrgIds){
List<Map<String, String>> userRecords = new ArrayList<>();
public List<String> getCBPAdminDetails(Set<String> rootOrgIds) throws Exception {
Map<String, Object> request = getSearchObject(rootOrgIds);
HashMap<String, String> headersValue = new HashMap<>();
headersValue.put("Content-Type", "application/json");
try {
List<String> providerIdEmails = new ArrayList<>();
StringBuilder url = new StringBuilder(configuration.getLmsServiceHost()).append(configuration.getLmsUserSearchEndPoint());
Map<String, Object> searchProfileApiResp = outboundReqService.fetchResultUsingPost(url.toString(), request, headersValue);
if (searchProfileApiResp != null
&& "OK".equalsIgnoreCase((String) searchProfileApiResp.get(Constants.RESPONSE_CODE))) {
Map<String, Object> map = (Map<String, Object>) searchProfileApiResp.get(Constants.RESULT);
Map<String, Object> response = (Map<String, Object>) map.get(Constants.RESPONSE);
List<Map<String, Object>> contents = (List<Map<String, Object>>) response.get(Constants.CONTENT);
if (!CollectionUtils.isEmpty(contents)) {
for(Map<String, Object> content: contents){
String rootOrgId = (String)content.get(Constants.ROOT_ORG_ID);
HashMap<String, Object> profileDetails = (HashMap<String, Object>) content.get(Constants.PROFILE_DETAILS);
if (!CollectionUtils.isEmpty(profileDetails)) {
HashMap<String, Object> personalDetails = (HashMap<String, Object>) profileDetails.get(Constants.PERSONAL_DETAILS);
if (!CollectionUtils.isEmpty(personalDetails) && personalDetails.get(Constants.PRIMARY_EMAIL)!= null ) {
if(rootOrgIds.contains(rootOrgId))
providerIdEmails.add((String)personalDetails.get(Constants.PRIMARY_EMAIL));
}
List<String> providerIdEmails = new ArrayList<>();
StringBuilder url = new StringBuilder(configuration.getLmsServiceHost())
.append(configuration.getLmsUserSearchEndPoint());
Map<String, Object> searchProfileApiResp = outboundReqService.fetchResultUsingPost(url.toString(), request,
headersValue);
if (searchProfileApiResp != null
&& "OK".equalsIgnoreCase((String) searchProfileApiResp.get(Constants.RESPONSE_CODE))) {
Map<String, Object> map = (Map<String, Object>) searchProfileApiResp.get(Constants.RESULT);
Map<String, Object> response = (Map<String, Object>) map.get(Constants.RESPONSE);
List<Map<String, Object>> contents = (List<Map<String, Object>>) response.get(Constants.CONTENT);
if (!CollectionUtils.isEmpty(contents)) {
for (Map<String, Object> content : contents) {
String rootOrgId = (String) content.get(Constants.ROOT_ORG_ID);
HashMap<String, Object> profileDetails = (HashMap<String, Object>) content
.get(Constants.PROFILE_DETAILS);
if (!CollectionUtils.isEmpty(profileDetails)) {
HashMap<String, Object> personalDetails = (HashMap<String, Object>) profileDetails
.get(Constants.PERSONAL_DETAILS);
if (!CollectionUtils.isEmpty(personalDetails)
&& personalDetails.get(Constants.PRIMARY_EMAIL) != null) {
if (rootOrgIds.contains(rootOrgId))
providerIdEmails.add((String) personalDetails.get(Constants.PRIMARY_EMAIL));
}
}
}
}
logger.info("CBP Admin emails fetched successfully: " + providerIdEmails);
return providerIdEmails;
} catch (Exception e) {
logger.error("Exception while fetching cbp admin details : " +e.getMessage() + " request : " + request,e);
throw new ApplicationException("Hub Service ERROR: ", e);
}
if (CollectionUtils.isEmpty(providerIdEmails)) {
throw new Exception("Failed to find CBP Admin for OrgIds: " + rootOrgIds);
}
logger.info("CBP Admin emails fetched successfully: " + providerIdEmails);
return providerIdEmails;
}

private Map<String, Object> getSearchObject(Set<String> rootOrgIds) {
Expand Down

0 comments on commit da6bb92

Please sign in to comment.