(function() { 'use strict'; angular .module('SportlogiqWebApp.services') .service('companies', Companies); Companies.$inject = ['$rootScope', '$http', 'apiEndpoint', 'userCompany']; function Companies($rootScope, $http, apiEndpoint, userCompany) { var that = this; // Private Members var COMPANY_ENDPOINT = apiEndpoint + "companies"; var IMPERSONATE_ENDPOINT = apiEndpoint + "user/impersonate/"; // Public Members this.selectedCompany = 0; this.affiliations = 0; this.gameDataBundles = 0; this.loadingAffiliations = false; this.loadingBundles = false; this.loadingError = ''; this.loadingUsers = false; // This is not yet available at this point. Do not attempt to store companies in a local var but // rather use the global `companies` var in $rootScope // this.list = $rootScope.companies; // Private Methods function nestedResource(companyId) { return COMPANY_ENDPOINT + "/" + companyId; } function isAllowed() { return $rootScope.permissions.canShareWithAnyone || $rootScope.permissions.canImpersonate; } // Public Methods this.getAllCompanies = getAllCompanies; this.getUsersInSelectedCompany = getUsersInSelectedCompany; this.getSelectedCompany = getSelectedCompany; this.getCompanyAffiliations = getCompanyAffiliations; this.getCompanyGameDataBundles = getCompanyGameDataBundles; this.impersonateUser = impersonateUser; function getAllCompanies() { if(!isAllowed()) return; return $rootScope.companies; } function getUsersInSelectedCompany(companyId, withInactive = 0) { if (!isAllowed()) return; this.loadingUsers = true; this.affiliations = 0; this.gameDataBundles = 0; this.loadingError = ''; userCompany.companyUsers = [] return $http.get(nestedResource(companyId) + "/users?withInactive=" + withInactive) .then(function(response) { userCompany.companyUsers = response.data.map(function(user) { user.checked = user.isActive === '1' ? true : false; return user; }); userCompany.companyNotSuperUsers = response.data.map(function(user) { var isSuperUser = 0; if (user.roles == undefined) { user.roles = []; } user.roles.forEach(function(role) { if (role.id == 1) { isSuperUser = 1; } }); if (!isSuperUser) { return user; } }); }) .catch(function(err) { that.loadingError = err; console.error(err); }) .finally(function() { that.loadingUsers = false; }); } function getSelectedCompany () { return +this.selectedCompany; } function impersonateUser(userId, user) { return $http.post(IMPERSONATE_ENDPOINT + userId, user) .then(function(response) { return response; }) .catch(function(err) { return err; }); } function getCompanyAffiliations(company) { this.loadingAffiliations = true; return $http.get(nestedResource(company.id) + "/affiliations") .success(function(data) { that.affiliations = data; }) .error(function(data, status) { that.loadingError = "Get affiliations: " + data; console.error("Get affiliations: ", data); return status }) .finally(function() { that.loadingAffiliations = false; }); } function getCompanyGameDataBundles(company) { this.loadingBundles = true; return $http.get(nestedResource(company.id) + "/gamedatabundles") .success(function(data) { that.gameDataBundles = data; }) .error(function(data, status) { that.loadingError = "Get game data bundles: " + data; console.error("Get game data bundles: ", data); return status }) .finally(function() { that.loadingBundles = false; }); } } })();