<commit_before>import consumerEditFormTpl from 'scripts/liveblog-syndication/views/consumer-edit-form.html';

consumerEdit.$inject = ['api', 'notify', 'lodash'];

export default function consumerEdit(api, notify, _) {
    return {
        templateUrl: consumerEditFormTpl,
        scope: {
            consumer: '=',
            onsave: '&',
            oncancel: '&',
            onupdate: '&'
        },
        link: function(scope, elem) {
            scope.consumerForm.attempted = false;

            scope.$watch('consumer', function(consumer) {
                scope.isEditing = consumer.hasOwnProperty('_id');
                scope.origConsumer = _.cloneDeep(consumer);
            });

            scope.save = function() {
                scope.consumerForm.attempted = true;

                if (!scope.consumerForm.$valid)
                    return;

                if (angular.equals(scope.origConsumer, scope.consumer))
                    return;

                var data = {};
                var apiQuery;

                data.contacts = scope.consumer.contacts;

                if (!scope.consumerForm.name.$pristine)
                    data.name = scope.consumer.name;

                if (!scope.consumerForm.webhook_url.$pristine)
                    data.webhook_url = scope.consumer.webhook_url;

                if (scope.isEditing)
                        apiQuery = api.save('consumers', scope.origConsumer, data);
                    else
                        apiQuery = api.consumers.save(data);

                apiQuery.then(function(result) {
                    var successMsg = gettext('Consumer saved.');

                    notify.pop();
                    notify.success(successMsg);

                    scope.onsave({ consumer: result });
                })
                .catch(function(err) {
                    var errorMsg = gettext('Fatal error!');

                    if (err.data.hasOwnProperty('_issues')) {
                        Object.keys(err.data._issues).forEach(function(key) {
                            var issue = err.data._issues[key];
                            if (typeof issue === 'object') {
                                if (issue.unique === true)
                                    issue = gettext('The selected field value is not unique.');
                            }
                            scope.consumerForm[key].issue = issue;
                        });
                    }

                    notify.pop();
                    notify.error(errorMsg);
                });
            };

            scope.cancel = function() {
                scope.oncancel();
            }
        }
    };
};<commit_msg>Wrong error message for unique webook<commit_after>import consumerEditFormTpl from 'scripts/liveblog-syndication/views/consumer-edit-form.html';

consumerEdit.$inject = ['api', 'notify', 'lodash'];

export default function consumerEdit(api, notify, _) {
    return {
        templateUrl: consumerEditFormTpl,
        scope: {
            consumer: '=',
            onsave: '&',
            oncancel: '&',
            onupdate: '&'
        },
        link: function(scope, elem) {
            scope.consumerForm.attempted = false;

            scope.$watch('consumer', function(consumer) {
                scope.isEditing = consumer.hasOwnProperty('_id');
                scope.origConsumer = _.cloneDeep(consumer);
            });

            scope.save = function() {
                scope.consumerForm.attempted = true;

                if (!scope.consumerForm.$valid)
                    return;

                if (angular.equals(scope.origConsumer, scope.consumer))
                    return;

                var data = {};
                var apiQuery;

                data.contacts = scope.consumer.contacts;

                if (!scope.consumerForm.name.$pristine)
                    data.name = scope.consumer.name;

                if (!scope.consumerForm.webhook_url.$pristine)
                    data.webhook_url = scope.consumer.webhook_url;

                if (scope.isEditing)
                        apiQuery = api.save('consumers', scope.origConsumer, data);
                    else
                        apiQuery = api.consumers.save(data);

                apiQuery.then(function(result) {
                    var successMsg = gettext('Consumer saved.');

                    notify.pop();
                    notify.success(successMsg);

                    scope.onsave({ consumer: result });
                })
                .catch(function(err) {
                    var errorMsg = gettext('Fatal error!');

                    if (err.data.hasOwnProperty('_issues')) {
                        Object.keys(err.data._issues).forEach(function(key) {
                            var issue = err.data._issues[key];

                            if (typeof issue === 'object' && issue.unique === 1) {
                                issue = gettext('The selected field value is not unique.');
                            }

                            scope.consumerForm[key].issue = issue;
                        });
                    }

                    notify.pop();
                    notify.error(errorMsg);
                });
            };

            scope.cancel = function() {
                scope.oncancel();
            }
        }
    };
};
{'fraction_non_alphanumeric': 0.109979633401222, 'fraction_numerical': 0.00018515089798185522, 'mean_word_length': 1.2157506152584086, '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': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5563781', 'n_tokens_mistral': 1378, 'n_tokens_neox': 1239, 'n_words': 222}