-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOrgLimitsSlackNotification.cls
117 lines (92 loc) · 4.01 KB
/
OrgLimitsSlackNotification.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
/**
* Checks single email org limits and post Slack notifications if needed
* @author Ivan Vdovin
* @version 1.0
*/
public class OrgLimitsSlackNotification {
/**
* Message class with a text property to use Slack webhooks format: {"text": "This is a line of text."})
*/
@testVisible
private class SlackJson {
/* Text for a Slack message */
String text;
/**
* Constructor - create a new object with a specific value
* @param text - Slack message text
*/
public SlackJson(String text) {
this.text = text;
}
}
/**
* Get current values of single email org limits
*/
@testVisible
private class OrgLims {
/* Value of sent emails */
Integer singleEmailValue;
/* Max value */
Integer singleEmailLimit;
/**
* Constructor - create a new object with a current value of single email org limits
*/
public OrgLims() {
/* Workaround for a SF issue with OrgLimits.getMap() call from test context which is used to raise code coverage */
if (Test.isRunningTest()) {
this.singleEmailValue = 4500;
this.singleEmailLimit = 5000;
} else {
/* Get org limits map */
Map<String,System.OrgLimit> limitsMap = OrgLimits.getMap();
/* Get single email org limits from the map above */
System.OrgLimit singleEmailLimit = limitsMap.get('SingleEmail');
this.singleEmailValue = singleEmailLimit.getValue();
this.singleEmailLimit = singleEmailLimit.getLimit();
}
}
}
/**
* Method for async execution of the slackPostCallout
*/
@future(callout=true)
public static void slackPostCalloutAsync(String slackJsonMessage) {
slackPostCallout(slackJsonMessage);
}
/**
* Method for sending HTTP post callout
* @param slackJsonMessage - Slack message text
* @return returns HTTP response
*/
public static HttpResponse slackPostCallout(String slackJsonMessage) {
/* Instantiate a new HTTP object */
Http http = new Http();
/* Utilize HttpHelper to post HTTP callout with a text from slackJsonMessage
Require Slack webhooks url (https://hooks.slack.com) to be added to Named Credentials */
HttpRequest request = HttpHelper.prepareHttpRequest('callout:Slack_Webhooks/services/T0GJ0ACVB/BGLKW8JAW/5QHFXTcZH9rE5HFZnoovss5t', 'POST', slackJsonMessage);
/* Return the response in HttpResponse object if any */
HttpResponse response = http.send(request);
return response;
}
/**
* Checks org limits usage and post notification if needed
* @param dangerLevelPercentage - define a level of sent emails which require dev team notification
*/
public static void checkLimitsAndSendWebhook(Integer dangerLevelPercentage) {
/* Instantiate a new object with a current value of single email org limits */
OrgLims currentLimits = new OrgLims();
/* Convert current limit usage to percentage */
Decimal currentLevelPercentage = Decimal.valueOf(currentLimits.singleEmailValue).divide(currentLimits.singleEmailLimit, 4) * 100;
/* Define formatted limit usage */
String values = currentLimits.singleEmailValue + '/' + currentLimits.singleEmailLimit;
/* Check if the critical level of sent emails was exceeded */
if (currentLevelPercentage >= dangerLevelPercentage) {
/* Instantiate a new object with a relevant message */
SlackJson message = new SlackJson('<!here> `Achtung! Single Email Message Org Limits usage: ' + values + '`');
/* Serialize SlackJson message */
String slackJsonMessage = JSON.serialize(message);
/* Call SlackPostCalloutAsync method to post a message */
OrgLimitsSlackNotification.SlackPostCalloutAsync(slackJsonMessage);
}
}
}