2024-02-12 15:58:54 -08:00
|
|
|
const { UP, DOWN, getMonitorRelativeURL } = require("../../src/util");
|
|
|
|
const { setting } = require("../util-server");
|
|
|
|
|
|
|
|
const NotificationProvider = require("./notification-provider");
|
|
|
|
const axios = require("axios");
|
|
|
|
class HeiiOnCall extends NotificationProvider {
|
|
|
|
name = "HeiiOnCall";
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @inheritdoc
|
|
|
|
*/
|
|
|
|
async send(notification, msg, monitorJSON = null, heartbeatJSON = null) {
|
2024-02-13 16:54:52 -08:00
|
|
|
const payload = heartbeatJSON || {};
|
2024-02-12 15:58:54 -08:00
|
|
|
|
|
|
|
const baseURL = await setting("primaryBaseURL");
|
|
|
|
if (baseURL && monitorJSON) {
|
|
|
|
payload["url"] = baseURL + getMonitorRelativeURL(monitorJSON.id);
|
|
|
|
}
|
|
|
|
|
2024-02-13 17:27:59 -08:00
|
|
|
try {
|
|
|
|
if (!heartbeatJSON) {
|
|
|
|
// Testing or general notification like certificate expiry
|
|
|
|
payload["msg"] = msg;
|
|
|
|
return this.postNotification(notification, "alert", payload);
|
|
|
|
}
|
2024-02-12 17:54:36 -08:00
|
|
|
|
2024-02-13 17:27:59 -08:00
|
|
|
if (heartbeatJSON.status === DOWN) {
|
|
|
|
return this.postNotification(notification, "alert", payload);
|
|
|
|
}
|
2024-02-12 15:58:54 -08:00
|
|
|
|
2024-02-13 17:27:59 -08:00
|
|
|
if (heartbeatJSON.status === UP) {
|
|
|
|
return this.postNotification(notification, "resolve", payload);
|
|
|
|
}
|
|
|
|
} catch (error) {
|
|
|
|
this.throwGeneralAxiosError(error);
|
2024-02-12 15:58:54 -08:00
|
|
|
}
|
2024-02-13 17:27:59 -08:00
|
|
|
|
2024-02-12 15:58:54 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Post to Heii On-Call
|
|
|
|
* @param {BeanModel} notification Message title
|
2024-02-12 16:14:41 -08:00
|
|
|
* @param {string} action Trigger action (alert, resovle)
|
2024-02-12 15:58:54 -08:00
|
|
|
* @param {object} payload Data for Heii On-Call
|
|
|
|
* @returns {Promise<string>} Success message
|
|
|
|
*/
|
|
|
|
async postNotification(notification, action, payload) {
|
|
|
|
const config = {
|
|
|
|
headers: {
|
|
|
|
Accept: "application/json",
|
|
|
|
"Content-Type": "application/json",
|
|
|
|
Authorization: "Bearer " + notification.heiiOnCallApiKey,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
// Post to Heii On-Call Trigger https://heiioncall.com/docs#manual-triggers
|
2024-02-13 17:27:59 -08:00
|
|
|
await axios.post(
|
|
|
|
`https://heiioncall.com/triggers/${notification.heiiOnCallTriggerId}/${action}`,
|
|
|
|
payload,
|
|
|
|
config
|
|
|
|
);
|
2024-02-12 16:14:41 -08:00
|
|
|
|
2024-02-13 16:54:52 -08:00
|
|
|
return "Sent Successfully";
|
2024-02-12 15:58:54 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = HeiiOnCall;
|