<commit_before>/*
 * This program is part of the OpenLMIS logistics management information system platform software.
 * Copyright © 2017 VillageReach
 *
 * This program is free software: you can redistribute it and/or modify it under the terms
 * of the GNU Affero General Public License as published by the Free Software Foundation, either
 * version 3 of the License, or (at your option) any later version.
 *  
 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
 * See the GNU Affero General Public License for more details. You should have received a copy of
 * the GNU Affero General Public License along with this program. If not, see
 * http://www.gnu.org/licenses.  For additional information contact <EMAIL>. 
 */

(function() {

    'use strict';

    /**
     * @ngdoc service
     * @name openlmis-user.userFactory
     *
     * @description
     * Allows user to perform actions on user resource
     */
    angular
        .module('openlmis-user')
        .factory('userFactory', factory);

    factory.$inject = ['openlmisUrlFactory', '$resource', '$q', '$injector', 'localStorageFactory'];

    function factory(openlmisUrlFactory, $resource, $q, $injector, localStorageFactory) {

        var offlineService = $injector.get('offlineService'),
            offlineUserDetails = localStorageFactory('offlineUserDetails'),
            resource = $resource(openlmisUrlFactory('/api/users/:id'), {}, {
                'update': {
                    url: openlmisUrlFactory('/api/users/update/:id'),
                    method: 'POST'
                }
            });

        var factory = {
            get: get
        };
        return factory;


        /**
         * @ngdoc function
         * @name  get
         * @methodOf openlmis-user.userFactory
         *
         * @description
         * Gets user by id.
         *
         * @param   {String}    id  User id
         * @returns {Resource}      User info
         */
        function get(id) {
            var deferred = $q.defer();

            if(offlineService.isOffline()) {
                var user = offlineUserDetails.getBy('id', id);
                if(user) {
                    deferred.resolve(user);
                } else {
                    deferred.reject();
                }
            } else {
                resource.get({id: id}).$promise.then(function(response) {
                    offlineUserDetails.put(response);<commit_msg>OLMIS-1720: change if to inline one in user factory<commit_after>/*
 * This program is part of the OpenLMIS logistics management information system platform software.
 * Copyright © 2017 VillageReach
 *
 * This program is free software: you can redistribute it and/or modify it under the terms
 * of the GNU Affero General Public License as published by the Free Software Foundation, either
 * version 3 of the License, or (at your option) any later version.
 *  
 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
 * See the GNU Affero General Public License for more details. You should have received a copy of
 * the GNU Affero General Public License along with this program. If not, see
 * http://www.gnu.org/licenses.  For additional information contact <EMAIL>. 
 */

(function() {

    'use strict';

    /**
     * @ngdoc service
     * @name openlmis-user.userFactory
     *
     * @description
     * Allows user to perform actions on user resource
     */
    angular
        .module('openlmis-user')
        .factory('userFactory', factory);

    factory.$inject = ['openlmisUrlFactory', '$resource', '$q', '$injector', 'localStorageFactory'];

    function factory(openlmisUrlFactory, $resource, $q, $injector, localStorageFactory) {

        var offlineService = $injector.get('offlineService'),
            offlineUserDetails = localStorageFactory('offlineUserDetails'),
            resource = $resource(openlmisUrlFactory('/api/users/:id'), {}, {
                'update': {
                    url: openlmisUrlFactory('/api/users/update/:id'),
                    method: 'POST'
                }
            });

        var factory = {
            get: get
        };
        return factory;


        /**
         * @ngdoc function
         * @name  get
         * @methodOf openlmis-user.userFactory
         *
         * @description
         * Gets user by id.
         *
         * @param   {String}    id  User id
         * @returns {Resource}      User info
         */
        function get(id) {
            var deferred = $q.defer();

            if(offlineService.isOffline()) {
                var user = offlineUserDetails.getBy('id', id);
                user ? deferred.resolve(user) : deferred.reject();
            } else {
                resource.get({id: id}).$promise.then(function(response) {
                    offlineUserDetails.put(response);
{'fraction_non_alphanumeric': 0.09874361994503338, 'fraction_numerical': 0.0027483313702394976, 'mean_word_length': 2.104814137720902, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6531812', 'n_tokens_mistral': 1333, 'n_tokens_neox': 1270, 'n_words': 432}