diff --git a/service/src/main/java/skills/skillLoading/SkillsLoader.groovy b/service/src/main/java/skills/skillLoading/SkillsLoader.groovy index 22e8d52ed1..ba18ac6d47 100644 --- a/service/src/main/java/skills/skillLoading/SkillsLoader.groovy +++ b/service/src/main/java/skills/skillLoading/SkillsLoader.groovy @@ -1235,7 +1235,6 @@ class SkillsLoader { }) } - Date achievedOn = achievedLevelRepository.getAchievedDateByUserIdAndProjectIdAndSkillId(userId, skillDef.projectId, skillDef.skillId) Date expirationDate Date mostRecentlyPerformedOn Date lastExpirationDate @@ -1244,7 +1243,7 @@ class SkillsLoader { if (skillDefAndUserPoints.attributes && skillDefAndUserPoints.attributes.type == SkillAttributesDef.SkillAttributesType.AchievementExpiration) { ExpirationAttrs expirationAttrs = skillAttributeService.convertAttrs(skillDefAndUserPoints.attributes, ExpirationAttrs) expirationDate = expirationAttrs.nextExpirationDate - if(!achievedOn) { + if(!skillDefAndUserPoints.achievedOn) { lastExpirationDate = skillDefAndUserPoints.expiredOn } isMotivationalSkill = expirationAttrs?.expirationType == ExpirationAttrs.DAILY @@ -1273,7 +1272,7 @@ class SkillsLoader { pointIncrementInterval: skillDef.pointIncrementInterval, maxOccurrencesWithinIncrementInterval: skillDef.numMaxOccurrencesIncrementInterval, totalPoints: skillDef.totalPoints, - achievedOn: achievedOn, + achievedOn: skillDefAndUserPoints.achievedOn, dependencyInfo: skillDefAndUserPoints.dependencyInfo, badgeDependencyInfo: badgeDependencySummary, selfReporting: loadSelfReportingFromApproval(skillDefAndUserPoints), diff --git a/service/src/main/java/skills/skillLoading/SubjectDataLoader.groovy b/service/src/main/java/skills/skillLoading/SubjectDataLoader.groovy index 988804ea3b..dc43dbc3a2 100644 --- a/service/src/main/java/skills/skillLoading/SubjectDataLoader.groovy +++ b/service/src/main/java/skills/skillLoading/SubjectDataLoader.groovy @@ -32,6 +32,7 @@ import skills.storage.repos.QuizToSkillDefRepo import skills.storage.repos.SkillApprovalRepo import skills.storage.repos.SkillDefRepo import skills.storage.repos.SkillDefWithExtraRepo +import skills.storage.repos.UserAchievedLevelRepo import skills.storage.repos.UserPerformedSkillRepo import skills.storage.repos.UserPointsRepo @@ -70,6 +71,9 @@ class SubjectDataLoader { @Autowired ExpiredUserAchievementRepo expiredUserAchievementRepo + @Autowired + UserAchievedLevelRepo achievedLevelRepository + static class SkillsAndPoints { SkillDef skillDef int points @@ -90,6 +94,7 @@ class SubjectDataLoader { List tags = [] SkillAttributesDef attributes Date expiredOn + Date achievedOn } static class SkillsData { @@ -145,6 +150,7 @@ class SubjectDataLoader { skillsAndPoints = handleBadges(projectId, skillsAndPoints) skillsAndPoints = handleSkillTags(projectId, skillsAndPoints) skillsAndPoints = handleSkillQuizInfo(projectId, skillsAndPoints) + skillsAndPoints = handleAchievements(projectId, userId, skillsAndPoints) skillsAndPoints = handleSkillExpirations(projectId, userId, skillsAndPoints) new SkillsData(childrenWithPoints: skillsAndPoints) @@ -178,9 +184,26 @@ class SubjectDataLoader { return skillsAndPoints; } - private List handleSkillExpirations(String projectId, String userId, List skillsAndPoints) { + private List handleAchievements(String projectId, String userId, List skillsAndPoints) { if(projectId) { List skillIds = collectSkillIds(skillsAndPoints) + List achievedSkills = achievedLevelRepository.getAchievedDateByUserIdAndProjectIdAndSkillBatch(userId, projectId, skillIds) + if (achievedSkills) { + skillsAndPoints.each { it -> + List achievements = achievedSkills.findAll{skill -> skill.skillId == it.skillDef.skillId} + if(achievements) { + achievements?.sort { skill -> skill.achievedOn } + it.achievedOn = achievements.first()?.achievedOn + } + } + } + } + return skillsAndPoints + } + + private List handleSkillExpirations(String projectId, String userId, List skillsAndPoints) { + if(projectId) { + List skillIds = collectUnachievedSkillIds(skillsAndPoints) def expiredSkills = expiredUserAchievementRepo.findMostRecentExpirationForAllSkills(projectId, userId, skillIds) if (expiredSkills) { skillsAndPoints.each { it -> @@ -254,6 +277,22 @@ class SubjectDataLoader { return skillIds } + private List collectUnachievedSkillIds(List skillsAndPoints) { + List skillIds = [] + skillsAndPoints.forEach { it -> + if(!it.achievedOn) { + if (it.skillDef.type == SkillDef.ContainerType.SkillsGroup) { + if (it.children) { + skillIds.addAll(it.children.findAll{ child -> !child.achievedOn }.collect { child -> child.skillDef.skillId }) + } + } else if (it.skillDef.type == SkillDef.ContainerType.Skill) { + skillIds.add(it.skillDef.skillId) + } + } + } + return skillIds + } + private List handleGroupDescriptions(String projectId, List skillsAndPoints, List relationshipTypes) { if (relationshipTypes.containsAll([SkillRelDef.RelationshipType.SkillsGroupRequirement, SkillRelDef.RelationshipType.GroupSkillToSubject])) { List groups = skillsAndPoints.findAll({ it.skillDef.type == SkillDef.ContainerType.SkillsGroup }) diff --git a/service/src/main/java/skills/storage/repos/UserAchievedLevelRepo.groovy b/service/src/main/java/skills/storage/repos/UserAchievedLevelRepo.groovy index fe369728c6..498ebb0707 100644 --- a/service/src/main/java/skills/storage/repos/UserAchievedLevelRepo.groovy +++ b/service/src/main/java/skills/storage/repos/UserAchievedLevelRepo.groovy @@ -48,6 +48,10 @@ interface UserAchievedLevelRepo extends CrudRepository @Query('''select ua.achievedOn from UserAchievement ua where ua.userId= ?1 and ua.projectId=?2 and ua.skillId=?3''') Date getAchievedDateByUserIdAndProjectIdAndSkillId(String userId, String projectId, String skillId) + @Nullable + @Query('''select ua from UserAchievement ua where ua.userId = ?1 and ua.projectId = ?2 and ua.skillId in ?3''') + List getAchievedDateByUserIdAndProjectIdAndSkillBatch(String userId, String projectId, List skillId) + @Query('''select ua from UserAchievement ua where ua.userId = ?1 and ua.projectId in ?2''') List findAllByUserAndProjectIds(String userId, Collection projectId)