(function () { 'use strict'; var serviceId = 'rolesAdminDataContext'; angular.module('rolesAdmin').factory(serviceId, ['$q', '$http', 'rolesAdminConfig', datacontext]); function datacontext($q, $http, config, $modal) { var service = { getAllRoles: getAllRoles, getAllRoleFunctions: getAllRoleFunctions, getDefaultRoles: getDefaultRoles, setSystemAdminDefaultRoles: setSystemAdminDefaultRoles, createRole: createRole, updateRole: updateRole, deleteRole: deleteRole, createRoleAllocation: createRoleAllocation, deleteRoleAllocation: deleteRoleAllocation, getAllRoleAllocations: getAllRoleAllocations, getAllRoleAllocationsForOrgForUser: getAllRoleAllocationsForOrgForUser, getAllRoleAllocationsForManyUsers: getAllRoleAllocationsForManyUsers, getAllocationsForOrgForManyUsers: getAllocationsForOrgForManyUsers, updateRoleAllocation: updateRoleAllocation, setLastActive: setLastActive, removeRoleAllocation: removeRoleAllocation, getRoleAllocation: getRoleAllocation, removeRoleAllocationsForUserInOrg: removeRoleAllocationsForUserInOrg }; 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) { if (response) return (response.data); else return null; } // Get all roles for app code function getAllRoles(appCode) { var request = $http({ method: "get", url: rolesUrl + 'api/roles/' + appCode + '?cache=' + Date.now(), }); return (request.then(handleSuccess, handleError)); } // Get all role functions for app code function getAllRoleFunctions(appCode) { var request = $http({ method: "get", url: rolesUrl + 'api/functions/' + appCode, }); return (request.then(handleSuccess, handleError)); } // Create then return the system default roles, SystemAdmin, Admin, User function getDefaultRoles(appCode, orgId) { var request = $http({ method: "get", url: rolesUrl + 'api/roles/' + appCode + '/default/' + orgId }); return (request.then(handleSuccess, handleError)); } // Set the default roles for the current system admin user function setSystemAdminDefaultRoles(appCodes, roleNames, orgId, userId) { var request = $http({ method: "post", data: { appCodes: appCodes, roleNames: roleNames, orgId: orgId, userId: userId }, url: rolesUrl + 'api/roles/default/' }); return (request.then(handleSuccess, handleError)); } // Create a new role function createRole(role) { var request = $http({ method: "post", url: rolesUrl + 'api/roles', data: role }); return (request.then(handleSuccess, handleError)); } // Update an existing role function updateRole(role) { var request = $http({ method: "put", url: rolesUrl + 'api/roles/' + role.id, data: role }); return (request.then(handleSuccess, handleError)); } // Delete an existing role function deleteRole(role) { var request = $http({ method: "delete", url: rolesUrl + 'api/roles/' + role.id, }); return (request.then(handleSuccess, handleError)); } // Create a new role allocation function createRoleAllocation(roleAllocation) { var request = $http({ method: "post", url: rolesUrl + 'api/roleallocations', data: roleAllocation }); return (request.then(handleSuccess, handleError)); } // Create a new role allocation function updateRoleAllocation(roleAllocation) { var request = $http({ method: "put", url: rolesUrl + 'api/roleallocations/' + roleAllocation.id, data: roleAllocation }); return (request.then(handleSuccess, handleError)); } // Delete a role allocation function deleteRoleAllocation(roleAllocation) { var request = $http({ method: "delete", url: rolesUrl + 'api/roleallocations/' + roleAllocation.id, data: roleAllocation }); return (request.then(handleSuccess, handleError)); } // Delete a role allocation function removeRoleAllocation(roleAllocation) { var request = $http({ method: "put", url: rolesUrl + 'api/roleallocations/', data: roleAllocation }); return (request.then(handleSuccess, handleError)); } // Get all role functions for app code function getAllRoleAllocations(appCode) { var request = $http({ method: "get", url: rolesUrl + 'api/roletokens/roleallocations/user/' + appCode + '?cache=' + Date.now(), }); return (request.then(handleSuccess, handleError)); } // Return all role allocations for a list of users for a given AppCode function getAllRoleAllocationsForManyUsers(appCode, userIds) { var request = $http({ method: 'post', url: rolesUrl + 'api/roleallocations/users/appcode/' + appCode, data: userIds }); return (request.then(handleSuccess, handleError)); } // Get all role functions for app code and org function getAllRoleAllocationsForOrgForUser(appCode, orgId, userId) { var request = $http({ method: "get", url: rolesUrl + 'api/roleallocations/user/' + userId + '/appcode/' + appCode + '/org/' + orgId + '?cache=' + Date.now(), }); return (request.then(handleSuccess, handleError)); } // Get all role allocations for a list of users within an organisation function getAllocationsForOrgForManyUsers(appCode, orgId, userIds) { var request = $http({ method: 'post', url: rolesUrl + 'api/roleallocations/users/appcode/' + appCode + '/org/' + orgId, data: userIds }); 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)); } function getRoleAllocation(id) { var request = $http({ method: "get", url: rolesUrl + 'api/roleallocations/' + id, }); return (request.then(handleSuccess, handleError)); } /* * Remove all the allocations a user has in an org */ function removeRoleAllocationsForUserInOrg(userId, org) { var request = $http({ method: "delete", url: rolesUrl + 'api/roleallocations/org/' + org + '/user/' + userId, }); return (request.then(handleSuccess, handleError)); } } })();