<commit_before>'use strict';

angular.module('app')
    .factory('rgiUserMethodSrvc', function (
        $http,
        $q,
        rgiIdentitySrvc,
        rgiUserSrvc
    ) {
        return {
            createUser: function (new_user_data) {
                var new_user = new rgiUserSrvc(new_user_data),
                    dfd = $q.defer(),
                    error_message;

                new_user.$save().then(function () {
                    dfd.resolve();
                }, function (response) {
                    error_message = response.data.reason.replace('ValidationError: ', '');
                    dfd.reject(error_message.charAt(0).toUpperCase() + error_message.slice(1));
                });
                return dfd.promise;
            },
            deleteUser: function (userDeletion) {
                var dfd = $q.defer(),
                    deleteID = new rgiUserSrvc();

                deleteID.id = userDeletion;

                deleteID.$delete().then(function () {
                    dfd.resolve();
                }, function (response) {
                    dfd.reject(response.data.reason);
                });
                return dfd.promise;
            },
            updateUser: function (new_user_data) {
                var dfd = $q.defer(),
                error_message;

                new_user_data.$update().then(function () {
                    dfd.resolve();
                }, function (response) {
                    error_message = response.data.reason.replace('ValidationError: ', '');
                    dfd.reject(error_message.charAt(0).toUpperCase() + error_message.slice(1));
                });
                return dfd.promise;
            }
        };
    });<commit_msg>refactor the user method service<commit_after>'use strict';

angular.module('app')
    .factory('rgiUserMethodSrvc', function (
        $q,
        rgiUserSrvc
    ) {
        var
            getFormattedErrorMessage = function(errorMessage) {
                errorMessage = errorMessage.replace('ValidationError: ', '');
                return errorMessage.charAt(0).toUpperCase() + errorMessage.slice(1);
            },
            manipulateUser = function(user, action, formatErrorMessage) {
                var dfd = $q.defer();

                user[action]().then(function () {
                    dfd.resolve();
                }, function (response) {
                    var errorMessage = response.data.reason;
                    dfd.reject(formatErrorMessage ? getFormattedErrorMessage(errorMessage) : errorMessage);
                });

                return dfd.promise;
            };

        return {
            createUser: function (user) {
                return manipulateUser(new rgiUserSrvc(user), '$save', true);
            },
            deleteUser: function (userId) {
                var user = new rgiUserSrvc();
                user.id = userId;
                return manipulateUser(user, '$delete', false);
            },
            updateUser: function (user) {
                return manipulateUser(user, '$update', true);
            }
        };
    });
{'fraction_non_alphanumeric': 0.11489497135582431, 'fraction_numerical': 0.0019096117122851686, 'mean_word_length': 1.3974065598779557, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '1453668', 'n_tokens_mistral': 839, 'n_tokens_neox': 777, 'n_words': 132}