(function () { 'use strict'; var serviceId = 'rolesDataContext'; angular.module('roles').factory(serviceId, ['$q', '$http', 'rolesConfig', datacontext]); function datacontext($q, $http, config) { var service = { createRoleAllocation: createRoleAllocation, removeRoleAllocation: removeRoleAllocation, getAllRoleAllocations: getAllRoleAllocations, setLastActive: setLastActive }; 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); } function getAllRoleAllocations(appCode) { var request = $http({ method: "get", url: rolesUrl + 'api/roletokens/roleallocations/user/' + appCode + '?cache=' + Date.now(), }); return (request.then(handleSuccess, handleError)); } // Create a new role allocation function createRoleAllocation(roleAllocation) { var request = $http({ method: "post", url: config.rolesUrl + 'api/roleallocations', data: roleAllocation }); return (request.then(handleSuccess, handleError)); } // Delete a role allocation function removeRoleAllocation(roleAllocation) { var request = $http({ method: "put", url: config.rolesUrl + 'api/roleallocations/', data: roleAllocation }); return (request.then(handleSuccess, handleError)); } // Set last active role function setLastActive(roleAllocationId, appCode, userId) { var request = $http({ method: "put", url: rolesUrl + 'api/roleallocations/lastactive/', data: { id: roleAllocationId, userId: userId, appCode: appCode, lastActive: true } }); return (request.then(handleSuccess, handleError)); } } })();