|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +var module = angular.module('supportAdminApp'); |
| 4 | + |
| 5 | +module.controller('permissionmanagement.GroupMembersListController', [ |
| 6 | + '$scope', '$rootScope', 'GroupMemberService', 'IdResolverService', '$stateParams', '$state', '$q', 'Alert', |
| 7 | + function ($scope, $rootScope, GroupMemberService, IdResolverService, $stateParams, $state, $q, $alert) { |
| 8 | + |
| 9 | + // keep member types for easy iterating |
| 10 | + $scope.memberTypes = ['group', 'user']; |
| 11 | + |
| 12 | + // true if list is loading |
| 13 | + $scope.isLoading = false; |
| 14 | + |
| 15 | + // true if something is we are removing a bulk of entries |
| 16 | + $scope.processing = false; |
| 17 | + |
| 18 | + // list data |
| 19 | + $scope.memberships = {}; |
| 20 | + $scope.memberTypes.forEach(function(memberType) { |
| 21 | + $scope.memberships[memberType] = []; |
| 22 | + }); |
| 23 | + |
| 24 | + // current group id |
| 25 | + $scope.groupId = $stateParams.groupId; |
| 26 | + |
| 27 | + // true if any members were selected in the list |
| 28 | + $scope.hasSelected = false; |
| 29 | + |
| 30 | + // keeps |
| 31 | + $scope.isAllSelected = {}; |
| 32 | + $scope.memberTypes.forEach(function(memberType) { |
| 33 | + $scope.isAllSelected[memberType] = false; |
| 34 | + }); |
| 35 | + |
| 36 | + /* Maps user ids, present in the page, into user handles. */ |
| 37 | + $scope.users = {}; |
| 38 | + var loadUser = IdResolverService.getUserResolverFunction($scope.users); |
| 39 | + |
| 40 | + /* Maps groups ids, present in the page, into group names. */ |
| 41 | + $scope.groups = {}; |
| 42 | + var loadGroup = IdResolverService.getGroupResolverFunction($scope.groups); |
| 43 | + |
| 44 | + /** |
| 45 | + * Return membership records which are selected in the table by checkboxes |
| 46 | + * |
| 47 | + * @return {Array} membership records |
| 48 | + */ |
| 49 | + function getSelectedMemberships() { |
| 50 | + return $scope.memberTypes.reduce(function(selectedMemberships, memberType) { |
| 51 | + return selectedMemberships.concat($scope.memberships[memberType].filter(function(membership) { |
| 52 | + return membership.isSelected; |
| 53 | + })); |
| 54 | + }, []); |
| 55 | + } |
| 56 | + |
| 57 | + /** |
| 58 | + * Get list of group members for specified group |
| 59 | + * |
| 60 | + * @param {String} groupId group id for getting members |
| 61 | + */ |
| 62 | + $scope.fetch = function(groupId) { |
| 63 | + $alert.clear(); |
| 64 | + $scope.isLoading = true; |
| 65 | + |
| 66 | + GroupMemberService.fetch(groupId).then(function(data) { |
| 67 | + $scope.memberTypes.forEach(function(memberType) { |
| 68 | + $scope.memberships[memberType] = data.content.filter(function(membership) { return membership.membershipType === memberType }); |
| 69 | + $scope.memberships[memberType].forEach(function(membership) { |
| 70 | + loadUser(membership.createdBy); |
| 71 | + loadUser(membership.modifiedBy); |
| 72 | + |
| 73 | + if (memberType === 'user') { |
| 74 | + // for user members load handles |
| 75 | + loadUser(membership.memberId); |
| 76 | + } else { |
| 77 | + // for group members load names |
| 78 | + loadGroup(membership.memberId); |
| 79 | + } |
| 80 | + }); |
| 81 | + }); |
| 82 | + }).catch(function (error) { |
| 83 | + $alert.error(error.error, $rootScope); |
| 84 | + }).finally(function() { |
| 85 | + $scope.isLoading = false; |
| 86 | + }); |
| 87 | + }; |
| 88 | + |
| 89 | + /** |
| 90 | + * Checks if any membership records are selected in the table |
| 91 | + * and updates $scope.hasSelected value |
| 92 | + */ |
| 93 | + $scope.checkSelected = function() { |
| 94 | + $scope.hasSelected = !!getSelectedMemberships().length; |
| 95 | + } |
| 96 | + |
| 97 | + /** |
| 98 | + * Toggle all selected membership records of specified type |
| 99 | + * |
| 100 | + * @param {String} memberType type of membership |
| 101 | + */ |
| 102 | + $scope.toggleAll = function(memberType) { |
| 103 | + $scope.memberships[memberType].forEach(function(membership) { membership.isSelected = $scope.isAllSelected[memberType] }); |
| 104 | + } |
| 105 | + |
| 106 | + /** |
| 107 | + * Removes member from the current group |
| 108 | + * After removing record from the server, it removes the record from the table |
| 109 | + * |
| 110 | + * @param {Object} membership membership record |
| 111 | + * @return {Promise} promise to remove member |
| 112 | + */ |
| 113 | + $scope.removeMember = function(membership) { |
| 114 | + membership.isRemoving = true; |
| 115 | + return GroupMemberService.removeMember($stateParams.groupId, membership.id).then(function() { |
| 116 | + $scope.memberships[membership.membershipType] = $scope.memberships[membership.membershipType].filter(function(record) { |
| 117 | + return record.id !== membership.id; |
| 118 | + }); |
| 119 | + }).catch(function(error) { |
| 120 | + membership.isRemoving = false; |
| 121 | + $alert.error('Cannot remove member with id `' + membership.memberId + '`. ' + error.error, $rootScope); |
| 122 | + }); |
| 123 | + } |
| 124 | + |
| 125 | + /** |
| 126 | + * Remove all selected membership records |
| 127 | + */ |
| 128 | + $scope.removeSelected = function() { |
| 129 | + $alert.clear(); |
| 130 | + $scope.processing = true; |
| 131 | + |
| 132 | + var selectedMemberships = getSelectedMemberships(); |
| 133 | + |
| 134 | + // for now we remove all members in parallel |
| 135 | + // it's preferable, because it's faster |
| 136 | + // though if there will be any issues with server overload |
| 137 | + // it can be rewritten so requests go one by one |
| 138 | + $q.all(selectedMemberships.map(function(membership) { |
| 139 | + return $scope.removeMember(membership); |
| 140 | + })).catch(function(error) { |
| 141 | + $alert.error(error.error, $rootScope); |
| 142 | + }).finally(function() { |
| 143 | + $scope.processing = false; |
| 144 | + }); |
| 145 | + } |
| 146 | + |
| 147 | + // load the clients on controller init |
| 148 | + $scope.fetch($stateParams.groupId); |
| 149 | + |
| 150 | + // load name of current group |
| 151 | + loadGroup($scope.groupId) |
| 152 | + } |
| 153 | +]); |
0 commit comments