<commit_before>/*
 * Copyright (c) 2014 Kagilum SAS.
 *
 * This file is part of iceScrum.
 *
 * iceScrum 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.
 *
 * iceScrum 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 General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with iceScrum.  If not, see <http://www.gnu.org/licenses/>.
 *
 * Authors:
 *
 * <NAME> (<EMAIL>)
 * <NAME> (<EMAIL>)
 *
 */

controllers.controller('abstractTeamCtrl', ['$scope', '$filter', 'Session', 'TeamService', 'UserService', function($scope, $filter, Session, TeamService, UserService) {
    // Functions
    $scope.removeTeamMember = function(member) {
        _.remove($scope.team.members, {email: member.email});
        _.remove($scope.team.scrumMasters, {email: member.email});
    };
    $scope.scrumMasterChanged = function(member) {
        if (member.scrumMaster) {
            $scope.team.scrumMasters.push(member);
        } else {
            _.remove($scope.team.scrumMasters, {email: member.email}); // equality on email because invited members have no id
        }
    };
    $scope.searchMembers = function(val) {
        return UserService.search(val, true).then(function(users) {
            return _.chain(users)
                .filter(function(member) {
                    var found = false;
                    if ($scope.project) {
                        found = _.find($scope.project.stakeHolders, {email: member.email});
                    }
                    if (!found) {
                        found = _.find($scope.team.members, {email: member.email});
                    }
                    return !found;
                })
                .map(function(member) {
                    member.name = $filter('userFullName')(member);
                    return member;
                })
                .value();
        });
    };
    $scope.invitationToUserMock = function(invitation) {
        return {email: invitation.email};
    };
    // Init
    $scope.team = $scope.team ? $scope.team : {};
    $scope.member = {};
}]);

controllers.controller('teamCtrl', ['$scope', '$controller', '$filter', 'Session', 'TeamService', function($scope, $controller, $filter, Session, TeamService) {
    $controller('abstractTeamCtrl', {$scope: $scope});
    // Functions
    $scope.searchTeam = function(val, create) {
        return TeamService.search(val, create).then(function(teams) {
            return teams;
        });
    };
    $scope.selectTeam = function($item, $model) {
        $scope.team = $model;
        $scope.team.selected = true;
        //Add current user to the team
        if (!$scope.team.id) {
            $scope.team.class = 'team';
            var current = angular.copy(Session.user);
            $scope.team.members = [];
            $scope.team.members.push(current);
            $scope.team.scrumMasters = [];
            $scope.team.scrumMasters.push(current);
        }
        if ($model.members && $model.scrumMasters) {
            $scope.team.members = $model.members.map(function(member) {
                member.scrumMaster = _.find($model.scrumMasters, {id: member.id}) ? true : false;
                return member;
            });
        }
        if ($scope.project) {
            var filteredStakeHolders = _.filter($scope.project.stakeHolders, function(u) {
                return !_.find($scope.team.members, {email: u.email});
            });
            var filteredProductOwners = _.filter($scope.project.productOwners, function(u) {
                var member = _.find($scope.team.members, {email: u.email});
                return member ? member.scrumMaster : true;
            });
            if (filteredProductOwners.length != $scope.project.productOwners.length || filteredStakeHolders.length != $scope.project.stakeHolders.length) {
                $scope.warning.on = true;
            }
            $scope.project.stakeHolders = filteredStakeHolders;
            $scope.project.productOwners = filteredProductOwners;
            $scope.team.members = $scope.team.members.map(function(member) {
                member.productOwner = _.find($scope.project.productOwners, {id: member.id}) ? true : false;
                return member;
            });
            $scope.project.team = $scope.team;
        }
        if (!_.isEmpty($model.invitedMembers)) {
            $scope.team.members = $scope.team.members.concat(_.map($model.invitedMembers, $scope.invitationToUserMock));
        }
        if (!_.isEmpty($model.invitedScrumMasters)) {
            $scope.team.members = $scope.team.members.concat(_.map($model.invitedScrumMasters, function(invitation) {
                return _.merge($scope.invitationToUserMock(invitation), {scrumMaster: true});
            }));
        }
    };
    $scope.unSelectTeam = function() {
        $scope.warning.on = false;
        $scope.project.team = {};
        if ($scope.team.selected) {
            $scope.team = {};
            $scope.member = {};
        }
    };
    $scope.addTeamMember = function(member) {
        var po = _.find($scope.project.productOwners, {email: member.email});
        if (po) {
            member.scrumMaster = true;
            member.productOwner = true;
            $scope.team.scrumMasters.push(member);
        }
        $scope.team.members.push(member);
        $scope.member = {};
    };
    // Init
    $scope.warning = {on: false};
    if ($scope.project && $scope.project.team) {
        $scope.selectTeam(null, $scope.project.team);
    }
}]);

