(function () { 'use strict'; var serviceId = 'stateService'; angular.module('app').factory(serviceId, ['DSCacheFactory', 'datacontext', '$rootScope', state]); //service handling state values (like invite ids) passed into the app via the url function state(DSCacheFactory, datacontext, $rootScope) { var stateCacheValue = 'stateCacheValue'; var stateCache = 'stateCache'; var service = { handleState: handleState, addState: addState, }; return service; function handleState() { //we use ds cache factory to stor the state values var cache = DSCacheFactory.get(stateCache); if (!cache) { cache = DSCacheFactory(stateCache, { storageMode: 'localStorage' }); cache.setOptions({ maxAge: 360000, deleteOnExpire: 'aggressive' }); } var theState = cache.get(stateCacheValue); if (theState) { //state should be in 2 parts _ - so split by id var parts = theState.split('_'); if (parts.length !== 2) //something wrong return; if (parts[0] === 'organisationinviteduser') { linkOrganisationInviteWithUser(parts[1]); cache.destroy(); } } } //as above but this is an organisation invite function linkOrganisationInviteWithUser(organisationInvitedUserId) { datacontext.linkOrganisationInviteWithUser(organisationInvitedUserId).then(function () { return datacontext.getNotifications().then(function (notifications) { $rootScope.$broadcast('UpdateNotifications', { notificationsNum: notifications.length }); }); }); } //add a state value function addState(value) { var cache = DSCacheFactory.get(stateCache); if (!cache) { cache = DSCacheFactory(stateCache, { storageMode: 'localStorage' }); cache.setOptions({ maxAge: 360000, deleteOnExpire: 'aggressive' }); } cache.removeAll(); cache.put(stateCacheValue, value); } } })();