(function () { var mod = angular.module('myusers', [ 'angular-data.DSCacheFactory' //better caching functionality ]); mod.factory('myUsersDataContext', ['$http', '$q', '$location', 'myUsersConfiguration', 'DSCacheFactory', 'config', datacontext]); function datacontext($http, $q, $location, config, DSCacheFactory, globalConfig) { var orgUserEndpoint = 'api/organisations/'; var orgAdminEndpoint = 'api/organisationadministration/'; var accountEndpoint = 'api/account/'; var orgIssuerEndpoint = 'issuers/'; var orgsCache = 'orgsCache'; var orgsFullCache = 'orgsFullCache'; var allOrgsCacheName = 'allorgs'; var allOrgsFullCacheName = 'allorgsfull'; var orgsFrameworksCache = 'orgsFrameworksCache'; var allOrgsFrameworksCache = 'allOrgsFrameworksCache'; var allOrgsFrameworksInParentHierarchyCache = 'allOrgsFrameworksInParentHierarchyCache'; var allOrgsFrameworksCacheName = 'allorgsframeworks'; var topLevelOrg; createOrgsCache(); createOrgsFullCache(); createOrgsFrameworksCache(); createAllOrgsFrameworksCache(); var service = { getOrganisationsFull: getOrganisationsFull, getOrganisationsFullSimple: getOrganisationsFullSimple, getOrganisationsFullSimpleBoth: getOrganisationsFullSimpleBoth, getOrganisationsFullFlat: getOrganisationsFullFlat, getOrganisationsFullFlatSimple: getOrganisationsFullFlatSimple, getOrganisationsFullIncludingUsers: getOrganisationsFullIncludingUsers, getOrganisations: getOrganisations, getOrganisationDetails: getOrganisationDetails, getOrganisationDetailsSimple: getOrganisationDetailsSimple, getOrganisationDetailsSimpleOther: getOrganisationDetailsSimpleOther, getOrganisationsForUser: getOrganisationsForUser, getSimpleOrganisation: getSimpleOrganisation, 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, sendInviteEmails: sendInviteEmails, getAdministratorsForOrganisation: getAdministratorsForOrganisation, createGroup: createGroup, createJobRole: createJobRole, createTeam: createTeam, getAllFrameworkAssignmentDeploymentsForOrg: getAllFrameworkAssignmentDeploymentsForOrg, getFrameworksForOrg: getFrameworksForOrg, editFrameworksForOrg: editFrameworksForOrg, getDiscoverableOrgNames: getDiscoverableOrgNames, leaveOrganisation: leaveOrganisation, joinToOrganisation: joinToOrganisation, getAllOrgFrameworks: getAllOrgFrameworks, getAllOrgFrameworksInParentHierarchy: getAllOrgFrameworksInParentHierarchy, acceptOrganisationInvite: acceptOrganisationInvite, deleteOrganisationInvite: deleteOrganisationInvite, getOrganisationIssuer: getOrganisationIssuer, createOrganisationIssuer: createOrganisationIssuer, deleteOrganisationIssuer: deleteOrganisationIssuer, updateOrganisationIssuer: updateOrganisationIssuer, getAllGroupsAgainstOrg: getAllGroupsAgainstOrg, associateGroupWithOrg: associateGroupWithOrg, reAssociateGroupWithOrg: reAssociateGroupWithOrg, getOrganisationsAndGroups: getOrganisationsAndGroups, getAllocatedFrameworkOrg: getAllocatedFrameworkOrg, getAllUsersForAllUserOrganisations: getAllUsersForAllUserOrganisations, //getAllocatedFrameworkOrg: getAllocatedFrameworkOrg, getTopLevelOrg: getTopLevelOrg, getUserIdsForOrg: getUserIdsForOrg, getNonAdminUserIdsForOrg: getNonAdminUserIdsForOrg, getFrameworkDetailAndEvidenceSummaryForUsers: getFrameworkDetailAndEvidenceSummaryForUsers, getAllMyFrameworkAssignmentDeployments: getAllMyFrameworkAssignmentDeployments, getOrganisationsFullSimpleMulti: getOrganisationsFullSimpleMulti }; return service; function getTopLevelOrg() { if (!topLevelOrg) { } return topLevelOrg; } function getDiscoverableOrgNames(emailAddresses,query) { var request = $http({ method: "post", url: config.myusersUrl + orgUserEndpoint + 'discover/', data: { emailAddresses: emailAddresses ,query:query} }); return (request.then(handleSuccess, handleError)); } function getUsers(orgId) { var request = $http({ method: "get", url: config.myusersUrl + orgAdminEndpoint + 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 createOrgsFrameworksCache() { var cache = DSCacheFactory(orgsFrameworksCache, { storageMode: 'localStorage' }); cache.setOptions({ maxAge: 10000, deleteOnExpire: 'aggressive' }); return cache; } function createAllOrgsFrameworksCache() { var cache = DSCacheFactory(allOrgsFrameworksCache, { storageMode: 'localStorage' }); cache.setOptions({ maxAge: 10000, deleteOnExpire: 'aggressive' }); return cache; } function createAllOrgsInParentHierarchyFrameworksCache() { var cache = DSCacheFactory(allOrgsFrameworksInParentHierarchyCache, { storageMode: 'localStorage' }); cache.setOptions({ maxAge: 10000, deleteOnExpire: 'aggressive' }); return cache; } function getAdministratorsForOrganisation(orgId) { var request = $http({ method: "get", url: config.myusersUrl + orgAdminEndpoint + 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.myusersUrl + orgAdminEndpoint + '?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.myusersUrl + orgAdminEndpoint + '/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.myusersUrl + orgAdminEndpoint + '/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.myusersUrl + orgAdminEndpoint + '/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.myusersUrl + orgAdminEndpoint + "/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.myusersUrl + orgAdminEndpoint }); return (request.then(function (response) { return response.data; }, handleError)); } function getOrganisations() { var request = $http({ method: "get", url: config.myusersUrl + orgUserEndpoint }); 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 sendInviteEmails(inviteData) { var request = $http({ method: "post", url: config.notificationsRemoteServiceUrl + 'organisation/invite', data: inviteData }); return (request.then(handleSuccess, handleError)); } function getSimpleOrganisation(orgId) { var request = $http({ method: "get", url: config.myusersUrl + orgUserEndpoint + orgId + '?cache=' + Date.now() }); return (request.then( function (response) { if (response.data.validEmailDomains === null) response.data.validEmailDomains = []; 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.myusersUrl + orgAdminEndpoint + "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.myusersUrl + orgAdminEndpoint + "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.myusersUrl + orgAdminEndpoint + 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.myusersUrl + orgUserEndpoint + '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.myusersUrl + orgAdminEndpoint + "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.myusersUrl + orgAdminEndpoint + "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.myusersUrl + orgAdminEndpoint + 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.myusersUrl + orgAdminEndpoint, 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.myusersUrl + orgAdminEndpoint, 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.myusersUrl + orgAdminEndpoint, 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.myusersUrl + orgAdminEndpoint + 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.myusersUrl + orgAdminEndpoint + orgId, data: org }); return (request.then(handleSuccess, handleError)); } function removeUsers(orgId, users) { var request = $http({ method: "delete", url: config.myusersUrl + orgAdminEndpoint + 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.myusersUrl + orgAdminEndpoint + orgId + '/users', data: users }); return (request.then(handleSuccess, handleError)); } function addAdministrators(orgId, users, orgAdmin, groupAdmin) { //Set roles api $http({ method: "post", url: config.myusersUrl + 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.myusersUrl + orgAdminEndpoint + orgId + '/administrators', data: users }); return (request.then(handleSuccess, handleError)); } function removeAdministrators(orgId, users) { var request = $http({ method: "delete", url: config.myusersUrl + orgAdminEndpoint + 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.myusersUrl + orgAdminEndpoint + orgId + '/invites', data: emails }); return (request.then(handleSuccess, handleError)); } function deleteInvitedUser(inviteId) { var request = $http({ method: "delete", url: config.myusersUrl + orgAdminEndpoint + '/invites/' + inviteId }); return (request.then(handleSuccess, handleError)); } function getInvitedUsers(orgId) { var request = $http({ method: "get", url: config.myusersUrl + orgAdminEndpoint + orgId + '/invites' }); return (request.then(handleSuccess, handleError)); } //new org has name, description props function createNewParentOrg(orgId, newOrg) { var request = $http({ method: "post", url: config.myusersUrl + orgAdminEndpoint + 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.myusersUrl + orgAdminEndpoint + 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.myusersUrl + orgUserEndpoint + orgId + '/frameworks' }); return (request.then(function (response) { return response.data; }, handleError)); } function editFrameworksForOrg(orgId, frameworkIds) { var request = $http({ method: "put", url: config.myusersUrl + orgAdminEndpoint + 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.myusersUrl + 'api/organisations/' + 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.myusersUrl + 'api/organisations/' + orgId + '/join' }); return (request.then(handleSuccess, handleError)); } function getAllOrgFrameworks() { var request = $http({ method: "get", url: config.myusersUrl + orgUserEndpoint + 'frameworks' }); return (request.then(function (response) { return response.data; }, handleError)); } function getAllOrgFrameworksInParentHierarchy() { var request = $http({ method: "get", url: config.myusersUrl + orgUserEndpoint + 'frameworks/groupallocated' }); return (request.then(function (response) { return response.data; }, handleError)); } function acceptOrganisationInvite(organisationInviteId) { var request = $http({ method: "post", url: config.myusersUrl + 'api/organisations/invite/' + organisationInviteId }); return (request.then(handleSuccess, handleError)); } function deleteOrganisationInvite(organisationInviteId) { var request = $http({ method: "delete", url: config.myusersUrl + 'api/organisations/invite/' + organisationInviteId }); return (request.then(function () { return; }, handleError)); } // Mybadges issuer creation, get and delete function getOrganisationIssuer(orgId) { var request = $http({ method: "get", url: config.mybadgesUrl + 'organisation/' + orgId + '/' + orgIssuerEndpoint }); return (request.then(handleSuccess, handleError)); } function deleteOrganisationIssuer(orgId) { var request = $http({ method: "delete", url: config.mybadgesUrl + orgIssuerEndpoint + orgId }); return (request.then(handleSuccess, handleError)); } function createOrganisationIssuer(issuer) { var request = $http({ method: "post", url: config.mybadgesUrl + orgIssuerEndpoint, data: issuer }); return (request.then(handleSuccess, handleError)); } function updateOrganisationIssuer(issuer) { var request = $http({ method: "put", url: config.mybadgesUrl + orgIssuerEndpoint + issuer.id, data: issuer }); return (request.then(handleSuccess, handleError)); } function getAllGroupsAgainstOrg(orgId) { var request = $http({ method: "get", url: config.myusersUrl + 'api/organisationadministration/organisationgroups/' + orgId }); return (request.then(handleSuccess, handleError)); } function associateGroupWithOrg(groups, orgId) { var request = $http({ method: "post", url: config.myusersUrl + 'api/organisationadministration/organisationgroups', data: { organisationId: orgId, groups: groups } }); return (request.then(handleSuccess, handleError)); } function reAssociateGroupWithOrg(groups, orgId) { var request = $http({ method: "put", url: config.myusersUrl + 'api/organisationadministration/organisationgroups', data: { organisationId: orgId, groups: groups } }); return (request.then(handleSuccess, handleError)); } function getAllocatedFrameworkOrg(frameworkId, userId) { var request = $http({ method: "get", url: config.myusersUrl + 'api/frameworks/' + frameworkId + '/allocated/organisations/user/' + userId }); return (request.then(handleSuccess, handleError)); } function getAllUsersForAllUserOrganisations() { var request = $http({ method: "get", url: config.myusersUrl + 'api/organisations/currentuser/organisations/ignore/' + globalConfig.noOrgId + '/users?cache=' + Date.now() }); return (request.then(handleSuccess, handleError)); } function getUserIdsForOrg(orgId) { var request = $http({ method: "get", url: config.myusersUrl + 'api/organisations/' + orgId + '/userids' }); return (request.then(handleSuccess, handleError)); } function getNonAdminUserIdsForOrg(orgId) { var request = $http({ method: "get", url: config.myusersUrl + 'api/organisations/' + 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 = { myusersUrl: myusersUrl, mybadgesUrl: mybadgesUrl, notificationsRemoteServiceUrl: notificationsRemoteServiceUrl, organisationTypeId: organisationTypeId, groupTypeId: groupTypeId, teamTypeId: teamTypeId, jobRoleTypeId: jobRoleTypeId }; mod.value('myUsersConfiguration', conf); })();