generated from navikt/crm-shared-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEmailSchedulingHelper.cls
246 lines (211 loc) · 8.79 KB
/
EmailSchedulingHelper.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
public with sharing class EmailSchedulingHelper {
public static final String AFTER_TEMPLATE = 'courseScheduledEmailAfter';
public static final String BEFORE_TEMPLATE = 'courseScheduledEmailBefore';
public static final String MANUAL_TEMPLATE = 'courseScheduledEmailManual';
public static final String REMINDER_TEMPLATE = 'courseScheduledEmailReminder';
public static final List<String> TEMPLATES = new List<String>{
AFTER_TEMPLATE,
BEFORE_TEMPLATE,
MANUAL_TEMPLATE,
REMINDER_TEMPLATE
};
public enum EmailTypes {
AFTER,
REMINDER,
BEFORE,
MANUAL
}
public class ScheduleModel {
public Id courseId;
public Id contactId;
public String emailTemplate;
}
public static Map<Id, Course__c> getCoursesWithScheduledEmails() {
List<Course__c> courses = [
SELECT
Id,
EmailAfterDate__c,
EmailBeforeDate__c,
EmailManualDate__c,
EmailReminderDate__c,
EmailAfterContent__c,
EmailBeforeContent__c,
EmailManualContent__c,
EmailReminderContent__c
FROM Course__c
WHERE
(EmailAfterDate__c = TODAY
AND EmailAfterSent__c = FALSE
AND EmailAfterConfirmation__c = TRUE)
OR (EmailBeforeDate__c = TODAY
AND EmailBeforeSent__c = FALSE
AND EmailBeforeConfirmation__c = TRUE)
OR (EmailManualDate__c = TODAY
AND EmailManualSent__c = FALSE
AND EmailManualConfirmation__c = TRUE)
OR (EmailReminderDate__c = TODAY
AND EmailReminderSent__c = FALSE
AND EmailReminderConfirmation__c = TRUE)
];
return new Map<Id, Course__c>(courses);
}
public static List<CourseRegistration__c> getCourseRegistrations(Map<Id, Course__c> courses) {
return [
SELECT Id, Course__c, CourseParticipant__c, Attendance__c
FROM CourseRegistration__c
WHERE Course__c IN :courses.keySet() AND Status__c = 'Påmeldt'
];
}
public static List<ScheduleModel> prepareEmails(
Map<Id, Course__c> courses,
List<CourseRegistration__c> courseRegistrations
) {
Map<String, Id> emailTemplates = getTemplates();
List<ScheduleModel> recipients = new List<ScheduleModel>();
for (CourseRegistration__c courseRegistration : courseRegistrations) {
ScheduleModel recipient = new ScheduleModel();
Course__c course = courses.get(courseRegistration.Course__c);
recipient.courseId = courseRegistration.Course__c;
recipient.contactId = courseRegistration.CourseParticipant__c;
switch on checkEmailType(course) {
when AFTER {
recipient.emailTemplate = emailTemplates.get(AFTER_TEMPLATE);
}
when BEFORE {
recipient.emailTemplate = emailTemplates.get(BEFORE_TEMPLATE);
}
when MANUAL {
recipient.emailTemplate = emailTemplates.get(MANUAL_TEMPLATE);
}
when REMINDER {
recipient.emailTemplate = emailTemplates.get(REMINDER_TEMPLATE);
}
}
if (recipient.courseId != null && recipient.contactId != null && recipient.emailTemplate != null) {
recipients.add(recipient);
}
}
return recipients;
}
public static List<EmailQueue__c> queueEmails(List<ScheduleModel> recipients) {
List<EmailQueue__c> emailQueue = new List<EmailQueue__c>();
for (ScheduleModel recipient : recipients) {
EmailQueue__c email = new EmailQueue__c();
email.TemplateId__c = recipient.emailTemplate;
email.TargetObjectId__c = recipient.contactId;
email.WhatId__c = recipient.courseId;
email.Priority__c = '5';
email.Status__c = 'Queued';
email.SaveAsActivity__c = false;
emailQueue.add(email);
}
insert emailQueue;
return emailQueue;
}
public static Map<Id, List<String>> getCoursesWithEmailQueue(List<EmailQueue__c> emailQueue) {
Map<Id, List<String>> courses = new Map<Id, List<String>>();
for (EmailQueue__c email : emailQueue) {
if (courses.containsKey(email.WhatId__c)) {
courses.get(email.WhatId__c).add(email.Id);
} else {
courses.put(email.WhatId__c, new List<String>{ email.Id });
}
}
return courses;
}
public static void updateCourses(Map<Id, Course__c> courseMap, Map<Id, List<String>> coursesWithEmailQueue) {
List<Course__c> courses = new List<Course__c>();
for (Id courseId : coursesWithEmailQueue.keySet()) {
Course__c course = courseMap.get(courseId);
switch on checkEmailType(course) {
when AFTER {
course.EmailAfterSent__c = true;
course.EmailAfterEmailQueueId__c = String.join(coursesWithEmailQueue.get(courseId), ';');
}
when BEFORE {
course.EmailBeforeSent__c = true;
course.EmailBeforeEmailQueueId__c = String.join(coursesWithEmailQueue.get(courseId), ';');
}
when MANUAL {
course.EmailManualSent__c = true;
course.EmailManualEmailQueueId__c = String.join(coursesWithEmailQueue.get(courseId), ';');
}
when REMINDER {
course.EmailReminderSent__c = true;
course.EmailReminderEmailQueueId__c = String.join(coursesWithEmailQueue.get(courseId), ';');
}
}
courses.add(course);
}
try {
update courses;
} catch (Exception e) {
LoggerUtility logger = new LoggerUtility();
logger.exception(e);
logger.publishSynch();
}
}
public static void createTasks(Map<Id, Course__c> courseMap, Map<Id, List<String>> coursesWithEmailQueue) {
List<Task> tasks = new List<Task>();
for (Id courseId : coursesWithEmailQueue.keySet()) {
Task task = new Task(WhatId = courseId, Status = 'Completed', TAG_NoPersonInformation__c = true);
Course__c course = courseMap.get(courseId);
switch on checkEmailType(course) {
when AFTER {
task.Subject = 'Evaluerings e-post sendt';
task.Description = course.EmailAfterContent__c;
}
when BEFORE {
task.Subject = 'E-post med informasjon før kurs sendt';
task.Description = course.EmailBeforeContent__c;
}
when MANUAL {
task.Subject = 'Manuell e-post sendt';
task.Description = course.EmailManualContent__c;
}
when REMINDER {
task.Subject = 'Påminnelse om kurs er sendt på e-post';
task.Description = course.EmailReminderContent__c;
}
}
tasks.add(task);
}
try {
insert tasks;
} catch (Exception e) {
LoggerUtility logger = new LoggerUtility();
logger.exception(e);
logger.publishSynch();
}
}
// ------------------------------------------------
// --------------- HELPER FUNCTIONS ---------------
// ------------------------------------------------
public static EmailTypes checkEmailType(Course__c course) {
if (Date.today() == course.EmailAfterDate__c) {
return EmailTypes.AFTER;
} else if (Date.today() == course.EmailBeforeDate__c) {
return EmailTypes.BEFORE;
} else if (Date.today() >= course.EmailManualDate__c) {
return EmailTypes.MANUAL;
} else if (Date.today() == course.EmailReminderDate__c) {
return EmailTypes.REMINDER;
}
return null;
}
public static Map<String, Id> getTemplates() {
List<EmailTemplate> emailTemplates = [
SELECT Id, DeveloperName
FROM EmailTemplate
WHERE DeveloperName IN :TEMPLATES
];
Map<String, Id> emailTemplateMap = new Map<String, Id>();
if (emailTemplates.size() < TEMPLATES.size()) {
// TODO throw error
}
for (EmailTemplate emailTemplate : emailTemplates) {
emailTemplateMap.put(emailTemplate.DeveloperName, emailTemplate.Id);
}
return emailTemplateMap;
}
}