Skip to content

Commit

Permalink
Fix for course progress
Browse files Browse the repository at this point in the history
  • Loading branch information
karthik-tarento committed Feb 21, 2022
1 parent c0fec6f commit cb25ef3
Show file tree
Hide file tree
Showing 8 changed files with 53 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -64,5 +64,5 @@ public interface UserCoursesDao {
List<String> getBatchParticipants(RequestContext requestContext, String batchId, boolean active);


List<Map<String, Object>> listEnrolments(RequestContext requestContext, String userId);
List<Map<String, Object>> listEnrolments(RequestContext requestContext, String userId, List<String> courseIdList);
}
Original file line number Diff line number Diff line change
Expand Up @@ -135,9 +135,12 @@ public List<String> getBatchParticipants(RequestContext requestContext, String b
}

@Override
public List<Map<String, Object>> listEnrolments(RequestContext requestContext, String userId) {
public List<Map<String, Object>> listEnrolments(RequestContext requestContext, String userId, List<String> courseIdList) {
Map<String, Object> primaryKey = new HashMap<>();
primaryKey.put(JsonKey.USER_ID, userId);
if(!CollectionUtils.isEmpty(courseIdList)){
primaryKey.put(JsonKey.COURSE_ID_KEY, courseIdList);
}
Response response = cassandraOperation.getRecordByIdentifier(requestContext, KEYSPACE_NAME, USER_ENROLMENTS, primaryKey, null);
List<Map<String, Object>> userCoursesList = (List<Map<String, Object>>) response.get(JsonKey.RESPONSE);
if (CollectionUtils.isEmpty(userCoursesList)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ public void listUserCoursesNullResponse() {
when(cassandraOperation.getRecordByIdentifier(
Mockito.any(), Mockito.anyString(), Mockito.anyString(), Mockito.anyMap(), Mockito.any()))
.thenReturn(readResponse);
List<Map<String, Object>> response = userCoursesDao.listEnrolments(null, JsonKey.USER_ID);
List<Map<String, Object>> response = userCoursesDao.listEnrolments(null, JsonKey.USER_ID, null);
Assert.assertEquals(null, response);
}

Expand All @@ -108,7 +108,7 @@ public void listUserCoursesSuccess() {
when(cassandraOperation.getRecordByIdentifier(
Mockito.any(), Mockito.anyString(), Mockito.anyString(), Mockito.anyMap(), Mockito.any()))
.thenReturn(readResponse);
List<Map<String, Object>> response = userCoursesDao.listEnrolments(null, JsonKey.USER_ID);
List<Map<String, Object>> response = userCoursesDao.listEnrolments(null, JsonKey.USER_ID, null);
Assert.assertNotEquals(null, response);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -255,8 +255,12 @@ class ContentConsumptionActor @Inject() extends BaseEnrolmentActor {
val inputStatus = inputContent.getOrDefault(JsonKey.STATUS, 0.asInstanceOf[AnyRef]).asInstanceOf[Number].intValue()
val updatedContent = new java.util.HashMap[String, AnyRef]()
updatedContent.putAll(inputContent)
val parsedMap = new java.util.HashMap[String, AnyRef]()
jsonFields.flatMap(f => if(inputContent.containsKey(f)) Some(parsedMap.put(f,mapper.writeValueAsString(inputContent.get(f)))) else None)
val parsedMap = new java.util.HashMap[String, AnyRef]()
jsonFields.foreach(field =>
if(inputContent.containsKey(field)) {
parsedMap.put(field, mapper.writeValueAsString(inputContent.get(field)))
}
)
updatedContent.putAll(parsedMap)
val inputCompletedTime = parseDate(inputContent.getOrDefault(JsonKey.LAST_COMPLETED_TIME, "").asInstanceOf[String])
val inputAccessTime = parseDate(inputContent.getOrDefault(JsonKey.LAST_ACCESS_TIME, "").asInstanceOf[String])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,14 +109,15 @@ class CourseEnrolmentActor @Inject()(@Named("course-batch-notification-actor") c

def list(request: Request): Unit = {
val userId = request.get(JsonKey.USER_ID).asInstanceOf[String]
val courseIdList = request.get(JsonKey.COURSE_IDS).asInstanceOf[java.util.List[String]]
logger.info(request.getRequestContext,"CourseEnrolmentActor :: list :: UserId = " + userId)
val response = if (isCacheEnabled && request.getContext.get("cache").asInstanceOf[Boolean])
getCachedEnrolmentList(userId, () => getEnrolmentList(request, userId)) else getEnrolmentList(request, userId)
getCachedEnrolmentList(userId, () => getEnrolmentList(request, userId, courseIdList)) else getEnrolmentList(request, userId, courseIdList)
sender().tell(response, self)
}

def getActiveEnrollments(userId: String, requestContext: RequestContext): java.util.List[java.util.Map[String, AnyRef]] = {
val enrolments: java.util.List[java.util.Map[String, AnyRef]] = userCoursesDao.listEnrolments(requestContext, userId)
def getActiveEnrollments(userId: String, courseIdList: java.util.List[String], requestContext: RequestContext): java.util.List[java.util.Map[String, AnyRef]] = {
val enrolments: java.util.List[java.util.Map[String, AnyRef]] = userCoursesDao.listEnrolments(requestContext, userId, courseIdList);
if (CollectionUtils.isNotEmpty(enrolments))
enrolments.filter(e => e.getOrDefault(JsonKey.ACTIVE, false.asInstanceOf[AnyRef]).asInstanceOf[Boolean]).toList.asJava
else
Expand Down Expand Up @@ -303,9 +304,9 @@ class CourseEnrolmentActor @Inject()(@Named("course-batch-notification-actor") c
}
}

def getEnrolmentList(request: Request, userId: String): Response = {
def getEnrolmentList(request: Request, userId: String, courseIdList: java.util.List[String]): Response = {
logger.info(request.getRequestContext,"CourseEnrolmentActor :: getCachedEnrolmentList :: fetching data from cassandra with userId " + userId)
val activeEnrolments: java.util.List[java.util.Map[String, AnyRef]] = getActiveEnrollments( userId, request.getRequestContext)
val activeEnrolments: java.util.List[java.util.Map[String, AnyRef]] = getActiveEnrollments( userId, courseIdList, request.getRequestContext)
val enrolments: java.util.List[java.util.Map[String, AnyRef]] = {
if (CollectionUtils.isNotEmpty(activeEnrolments)) {
val courseIds: java.util.List[String] = activeEnrolments.map(e => e.getOrDefault(JsonKey.COURSE_ID, "").asInstanceOf[String]).distinct.filter(id => StringUtils.isNotBlank(id)).toList.asJava
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class CourseEnrolmentTest extends FlatSpec with Matchers with MockFactory {
val response = callActor(getEnrolRequest(), Props(new CourseEnrolmentActor(null)(cacheUtil).setDao(courseDao, userDao, groupDao)))
assert("Success".equalsIgnoreCase(response.get("response").asInstanceOf[String]))
}

"On invalid course batch" should "return client error" in {
(courseDao.readById(_: String, _: String,_: RequestContext)).expects(*,*,*).returns(null)
(userDao.read(_: RequestContext, _: String,_: String,_: String)).expects(*,*,*,*).returns(null)
Expand Down Expand Up @@ -149,8 +149,8 @@ class CourseEnrolmentTest extends FlatSpec with Matchers with MockFactory {
userCourse.setActive(true)
userCourse.setCourseId("do_11305605610466508811")
userCourse.setBatchId("0130598559365038081")
(userDao.listEnrolments(_: RequestContext, _: String)).expects(*,*).returns(getEnrolmentLists())
val response = callActor(getListEnrolRequest(), Props(new CourseEnrolmentActor(null)(cacheUtil).setDao(courseDao, userDao, groupDao)))
(userDao.listEnrolments(_: RequestContext, _: String, null: java.util.List[String])).expects(*,*).returns(getEnrolmentLists())
val response = callActor(getListEnrolRequest(), Props(new CourseEnrolmentActor(null)(cacheUtil).setDao(courseDao, userDao, groupDao)))
println(response)
assert(null != response)
}
Expand All @@ -160,11 +160,11 @@ class CourseEnrolmentTest extends FlatSpec with Matchers with MockFactory {
userCourse.setActive(true)
userCourse.setCourseId("do_11305984881537024012255")
userCourse.setBatchId("0130598559365038081")

val enrolmentsString = "[{\"dateTime\":1594219912979,\"lastReadContentStatus\":2,\"completionpercentage\":100,\"enrolledDate\":\"1594219912979\",\"addedBy\":\"6cf06951-55fe-2a81-4e37-4475428ece80\",\"delta\":null,\"active\":true,\"contentstatus\":{\"do_11305605610466508811\":2},\"batchId\":\"0130598559365038081\",\"userId\":\"95e4942d-cbe8-477d-aebd-ad8e6de4bfc8\",\"certificates\":[],\"completedOn\":1595422618082,\"grade\":null,\"progress\":1,\"lastReadContentId\":\"do_11305605610466508811\",\"courseId\":\"do_11305984881537024012255\",\"status\":2},{\"dateTime\":1594219912979,\"completionpercentage\":0,\"enrolledDate\":\"1594219912978\",\"addedBy\":\"6cf06951-55fe-2a81-4e37-4475428ece80\",\"delta\":null,\"active\":true,\"batchId\":\"0130598559365038083\",\"userId\":\"95e4942d-cbe8-477d-aebd-ad8e6de4bfc8\",\"certificates\":[],\"grade\":null,\"progress\":0,\"lastReadContentId\":\"do_11305605610466508811\",\"courseId\":\"do_11305984881537024012255\",\"status\":0}]"
val enrolmentsList = mapper.readValue(enrolmentsString, classOf[java.util.List[java.util.Map[String, AnyRef]]])
(userDao.listEnrolments(_: RequestContext, _: String)).expects(*,*).returns(enrolmentsList)

(userDao.listEnrolments(_: RequestContext, _: String, null: java.util.List[String])).expects(*,*).returns(enrolmentsList)
val response = callActor(getListEnrolRequest(), Props(new CourseEnrolmentActor(null)(cacheUtil).setDao(courseDao, userDao, groupDao)))
println(response)
assert(null != response)
Expand Down Expand Up @@ -192,7 +192,7 @@ class CourseEnrolmentTest extends FlatSpec with Matchers with MockFactory {
userCourse.setCourseId("do_11305605610466508811")
userCourse.setBatchId("0130598559365038081")
(cacheUtil.get(_: String, _: String => String, _: Int)).expects(*, *, *).returns(null)
(userDao.listEnrolments(_: RequestContext, _: String)).expects(*, *).returns(getEnrolmentLists())
(userDao.listEnrolments(_: RequestContext, _: String, null: java.util.List[String])).expects(*, *).returns(getEnrolmentLists())
(cacheUtil.set(_: String, _: String, _: Int)).expects(*, *, *).once()
val request = getListEnrolRequest()
request.getContext.put("cache", true.asInstanceOf[AnyRef])
Expand Down Expand Up @@ -303,7 +303,7 @@ class CourseEnrolmentTest extends FlatSpec with Matchers with MockFactory {
val responseString = "{\"id\":null,\"ver\":null,\"ts\":null,\"params\":null,\"responseCode\":\"OK\",\"result\":{\"response\":[{\"agg\":{\"completedCount\":1},\"user_id\":\"95e4942d-cbe8-477d-aebd-ad8e6de4bfc8\",\"activity_type\":\"Course\",\"agg_last_updated\":{\"completedCount\":1595506598142},\"activity_id\":\"do_11305984881537024012255\",\"context_id\":\"cb:0130598559365038081\"}]}}"
JsonUtil.deserialize(responseString, classOf[Response])
}

private def getBatchWithValidEnrolmentEndDateAndBatchEndDate(): CourseBatch = {
val startDate = sd.format(simpleDateFormat.parse(LocalDateTime.now().minusDays(3).format(dateTimeFormatter)))
val enrolmentEndDate = sd.format(simpleDateFormat.parse(LocalDateTime.now().plusDays(7).format(dateTimeFormatter)))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -232,4 +232,29 @@ public CompletionStage<Result> getParticipantsForFixedBatch(Http.Request httpReq
getAllRequestHeaders(httpRequest),
httpRequest);
}

public CompletionStage<Result> adminGetUserEnrolledCourses(Http.Request httpRequest) {
return handleRequest(
courseEnrolmentActor, "listEnrol",
httpRequest.body().asJson(),
(req) -> {
Request request = (Request) req;
Map<String, String[]> queryParams = new HashMap<>(httpRequest.queryString());
if(queryParams.containsKey("fields")) {
Set<String> fields = new HashSet<>(Arrays.asList(queryParams.get("fields")[0].split(",")));
fields.addAll(Arrays.asList(JsonKey.NAME, JsonKey.DESCRIPTION, JsonKey.LEAF_NODE_COUNT, JsonKey.APP_ICON));
queryParams.put("fields", fields.toArray(new String[0]));
}
request
.getContext()
.put(JsonKey.URL_QUERY_STRING, getQueryString(queryParams));
request
.getContext()
.put(JsonKey.BATCH_DETAILS, httpRequest.queryString().get(JsonKey.BATCH_DETAILS));

return null;
},
getAllRequestHeaders((httpRequest)),
httpRequest);
}
}
1 change: 1 addition & 0 deletions service/conf/routes
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ DELETE /v1/cache/clear/:mapName @controllers.cache.CacheController.clearCache(ma
GET /v1/user/courses/list/:uid @controllers.courseenrollment.CourseEnrollmentController.getEnrolledCourses(uid:String, request: play.mvc.Http.Request)
GET /private/v1/user/courses/list/:uid @controllers.courseenrollment.CourseEnrollmentController.privateGetEnrolledCourses(uid:String, request: play.mvc.Http.Request)
POST /v2/user/courses/list @controllers.courseenrollment.CourseEnrollmentController.getUserEnrolledCourses(request: play.mvc.Http.Request)
POST /v2/user/courses/admin/list @controllers.courseenrollment.CourseEnrollmentController.adminGetUserEnrolledCourses(request: play.mvc.Http.Request)
POST /private/v2/user/courses/list @controllers.courseenrollment.CourseEnrollmentController.privateGetUserEnrolledCourses(request: play.mvc.Http.Request)
POST /v1/course/enroll @controllers.courseenrollment.CourseEnrollmentController.enrollCourse(request: play.mvc.Http.Request)
POST /v1/course/unenroll @controllers.courseenrollment.CourseEnrollmentController.unenrollCourse(request: play.mvc.Http.Request)
Expand Down

0 comments on commit cb25ef3

Please sign in to comment.