(function () { 'use strict'; var serviceId = 'skillsPassportsDataContext'; //note: job role training requirements data lives in course training hence coursesConfig. angular.module('skillsPassportsManager').factory(serviceId, ['$q', '$http', 'coursesConfig', '$modal', datacontext]); function datacontext($q, $http, config, $modal) { //note: a job role must exist for there to be training requirements, hence there are just functions to get them and update them var service = { getJobRoleTrainingRequirementsForJobRole: getJobRoleTrainingRequirementsForJobRole, getJobRoleTrainingRequirementsForJobRoles: getJobRoleTrainingRequirementsForJobRoles, updateJobRoleTrainingRequirements: updateJobRoleTrainingRequirements, updateUserTrainingRequirements: updateUserTrainingRequirements }; 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 getJobRoleTrainingRequirementsForJobRole(jobRoleId) { var request = $http({ method: "get", url: config.apigatewayUrl + 'coursetraining/api/jobroles/trainingrequirements/jobrole/' + jobRoleId }); return (request.then(handleSuccess, handleError)); } function getJobRoleTrainingRequirementsForJobRoles(jobRoleIds) { var request = $http({ method: "post", url: config.apigatewayUrl + 'coursetraining/api/jobroles/trainingrequirements/jobroles', data: jobRoleIds }); return (request.then(handleSuccess, handleError)); } function updateJobRoleTrainingRequirements(jobRoleId, courseIds) { var request = $http({ method: "put", url: config.apigatewayUrl + 'coursetraining/api/jobroles/trainingrequirements/' + jobRoleId + '/courses', data: courseIds }); return (request.then(handleSuccess, handleError)); } /* * Give a user relevant training requirements according to their job roles */ function updateUserTrainingRequirements(userId, jobRoleIds) { var request = $http({ method: "put", url: config.apigatewayUrl + 'coursetraining/api/UserTrainingRequirements/users/' + userId + '/jobroles/trainingRequirements', data: { userId: userId, jobRoleIds: jobRoleIds } }); return (request.then(handleSuccess, handleError)); } } })();