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.module('app.common.services').service('userModel', [
14    'APIUtils',
15    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 (
25                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