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