1/**
2 * api Interceptor
3 *
4 * @module app/common/services/apiInterceptor
5 * @exports apiInterceptor
6 * @name apiInterceptor
7
8 * @version 0.0.1
9 */
10
11window.angular && (function (angular) {
12    'use strict';
13
14    angular
15        .module('app.common.services')
16        .service('apiInterceptor', ['$q', '$rootScope', 'dataService', function($q, $rootScope, dataService){
17            return {
18                'request': function(config){
19                    dataService.server_unreachable = false;
20                    dataService.loading = true;
21                    return config;
22                },
23                'response': function(response){
24                    dataService.loading = false;
25                    dataService.last_updated = new Date();
26
27                    if(response == null){
28                        dataService.server_unreachable = true;
29                    }
30
31                    if(response && response.status == 'error' &&
32                       dataService.path != '/login'){
33                        $rootScope.$emit('timedout-user', {});
34                    }
35
36                    return response;
37                },
38                'responseError': function(rejection){
39                    dataService.server_unreachable = true;
40                    dataService.loading = false;
41                    if(dataService.path != '/login'){
42                        $rootScope.$emit('timedout-user', {});
43                    }
44                    return $q.reject(rejection);
45                }
46            };
47        }]);
48
49})(window.angular);