Repository: louislam/uptime-kuma
File: server/notification-providers/sevenio.js (JavaScript)

Code before:
            // testing or certificate expiry notification
            if (heartbeatJSON == null) {
                await axios.post("sms", data);
                return okMsg;
            }

            let address;

            switch (monitorJSON["type"]) {
                case "ping":
                    address = monitorJSON["hostname"];
                    break;
                case "port":
                case "dns":
                case "gamedig":
                case "steam":
                    address = monitorJSON["hostname"];
                    if (monitorJSON["port"]) {
                        address += ":" + monitorJSON["port"];
                    }
                    break;
                default:
                    if (![ "https://", "http://" ].includes(monitorJSON["url"])) {
                        address = monitorJSON["url"];
                    } else {
                        address = "n/a";
                    }
                    break;
            }

            // If heartbeatJSON is not null, we go into the normal alerting loop.
            if (heartbeatJSON["status"] === DOWN) {
                data.text = `Your service ${monitorJSON["name"]} (${address}) went down at ${heartbeatJSON["localDateTime"]} ` +
                    `(${heartbeatJSON["timezone"]}). Error: ${heartbeatJSON["msg"]}`;
            } else if (heartbeatJSON["status"] === UP) {
                data.text = `Your service ${monitorJSON["name"]} (${address}) went back up at ${heartbeatJSON["localDateTime"]} ` +
                    `(${heartbeatJSON["timezone"]}).`;
            }
            await axios.post("sms", data);
            return okMsg;
        } catch (error) {
            this.throwGeneralAxiosError(error);
        }
    }

}

module.exports = SevenIO;


Review comment (refactor):
nit:
I think adding ` (n/a)` to the sms should not be done in this case as this will likely just confuse users.
I think extracting this into a function and don't add the address in this case would be better.

Code after:
            if (heartbeatJSON == null) {
                await axios.post("sms", data, config);
                return okMsg;
            }

            let address = "";

            switch (monitorJSON["type"]) {
                case "ping":
                    address = monitorJSON["hostname"];
                    break;
                case "port":
                case "dns":
                case "gamedig":
                case "steam":
                    address = monitorJSON["hostname"];
                    if (monitorJSON["port"]) {
                        address += ":" + monitorJSON["port"];
                    }
                    break;
                default:
                    if (![ "https://", "http://", "" ].includes(monitorJSON["url"])) {
                        address = monitorJSON["url"];
                    }
                    break;
            }

            if (address !== "") {
                address = `(${address}) `;
            }

            // If heartbeatJSON is not null, we go into the normal alerting loop.
            if (heartbeatJSON["status"] === DOWN) {
                data.text = `Your service ${monitorJSON["name"]} ${address}went down at ${heartbeatJSON["localDateTime"]} ` +
                    `(${heartbeatJSON["timezone"]}). Error: ${heartbeatJSON["msg"]}`;
            } else if (heartbeatJSON["status"] === UP) {
                data.text = `Your service ${monitorJSON["name"]} ${address}went back up at ${heartbeatJSON["localDateTime"]} ` +
                    `(${heartbeatJSON["timezone"]}).`;
            }
            await axios.post("sms", data, config);
            return okMsg;
        } catch (error) {
            this.throwGeneralAxiosError(error);
        }
    }

}

module.exports = SevenIO;