-
Notifications
You must be signed in to change notification settings - Fork 2
/
AchievementReachedEventsManager.cls
84 lines (70 loc) · 3.18 KB
/
AchievementReachedEventsManager.cls
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
public with sharing class AchievementReachedEventsManager {
Map<Id, UserMeasurement__c> userMeasurementsByIdMap = new Map<Id, UserMeasurement__c>();
Map<Id, List<Achievement__c>> achievementByMeasurementIdMap = new Map<Id, List<Achievement__c>>();
Map<String, ReachedAchievement__c> existingReachedAchievementsByKey = new Map<String, ReachedAchievement__c>();
public AchievementReachedEventsManager(Map<Id, UserMeasurement__c> userMeasurementsByIdMap) {
this.userMeasurementsByIdMap = userMeasurementsByIdMap;
this.populateAchievementsByMeasurementIdMap();
this.populateExistingReachedAchievementsByKey();
}
public List<Database.SaveResult> fireEvents() {
List<Database.SaveResult> results;
if (validate()) {
List<AchievementReached__e> achievementReachedEvents = new List<AchievementReached__e>();
for (UserMeasurement__c userMeasurement : userMeasurementsByIdMap.values()) {
List<Achievement__c> relatedAchievements = achievementByMeasurementIdMap.get(userMeasurement.Measurement__c);
for (Achievement__c achievement : relatedAchievements) {
if (achievement.Goal__c <= userMeasurement.Value__c &&
!achievementReachedExists(userMeasurement.User__c, achievement.Id)) {
AchievementReached__e event = new AchievementReached__e();
event.UserId__c = userMeasurement.User__c;
event.AchievementId__c = achievement.Id;
achievementReachedEvents.add(event);
}
}
}
if (!achievementReachedEvents.isEmpty()) {
results = EventBus.publish(achievementReachedEvents);
}
}
return results;
}
private void populateAchievementsByMeasurementIdMap() {
Set<Id> measurementIds = new Set<Id>();
for(UserMeasurement__c userMeasurement : userMeasurementsByIdMap.values()) {
measurementIds.add(userMeasurement.Measurement__c);
}
List<Achievement__c> achievements = AchievementSelector.getInstance().getByFieldValue('Measurement__c', 'IN', measurementIds);
for (Achievement__c achievement : achievements) {
if (!achievementByMeasurementIdMap.containsKey(achievement.Measurement__c)) {
achievementByMeasurementIdMap.put(achievement.Measurement__c, new List<Achievement__c>());
}
achievementByMeasurementIdMap.get(achievement.Measurement__c).add(achievement);
}
}
private void populateExistingReachedAchievementsByKey() {
Set<Id> userIds = new Set<Id>();
for(UserMeasurement__c userMeasurement : userMeasurementsByIdMap.values()) {
userIds.add(userMeasurement.User__c);
}
List<ReachedAchievement__c> reachedAchievements = ReachedAchievementSelector.getInstance().getByFieldValue('User__c', 'IN', userIds);
for (ReachedAchievement__c reachedAchievement : reachedAchievements) {
existingReachedAchievementsByKey.put(reachedAchievement.Key__c, reachedAchievement);
}
}
private Boolean validate() {
Boolean result = true;
if (userMeasurementsByIdMap.isEmpty() || achievementByMeasurementIdMap.isEmpty()) {
result = false;
}
return result;
}
private Boolean achievementReachedExists(Id userId, Id achievementId) {
Boolean result = false;
String key = UniqueKeyFormatter.generateUniqueKey(userId, achievementId);
if (existingReachedAchievementsByKey.containsKey(key)) {
result = true;
}
return result;
}
}