From ab49a6db5f8b43a297b3ba450259b5d28f79eef4 Mon Sep 17 00:00:00 2001 From: Sahil-tarento <140611066+Sahil-tarento@users.noreply.github.com> Date: Fri, 28 Jun 2024 11:34:49 +0530 Subject: [PATCH] 4.8.15 dev v3 (#629) * Updating for calendar API (#619) * 4.8.15 dev competencys (#627) * Adding API for orgCompetencies * Do code formatting --- .../common/util/CbExtServerProperties.java | 22 +++++ .../org/sunbird/common/util/Constants.java | 4 + .../controller/SearchByController.java | 6 ++ .../searchby/service/SearchByService.java | 86 ++++++++++++++++--- src/main/resources/application.properties | 6 +- 5 files changed, 110 insertions(+), 14 deletions(-) diff --git a/src/main/java/org/sunbird/common/util/CbExtServerProperties.java b/src/main/java/org/sunbird/common/util/CbExtServerProperties.java index 614dd9b87..31b51f73c 100644 --- a/src/main/java/org/sunbird/common/util/CbExtServerProperties.java +++ b/src/main/java/org/sunbird/common/util/CbExtServerProperties.java @@ -762,6 +762,12 @@ public void setRedisWheeboxKey(String redisWheeboxKey) { @Value("#{${insights.mapping.key}}") private Map insightsMappingKey; + @Value("${sb.search.service.host}") + private String sbSearchServiceHost; + + @Value("${sb.composite.v4.search}") + private String sbCompositeV4Search; + public boolean qListFromCacheEnabled() { return qListFromCacheEnabled; } @@ -2696,4 +2702,20 @@ public Map getInsightsMappingKey() { public void setInsightsMappingKey(Map insightsMappingKey) { this.insightsMappingKey = insightsMappingKey; } + + public String getSbSearchServiceHost() { + return sbSearchServiceHost; + } + + public void setSbSearchServiceHost(String sbSearchServiceHost) { + this.sbSearchServiceHost = sbSearchServiceHost; + } + + public String getSbCompositeV4Search() { + return sbCompositeV4Search; + } + + public void setSbCompositeV4Search(String sbCompositeV4Search) { + this.sbCompositeV4Search = sbCompositeV4Search; + } } \ No newline at end of file diff --git a/src/main/java/org/sunbird/common/util/Constants.java b/src/main/java/org/sunbird/common/util/Constants.java index 226e0f497..a65541647 100644 --- a/src/main/java/org/sunbird/common/util/Constants.java +++ b/src/main/java/org/sunbird/common/util/Constants.java @@ -1091,6 +1091,10 @@ public class Constants { public static final String SEQUENCE_NO = "seqno"; public static final String API_EXPLORE_COURSE_UPDATE = "api.explore.course.update"; public static final String API_EXPLORE_COURSE_DELETE = "api.explore.course.delete"; + public static final String COMPETENCIES_ORG_COURSES_REDIS_KEY = "dashboard_core_competencies_by_user_org"; + public static final String ORG_COMPETENCY_SEARCH_API = "api.org.competency.search"; + public static final String SEARCH_COMPETENCY_THEMES = "competencyTheme"; + public static final String SEARCH_COMPETENCY_SUB_THEMES = "competencySubTheme"; public static final String MCQ_MCA_W = "mcq-mca-w"; public static final String MCQ_SCA_TF = "mcq-sca-tf"; diff --git a/src/main/java/org/sunbird/searchby/controller/SearchByController.java b/src/main/java/org/sunbird/searchby/controller/SearchByController.java index 38546cada..cf8f82588 100644 --- a/src/main/java/org/sunbird/searchby/controller/SearchByController.java +++ b/src/main/java/org/sunbird/searchby/controller/SearchByController.java @@ -4,6 +4,7 @@ import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestHeader; import org.springframework.web.bind.annotation.RestController; import org.sunbird.common.model.FracApiResponse; @@ -33,4 +34,9 @@ public ResponseEntity listPositions(@RequestHeader(Constants.X_AUTH_TOKEN) St FracApiResponse response = searchByService.listPositions(userToken); return new ResponseEntity<>(response, HttpStatus.valueOf(response.getStatusInfo().getStatusCode())); } + + @GetMapping("/v1/competenciesByOrg/{orgId}") + public ResponseEntity listOfCompetenciesByOrg(@PathVariable String orgId, @RequestHeader(Constants.X_AUTH_TOKEN) String userToken) { + return new ResponseEntity<>(searchByService.listCompetenciesByOrg(orgId, userToken), HttpStatus.OK); + } } diff --git a/src/main/java/org/sunbird/searchby/service/SearchByService.java b/src/main/java/org/sunbird/searchby/service/SearchByService.java index 0235acd65..f8217fa7f 100644 --- a/src/main/java/org/sunbird/searchby/service/SearchByService.java +++ b/src/main/java/org/sunbird/searchby/service/SearchByService.java @@ -1,13 +1,9 @@ package org.sunbird.searchby.service; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Collection; -import java.util.HashMap; -import java.util.Iterator; -import java.util.List; -import java.util.Map; - +import com.fasterxml.jackson.core.type.TypeReference; +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.ObjectMapper; +import org.apache.commons.collections4.MapUtils; import org.apache.commons.lang.StringUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpStatus; @@ -16,18 +12,19 @@ import org.springframework.util.ObjectUtils; import org.sunbird.cache.RedisCacheMgr; import org.sunbird.common.model.FracApiResponse; +import org.sunbird.common.model.SBApiResponse; import org.sunbird.common.service.OutboundRequestHandlerServiceImpl; +import org.sunbird.common.util.AccessTokenValidator; import org.sunbird.common.util.CbExtServerProperties; import org.sunbird.common.util.Constants; +import org.sunbird.common.util.ProjectUtil; import org.sunbird.core.logger.CbExtLogger; import org.sunbird.searchby.model.CompetencyInfo; import org.sunbird.searchby.model.FracCommonInfo; import org.sunbird.searchby.model.ProviderInfo; import org.sunbird.workallocation.model.FracStatusInfo; -import com.fasterxml.jackson.core.type.TypeReference; -import com.fasterxml.jackson.databind.JsonNode; -import com.fasterxml.jackson.databind.ObjectMapper; +import java.util.*; @Service @SuppressWarnings("unchecked") @@ -40,13 +37,16 @@ public class SearchByService { @Autowired RedisCacheMgr redisCacheMgr; - + @Autowired ObjectMapper mapper; @Autowired OutboundRequestHandlerServiceImpl outboundRequestHandlerService; + @Autowired + AccessTokenValidator accessTokenValidator; + public Collection getCompetencyDetails(String authUserToken) throws Exception { String strCompetencyMap = redisCacheMgr.getCache(Constants.COMPETENCY_CACHE_NAME); Map competencyMap = new HashMap<>(); @@ -183,7 +183,7 @@ private Map updateCompetencyDetails(String authUserToken Map fracSearchRes = outboundRequestHandlerService.fetchResultUsingPost( cbExtServerProperties.getFracHost() + cbExtServerProperties.getFracSearchPath(), reqBody, headers); - + List> fracResponseList = (List>) fracSearchRes .get(Constants.RESPONSE_DATA); @@ -408,4 +408,64 @@ private List getMasterPositionList(List positionNameList } return positionList; } + + public SBApiResponse listCompetenciesByOrg(String orgId, String userToken) { + SBApiResponse response = ProjectUtil.createDefaultResponse(Constants.ORG_COMPETENCY_SEARCH_API); + try { + String userId = validateAuthTokenAndFetchUserId(userToken); + if (org.apache.commons.lang3.StringUtils.isBlank(userId)) { + response.getParams().setStatus(Constants.FAILED); + response.getParams().setErrmsg(Constants.USER_ID_DOESNT_EXIST); + response.setResponseCode(HttpStatus.BAD_REQUEST); + return response; + } + List courseIdList = redisCacheMgr.hget(Constants.COMPETENCIES_ORG_COURSES_REDIS_KEY, cbExtServerProperties.getRedisInsightIndex(), orgId); + if (org.apache.commons.collections4.CollectionUtils.isNotEmpty(courseIdList)) { + Map competencyMap = listCompetencyDetails(Arrays.asList(courseIdList.get(0).split(","))); + logger.info("Initializing/Refreshing the Cache Value for Key : " + Constants.COMPETENCY_CACHE_NAME); + if (MapUtils.isNotEmpty(competencyMap)) { + response.setResult((Map) competencyMap.get(Constants.RESULT)); + } else { + response.getParams().setStatus(Constants.FAILED); + response.getParams().setErrmsg("Competency map is empty"); + response.setResponseCode(HttpStatus.INTERNAL_SERVER_ERROR); + } + } else { + response.getParams().setStatus(Constants.FAILED); + response.getParams().setErrmsg("Issue while fetching the competency for org: " + orgId); + response.setResponseCode(HttpStatus.INTERNAL_SERVER_ERROR); + } + } catch (Exception e) { + logger.error("Issue while fetching the competency for org: " + orgId, e); + response.getParams().setStatus(Constants.FAILED); + response.getParams().setErrmsg(e.getMessage()); + response.setResponseCode(HttpStatus.INTERNAL_SERVER_ERROR); + } + return response; + } + + private Map listCompetencyDetails(List identifiers) throws Exception { + + HashMap reqBody = new HashMap<>(); + HashMap req = new HashMap<>(); + req.put(Constants.FACETS, Arrays.asList(Constants.COMPETENCIES_V5 + "." + Constants.COMPETENCY_AREA, + Constants.COMPETENCIES_V5 + "." + Constants.SEARCH_COMPETENCY_THEMES, + Constants.COMPETENCIES_V5 + "." + Constants.SEARCH_COMPETENCY_SUB_THEMES)); + Map filters = new HashMap<>(); + filters.put(Constants.IDENTIFIER, identifiers); + filters.put(Constants.STATUS, Arrays.asList(Constants.LIVE)); + req.put(Constants.FILTERS, filters); + req.put(Constants.LIMIT, 0); + reqBody.put(Constants.REQUEST, req); + + Map compositeSearchRes = outboundRequestHandlerService.fetchResultUsingPost( + cbExtServerProperties.getSbSearchServiceHost() + cbExtServerProperties.getSbCompositeV4Search(), reqBody, + null); + + return compositeSearchRes; + } + + private String validateAuthTokenAndFetchUserId(String authUserToken) { + return accessTokenValidator.fetchUserIdFromAccessToken(authUserToken); + } } diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index 30ad89ca2..1a6f8d339 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -426,4 +426,8 @@ user.bulk.upload.category.value=General,OBC,SC,ST insights.mapping.key={"PROVIDER_INSIGHT":"organisation","MDO_INSIGHT":"mdo"} mdo.insights.fields={"Total Users":"https://portal.karmayogi.nic.in/content-store/orgStore/01376822290813747263/1716545372950_Network.svg","Certificates":"https://portal.karmayogi.nic.in/content-store/orgStore/01376822290813747263/1715594032620_badges.svg","Enrolments":"https://portal.karmayogi.nic.in/content-store/orgStore/01376822290813747263/1716545463334_knowledge-resources.svg","Content Published":"https://portal.karmayogi.nic.in/content-store/orgStore/01376822290813747263/1716545105444_Program.svg","24hr Login":"https://portal.karmayogi.nic.in/content-store/orgStore/01376822290813747263/1716545326988_percent.svg"} mdo.insights.property.fields={"valueColor": "#FFFFFF","labelColor": "#FFFFFF","linebreak":"false","background": "#1B4CA1","iconColor": "#FFFFFF"} -mdo.insights.redis.key.mapping={"Total Users":"dashboard_user_count_by_user_org","Certificates":"dashboard_certificates_generated_count_by_user_org","Enrolments":"dashboard_enrolment_count_by_user_org","Content Published":"dashboard_live_course_count_by_course_org","24hr Login":"dashboard_login_percent_last_24_hrs_by_user_org"} \ No newline at end of file +mdo.insights.redis.key.mapping={"Total Users":"dashboard_user_count_by_user_org","Certificates":"dashboard_certificates_generated_count_by_user_org","Enrolments":"dashboard_enrolment_count_by_user_org","Content Published":"dashboard_live_course_count_by_course_org","24hr Login":"dashboard_login_percent_last_24_hrs_by_user_org"} + +#SearchService +sb.search.service.host=http://search-service:9000 +sb.composite.v4.search=/v4/search