(function () { 'use strict'; var serviceId = 'licencingDataContext'; angular.module('licencing').factory(serviceId, ['$q', '$http', 'licencingConfig', 'DSCacheFactory','myUsersDataContext', datacontext]); function datacontext($q, $http, config, DSCacheFactory, myUsersDataContext) { var service = { getLicencingAllocationForOrg: getLicencingAllocationForOrg, getMatchingOrganisationsThatRequireLicencing: getMatchingOrganisationsThatRequireLicencing, editLicencingAllocation: editLicencingAllocation, createLicencingAllocation: createLicencingAllocation }; return service; 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)); } // I transform the successful response, unwrapping the application data // from the API response payload. function handleSuccess(response) { return (response.data); } //create a new licencing allocation function createLicencingAllocation(licencingAllocation) { var request = $http({ method: "post", url: config.licencingRemoteServiceUrl, data: licencingAllocation }); return (request.then(handleSuccess, handleError)); } //edit an existing licencing allocation function editLicencingAllocation(licencingAllocation) { var request = $http({ method: "put", url: config.licencingRemoteServiceUrl, data: licencingAllocation }); return (request.then(handleSuccess, handleError)); } //get a licencing application for a specific organisation function getLicencingAllocationForOrg(organisationId) { var request = $http({ method: "get", url: config.licencingRemoteServiceUrl + organisationId }); return (request.then(function (response) { return response.data; }, handleError)); } function getMatchingOrganisationsThatRequireLicencing() { //first get all orgs that we have access to return myUsersDataContext.getOrganisationsFull(organisationTypeId).then(function(orgs) { var orgsWeNeed = []; //get the id for each org and put into an array for (var i = 0; i < orgs.length; i++) { orgsWeNeed.push(orgs[i].id); } //now call the licencing remorte service passing in the organisation ids var request = $http({ method: "post", url: config.licencingRemoteServiceUrl + 'organisations', data: {orgIds:orgsWeNeed} }); return (request.then(function (response) { var allocations = response.data; var filteredOrgs = []; //not all orgs may have associated licences, for ones that do add the licence object to the org object and return for (var i = 0; i < allocations.length; i++) { for (var j = 0; j < orgs.length; j++) { if (allocations[i].organisationId === orgs[j].id) { orgs[j].requiresLicencing = true; orgs[j].licencingAllocation = allocations[i]; orgs[j].numUsers = 876; //TODO - this should come from myusers - how many users are there in the organisation filteredOrgs.push(orgs[j]); break; } } } return filteredOrgs; }, handleError)); }); } } })();