Skip to content

Commit

Permalink
Merge pull request #894 from NationalSecurityAgency/t#890/delete_skill
Browse files Browse the repository at this point in the history
#890: when a skill is deleted award project and subject levels to any…
  • Loading branch information
rmmayo authored Nov 1, 2021
2 parents 49d953c + 27bc56b commit a19d42f
Show file tree
Hide file tree
Showing 6 changed files with 429 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,7 @@ class UserAchievementsAndPointsManagement {
SettingsService settingsService

@Transactional
void handleSkillRemoval(SkillDef skillDef) {
SkillDef subject = ruleSetDefGraphService.getParentSkill(skillDef)
void handleSkillRemoval(SkillDef skillDef, SkillDef subject) {
nativeQueriesRepo.decrementPointsForDeletedSkill(skillDef.projectId, skillDef.skillId, subject.skillId)
userPointsRepo.deleteByProjectIdAndSkillId(skillDef.projectId, skillDef.skillId)

Expand Down Expand Up @@ -125,15 +124,12 @@ class UserAchievementsAndPointsManagement {
}

@Transactional
void identifyAndAddLevelAchievements(String projectId, Integer skillRefId) {
List<SkillRelDef> parent = skillRelDefRepo.findAllByChildIdAndType(skillRefId, SkillRelDef.RelationshipType.RuleSetDefinition)
assert parent.size() == 1

SettingsResult settingsResult = settingsService.getProjectSetting(projectId, Settings.LEVEL_AS_POINTS.settingName)
void identifyAndAddLevelAchievements(SkillDef subject) {
SettingsResult settingsResult = settingsService.getProjectSetting(subject.projectId, Settings.LEVEL_AS_POINTS.settingName)
boolean pointsBased = settingsResult ? settingsResult.isEnabled() : false

nativeQueriesRepo.identifyAndAddProjectLevelAchievements(projectId, pointsBased)
nativeQueriesRepo.identifyAndAddSubjectLevelAchievements(projectId, parent[0].parent.skillId, pointsBased)
nativeQueriesRepo.identifyAndAddProjectLevelAchievements(subject.projectId, pointsBased)
nativeQueriesRepo.identifyAndAddSubjectLevelAchievements(subject.projectId, subject.skillId, pointsBased)
}

@Transactional
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,8 @@ class SkillsAdminService {
}

if (pointIncrementDelta < 0 || occurrencesDelta < 0) {
userPointsManagement.identifyAndAddLevelAchievements(savedSkill.projectId, savedSkill.id)
SkillDef parent = ruleSetDefGraphService.getParentSkill(savedSkill)
userPointsManagement.identifyAndAddLevelAchievements(parent)
}
}

Expand All @@ -237,20 +238,24 @@ class SkillsAdminService {
if (globalBadgesService.isSkillUsedInGlobalBadge(skillDefinition)) {
throw new SkillException("Skill with id [${skillId}] cannot be deleted as it is currently referenced by one or more global badges")
}

SkillDef parentSkill = ruleSetDefGraphService.getParentSkill(skillDefinition)

//we need to check to see if this skill belongs to any badges, if so we need to look for any users who now qualify
//for those badges
ruleSetDefinitionScoreUpdater.skillToBeRemoved(skillDefinition)
userPointsManagement.handleSkillRemoval(skillDefinition)

// this MUST happen before the skill was removed as sql relies on the skill to exist
userPointsManagement.handleSkillRemoval(skillDefinition, parentSkill)

//identify any badges that this skill belonged to and award the badge if any users now qualify for this badge
List<SkillDef> badges = findAllBadgesSkillBelongsTo(skillDefinition.skillId)

ruleSetDefGraphService.deleteSkillWithItsDescendants(skillDefinition)
log.debug("Deleted skill [{}]", skillDefinition.skillId)

// this MUST happen after the skill was removed as sql relies on the skill to be gone
userPointsManagement.identifyAndAddLevelAchievements(parentSkill)

badges?.each {
badgeAdminService.awardBadgeToUsersMeetingRequirements(it)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -644,11 +644,12 @@ class H2NativeRepo implements NativeQueriesRepo {

if (exists.isEmpty() || exists[0] < 1) {
Query insertAchievement = entityManager.createNativeQuery('''
INSERT INTO user_achievement (user_id, skill_id, level, points_when_achieved, project_id, notified)
VALUES (:userId, :skillId, :level, :userPoints, :projectId, 'false')
INSERT INTO user_achievement (user_id, skill_id, skill_ref_id, level, points_when_achieved, project_id, notified)
VALUES (:userId, :skillId, :skillRefId, :level, :userPoints, :projectId, 'false')
''')
insertAchievement.setParameter("userId", userId)
insertAchievement.setParameter("skillId", skillId)
insertAchievement.setParameter("skillRefId", id)
insertAchievement.setParameter("level", levelValue)
insertAchievement.setParameter("userPoints", userPoints.toInteger())
insertAchievement.setParameter("projectId", projectId)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -504,8 +504,8 @@ where sum.sumUserId = points.user_id and (sum.sumDay = points.day OR (sum.sumDay
WHERE project_id = :projectId AND skill_id = :skillId AND day is null
GROUP BY user_id
)
INSERT INTO user_achievement (user_id, skill_id, level, points_when_achieved, project_id, notified)
SELECT user_totals.user_id, subject_score.skill_id, subject_levels.level, user_totals.totalPoints, ''' + "'$projectId', 'false'" +
INSERT INTO user_achievement (user_id, skill_id, skill_ref_id, level, points_when_achieved, project_id, notified)
SELECT user_totals.user_id, subject_score.skill_id, subject_score.id, subject_levels.level, user_totals.totalPoints, ''' + "'$projectId', 'false'" +
'''
FROM user_totals, subject_score, subject_levels
WHERE user_totals.totalPoints >= TRUNC(subject_levels.pointsRequired)
Expand Down
Loading

0 comments on commit a19d42f

Please sign in to comment.