(function() { 'use strict'; angular .module('SportlogiqWebApp.services') .service('userCompany', UserCompany); UserCompany.$inject = ['$http', 'apiEndpoint']; function UserCompany($http, apiEndpoint) { var that = this; // Private Members var apiEndpointV2 = apiEndpoint.replace('0.1', '0.2'); var ENDPOINT = apiEndpoint + "usercompany/users"; // Public Members this.companyUsers = undefined; this.companyUserRoles = undefined; this.newUser = undefined; this.roles = []; // Public Methods this.getLeagues = getLeagues; this.getTeams = getTeams; this.getGameDataBundles = getGameDataBundles; this.getLeagueSeasonPlayers = getLeagueSeasonPlayers; this.getPlayerUser = getPlayerUser; this.addPlayerUser = addPlayerUser; this.deletePlayerUser = deletePlayerUser; this.addCompanyAffiliation = addCompanyAffiliation; this.addCompanyGameDataBundle = addCompanyGameDataBundle; this.deleteCompanyAffiliation = deleteCompanyAffiliation; this.deleteCompanyGameDataBundle = deleteCompanyGameDataBundle; this.refreshCompanyLeagueSummary = refreshCompanyLeagueSummary; this.refreshCompanyLeagueAffiliations = refreshCompanyLeagueAffiliations; this.addUserAffiliation = addUserAffiliation; this.deleteUserAffiliation = deleteUserAffiliation; this.getCompanyUsers = getCompanyUsers; this.getCompanyUsersWithPlayerUsersInfo = getCompanyUsersWithPlayerUsersInfo; this.updateCompanyUserStatus = updateCompanyUserStatus; this.getCompanyUserRoles = getCompanyUserRoles; this.updateCompanyUserRole = updateCompanyUserRole; this.updateAllUsersCompanyRole = updateAllUsersCompanyRole; this.createUser = createUser; this.getRoles = getRoles; //////////////// function getLeagues() { return $http.get(apiEndpointV2 + "leagues") .then(function(response) { return response; }) .catch(function(err) { return err; console.error(err); }); } function getTeams() { return $http.get(apiEndpoint + "teams") .then(function(response) { return response; }) .catch(function(err) { return err; console.error(err); }); } function getGameDataBundles() { return $http.get(apiEndpoint + "gamedatabundles") .then(function(response) { return response; }) .catch(function(err) { return err; console.error(err); }); } function getLeagueSeasonPlayers(leagueId, seasonId) { return $http.get(apiEndpointV2 + "leagues/" + leagueId + "/seasons/" + seasonId + "/players") .then(function(response) { return response; }) .catch(function(err) { return err; console.error(err); }); } function getPlayerUser(userId) { return $http.get(apiEndpointV2 + "users/" + userId + "/player") .then(function(response) { return response; }) .catch(function(err) { return err; console.error(err); }); } function addPlayerUser(userId, playerId) { return $http.post(apiEndpointV2 + "users/" + userId + "/player/" + playerId) .then(function(response) { return response; }) .catch(function(err) { return err; console.error(err); }); } function deletePlayerUser(userId) { return $http.delete(apiEndpointV2 + "users/" + userId + "/player") .then(function(response) { return response; }) .catch(function(err) { return err; console.error(err); }); } function addCompanyAffiliation(companyId, leagueId, teamId) { var route = apiEndpoint + "companies/" + companyId + "/leagues/" + leagueId; route += teamId != -1 ? "/team/" + teamId : ""; route += "/affiliation"; return $http.post(route) .then(function(response) { return response; }) .catch(function(err) { return err; console.error(err); }); } function addCompanyGameDataBundle(companyId, gameDataBundleId) { var route = apiEndpoint + "companies/" + companyId + "/gamedatabundles/" + gameDataBundleId; return $http.post(route) .then(function(response) { return response; }) .catch(function(err) { return err; console.error(err); }); } function deleteCompanyAffiliation(companyId, leagueId) { return $http.delete(apiEndpoint + "companies/" + companyId + "/leagues/" + leagueId + "/affiliation") .then(function(response) { return response; }) .catch(function(err) { return err; console.error(err); }); } function deleteCompanyGameDataBundle(companyId, gameDataBundleId) { return $http.delete(apiEndpoint + "companies/" + companyId + "/gamedatabundles/" + gameDataBundleId) .then(function(response) { return response; }) .catch(function(err) { return err; console.error(err); }); } function refreshCompanyLeagueSummary(companyId, leagueId) { return $http.post(apiEndpoint + "companies/" + companyId + "/leagues/" + leagueId + "/refreshsummary") .then(function(response) { return response; }) .catch(function(err) { return err; console.error(err); }); } function refreshCompanyLeagueAffiliations(companyId) { return $http.post(apiEndpoint + "companies/" + companyId + "/refreshaffiliations") .then(function(response) { return response; }) .catch(function(err) { return err; console.error(err); }); } function addUserAffiliation(userId, leagueId) { return $http.post(apiEndpointV2 + "users/" + userId + "/leagues/" + leagueId + "/affiliation") .then(function(response) { return response; }) .catch(function(err) { return err; console.error(err); }); } function deleteUserAffiliation(userId, leagueId) { return $http.delete(apiEndpointV2 + "users/" + userId + "/leagues/" + leagueId + "/affiliation") .then(function(response) { return response; }) .catch(function(err) { return err; console.error(err); }); } function getCompanyUsers() { return $http.get(ENDPOINT) .success(function(data) { that.companyUsers = data; }) .error(function(data, status) { console.error("Users Company Error: ", data); return status }); } function getCompanyUsersWithPlayerUsersInfo(companyId) { return $http.get(apiEndpointV2 + "companies/" + companyId + "/users/playerusers") .success(function(response) { return response; }) .catch(function(err) { return err; console.error(err); }); } function updateCompanyUserStatus(userId, userStatus) { return $http.post(apiEndpoint + "users/" + userId + "/isactive/" + +userStatus) .then(function(response) { return response; }) .catch(function(err) { return err; console.error(err); }); } function getCompanyUserRoles(userId) { return $http.get(apiEndpoint + "users/" + userId + "/roles") .success(function(data) { data.map(role => role.id = +role.id); that.companyUserRoles = data; }) .error(function(data, status) { console.error("Company User Roles Error: ", data); return status }); } function getRoles() { return $http.get(apiEndpoint + "users/rolespermissions") .success(function(data) { data.map(role => role.id = +role.id); that.roles = data; }) .error(function(data, status) { console.error("Get roles: ", data); return status }); } function updateCompanyUserRole(userId, roleId, addOrRemove) { if (roleId != 1) { var endpoint = apiEndpoint + "users/" + userId + "/role/" + roleId; console.log(endpoint); if (addOrRemove == true) { return $http.post(endpoint) .then(function(response) { return response; }) .catch(function(err) { return err; }); } else { return $http.delete(endpoint) .then(function(response) { return response; }) .catch(function(err) { return err; }); } } else { return ('Cannot update SuperUser role'); } } function updateAllUsersCompanyRole(roleId, companyId, addOrRemove) { if (roleId != 1) { var endpoint = apiEndpoint + "users/company/" + companyId + "/role/" + roleId; if (addOrRemove == true) { return $http.post(endpoint) .then(function(response) { return response; }) .catch(function(err) { return err; }); } else { return $http.delete(endpoint) .then(function(response) { return response; }) .catch(function(err) { return err; }); } } else { return ('Cannot update SuperUser role'); } } function createUser(companyId, accesscode, firstName, lastName, email, roles, isActive, leagueAffiliations) { return $http.post(apiEndpoint + "users/add", {companyid: companyId, accesscode: accesscode, firstname: firstName, lastname: lastName, email: email, roles: roles, isactive: isActive, leagueAffiliations: leagueAffiliations}) .success(function(data) { that.newUser = data; }) .error(function(data, status) { console.error("Error in creating user: ", data); return status }) } } })();