Viewing File: /usr/local/cpanel/whostmgr/docroot/templates/gsw/initial_setup/services/setupService.js

/*
 * services/setupService.js                           Copyright 2022 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
 */

/* eslint-env amd */

define([
    "angular",
    "cjt/io/whm-v1-request",
    "cjt/io/whm-v1",
    "cjt/services/APIService",
    "cjt/services/whm/nvDataService",
], function(
    angular,
    WHMAPI1_REQUEST
) {

    "use strict";

    var module = angular.module("whm.initialSetup.setupService", [
        "cjt2.services.api",
        "cjt2.services.whm.nvdata"
    ]);

    module.factory("setupService", [
        "$q",
        "APIService",
        "nvDataService",
        function(
            $q,
            APIService,
            nvDataService
        ) {

            var NO_MODULE = "";

            var SetupService = function() {
                this.apiService = new APIService();
            };

            angular.extend(SetupService.prototype, {

                /**
                 * Stores if the box is eligible for trial
                 */
                isEligibleForTrial: false,

                /**
                 * Records the root user's acceptance of the current legal agreements.
                 *
                 * @method recordAcceptance
                 * @return {Promise}   When resolved, the server has successfully recorded the user's acceptance.
                 */
                recordAcceptance: function() {

                    var apiCall = new WHMAPI1_REQUEST.Class();
                    apiCall.initialize(NO_MODULE, "accept_eula");

                    return this.apiService.deferred(apiCall).promise;
                },

                /**
                 * Sets the contact email and default nameservers in wwwacct.conf
                 *
                 * @param {Object} args   An object containing values for the contact email and/or default nameservers.
                 * @param {String} [args.email]         The contact email for root.
                 * @param {String} [args.nameservers]   An array, containing the hostnames of the nameservers. Currently,
                 *                                      only the first two values in the array are used.
                 */


                /**
                 * Sets the contact_email in /etc/wwwacct.conf
                 *
                 * @param {String} email   The contact email for root.
                 * @return {Promise}       Resolves once the API call is complete.
                 */
                setEmail: function(email) {
                    if (!angular.isString(email)) {
                        throw new TypeError("Developer Error: Them email argument must be a string");
                    }

                    var apiCall = new WHMAPI1_REQUEST.Class();
                    apiCall.initialize(NO_MODULE, "update_contact_email");
                    apiCall.addArgument("contact_email", email);

                    return this.apiService.deferred(apiCall).promise;
                },

                /**
                 * Sets nameservers 1 and 2 in /etc/wwwacct.conf. This method
                 * does not currently support nameservers 3 and 4.
                 *
                 * @param  {String[]} nameservers   An array of nameserver hostnames.
                 * @return {Promise}                Resolves once the API call is complete.
                 */
                setNameservers: function(nameservers) {
                    if (!angular.isArray(nameservers)) {
                        throw new TypeError("Developer Error: The nameservers argument must be an array");
                    }

                    var apiCall = new WHMAPI1_REQUEST.Class();
                    apiCall.initialize(NO_MODULE, "update_nameservers_config");

                    [
                        "nameserver",
                        "nameserver2",
                    ].forEach(function(apiKey, index) {
                        apiCall.addArgument(apiKey, nameservers[index]);
                    });

                    return this.apiService.deferred(apiCall).promise;
                },

                /**
                 * Creates NVData entries for API failures that take place during
                 * the setup process so we can create additional notices inside
                 * the product to draw users' attention.
                 *
                 * @param  {String[]} errorKeys    An array of error keys to base the NVData names on.
                 * @return {Promise}               Resolves once the API call is complete.
                 */
                saveErrors: function(errorKeys) {
                    if (!angular.isArray(errorKeys)) {
                        throw new TypeError("Developer Error: The errorKeys argument must be an array");
                    }

                    if (!errorKeys.length) {
                        return $q.resolve();
                    }

                    var nvData = {};
                    errorKeys.forEach(function(key) {
                        key = "isa:" + key + "_save_error";
                        nvData[key] = 1;
                    });

                    return nvDataService.setObject(nvData);
                },

                /**
                 * Checks whether an initial website was requested.
                 *
                 * @return {Promise}     Resolves once the API call is complete.
                 */
                initialWebsiteRequested: function() {
                    var apiCall = new WHMAPI1_REQUEST.Class();
                    apiCall.initialize(NO_MODULE, "initialwebsite_requested");

                    return this.apiService.deferred(apiCall).promise;
                },

                /**
                 * Creates the initial website if specified in a config file.
                 *
                 * @return {Promise}     Resolves once the API call is complete.
                 */
                initialWebsite: function() {
                    var apiCall = new WHMAPI1_REQUEST.Class();
                    apiCall.initialize(NO_MODULE, "initialwebsite_create");

                    return this.apiService.deferred(apiCall).promise;
                },

            });

            return new SetupService();
        }
    ]);
});
Back to Directory File Manager