Code coverage report for app/components/_common/services/user.service.js

Statements: 100% (81 / 81)      Branches: 100% (26 / 26)      Functions: 100% (21 / 21)      Lines: 100% (55 / 55)      Ignored: 2 statements, 4 branches     

All files » app/components/_common/services/ » user.service.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119    15×   15× 15×                                                                                                                            
const [isLoggedIn, userInfo] = [Symbol(), Symbol()];
class UserSerivce {
    constructor ($http, $q, $rootScope, Event, AjaxError) {
        Object.assign(this, {$http, $q, $rootScope, Event, AjaxError});
        // private variable
        this[isLoggedIn] = false;
        this[userInfo] = null;
    }
 
    isLoggedIn () {
        return this[isLoggedIn];
    }
 
    checkLoggedInStatus () {
        const self = this;
        return this.$http.get('api/user/loginstatus')
            .then(_success)
            .catch(_error);
 
        function _success (response) {
            const data = response.data;
            if (response.status === 200 && data.code === 0) {
                self._setUser(data.result.user);
                self.$rootScope.$broadcast(self.Event.AUTH_SESSION_VALID, data.result.user);
                return data.result.user;
            }
            return self.$q.reject(data.message);
        }
 
        function _error (reason) {
            self._clearUser();
            return self.AjaxError.catcher(reason);
        }
    }
 
    login (email, password) {
        const self = this;
        const req = {
            email,
            password
        };
        return this.$http.post('api/user/login', req)
            .then(_success)
            .catch(_error);
 
        function _success (response) {
            const data = response.data;
            if (response.status === 200 && data.code === 0) {
                self._setUser(data.result.user);
                self.$rootScope.$broadcast(self.Event.AUTH_LOGIN, data.result.user);
                return data.result.user;
            }
            return self.$q.reject(data.message);
        }
 
        function _error (reason) {
            self._clearUser();
            return self.AjaxError.catcher(reason);
        }
    }
 
    logout () {
        const self = this;
        return this.$http.post('api/user/logout')
            .then(_success)
            .catch(_error);
 
        function _success (response) {
            const data = response.data;
            if (response.status === 200 && data.code === 0) {
                self._clearUser();
                self.$rootScope.$broadcast(self.Event.AUTH_LOGOUT);
            } else {
                return self.$q.reject(data.message);
            }
        }
 
        function _error (reason) {
            // log out user even API is failed
            self._clearUser();
            return self.AjaxError.catcher(reason);
        }
    }
 
    getUserInfo () {
        return this[userInfo];
    }
 
    getProductSummary () {
        const self = this;
        return this.$http.get('api/user/products')
            .then(_success)
            .catch(this.AjaxError.catcher.bind(this.AjaxError));
 
        function _success (response) {
            const data = response.data;
            if (response.status === 200 && data.code === 0) {
                return data.result.summary;
            }
            return self.$q.reject(data.message);
        }
    }
 
    _setUser (userData) {
        this[isLoggedIn] = true;
        this[userInfo] = userData;
    }
 
    _clearUser () {
        this[isLoggedIn] = false;
        this[userInfo] = null;
    }
}
 
UserSerivce.$inject = ['$http', '$q', '$rootScope', 'Event', 'AjaxErrorHandler'];
 
export default UserSerivce;