Skip to content

Commit

Permalink
Fix for user auto enrollment
Browse files Browse the repository at this point in the history
  • Loading branch information
karthik-tarento committed Oct 13, 2022
1 parent 6b10463 commit e05ba57
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,29 @@
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
import org.sunbird.assessment.repo.CohortUsers;
import org.sunbird.assessment.repo.UserAssessmentTopPerformerRepository;
import org.sunbird.common.model.*;
import org.sunbird.common.model.OpenSaberApiUserProfile;
import org.sunbird.common.model.Response;
import org.sunbird.common.model.SearchUserApiContent;
import org.sunbird.common.model.SunbirdApiBatchResp;
import org.sunbird.common.model.SunbirdApiHierarchyResultContent;
import org.sunbird.common.model.SunbirdApiResp;
import org.sunbird.common.model.SunbirdApiUserCourse;
import org.sunbird.common.model.SunbirdApiUserCourseListResp;
import org.sunbird.common.model.SunbirdUserProfessionalDetail;
import org.sunbird.common.service.ContentService;
import org.sunbird.common.service.OutboundRequestHandlerServiceImpl;
import org.sunbird.common.util.CbExtServerProperties;
import org.sunbird.common.util.Constants;
import org.sunbird.core.logger.CbExtLogger;
import org.sunbird.user.service.UserUtilityService;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.type.CollectionType;

@Service
public class CohortsServiceImpl implements CohortsService {

Expand Down Expand Up @@ -139,7 +151,7 @@ public List<CohortUsers> getActiveUsers(String xAuthUser, String rootOrg, String
@Override
public Response autoEnrollmentInCourse(String authUserToken, String rootOrg, String contentId, String userUUID)
throws Exception {
List<SunbirdApiBatchResp> batchResp = fetchBatchsDetails(contentId);
List<SunbirdApiBatchResp> batchResp = fetchBatchDetails(contentId);
List<String> batchIdList = null;
if (!CollectionUtils.isEmpty(batchResp))
batchIdList = batchResp.stream().map(SunbirdApiBatchResp::getBatchId).collect(Collectors.toList());
Expand Down Expand Up @@ -303,14 +315,22 @@ private List<String> fetchBatchIdDetails(String contentId) {
return Collections.emptyList();
}

private List<SunbirdApiBatchResp> fetchBatchsDetails(String contentId) {
private List<SunbirdApiBatchResp> fetchBatchDetails(String contentId) {
try {
SunbirdApiResp contentHierarchy = contentService.getHeirarchyResponse(contentId);
if (contentHierarchy != null && "successful".equalsIgnoreCase(contentHierarchy.getParams().getStatus())) {
return contentHierarchy.getResult().getContent().getBatches();
Map<String, Object> contentResponse = contentService.searchLiveContent(contentId);
if (!ObjectUtils.isEmpty(contentResponse)) {
Map<String, Object> contentResult = (Map<String, Object>) contentResponse.get(Constants.RESULT);
List<Map<String, Object>> contentList = (List<Map<String, Object>>) contentResult
.get(Constants.CONTENT);
if (!CollectionUtils.isEmpty(contentList)) {
ObjectMapper ob = new ObjectMapper();
CollectionType listType = ob.getTypeFactory().constructCollectionType(ArrayList.class,
SunbirdApiBatchResp.class);
return ob.readValue(ob.writeValueAsString(contentList.get(0).get(Constants.BATCHES)), listType);
}
}
} catch (Exception e) {
logger.error(e);
logger.error("Failed to get batch details. Exception: ", e);
}
return Collections.emptyList();
}
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/org/sunbird/common/service/ContentService.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
package org.sunbird.common.service;

import java.util.List;
import java.util.Map;

import org.sunbird.common.model.SunbirdApiResp;
import org.sunbird.common.model.SunbirdApiUserCourseListResp;
Expand All @@ -20,5 +21,7 @@ public interface ContentService {
public SunbirdApiUserCourseListResp getUserCourseListResponse(String authToken, String userId);

public SunbirdApiResp getQuestionListDetails(List<String> questionIdList);

public Map<String, Object> searchLiveContent(String contentId);
}

23 changes: 23 additions & 0 deletions src/main/java/org/sunbird/common/service/ContentServiceImpl.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.sunbird.common.service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -173,4 +174,26 @@ public SunbirdApiResp getQuestionListDetails(List<String> questionIdList) {

return null;
}

public Map<String, Object> searchLiveContent(String contentId) {
Map<String, Object> response = null;
HashMap<String, String> headerValues = new HashMap<>();
headerValues.put(Constants.CONTENT_TYPE, Constants.APPLICATION_JSON);
Map<String, Object> filters = new HashMap<>();
filters.put(Constants.PRIMARY_CATEGORY, Arrays.asList(Constants.COURSE, Constants.PROGRAM));
filters.put(Constants.STATUS, Arrays.asList(Constants.LIVE));
filters.put(Constants.IDENTIFIER, contentId);
Map<String, Object> contentRequestValue = new HashMap<>();
contentRequestValue.put(Constants.FILTERS, filters);
contentRequestValue.put(Constants.FIELDS,
Arrays.asList(Constants.IDENTIFIER, Constants.NAME, Constants.PRIMARY_CATEGORY, Constants.BATCHES));
Map<String, Object> contentRequest = new HashMap<>();
contentRequest.put(Constants.REQUEST, contentRequestValue);
response = outboundRequestHandlerService.fetchResultUsingPost(
serverConfig.getKmBaseHost() + serverConfig.getKmBaseContentSearch(), contentRequest, headerValues);
if (null != response && Constants.OK.equalsIgnoreCase((String) response.get(Constants.RESPONSE_CODE))) {
return response;
}
return null;
}
}
1 change: 1 addition & 0 deletions src/main/java/org/sunbird/common/util/Constants.java
Original file line number Diff line number Diff line change
Expand Up @@ -519,6 +519,7 @@ public class Constants {
public static final String INITIATED_CAPITAL = "INITIATED";
public static final List<String> COURSE_REMINDER_EMAIL_FIELDS = Arrays.asList(RATINGS_USER_ID, BATCH_ID_COLUMN,
COURSE_ID_COLUMN, COMPLETION_PERCENTAGE_COLUMN, LAST_ACCESS_TIME);
public static final String BATCHES = "batches";

private Constants() {
throw new IllegalStateException("Utility class");
Expand Down

0 comments on commit e05ba57

Please sign in to comment.