From e05ba573c82de8c8ba903ea310de0905ac7913b8 Mon Sep 17 00:00:00 2001 From: karthik-tarento Date: Thu, 13 Oct 2022 17:42:07 +0530 Subject: [PATCH] Fix for user auto enrollment --- .../service/CohortsServiceImpl.java | 34 +++++++++++++++---- .../common/service/ContentService.java | 3 ++ .../common/service/ContentServiceImpl.java | 23 +++++++++++++ .../org/sunbird/common/util/Constants.java | 1 + 4 files changed, 54 insertions(+), 7 deletions(-) diff --git a/src/main/java/org/sunbird/assessment/service/CohortsServiceImpl.java b/src/main/java/org/sunbird/assessment/service/CohortsServiceImpl.java index 3bb4f35f1..fbb0a5c96 100644 --- a/src/main/java/org/sunbird/assessment/service/CohortsServiceImpl.java +++ b/src/main/java/org/sunbird/assessment/service/CohortsServiceImpl.java @@ -15,10 +15,19 @@ 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; @@ -26,6 +35,9 @@ 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 { @@ -139,7 +151,7 @@ public List getActiveUsers(String xAuthUser, String rootOrg, String @Override public Response autoEnrollmentInCourse(String authUserToken, String rootOrg, String contentId, String userUUID) throws Exception { - List batchResp = fetchBatchsDetails(contentId); + List batchResp = fetchBatchDetails(contentId); List batchIdList = null; if (!CollectionUtils.isEmpty(batchResp)) batchIdList = batchResp.stream().map(SunbirdApiBatchResp::getBatchId).collect(Collectors.toList()); @@ -303,14 +315,22 @@ private List fetchBatchIdDetails(String contentId) { return Collections.emptyList(); } - private List fetchBatchsDetails(String contentId) { + private List fetchBatchDetails(String contentId) { try { - SunbirdApiResp contentHierarchy = contentService.getHeirarchyResponse(contentId); - if (contentHierarchy != null && "successful".equalsIgnoreCase(contentHierarchy.getParams().getStatus())) { - return contentHierarchy.getResult().getContent().getBatches(); + Map contentResponse = contentService.searchLiveContent(contentId); + if (!ObjectUtils.isEmpty(contentResponse)) { + Map contentResult = (Map) contentResponse.get(Constants.RESULT); + List> contentList = (List>) 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(); } diff --git a/src/main/java/org/sunbird/common/service/ContentService.java b/src/main/java/org/sunbird/common/service/ContentService.java index 534ba11b0..287e60395 100644 --- a/src/main/java/org/sunbird/common/service/ContentService.java +++ b/src/main/java/org/sunbird/common/service/ContentService.java @@ -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; @@ -20,5 +21,7 @@ public interface ContentService { public SunbirdApiUserCourseListResp getUserCourseListResponse(String authToken, String userId); public SunbirdApiResp getQuestionListDetails(List questionIdList); + + public Map searchLiveContent(String contentId); } diff --git a/src/main/java/org/sunbird/common/service/ContentServiceImpl.java b/src/main/java/org/sunbird/common/service/ContentServiceImpl.java index 022284dd8..f22af0f2c 100644 --- a/src/main/java/org/sunbird/common/service/ContentServiceImpl.java +++ b/src/main/java/org/sunbird/common/service/ContentServiceImpl.java @@ -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; @@ -173,4 +174,26 @@ public SunbirdApiResp getQuestionListDetails(List questionIdList) { return null; } + + public Map searchLiveContent(String contentId) { + Map response = null; + HashMap headerValues = new HashMap<>(); + headerValues.put(Constants.CONTENT_TYPE, Constants.APPLICATION_JSON); + Map 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 contentRequestValue = new HashMap<>(); + contentRequestValue.put(Constants.FILTERS, filters); + contentRequestValue.put(Constants.FIELDS, + Arrays.asList(Constants.IDENTIFIER, Constants.NAME, Constants.PRIMARY_CATEGORY, Constants.BATCHES)); + Map 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; + } } diff --git a/src/main/java/org/sunbird/common/util/Constants.java b/src/main/java/org/sunbird/common/util/Constants.java index eccc7eba7..2c89041df 100644 --- a/src/main/java/org/sunbird/common/util/Constants.java +++ b/src/main/java/org/sunbird/common/util/Constants.java @@ -519,6 +519,7 @@ public class Constants { public static final String INITIATED_CAPITAL = "INITIATED"; public static final List 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");