(function () { 'use strict'; var serviceId = 'userDataContext'; angular.module('user').factory(serviceId, ['appDataContext', 'userConfig', 'config', dataContext]); function dataContext(appDataContext, config, appConfig) { var service = {}; /** * Get the details for this user */ service.getUser = function (refresh) { return appDataContext.get(config.userRemoteServiceUrl, config.cache, config.userCacheItem, refresh); } /** * Save the user's details */ service.saveUser = function (changedUser) { appDataContext.clearCache(config.cache); return appDataContext.put(config.userRemoteServiceUrl + changedUser.id); } /** * Get the profile details for this user */ service.getProfile = function () { var includeFields = 'title,firstname,lastname,emailaddresses,gender,dateofbirth,country,avatarurl,avatartype,managedaccount,addresses,telephones,socialmedias,createdon'; return appDataContext.get(config.userProfileServiceUrl + 'user?includefields=' + includeFields); } /** * Save the user's profile details */ service.saveProfile = function(changedUser) { return appDataContext.put(config.userProfileServiceUrl + changedUser.id, changedUser); } /** * Get all the frameworks for this user */ service.getAllMyFrameworks = function () { //is the data in cache var cache = DSCacheFactory.get(myFrameworksCache); if (cache) { var cached = DSCacheFactory.get(myFrameworksCache).get(allMyFrameworksName); if (cached) { var deferred = $q.defer(); deferred.resolve(cached); return deferred.promise; } } //its not in the cacahe so get from server return getMyIndividualFrameworks().then(function (individualFrameworks) { return myUsersDataContext.getAllOrgFrameworksInParentHierarchy().then(function (orgFrameworks) { //The list of frameworks that are visible to the user, based on self and org allocation values var visibleFrameworks = []; /* * Only make the users' individualframeworks that have either been 'self', 'group', or both allocated, visible */ for (var ind in individualFrameworks) { //initialise check flag var groupAllocated = false; //is it self allocated? var selfAllocated = individualFrameworks[ind].selfAllocated; //Check each of the org allocated frameworks to see if the user has it, //if so, set it to 'group' allocated for (var o in orgFrameworks) { if (orgFrameworks[o] == individualFrameworks[ind].id) { //has been allocated groupAllocated = true; //stop checking framework goto next break; } else { //not yet matched so is deallocated (at the moment) groupAllocated = false; } } //If it's self org group allocated, add it to the 'visible' list if (groupAllocated || selfAllocated) { visibleFrameworks.push({ id: individualFrameworks[ind].id, deactivated: false, groupAllocated: groupAllocated, selfAllocated: selfAllocated }); } } /* * Push orgallocated frameworks to the 'visible' list if the user doesn't have an individual framework for it yet * i.e. allocated org frameworks that the user has not accessed yet */ for (var i = 0; i < orgFrameworks.length; i++) { var alreadyPresent = false; for (var j = 0; j < individualFrameworks.length; j++) { if (individualFrameworks[j].id === orgFrameworks[i]) { alreadyPresent = true; break; } } if (!alreadyPresent) visibleFrameworks.push({ id: orgFrameworks[i], deactivated: false, groupAllocated: true, selfAllocated: false }); } var theCache = DSCacheFactory.get(myFrameworksCache); if (!theCache) createMyFrameworksCache(); //add user data to the cache DSCacheFactory.get(myFrameworksCache).put(allMyFrameworksName, visibleFrameworks); return visibleFrameworks; }); }); } /** * Get the individual frameworks for this user */ service.getMyIndividualFrameworks = function () { return appDataContext.get(config.userRemoteServiceUrl + 'frameworks/individual', config.cache, config.frameworksCacheItem); } /** * Edit individual frameworks for this user */ service.editIndividualFrameworks = function (editedFrameworks) { appDataContext.removeCacheItem(config.cache, config.frameworksCacheItem); return appDataContext.post(config.userRemoteServiceUrl + 'frameworks', { editedFrameworks: editedFrameworks }); } /** * Get all the deployed framework assignments for this user */ service.getAllMyFrameworkAssignmentDeployments = function () { return appDataContext.get(appConfig.apigatewayUrl + 'deployedassignment/formewithgroups') .then(function (frameworks) { return frameworks.deploymentSummaries; }); //return appDataContext.get(appConfig.apigatewayUrl + 'deployedassignment/formewithgroups', config.cache, config.deployedAssignmentsCacheItem) // .then(function (frameworks) { // return frameworks.deploymentSummaries; // }); } /** * Get a deployed framework assignment for the user */ service.getFrameworkAssignment = function (deployedAssignmentId) { return appDataContext.get(appConfig.apigatewayUrl + 'deployedassignment/forme/' + deployedAssignmentId); } /** * Get the competencies / evidence summary for a deployed framework assignment */ service.getFrameworkEvidenceSummary = function (deployedAssignmentId) { return appDataContext.get(appConfig.apigatewayUrl + 'deployedassignment/evidenceforme/' + deployedAssignmentId + '/evidencesummary'); } /** * Get the onboards for this user */ service.getOnboards = function () { return appDataContext.get( appConfig.myShowcaseSiteUrl ? appConfig.myShowcaseSiteUrl + 'api/onboards' : 'api/onboards' ) .then(function (response) { return response.reduce(function (onboards, onboard) { onboards[onboard.onBoardTypeName] = onboard.accepted; return onboards; }, {}); }); } /** * Get off the job apprenticeship reports for this user */ service.getMyOffTheJobApprenticeship = function () { return appDataContext.get(appConfig.apigatewayUrl + 'UserService/api/apprenticeships/reports/currentuser/offthejob'); } /* * Get a summary of all tags for this user */ service.getTagSummary = function () { return appDataContext.get(config.tagsRemoteServiceUrl, config.cache, config.tagSummaryCacheItem); } /** * Get notifications for the user */ service.getNotifications = function () { return appDataContext.get(appConfig.apigatewayUrl + 'notifications/api/notifications/appcode/' + appConfig.appCode + '/inapp'); } /** * Mark a notification as seen */ service.markNotificationAsSeen = function (notificationId) { return appDataContext.put(appConfig.apigatewayUrl + 'notifications/api/notifications/' + notificationId); } /** * Get the list of reviewers this user has sent showcases to */ service.getReviewers = function () { return appDataContext.get(config.userRemoteServiceUrl + 'myreviewers'); } /** * Get an ouath url for Evernote for this user */ service.getEvernoteCallForwardUrl = function () { return appDataContext.get(config.evernoteRemoteServiceUrl + 'evernotecallforward/'); } /** * Remove the Evernote OAuth credentials */ service.removeEvernoteOAuth = function () { return appDataContext.delete(config.evernoteRemoteServiceUrl + 'oauth'); } /** * Get the token that allows the user to directly upload files to Microsoft Azure blob storage */ service.getUploadBucketSas = function () { return appDataContext.get(config.userRemoteServiceUrl + 'uploadbucket/sas'); } /** * Update the last activity for a user */ service.updateLastActivity = function (userId) { return appDataContext.post(config.userProfileServiceUrl + 'lastactivity/' + userId); } return service; } })();