(function () { 'use strict'; var serviceId = 'userSettingsDataContext'; angular.module('userSettings').factory(serviceId, ['$q', '$http', 'userSettingsConfig', '$modal', '$window', datacontext]); function datacontext($q, $http, config, $modal, $window) { var service = { getProfile: getProfile, saveProfile: saveProfile, isUsernameAvailable: isUsernameAvailable, changePassword: changePassword, isEmailAvailable: isEmailAvailable, resendEmailVerification: resendEmailVerification, sendActivation: sendActivation, getNotificationSchedule: getNotificationSchedule, createNotificationSchedule: createNotificationSchedule, updateNotificationSchedule: updateNotificationSchedule }; 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); } //get the user details function getProfile(refresh) { //no - so get from server var request = $http({ method: "get", url: userProfileServiceUrl + 'user', params: { includefields: "title,firstname,lastname,emailaddresses,gender,dateofbirth,country,avatarurl,avatartype,managedaccount,addresses,telephones,socialmedias,createdon" } }); return (request.then(handleSuccess, handleError)); } //save the user details function saveProfile(changedUser) { var request = $http({ data: changedUser, method: "put", url: userProfileServiceUrl + changedUser.id }); return (request.then(handleSuccess, handleError)); } function isEmailAvailable(email) { var request = $http({ method: "get", url: userProfileServiceUrl + 'emailavailable?email=' + email }); return (request.then(handleSuccess, handleError)); } function isUsernameAvailable(username) { var request = $http({ method: "get", url: myusersUrl + 'api/Account/usernameavailable?username=' + username }); return (request.then(handleSuccess, handleError)); } function resendEmailVerification(email, brandOrganisationId) { var request = $http({ method: "post", url: myusersUrl + 'api/emailverifications' + '?host=' + $window.location.host, data: { emailToVerify: email, applicationId: config.applicationId, appCode: config.appCode, organisationBrandingId: brandOrganisationId } }); return (request.then(handleSuccess, handleError)); } function sendActivation(email, isSetPassword) { var request = $http({ method: "post", url: myusersUrl + 'api/emailverifications' + '?host=' + $window.location.host, data: { emailToVerify: email, applicationId: config.applicationId, appCode: config.appCode, isSetPassword: isSetPassword } }); return (request.then(handleSuccess, handleError)); } function changePassword(model) { var request = $http({ method: "post", url: myusersUrl + 'api/account/changepassword', data: model }); return (request.then(handleSuccess, handleError)); } function getNotificationSchedule() { var request = $http({ method: "get", url: apigatewayUrl + 'notifications/api/schedule/user/appcode/' + config.appCode }); return (request.then(handleSuccess, handleError)); } function createNotificationSchedule(schedule) { var request = $http({ method: "post", url: apigatewayUrl + 'notifications/api/schedule', data: schedule }); return (request.then(handleSuccess, handleError)); } function updateNotificationSchedule(schedule) { var request = $http({ method: "put", url: apigatewayUrl + 'notifications/api/schedule/' + schedule.id, data: schedule }); return (request.then(handleSuccess, handleError)); } } })();