1/**
2 * userModel
3 *
4 * @module app/common/services/userModel
5 * @exports userModel
6 * @name userModel
7
8 */
9
10window.angular && (function (angular) {
11    'use strict';
12
13    angular
14        .module('app.common.services')
15        .service('userModel', ['APIUtils',function(APIUtils){
16            return {
17                login : function(username, password, callback){
18                    APIUtils.login(username, password, function(response, error){
19                        if(response &&
20                            (response.status == APIUtils.API_RESPONSE.SUCCESS_STATUS
21                                || response.status === undefined)){
22                            sessionStorage.setItem('LOGIN_ID', username);
23                            callback(true);
24                        }else if(response && response.data && response.data.data
25                                 && response.data.data.description){
26                            callback(false, response.data.data.description);
27                        }else if(response && response.message){
28                            callback(false, response.message);
29                        }else if(error){
30                            callback(false, 'Server unreachable');
31                        }else{
32                            callback(false, 'Internal error');
33                        }
34                    });
35                },
36                isLoggedIn : function(){
37                    if(sessionStorage.getItem('LOGIN_ID') === null){
38                        return false;
39                    }
40                    return true;
41                },
42                logout : function(callback){
43                    APIUtils.logout(function(response, error){
44                        if(response &&
45                           response.status == APIUtils.API_RESPONSE.SUCCESS_STATUS){
46                            sessionStorage.removeItem('LOGIN_ID');
47                            sessionStorage.removeItem(APIUtils.HOST_SESSION_STORAGE_KEY);
48                            callback(true);
49                        }else if(response.status == APIUtils.API_RESPONSE.ERROR_STATUS){
50                            callback(false);
51                        }else{
52                            callback(false, error);
53                        }
54                    });
55                }
56            };
57        }]);
58
59})(window.angular);
60