/*
* site-monitor/services/contacts.js Copyright 2021 cPanel, L.L.C.
* All rights reserved.
* copyright@cpanel.net http://cpanel.net
* This code is subject to the cPanel license. Unauthorized copying is prohibited
*/
/* global define, PAGE */
/** @namespace cpanel.siteMonitor.services.contacts */
define(
[
"angular",
"lodash",
"cjt/util/locale",
"cjt/io/uapi-request",
"cjt/io/uapi",
"cjt/modules",
"cjt/services/APICatcher",
],
function(angular, _, LOCALE, UAPIRequest) {
"use strict";
var app = angular.module("cpanel.siteMonitor.contactsService", [
"cjt2.services.apicatcher",
]);
app.factory("contacts", [
"$q",
"APICatcher",
"PAGE",
function($q, APICatcher, PAGE) {
var ContactsAPI = function() {};
ContactsAPI.prototype = Object.create(APICatcher);
/**
* Get the store email address saved on the cPanel Account.
*
* @async
* @returns {string} the store email address
*/
ContactsAPI.prototype.getStoreEmailAddress = function list() {
var self = this;
var apiCall = new UAPIRequest.Class();
apiCall.initialize("Monitoring", "get_store_email_address");
return self.promise(apiCall).then(function(result) {
return result.data;
});
}
/**
* Get all contacts configured in Nixstats
*
* @async
* @method get
* @returns {IMonitorItem[]} - list of all the nixstats contacts already configured.
*/
ContactsAPI.prototype.list = function list() {
var self = this;
var apiCall = new UAPIRequest.Class();
apiCall.initialize("Monitoring", "list_contacts");
return self.promise(apiCall).then(function(result) {
return result.data.response.contacts || [];
});
}
/**
* Get the contact by its uniqe id
*
* @async
* @method get
* @params {String} id - the id of the monitor
* @returns {IMonitorDetails} The details of the requested monitor.
*/
ContactsAPI.prototype.get = function get(id) {
var self = this;
if (typeof(id) !== 'string' || id === '') {
throw new Error('The `id` argument is required.')
}
var apiCall = new UAPIRequest.Class();
apiCall.initialize("Monitoring", "get_contact");
apiCall.addArgument("id", id);
return self.promise(apiCall).then(function(result) {
return result.data.response;
});
};
/**
* Create a contact
*
* @async
* @method enable
* @params {String} name - The
* @params {String} email - optional name of the monitor, will default to the domain from the url if not provided
* @returns {boolean} returns true if successful, false otherwise.
*/
ContactsAPI.prototype.create = function create(name, email) {
var self = this;
if (typeof(name) !== 'string' || name === '') {
throw new Error('The `name` argument is required.')
}
if (typeof(email) !== 'string' || email === '') {
throw new Error('The `email` argument is required.')
}
var apiCall = new UAPIRequest.Class();
apiCall.initialize("Monitoring", "create_contact");
apiCall.addArgument("name", name);
apiCall.addArgument("email", email);
return self.promise(apiCall).then(function(result) {
if (result.error) {
throw new Error(result.error);
} else if(result.id) {
return result.id;
} else {
throw new Error("Unknown response");
}
});
};
return new ContactsAPI();
}
]);
}
);