controllers.controller('manageTeamsModalCtrl', ['$scope', '$controller', '$filter', 'TeamService', 'ProjectService', function($scope, $controller, $filter, TeamService, ProjectService) {
    $controller('abstractTeamCtrl', {$scope: $scope});
    // Functions
    $scope.selectTeam = function(team) {
        $scope.team = angular.copy(team);
        if (team.members && team.scrumMasters) {
            $scope.team.members = team.members.map(function(member) {
                member.scrumMaster = _.find(team.scrumMasters, {id: member.id}) ? true : false;
                return member;
            });
            if (!_.isEmpty(team.invitedMembers)) {
                $scope.team.members = $scope.team.members.concat(_.map(team.invitedMembers, $scope.invitationToUserMock));
            }
            if (!_.isEmpty(team.invitedScrumMasters)) {
                $scope.team.members = $scope.team.members.concat(_.map(team.invitedScrumMasters, function(invitation) {
                    return _.merge($scope.invitationToUserMock(invitation), {scrumMaster: true});
                }));
            }<commit_msg>T1342-0 Invited users appear twice in the member list when opening team management from project settings<commit_after>/*
 * Copyright (c) 2014 Kagilum SAS.
 *
 * This file is part of iceScrum.
 *
 * iceScrum 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.
 *
 * iceScrum 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 General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with iceScrum.  If not, see <http://www.gnu.org/licenses/>.
 *
 * Authors:
 *
 * <NAME> (<EMAIL>)
 * <NAME> (<EMAIL>)
 *
 */

controllers.controller('abstractTeamCtrl', ['$scope', '$filter', 'Session', 'TeamService', 'UserService', function($scope, $filter, Session, TeamService, UserService) {
    // Functions
    $scope.removeTeamMember = function(member) {
        _.remove($scope.team.members, {email: member.email});
        _.remove($scope.team.scrumMasters, {email: member.email});
    };
    $scope.scrumMasterChanged = function(member) {
        if (member.scrumMaster) {
            $scope.team.scrumMasters.push(member);
        } else {
            _.remove($scope.team.scrumMasters, {email: member.email}); // equality on email because invited members have no id
        }
    };
    $scope.searchMembers = function(val) {
        return UserService.search(val, true).then(function(users) {
            return _.chain(users)
                .filter(function(member) {
                    var found = false;
                    if ($scope.project) {
                        found = _.find($scope.project.stakeHolders, {email: member.email});
                    }
                    if (!found) {
                        found = _.find($scope.team.members, {email: member.email});
                    }
                    return !found;
                })
                .map(function(member) {
                    member.name = $filter('userFullName')(member);
                    return member;
                })
                .value();
        });
    };
    $scope.invitationToUserMock = function(invitation) {
        return {email: invitation.email};
    };
    // Init
    $scope.team = $scope.team ? $scope.team : {};
    $scope.member = {};
}]);

controllers.controller('teamCtrl', ['$scope', '$controller', '$filter', 'Session', 'TeamService', function($scope, $controller, $filter, Session, TeamService) {
    $controller('abstractTeamCtrl', {$scope: $scope});
    // Functions
    $scope.searchTeam = function(val, create) {
        return TeamService.search(val, create).then(function(teams) {
            return teams;
        });
    };
    $scope.selectTeam = function($item, $model) {
        $scope.team = $model;
        $scope.team.selected = true;
        //Add current user to the team
        if (!$scope.team.id) {
            $scope.team.class = 'team';
            var current = angular.copy(Session.user);
            $scope.team.members = [];
            $scope.team.members.push(current);
            $scope.team.scrumMasters = [];
            $scope.team.scrumMasters.push(current);
        }
        if ($model.members && $model.scrumMasters) {
            $scope.team.members = $model.members.map(function(member) {
                member.scrumMaster = _.find($model.scrumMasters, {id: member.id}) ? true : false;
                return member;
            });
        }
        if ($scope.project) {
            var filteredStakeHolders = _.filter($scope.project.stakeHolders, function(u) {
                return !_.find($scope.team.members, {email: u.email});
            });
            var filteredProductOwners = _.filter($scope.project.productOwners, function(u) {
                var member = _.find($scope.team.members, {email: u.email});
                return member ? member.scrumMaster : true;
            });
            if (filteredProductOwners.length != $scope.project.productOwners.length || filteredStakeHolders.length != $scope.project.stakeHolders.length) {
                $scope.warning.on = true;
            }
            $scope.project.stakeHolders = filteredStakeHolders;
            $scope.project.productOwners = filteredProductOwners;
            $scope.team.members = $scope.team.members.map(function(member) {
                member.productOwner = _.find($scope.project.productOwners, {id: member.id}) ? true : false;
                return member;
            });
            $scope.project.team = $scope.team;
        }
        if (!_.isEmpty($model.invitedMembers)) {
            $scope.team.members = $scope.team.members.concat(_.map($model.invitedMembers, $scope.invitationToUserMock));
        }
        if (!_.isEmpty($model.invitedScrumMasters)) {
            $scope.team.members = $scope.team.members.concat(_.map($model.invitedScrumMasters, function(invitation) {
                return _.merge($scope.invitationToUserMock(invitation), {scrumMaster: true});
            }));
        }
    };
    $scope.unSelectTeam = function() {
        $scope.warning.on = false;
        $scope.project.team = {};
        if ($scope.team.selected) {
            $scope.team = {};
            $scope.member = {};
        }
    };
    $scope.addTeamMember = function(member) {
        var po = _.find($scope.project.productOwners, {email: member.email});
        if (po) {
            member.scrumMaster = true;
            member.productOwner = true;
            $scope.team.scrumMasters.push(member);
        }
        $scope.team.members.push(member);
        $scope.member = {};
    };
    // Init
    $scope.warning = {on: false};
    if ($scope.project && $scope.project.team) {
        $scope.selectTeam(null, $scope.project.team);
    }
}]);

controllers.controller('manageTeamsModalCtrl', ['$scope', '$controller', '$filter', 'TeamService', 'ProjectService', function($scope, $controller, $filter, TeamService, ProjectService) {
    $controller('abstractTeamCtrl', {$scope: $scope});
    // Functions
    $scope.selectTeam = function(team) {
        $scope.team = angular.copy(team);
        if (team.members && team.scrumMasters) {
            $scope.team.members = team.members.map(function(member) {
                member.scrumMaster = _.find(team.scrumMasters, {id: member.id}) ? true : false;
                return member;
            });
            if (!_.isEmpty(team.invitedMembers)) {
                $scope.team.members = $scope.team.members.concat(_.map(team.invitedMembers, $scope.invitationToUserMock));
            }
            if (!_.isEmpty(team.invitedScrumMasters)) {
                $scope.team.members = $scope.team.members.concat(_.map(team.invitedScrumMasters, function(invitation) {
                    return _.merge($scope.invitationToUserMock(invitation), {scrumMaster: true});
                }));
            }
            $scope.team.members = _.uniqBy($scope.team.members, 'email'); // Invited members may have already been added, we don't want to add them again
{'fraction_non_alphanumeric': 0.1384918679152292, 'fraction_numerical': 0.0010561149053016968, 'mean_word_length': 2.195500562429696, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, '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': '3633396', 'n_tokens_mistral': 4007, 'n_tokens_neox': 3906, 'n_words': 806}