(function () { var app = angular.module("app"); angular .module("app") .factory("tenantService", ["config", "$http", "$q", tenantService]); function tenantService(config, $http, $q) { var service = { getUISettings: getUISettings, getOrgUISettings: getOrgUISettings, getTerminology: getTerminology, getOrgTerminology: getOrgTerminology, getAllRoleFunctionHelpTextLinks: getAllRoleFunctionHelpTextLinks, getSpecificRoleFunctionHelpText: getSpecificRoleFunctionHelpText, getAllRoleFunctionHelpTexts: getAllRoleFunctionHelpTexts, saveRoleFunctionHelpText: saveRoleFunctionHelpText, saveRoleFunctionHelpTexts: saveRoleFunctionHelpTexts, getUserStatus: getUserStatus }; return service; function getUISettings(hostName) { var request = $http({ method: "get", url: config.tenantUrl + "uisettings/host/" + encodeURIComponent(hostName), }); return request.then(handleSuccess, handleError); } function getOrgUISettings(orgId) { var request = $http({ method: "get", url: config.tenantUrl + "uisettings", headers: { ownerid: orgId.toUpperCase(), tenantid: orgId.toUpperCase(), }, }); return request.then(handleSuccess, handleError); } function getUserStatus(orgId) { var request = $http({ method: "get", url: config.tenantUrl + "userstatus", headers: { ownerid: orgId.toUpperCase(), tenantid: orgId.toUpperCase(), }, }); return request.then(handleSuccess, handleError); } function getTerminology(hostName) { var request = $http({ method: "get", url: config.tenantUrl + "terminology/host/" + encodeURIComponent(hostName), }); return request.then(handleSuccess, handleError); } function getOrgTerminology(orgId) { var request = $http({ method: "get", url: config.tenantUrl + "terminology", headers: { ownerid: orgId.toUpperCase(), tenantid: orgId.toUpperCase(), }, }); return request.then(handleSuccess, handleError); } //Get all role function help texts links for role function getAllRoleFunctionHelpTextLinks(roleId, organisationId) { var request = $http({ method: "get", url: tenantUrl + "RoleFunctionHelpTexts/RoleIds/" + roleId + "/functions/links", headers: { tenantId: organisationId.toUpperCase(), }, }); return request.then(handleSuccess, handleError); } //Get all role function help texts for role function getAllRoleFunctionHelpTexts(role) { var request = $http({ method: "get", url: tenantUrl + "RoleFunctionHelpTexts/RoleIds/" + role.id, headers: { tenantId: role.organisationId.toUpperCase(), }, }); return request.then(handleSuccess, handleError); } //Get specific role function help text function getSpecificRoleFunctionHelpText( roleId, functionId, organisationId ) { var request = $http({ method: "get", url: tenantUrl + "RoleFunctionHelpTexts/RoleIds/" + roleId + "/functions/" + functionId, headers: { tenantId: organisationId.toUpperCase(), }, }); return request.then(handleSuccess, handleError); } // Save a role function help text function saveRoleFunctionHelpText(helpText) { var request = $http({ method: "put", url: tenantUrl + "RoleFunctionHelpTexts", data: helpText, headers: { tenantId: helpText.organisationId.toUpperCase(), }, }); return request.then(handleSuccess, handleError); } // Save a role's role function help texts function saveRoleFunctionHelpTexts(helpTexts, organisationId) { var request = $http({ method: "put", url: tenantUrl + "RoleFunctionHelpTexts", data: helpTexts, headers: { tenantId: organisationId.toUpperCase(), }, }); return request.then(handleSuccess, handleError); } // I transform the successful response, unwrapping the application data // from the API response payload. function handleSuccess(response) { return response.data; } function handleError(response) { // The API response from the server should be returned in a // nomralized format. However, if the request was not handled by the // server (or what not handles properly - ex. server error), then we // may have to normalize it on our end, as best we can. if (!angular.isObject(response.data) || !response.data.message) { return $q.reject("An unknown error occurred."); } // Otherwise, use expected error message. return $q.reject(response.data.message); } } function mapConfigs(values, config) { for (var i = 0; i < values.length; i++) { if (values[i].value) { config = replaceValue(config, values[i].key, values[i].value); } if (values[i].key === "systemName" && values[i].value) config.docTitle = values[i].value + " >"; if (config.msalConfig) { if (config.appCode === "MS") { if (values[i].key === "b2cMyshowcaseSignInProfile" && values[i].value) config.msalConfig.auth.authority = "https://" + b2cName + ".b2clogin.com/" + b2cName + ".onmicrosoft.com/" + values[i].value; if (values[i].key === "b2cMyshowcaseClientId" && values[i].value) config.msalConfig.auth.clientId = values[i].value; } if (config.appCode === "MSA") { if ( values[i].key === "b2cMyshowcaseAdminSignInProfile" && values[i].value ) config.msalConfig.auth.authority = "https://" + b2cName + ".b2clogin.com/" + b2cName + ".onmicrosoft.com/" + values[i].value; if (values[i].key === "b2cMyshowcaseAdminClientId" && values[i].value) config.msalConfig.auth.clientId = values[i].value; } if (config.appCode === "OB") { if (values[i].key === "b2cOpenbadgesSignInProfile" && values[i].value) config.msalConfig.auth.authority = "https://" + b2cName + ".b2clogin.com/" + b2cName + ".onmicrosoft.com/" + values[i].value; if (values[i].key === "b2cOpenbadgesClientId" && values[i].value) config.msalConfig.auth.clientId = values[i].value; } } } if (config.msalConfig) config.msal = new window.msal.PublicClientApplication(config.msalConfig); return config; } function replaceValue(object, key, value) { Object.keys(object).some(function (k) { if (k === key) { object[k] = value; return object; } if (object[k] && typeof object[k] === "object") { replaceValue(object[k], key, value); } }); return object; } // Get and set UI settings app.run([ "$rootScope", "config", "branding", "terminology", "$location", "tenantService", "OrganisationAdminService", "$route", function ( $rootScope, config, branding, terminology, $location, tenantService, OrganisationAdminService, $route ) { var host = $location.$$host; tenantService .getUISettings(host) .then(function (settings) { // Set the required settings app.value("config", mapConfigs(settings, config)); app.value("branding", mapConfigs(settings, branding)); $rootScope.branding = mapConfigs(settings, branding); $rootScope.appLoaded = true; }) .catch(function () { OrganisationAdminService.getTopLevelOrg(true) .then(function (org) { if (org) { tenantService .getOrgUISettings(org.id) .then(function (settings) { // Set the required settings app.value("config", mapConfigs(settings, config)); app.value("branding", mapConfigs(settings, branding)); $rootScope.branding = mapConfigs(settings, branding); $rootScope.appLoaded = true; }) .catch(function () { console.log("Loaded default config"); $rootScope.appLoaded = true; }); } else { console.log("Loaded default config"); $rootScope.appLoaded = true; } }) .catch(function () { console.log("Loaded default config"); $rootScope.appLoaded = true; }); }) .catch(function () { console.log("Loaded default config"); $rootScope.appLoaded = true; }); tenantService .getTerminology(host) .then(function (settings) { // Set the required settings app.value("terminology", mapConfigs(settings, terminology)); }) .catch(function () { OrganisationAdminService.getTopLevelOrg(true) .then(function (org) { if (org) { tenantService .getOrgTerminology(org.id) .then(function (settings) { // Set the required settings app.value("terminology", mapConfigs(settings, terminology)); }) .catch(function () { console.log("Loaded default terminology"); }); } else { console.log("Loaded default terminology"); } }) .catch(function () { console.log("Loaded default terminology"); }); }); $rootScope.$on("setOrganisationBranding", function (event, args) { OrganisationAdminService.getTopLevelOrg(true).then(function (org) { if (org) { tenantService.getOrgUISettings(org.id).then(function (settings) { // Set the required settings app.value("config", mapConfigs(settings, config)); app.value("branding", mapConfigs(settings, branding)); $rootScope.branding = mapConfigs(settings, branding); if ($rootScope.updateLinks) $rootScope.updateLinks(); if ($rootScope.appLoaded) $route.reload(); }); tenantService.getOrgTerminology(org.id).then(function (settings) { // Set the required settings app.value("terminology", mapConfigs(settings, terminology)); if ($rootScope.updateLinks) $rootScope.updateLinks(); }); } }); }); }, ]); })();