<commit_before>'use strict';

angular.module('app')
    .controller('rgiAssignAssessmentDialogCtrl', function (
        $scope,
        $location,
        $route,
        $q,
        ngDialog,
        rgiNotifier,
        rgiAssessmentSrvc,
        rgiAssessmentMethodSrvc,
        rgiAssessmentRolesGuideSrvc,
        rgiIdentitySrvc,
        rgiUserSrvc,
        rgiUserMethodSrvc
    ) {
        $scope.current_user = rgiIdentitySrvc.currentUser;
        var originalAssessment = {};
        $scope.assessmentRoles = _.without(rgiAssessmentRolesGuideSrvc.list(),'ext_reviewer');
        $scope.availableUsers = {};

        $scope.assessmentRoles.forEach(function(role) {
            $scope.availableUsers[role] = [];
        });

        rgiUserSrvc.query(function(users) {
            users.forEach(function(user) {
                if($scope.assessmentRoles.indexOf(user.role) !== -1) {
                    $scope.availableUsers[user.role].push(user);
                }
            });
        });

        rgiAssessmentSrvc.get({assessment_ID: $scope.$parent.assessment_update_ID}, function(assessment) {
            $scope.assessmentRoles.forEach(function(role) {
                var field = role + '_ID';
                assessment[field] = assessment[field] && assessment[field]._id ? assessment[field]._id : assessment[field];
            });

            $scope.assessment = assessment;
            angular.extend(originalAssessment, assessment);
        });

        var getUser = function(role, userId) {
            var foundUser = {};

            $scope.availableUsers[role].forEach(function(user) {
                if(user._id === userId) {
                    foundUser = user;
                }
            });

            return foundUser;
        };

        var updateAssessment = function(notificationMessage) {
            rgiAssessmentMethodSrvc.updateAssessment($scope.assessment)
                .then(function () {
                    rgiNotifier.notify(notificationMessage);
                    $location.path('/');
                    $scope.closeThisDialog();
                }, function (reason) {
                    rgiNotifier.error(reason);
                });
        };

        $scope.assignAssessment = function () {
            if (!$scope.assessment.researcher_ID) {
                rgiNotifier.error('You must select a researcher!');
            } else {
                $scope.assessment.mail = true;
                $scope.assessment.status = 'researcher_trial';
                $scope.assessment.edit_control = $scope.assessment.researcher_ID;

                var assignUser = function(role, linkedAssessmentData) {
                    var field = role + '_ID';
                    var user = new rgiUserSrvc(getUser(role, $scope.assessment[field]));

                    user.assessments.push(linkedAssessmentData);
                    $scope.assessment[field] = user._id;

                    return rgiUserMethodSrvc.updateUser(user).$promise;
                };

                var promises = [];
                $scope.assessmentRoles.forEach(function(role) {
                    if ($scope.assessment[role + '_ID']) {
                        promises.push(assignUser(role, {
                            assessment_ID: $scope.$parent.assessment_update_ID,
                            country_name: $scope.assessment.country,
                            year: $scope.assessment.year,
                            version: $scope.assessment.version
                        }));
                    }
                });

                $q.all(promises).then(function() {
                    updateAssessment('Assessment assigned!');
                });
            }
        };

        $scope.isAnyAssessmentRoleChanged = function() {
            var roleChanged = false;

            $scope.assessmentRoles.forEach(function(role) {
                if($scope.assessment && (originalAssessment[role + '_ID'] !== $scope.assessment[role + '_ID'])) {
                    roleChanged = true;
                }
            });

            return roleChanged;
        };

        $scope.reassignAssessment = function () {
            if (!$scope.assessment.researcher_ID) {
                rgiNotifier.error('No researcher data!');
            } else {
                var promises = [];

                $scope.assessmentRoles.forEach(function(role) {
                    if(originalAssessment[role + '_ID'] !== $scope.assessment[role + '_ID']) {
                        var oldAssignee = getUser(role, originalAssessment[role + '_ID']);
                        var newAssignee = getUser(role, $scope.assessment[role + '_ID']);

                        //remove assessment from the old assignee
                        if (oldAssignee && oldAssignee.assessments) {
                            var assessmentIndex = -1;

                            oldAssignee.assessments.forEach(function(assessment, i) {
                                if ($scope.assessment.assessment_ID === assessment.assessment_ID) {
                                    if (i > -1) {
                                        assessmentIndex = i;
                                    }
                                }
                            });

                            oldAssignee.assessments.splice(assessmentIndex, 1);
                            rgiUserMethodSrvc.updateUser(oldAssignee);
                        }

                        //add assessment to the new assignee
                        newAssignee.assessments.push({
                            assessment_ID: $scope.assessment.assessment_ID,
                            country_name: $scope.assessment.country
                        });
                        promises.push(rgiUserMethodSrvc.updateUser(newAssignee).$promise);

                        //change assignee in the assessment
                        if (newAssignee._id) {
                            $scope.assessment[role + '_ID'] = newAssignee._id;
                        }

                        //change control if applicable
                        if ($scope.assessment.edit_control === oldAssignee._id) {
                            $scope.assessment.edit_control = newAssignee._id;
                        }
                    }
                });<commit_msg>refactor 'Assign Assessment' controller using 'User Assessments' service<commit_after>'use strict';

angular.module('app')
    .controller('rgiAssignAssessmentDialogCtrl', function (
        $scope,
        $location,
        $route,
        $q,
        ngDialog,
        rgiNotifier,
        rgiAssessmentSrvc,
        rgiAssessmentMethodSrvc,
        rgiAssessmentRolesGuideSrvc,
        rgiIdentitySrvc,
        rgiUserAssessmentsSrvc,
        rgiUserSrvc,
        rgiUserMethodSrvc
    ) {
        $scope.current_user = rgiIdentitySrvc.currentUser;
        var originalAssessment = {};
        $scope.assessmentRoles = _.without(rgiAssessmentRolesGuideSrvc.list(),'ext_reviewer');
        $scope.availableUsers = {};

        $scope.assessmentRoles.forEach(function(role) {
            $scope.availableUsers[role] = [];
        });

        rgiUserSrvc.query(function(users) {
            users.forEach(function(user) {
                if($scope.assessmentRoles.indexOf(user.role) !== -1) {
                    $scope.availableUsers[user.role].push(user);
                }
            });
        });

        rgiAssessmentSrvc.get({assessment_ID: $scope.$parent.assessment_update_ID}, function(assessment) {
            $scope.assessmentRoles.forEach(function(role) {
                var field = role + '_ID';
                assessment[field] = assessment[field] && assessment[field]._id ? assessment[field]._id : assessment[field];
            });

            $scope.assessment = assessment;
            angular.extend(originalAssessment, assessment);
        });

        var getUser = function(role, userId) {
            var foundUser = {};

            $scope.availableUsers[role].forEach(function(user) {
                if(user._id === userId) {
                    foundUser = user;
                }
            });

            return foundUser;
        };

        var updateAssessment = function(notificationMessage) {
            rgiAssessmentMethodSrvc.updateAssessment($scope.assessment)
                .then(function () {
                    rgiNotifier.notify(notificationMessage);
                    $location.path('/');
                    $scope.closeThisDialog();
                }, function (reason) {
                    rgiNotifier.error(reason);
                });
        };

        $scope.assignAssessment = function () {
            if (!$scope.assessment.researcher_ID) {
                rgiNotifier.error('You must select a researcher!');
            } else {
                $scope.assessment.mail = true;
                $scope.assessment.status = 'researcher_trial';
                $scope.assessment.edit_control = $scope.assessment.researcher_ID;

                var assignUser = function(role, linkedAssessmentData) {
                    var field = role + '_ID';
                    var user = new rgiUserSrvc(getUser(role, $scope.assessment[field]));

                    user.assessments.push(linkedAssessmentData);
                    $scope.assessment[field] = user._id;

                    return rgiUserMethodSrvc.updateUser(user).$promise;
                };

                var promises = [];
                $scope.assessmentRoles.forEach(function(role) {
                    if ($scope.assessment[role + '_ID']) {
                        promises.push(assignUser(role, {
                            assessment_ID: $scope.$parent.assessment_update_ID,
                            country_name: $scope.assessment.country,
                            year: $scope.assessment.year,
                            version: $scope.assessment.version
                        }));
                    }
                });

                $q.all(promises).then(function() {
                    updateAssessment('Assessment assigned!');
                });
            }
        };

        $scope.isAnyAssessmentRoleChanged = function() {
            var roleChanged = false;

            $scope.assessmentRoles.forEach(function(role) {
                if($scope.assessment && (originalAssessment[role + '_ID'] !== $scope.assessment[role + '_ID'])) {
                    roleChanged = true;
                }
            });

            return roleChanged;
        };

        $scope.reassignAssessment = function () {
            if (!$scope.assessment.researcher_ID) {
                rgiNotifier.error('No researcher data!');
            } else {
                var promises = [];

                $scope.assessmentRoles.forEach(function(role) {
                    if(originalAssessment[role + '_ID'] !== $scope.assessment[role + '_ID']) {
                        var oldAssignee = getUser(role, originalAssessment[role + '_ID']);
                        var newAssignee = getUser(role, $scope.assessment[role + '_ID']);

                        //remove assessment from the old assignee
                        if (oldAssignee && oldAssignee.assessments) {
                            promises.push(rgiUserAssessmentsSrvc.remove(oldAssignee, $scope.assessment));
                        }

                        //add assessment to the new assignee
                        promises.push(rgiUserAssessmentsSrvc.add(newAssignee, $scope.assessment));

                        //change assignee in the assessment
                        if (newAssignee._id) {
                            $scope.assessment[role + '_ID'] = newAssignee._id;
                        }

                        //change control if applicable
                        if ($scope.assessment.edit_control === oldAssignee._id) {
                            $scope.assessment.edit_control = newAssignee._id;
                        }
                    }
                });
{'fraction_non_alphanumeric': 0.10498338870431893, 'fraction_numerical': 0.0004152823920265781, 'mean_word_length': 1.4048332334731377, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 4, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 5, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3019104', 'n_tokens_mistral': 3136, 'n_tokens_neox': 2910, 'n_words': 430}