(function () { 'use strict'; var serviceId = 'otjActivityTypesDataContext'; angular.module('otjActivities').factory(serviceId, ['$q', '$http', 'otjActivitiesConfig', datacontext]); function datacontext($q, $http, config, $modal) { var service = { getOrganisationOTJActivityTypes: getOrganisationOTJActivityTypes, createOTJActivityType: createOTJActivityType, updateOTJActivityType: updateOTJActivityType, deleteOTJActivityType: deleteOTJActivityType }; 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 activities for organisation function getOrganisationOTJActivityTypes(organisationId) { var request = $http({ method: "get", url: config.apigatewayUrl + 'UserService/api/apprenticeships/organisation/activitytypes?activeonly=false', }); return (request.then(handleSuccess, handleError)); } // Update an existing activity type function updateOTJActivityType(type) { var request = $http({ method: "put", url: config.apigatewayUrl + 'UserService/api/apprenticeships/organisation/activitytype', data: type }); return (request.then(handleSuccess, handleError)); } // Create a new activity type function createOTJActivityType(type) { var request = $http({ method: "post", url: config.apigatewayUrl + 'UserService/api/apprenticeships/organisation/activitytype', data: type }); return (request.then(handleSuccess, handleError)); } // Delete actiity type for organisation function deleteOTJActivityType(type) { var request = $http({ method: "delete", url: config.apigatewayUrl + 'UserService/api/apprenticeships/organisation/activitytype/' + type.id, }); return (request.then(handleSuccess, handleError)); } } })();