{"size": 10822, "ext": "js", "max_stars_repo_path": "site/components/services/user/user.js", "max_stars_repo_name": "dcastil1983/UCRcareer", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "site/components/services/user/user.js", "max_issues_repo_name": "dcastil1983/UCRcareer", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "site/components/services/user/user.js", "max_forks_repo_name": "dcastil1983/UCRcareer", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 31.5510204082, "max_line_length": 138, "alphanum_fraction": 0.4541674367}
angular.module('huntEdu.services')
    .factory('User', ['$http', '$q', 'USER_ROLES', '_', 'Util', 'JobPost', function UserFactory($http, $q, USER_ROLES, _, Util, JobPost) {
        var forEach = _.forEach,
            isFunction = _.isFunction,
            isObject = _.isObject,
            isArray = _.isArray,
            clone = _.clone,
            uniq = _.uniq,
            union = _.union,
            pick = _.pick,
            findIndex = _.findIndex,
            diffObject = Util.diffObject,
            compactObjectDeep = Util.compactObjectDeep,
            compactObject = Util.compactObject,
            prettyList = Util.prettyList;
       
        var PROFILE_DATA_FIELDS = {};

        PROFILE_DATA_FIELDS[USER_ROLES.all] =  ['personal', 'contact', 'location', 'subscription'];
        PROFILE_DATA_FIELDS[USER_ROLES.applicant] = ['spec', 'interests', 'bookmarkedPosts', 'postNotifications'];
        PROFILE_DATA_FIELDS[USER_ROLES.employer] = ['companyName', 'posts'];
        
        var employerProfileData = union(PROFILE_DATA_FIELDS[USER_ROLES.all], 
                                            PROFILE_DATA_FIELDS[USER_ROLES.employer]), 
            applicantProfileData = union(PROFILE_DATA_FIELDS[USER_ROLES.all],
                                            PROFILE_DATA_FIELDS[USER_ROLES.applicant]); 

        var User = {
            'companyName': null,
            'credentials': {
                'email': null, 
                'password': null
            },
            'personal': {
                'fName': null,
                'mInit': null,
                'lName': null
            },
            'contact': {
                'website': null,
                'linkedIn': null,
                'facebook': null,
                'twitter': null,
                'phoneNum': null
            }, 
            'spec': {
                'degree': null,
                'univ': null,
                'year': null,
                'resume': null,
                'focus': null
            },
            'location': {
                'city': null,
                'state': null,
                'zip': null,
                'address1': null,
                'address2': null,
                'country': null
            }, 
            'interests': [],
            'bookmarkedPosts': [],
            'postNotifications': [],
            'posts': [],
            'subscription': "0",
            'role': USER_ROLES.guest, 
        };


        /*
         *  Copies properties from data that are 
         *  different than the current User model.
         *  
         *  @param data {Object} Data to be copied
         *  @param(optional) role {String} role of the current user object
         *
         *  @returns {Object} data with nulled properties and properties
         *  that have the same value of the properties in User deleted
         */
        

        var getProfileDataFields = function(role) {
            role = role || User.role;
            if(role === USER_ROLES.applicant) {
                return applicantProfileData;
            } else if (role === USER_ROLES.employer) {
                return employerProfileData;
            } else {
                return PROFILE_DATA_FIELDS[USER_ROLES.all];
            }
        };

        var updatedProfileData = function(data, role) {
             role = role || User.role;
            var profileDataFields = getProfileDataFields(role),
                compactData = compactObjectDeep(data),
                diffData = diffObject(compactData, User); 

            return pick(diffData, profileDataFields);  
        };

        User.setCredentials = function(email, password) {
            this.credentials.email = email;
            this.credentials.password = password;
        };

        User.getCredentials = function() {
            return User.credentials;
        };

        User.setEmail = function(email) {
            User.credentials.email = email;
        };

        User.getEmail = function() {
            return User.credentials.email;
        };

        User.getMajor = function() {
            return User.spec.focus;
        };
    
        User.getInterests = function() {
            return clone(User.interests);
        };

        User.getSubscription = function() {
            return User.subscription;
        };

        User.prettyListInterests = function(conjunction, noneString) {
            return prettyList(User.interests, conjunction, noneString);
        };

        User.getUserRole = function() {
            return User.role;
        };

        User.setUserRole = function(role) {
            User.role = role;
        };

        User.isLoggedIn = function() {
            return User.role === USER_ROLES.applicant ||
                User.role === USER_ROLES.employer;
        };

        User.isGuest = function() {
            return User.role === USER_ROLES.guest;
        };

        User.isEmployer = function() {
            return User.role === USER_ROLES.employer;
        };

        User.isApplicant = function() {
            return User.role === USER_ROLES.applicant;
        };

        User.hasResume = function() {
            return !!User.spec.resume;
        };

        User.hasBookmark = function(jobId) { 
            var jobPos = findIndex(User.bookmarkedPosts, function(post){
                return post.meta.id === jobId;
            });

            if (jobPos === -1){
                return false;
            }
            else {
                return true;
            }
        };

        User.removeBookmark = function(jobId) {
            var deferred = $q.defer();
            if (!this.hasBookmark(jobId)){
                return deferred.promise;
            } 
            
            $http.post('/bookmark/remove', { 'id' : jobId })
                .then(function(){
                    var jobPos = findIndex(User.bookmarkedPosts, function(post){
                        return post.meta.id === jobId;
                    });
                    User.bookmarkedPosts.splice(jobPos, 1);
                    deferred.resolve();
                }, function(){
                    deferred.reject();
                });
            return deferred.promise;
        };

        User.addBookmark = function(job){
            var deferred = $q.defer(),
                jobId = job.getId();
            if (this.hasBookmark(jobId)){
                return deferred.promise;
            }
            $http.post('/bookmark/add', { 'id' : jobId })
                .then(function(){
                    User.bookmarkedPosts.push(job);
                    deferred.resolve();
                }, function(){
                    deferred.reject();
                });
            return deferred.promise;
        };

        User.clearPassword = function() {
            User.credentials.password = null;
        };

        User.getAll = function() {
            return compactObjectDeep();
        };

        User.updateSubscription = function(days) {
            User.subscription += days;
        };

        User.getExpiration = function() {
            if(User.isApplicant()) {
                return new Date(User.subscription.expires);
            } else {
                return null;
            }
        };

        User.isSubscribed = function() {
            var now = new Date();
            return User.isApplicant() && User.getExpiration() > now;
        };

        User.isOwnJob = function(job) {
            if(User.isEmployer()) {
                return job.isIn(User.posts);
            } else {
                return false;
            }
        };

        User.setProfileData = function(data,  role) {
            var updatedData = updatedProfileData(data, role);

            forEach(updatedData, function(value, key) {
                    if(isArray(value)) {
                        User[key] = uniq(value);
                        if(key === "posts") {
                            forEach(User[key], function(post, index) {
                                User[key][index] = new JobPost(post);
                            });
                        }
                    } else {
                        User[key] = value;
                    }
            });
        };

        User.getProfileData = function(role) {
            role = role || User.role;
            var profileDataFields = getProfileDataFields(role),
                data = compactObject(User);
            
            return pick(data, profileDataFields);
        };

        User.clearAll = function() {
            forEach(User, function(value, key) {
                if(!isFunction(value)) {
                   if(isArray(value)){
                        User[key] = [];
                    } else if (isObject(value)) {
                        User[key] = {};
                    } else {
                        User[key] = null;
                    }
                }
            });
            User.setUserRole(USER_ROLES.guest);
        };
        
        User.setJobPost = function(newJobPostRawData) {
          var newJobPost = new JobPost(newJobPostRawData);
          if(newJobPost) {
             var i = 0,
                len = User.posts.length,
                found = false,
                id = newJobPost.getId();
             
             for(; i < len; ++i) {
                if(User.posts[i].meta.id === id) {
                    User.posts[i] = newJobPost;
                    found = true;
                    break;
                }
            }
            if(!found) {
                User.posts.push(newJobPost);
            }
          }
        };
        /**
        * Sends a request to update profile information in the server,
        * if it is valid info, then merge all changes from the backend
        * to the User model in the frontend
        */
        User.updateProfileData = function(data) {
           
            /*
             * crude hack since you cant use email as a URI
             */
           
            var deferred = $q.defer(),
                role = User.role;


            if(!User.isEmployer() && !User.isApplicant()) {
                deferred.reject();
                return deferred.promise;
            }
        
            var updatedData = updatedProfileData(data); 
            updatedData.email = User.credentials.email;


            $http.post('/profile/' + role, updatedData)
                .then(function(res) {
                    var updatedDataRes = res.data;
                    User.setProfileData(updatedDataRes);
                    deferred.resolve(res);
                }, function(err) {
                    deferred.reject(err);   
                });

            return deferred.promise;
        };

        return User;
    }]);
