(function () { var mod = angular.module("userService", [ "angular-data.DSCacheFactory", //better caching functionality ]); mod.factory("userServiceDataContext", [ "$http", "$q", "$location", "userServiceConfiguration", "DSCacheFactory", "config", datacontext, ]); function datacontext( $http, $q, $location, config, DSCacheFactory, globalConfig ) { var userServiceAdminEndPoint = "UserService/api/organisationadmin/"; var userServiceOrganisationEndPoint = "UserService/api/organisations/"; var userServiceProfileEndPoint = "UserService/api/myprofiles/"; var accountEndpoint = "api/account/"; var orgsCache = "orgsCacheUserService"; var orgsFullCache = "orgsFullCacheUserService"; var allOrgsFullCacheName = "allorgsfullUserService"; var topLevelOrg; createOrgsCache(); createOrgsFullCache(); var service = { getOrganisationsFull: getOrganisationsFull, getOrganisationsFullSimple: getOrganisationsFullSimple, getOrganisationsFullSimpleBoth: getOrganisationsFullSimpleBoth, getOrganisationsFullFlat: getOrganisationsFullFlat, getOrganisationsFullFlatSimple: getOrganisationsFullFlatSimple, getOrganisationsFullIncludingUsers: getOrganisationsFullIncludingUsers, getOrganisations: getOrganisations, getOrganisationDetails: getOrganisationDetails, getOrganisationDetailsSimple: getOrganisationDetailsSimple, getOrganisationDetailsSimpleOther: getOrganisationDetailsSimpleOther, getOrganisationsForUser: getOrganisationsForUser, getUsers: getUsers, addChildOrganisation: addChildOrganisation, editOrganisation: editOrganisation, deleteOrganisation: deleteOrganisation, getInvitedUsers: getInvitedUsers, addInvitedUsers: addInvitedUsers, removeUsers: removeUsers, addAdministrators: addAdministrators, removeAdministrators: removeAdministrators, addUsers: addUsers, deleteInvitedUser: deleteInvitedUser, createNewParentOrg: createNewParentOrg, moveOrg: moveOrg, getAdministratorsForOrganisation: getAdministratorsForOrganisation, createGroup: createGroup, createJobRole: createJobRole, createTeam: createTeam, getAllFrameworkAssignmentDeploymentsForOrg: getAllFrameworkAssignmentDeploymentsForOrg, getFrameworksForOrg: getFrameworksForOrg, editFrameworksForOrg: editFrameworksForOrg, leaveOrganisation: leaveOrganisation, joinToOrganisation: joinToOrganisation, getAllGroupsAgainstOrg: getAllGroupsAgainstOrg, associateGroupWithOrg: associateGroupWithOrg, reAssociateGroupWithOrg: reAssociateGroupWithOrg, getOrganisationsAndGroups: getOrganisationsAndGroups, getAllocatedFrameworkOrg: getAllocatedFrameworkOrg, getAllUsersForAllUserOrganisations: getAllUsersForAllUserOrganisations, getTopLevelOrg: getTopLevelOrg, getUserIdsForOrg: getUserIdsForOrg, getNonAdminUserIdsForOrg: getNonAdminUserIdsForOrg, getFrameworkDetailAndEvidenceSummaryForUsers: getFrameworkDetailAndEvidenceSummaryForUsers, getAllMyFrameworkAssignmentDeployments: getAllMyFrameworkAssignmentDeployments, getOrganisationsFullSimpleMulti: getOrganisationsFullSimpleMulti, getOrganisationExternalId: getOrganisationExternalId, saveOrganisationExternalId: saveOrganisationExternalId, getUserProfile: getUserProfile, getOrganisationUserStatuses: getOrganisationUserStatuses, }; return service; function getOrganisationUserStatuses() { var request = $http({ method: "get", url: config.apigatewayUrl + userServiceAdminEndPoint + "organisationUserStatuses", }); return request.then(function (response) { return response.data; }, handleError); } function getUserProfile(userId) { var request = $http({ method: "get", url: config.apigatewayUrl + userServiceProfileEndPoint + "user/" + userId, params: { includefields: "title,firstname,lastname,emailaddresses,gender,dateofbirth,country,avatarurl,avatartype,managedaccount,addresses,createdon,associatedUsers", }, }); return request.then(function (response) { return response.data; }, handleError); } function getTopLevelOrg() { if (!topLevelOrg) { } return topLevelOrg; } function getUsers(orgId) { var request = $http({ method: "get", url: config.apigatewayUrl + userServiceAdminEndPoint + orgId + "/users", }); return request.then(handleSuccess, handleError); } function createOrgsCache() { var cache = DSCacheFactory(orgsCache, { storageMode: "localStorage" }); cache.setOptions({ maxAge: 100000, deleteOnExpire: "aggressive", }); return cache; } function createOrgsFullCache() { var cache = DSCacheFactory(orgsFullCache, { storageMode: "localStorage", }); cache.setOptions({ maxAge: 10000, deleteOnExpire: "aggressive", }); return cache; } function getAdministratorsForOrganisation(orgId) { var request = $http({ method: "get", url: config.apigatewayUrl + userServiceAdminEndPoint + orgId + "/administrators", }); return request.then(handleSuccess, handleError); } function filterOrgs(orgs, orgType) { var filteredOrgs = orgs.filter(function (org) { return org.type === orgType; }); if (filteredOrgs.length === 0) { filteredOrgs = []; //Because there could be multiple orgs, find all orgs in all structures of the selected orgType $.each(orgs, function (i) { var validOrgs = orgs[i].children.filter(function (org) { return org.type === orgType; }); if (validOrgs.length) { $.each(validOrgs, function (index) { filteredOrgs.push(validOrgs[index]); }); } }); } var resArr = []; filteredOrgs.filter(function (org) { var index = -1; for (var i = 0; i < resArr.length; ++i) { if (resArr[i].id === org.id) { index = i; break; } } if (index <= -1) { resArr.push(org); } return null; }); return resArr; } function getOrganisationsFull(orgType) { var request = $http({ method: "get", url: config.apigatewayUrl + userServiceAdminEndPoint + "?orgTypeId=" + orgType + "&cache=" + Date.now(), }); return request.then(function (response) { var filteredOrgs = filterOrgs(response.data, orgType); return filteredOrgs; }, handleError); } function getOrganisationsFullSimple(orgType) { var request = $http({ method: "get", url: config.apigatewayUrl + userServiceAdminEndPoint + "/simple/?orgTypeId=" + orgType + "&cache=" + Date.now(), }); return request.then(function (response) { var filteredOrgs = filterOrgs(response.data, orgType); return filteredOrgs; }, handleError); } function getOrganisationsFullSimpleBoth(orgType, groupTypeId) { var request = $http({ method: "get", url: config.apigatewayUrl + userServiceAdminEndPoint + "simple/?orgTypeId=" + orgType + "&groupTypeId=" + groupTypeId + "&cache=" + Date.now(), }); return request.then(function (response) { var filteredOrgs = filterOrgs(response.data, orgType); return filteredOrgs; }, handleError); } function getOrganisationsFullSimpleMulti( orgType, groupTypeId, otherGroupTypeId ) { var request = $http({ method: "get", url: config.apigatewayUrl + userServiceAdminEndPoint + "simple/other/?orgTypeId=" + orgType + "&groupTypeId=" + groupTypeId + "&otherGroupTypeId=" + otherGroupTypeId + "&cache=" + Date.now(), }); return request.then(function (response) { var filteredOrgs = filterOrgs(response.data, orgType); return filteredOrgs; }, handleError); } /* * Get the full org structure for this user including the users in those orgs */ function getOrganisationsFullIncludingUsers(orgType) { var cache = DSCacheFactory.get(orgsFullCache); //if (cache) { // var cacheOrgs = cache.get(allOrgsFullCacheName); // if (cacheOrgs) { // var filteredOrgs = filterOrgs(cacheOrgs, orgType); // var deferred = $q.defer(); // deferred.resolve(filteredOrgs); // return deferred.promise; // } //} else // cache = createOrgsFullCache(); var request = $http({ method: "get", url: config.apigatewayUrl + userServiceAdminEndPoint + "/orgswithusers?orgTypeId=" + orgType, }); return request.then(function (response) { //cache.put(allOrgsFullCacheName, response.data); var filteredOrgs = filterOrgs(response.data, orgType); return filteredOrgs; }, handleError); } function getOrganisationsAndGroups() { var request = $http({ method: "get", url: config.apigatewayUrl + userServiceAdminEndPoint, }); return request.then(function (response) { return response.data; }, handleError); } function getOrganisations() { var request = $http({ method: "get", url: config.apigatewayUrl + userServiceOrganisationEndPoint, }); return request.then(function (response) { var index = -1; for (var i = 0; i < response.data.length; i++) { if (response.data[i].type === config.organisationTypeId) response.data[i].isOrg = true; else response.data[i].isGroup = true; //We check if this org is the 'no organisation' org if (response.data[i].id === noOrgId) { index = i; break; } } if (index > -1) response.data.splice(index, 1); return response.data; }, handleError); } function getOrganisationExternalId(orgId) { var request = $http({ method: "get", url: config.apigatewayUrl + userServiceAdminEndPoint + "externalId/" + orgId, }); return request.then(function (response) { if (response.data === null) response.data = ""; return response.data; }, handleError); } function saveOrganisationExternalId(orgId, externalModel) { var request = $http({ method: "post", url: config.apigatewayUrl + userServiceAdminEndPoint + "externalId/" + orgId, data: externalModel, }); return request.then(function (response) { if (response.data === null) response.data = ""; return response.data; }, handleError); } function getOrganisationDetailsSimple( orgId, organisationTypeId, groupTypeId ) { organisationTypeId = organisationTypeId ? organisationTypeId : config.organisationTypeId; groupTypeId = groupTypeId ? groupTypeId : config.groupTypeId; var request = $http({ method: "get", url: config.apigatewayUrl + userServiceAdminEndPoint + "simple/" + orgId + "?orgTypeId=" + organisationTypeId + "&groupTypeId=" + groupTypeId + "&cache=" + Date.now(), }); return request.then(function (response) { if (response.data.validEmailDomains === null) response.data.validEmailDomains = []; return response.data; }, handleError); } function getOrganisationDetailsSimpleOther( orgId, organisationTypeId, groupTypeId, otherGroupTypeId ) { organisationTypeId = organisationTypeId ? organisationTypeId : config.organisationTypeId; groupTypeId = groupTypeId ? groupTypeId : config.groupTypeId; var request = $http({ method: "get", url: config.apigatewayUrl + userServiceAdminEndPoint + "simple/other/" + orgId + "?orgTypeId=" + organisationTypeId + "&groupTypeId=" + groupTypeId + "&otherGroupTypeId=" + otherGroupTypeId + "&cache=" + Date.now(), }); return request.then(function (response) { if (response.data.validEmailDomains === null) response.data.validEmailDomains = []; return response.data; }, handleError); } function getOrganisationDetails(orgId, organisationTypeId, groupTypeId) { organisationTypeId = organisationTypeId ? organisationTypeId : config.organisationTypeId; groupTypeId = groupTypeId ? groupTypeId : config.groupTypeId; var request = $http({ method: "get", url: config.apigatewayUrl + userServiceAdminEndPoint + orgId + "?orgTypeId=" + organisationTypeId + "&groupTypeId=" + groupTypeId + "&cache=" + Date.now(), }); return request.then(function (response) { if (response.data.validEmailDomains === null) response.data.validEmailDomains = []; return response.data; }, handleError); } function getOrganisationsForUser(userId) { var request = $http({ method: "get", url: config.apigatewayUrl + userServiceOrganisationEndPoint + "user/" + userId, }); return request.then(function (response) { return response.data; }, handleError); } function getOrganisationsFullFlat(orgType) { var cache = DSCacheFactory.get(orgsFullCache); if (cache) { var cacheOrgs = cache.get(allOrgsFullCacheName); if (cacheOrgs) { var filteredOrgs = filterOrgs(cacheOrgs, orgType); var deferred = $q.defer(); deferred.resolve(filteredOrgs); return deferred.promise; } } else cache = createOrgsFullCache(); var request = $http({ method: "get", url: config.apigatewayUrl + userServiceAdminEndPoint + "FlatList?orgTypeId=" + orgType, }); return request.then(function (response) { cache.put(allOrgsFullCacheName, response.data); var filteredOrgs = filterOrgs(response.data, orgType); return filteredOrgs; }, handleError); } function getOrganisationsFullFlatSimple() { var request = $http({ method: "get", url: config.apigatewayUrl + userServiceAdminEndPoint + "FlatList/Simple", }); return request.then(function (response) { return response.data; }, handleError); } function deleteOrganisation(orgId) { DSCacheFactory.get(orgsCache).removeAll(); DSCacheFactory.get(orgsFullCache).removeAll(); var request = $http({ method: "delete", url: config.apigatewayUrl + userServiceAdminEndPoint + orgId, }); return request.then(handleSuccess, handleError); } function createGroup(group) { group.type = group.type ? group.type : config.groupTypeId; DSCacheFactory.get(orgsCache).removeAll(); DSCacheFactory.get(orgsFullCache).removeAll(); var request = $http({ method: "post", url: config.apigatewayUrl + userServiceAdminEndPoint, data: group, }); return request.then(function (response) { return response.data; }, handleError); } function createJobRole(jobRole) { jobRole.type = config.jobRoleTypeId; DSCacheFactory.get(orgsCache).removeAll(); DSCacheFactory.get(orgsFullCache).removeAll(); var request = $http({ method: "post", url: config.apigatewayUrl + userServiceAdminEndPoint, data: jobRole, }); return request.then(function (response) { return response.data; }, handleError); } function createTeam(team) { team.type = config.teamTypeId; DSCacheFactory.get(orgsCache).removeAll(); DSCacheFactory.get(orgsFullCache).removeAll(); var request = $http({ method: "post", url: config.apigatewayUrl + userServiceAdminEndPoint, data: team, }); return request.then(function (response) { return response.data; }, handleError); } function addChildOrganisation(orgId, childOrg) { DSCacheFactory.get(orgsCache).removeAll(); DSCacheFactory.get(orgsFullCache).removeAll(); var request = $http({ method: "post", url: config.apigatewayUrl + userServiceAdminEndPoint + orgId, data: childOrg, }); return request.then(handleSuccess, handleError); } function editOrganisation(orgId, org) { DSCacheFactory.get(orgsCache).removeAll(); DSCacheFactory.get(orgsFullCache).removeAll(); var request = $http({ method: "put", url: config.apigatewayUrl + userServiceAdminEndPoint + orgId, data: org, }); return request.then(handleSuccess, handleError); } function removeUsers(orgId, users) { var request = $http({ method: "delete", url: config.apigatewayUrl + userServiceAdminEndPoint + orgId + "/users", //TODO - change to DELETE /users when API changes data: users, headers: { "Content-Type": "text/json", }, }); return request.then(handleSuccess, handleError); } function addUsers(orgId, users) { var request = $http({ method: "post", url: config.apigatewayUrl + userServiceAdminEndPoint + orgId + "/users", data: users, }); return request.then(handleSuccess, handleError); } function addAdministrators(orgId, users, orgAdmin, groupAdmin) { //Set roles api $http({ method: "post", url: config.apigatewayUrl + accountEndpoint + "roles/admin", data: { userIds: users, roles: ["mybadges", "myshowcase"], orgAdmin: orgAdmin, groupAdmin: groupAdmin, }, }).then( function () {}, function () {} ); //Set org admin api var request = $http({ method: "post", url: config.apigatewayUrl + userServiceAdminEndPoint + orgId + "/administrators", data: users, }); return request.then(handleSuccess, handleError); } function removeAdministrators(orgId, users) { var request = $http({ method: "delete", url: config.apigatewayUrl + userServiceAdminEndPoint + orgId + "/administrators", data: users, headers: { "Content-Type": "text/json", }, }); return request.then(handleSuccess, handleError); } //this returns an array of invited user objects function addInvitedUsers(orgId, emails) { var request = $http({ method: "post", url: config.apigatewayUrl + userServiceAdminEndPoint + orgId + "/invites", data: emails, }); return request.then(handleSuccess, handleError); } function deleteInvitedUser(inviteId) { var request = $http({ method: "delete", url: config.apigatewayUrl + userServiceAdminEndPoint + "/invites/" + inviteId, }); return request.then(handleSuccess, handleError); } function getInvitedUsers(orgId) { var request = $http({ method: "get", url: config.apigatewayUrl + userServiceAdminEndPoint + orgId + "/invites", }); return request.then(handleSuccess, handleError); } //new org has name, description props function createNewParentOrg(orgId, newOrg) { var request = $http({ method: "post", url: config.apigatewayUrl + userServiceAdminEndPoint + orgId + "/addparent", data: newOrg, }); return request.then(function (response) { newOrg.id = response.data.replace(/"/g, ""); return newOrg; }, handleError); } //newParent has props of id of new parent org and order of its position in the list function moveOrg(orgId, newParent) { var request = $http({ method: "post", url: config.apigatewayUrl + userServiceAdminEndPoint + orgId + "/move", data: newParent, }); return request.then(handleSuccess, handleError); } //get all the deployed framework assignments visible to this user for this org function getAllFrameworkAssignmentDeploymentsForOrg(orgId, userId) { var request = $http({ method: "get", url: globalConfig.apigatewayUrl + "AssignmentDeployedToUser/ids?ids=" + orgId + "&ids=" + userId, }); return request .then(function (response) { return response.data; }) .catch(function (reason) { //TODO: log reason return []; }); } function getFrameworkDetailAndEvidenceSummaryForUsers( deployedAssignmentId, groupId ) { var request = $http({ method: "get", url: globalConfig.apigatewayUrl + "deployedassignment/evidenceforother/" + deployedAssignmentId + "/group/" + groupId, }); return request .then(function (response) { return response.data; }) .catch(function (reason) { //TODO: log reason return []; }); } //get all the deployed framework assignments for this user function getAllMyFrameworkAssignmentDeployments() { //url: 'https://mkm-apigateway-dev.azurewebsites.net/deployedassignment/formewithgroups', var request = $http({ method: "get", url: globalConfig.apigatewayUrl + "deployedassignment/forotherswithgroups", }); return request .then(function (response) { return response.data.deploymentSummaries; }) .catch(function (reason) { //TODO: log reason return []; }); } //HS replaced in admin framework reports function getFrameworksForOrg(orgId) { var request = $http({ method: "get", url: config.apigatewayUrl + userServiceOrganisationEndPoint + orgId + "/frameworks", }); return request.then(function (response) { return response.data; }, handleError); } function editFrameworksForOrg(orgId, frameworkIds) { var request = $http({ method: "put", url: config.apigatewayUrl + userServiceAdminEndPoint + orgId + "/frameworks", data: frameworkIds, }); return request.then(function (response) { return response.data; }, handleError); } function leaveOrganisation(orgId) { DSCacheFactory.get(orgsCache).removeAll(); DSCacheFactory.get(orgsFullCache).removeAll(); var request = $http({ method: "post", url: config.apigatewayUrl + userServiceOrganisationEndPoint + orgId + "/leave", }); return request.then(handleSuccess, handleError); } function joinToOrganisation(orgId) { DSCacheFactory.get(orgsCache).removeAll(); DSCacheFactory.get(orgsFullCache).removeAll(); var request = $http({ method: "post", url: config.apigatewayUrl + userServiceOrganisationEndPoint + orgId + "/join", }); return request.then(handleSuccess, handleError); } function getAllGroupsAgainstOrg(orgId) { var request = $http({ method: "get", url: config.apigatewayUrl + "api/organisationadministration/organisationgroups/" + orgId, }); return request.then(handleSuccess, handleError); } function associateGroupWithOrg(groups, orgId) { var request = $http({ method: "post", url: config.apigatewayUrl + "api/organisationadministration/organisationgroups", data: { organisationId: orgId, groups: groups, }, }); return request.then(handleSuccess, handleError); } function reAssociateGroupWithOrg(groups, orgId) { var request = $http({ method: "put", url: config.apigatewayUrl + "api/organisationadministration/organisationgroups", data: { organisationId: orgId, groups: groups, }, }); return request.then(handleSuccess, handleError); } function getAllocatedFrameworkOrg(frameworkId, userId) { var request = $http({ method: "get", url: config.apigatewayUrl + "api/frameworks/" + frameworkId + "/allocated/organisations/user/" + userId, }); return request.then(handleSuccess, handleError); } function getAllUsersForAllUserOrganisations() { var request = $http({ method: "get", url: config.apigatewayUrl + userServiceOrganisationEndPoint + "currentuser/organisations/ignore/" + globalConfig.noOrgId + "/users?cache=" + Date.now(), }); return request.then(handleSuccess, handleError); } function getUserIdsForOrg(orgId) { var request = $http({ method: "get", url: config.apigatewayUrl + userServiceOrganisationEndPoint + orgId + "/userids", }); return request.then(handleSuccess, handleError); } function getNonAdminUserIdsForOrg(orgId) { var request = $http({ method: "get", url: config.apigatewayUrl + userServiceOrganisationEndPoint + orgId + "/nonadminusers/userids", }); return request.then(handleSuccess, handleError); } 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 (response.status === 404) { return $q.reject("Not available"); } if (response.status === 403) { return $q.reject( "You do not have permissions to perform this operation" ); } 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); } // I transform the successful response, unwrapping the application data // from the API response payload. function handleSuccess(response) { return response.data; } } var conf = { apigatewayUrl: apigatewayUrl, notificationsRemoteServiceUrl: notificationsRemoteServiceUrl, organisationTypeId: organisationTypeId, groupTypeId: groupTypeId, teamTypeId: teamTypeId, jobRoleTypeId: jobRoleTypeId }; mod.value("userServiceConfiguration", conf); })();