forked from rakutentech/node-alertnotification
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
65 lines (58 loc) · 1.69 KB
/
index.js
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
const MsTeamsMessageCard = require('./lib/ms_teams');
const EmailMessage = require('./lib/email');
const throttler = require('./lib/throttler');
const debug = require('debug');
/*
*
*/
function Notify(err, doNotAlert) {
this.err = err;
this.doNotAlert = doNotAlert || [];
}
Notify.prototype.send = async function() {
if (await this.shouldAlert()) {
await this.dispatch()
}
}
Notify.prototype.dispatch = async function() {
if (this.shouldSendEmail()) {
let email = new EmailMessage();
email.sendAlertMail(this.err);
}
if (this.shouldSendMsTeams()) {
let msTeamsCard = new MsTeamsMessageCard(this.err)
await msTeamsCard.sendMessageCard()
}
}
Notify.prototype.shouldAlert = async function() {
if (!this.isThrottlingEnabled()) {
//Always alert when throttling is disabled.
return true;
}
if (this.isDoNotAlert()) {
return false;
}
const isThrottled = await throttler.isThrottled(this.err)
return !isThrottled
}
Notify.prototype.isDoNotAlert = function() {
let found = false
this.doNotAlert.forEach(element => {
if (typeof this.err === typeof element && this.err.message === element.message) {
debug("Do Not Alert error : " + this.err.toString());
found = true;
return;
}
});
return found;
}
Notify.prototype.shouldSendMsTeams = function() {
return process.env.MS_TEAMS_ALERT_ENABLED == "true";
}
Notify.prototype.shouldSendEmail = function() {
return process.env.EMAIL_ALERT_ENABLED == "true";
}
Notify.prototype.isThrottlingEnabled = function() {
return process.env.THROTTLING_ENABLED != "false";
}
module.exports = Notify