2 * ================================================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ================================================================================
21 * Created by nnaffar on 12/20/15.
25 class NewUserModalCtrl {
26 constructor($scope, $log, usersService, applicationsService, confirmBoxService) {
27 var extRequestValue = false;
29 //$log.info('NewUserModalCtrl::init');
30 this.isSaving = false;
31 this.anyChanges = false;
33 this.isGettingAdminApps = false;
34 if($scope.ngDialogData && $scope.ngDialogData.selectedUser && $scope.ngDialogData.dialogState){
35 this.selectedUser = $scope.ngDialogData.selectedUser;
36 this.dialogState = $scope.ngDialogData.dialogState;
37 this.isShowBack = false;
38 if(this.dialogState === 3){
39 this.getUserAppsRoles();
42 this.isShowBack = true;
43 this.selectedUser = null;
48 this.appChanged = (index) => {
49 let myApp = this.adminApps[index];
50 //$log.debug('NewUserModalCtrl::appChanged: index: ', index, '; app id: ', myApp.id, 'app name: ',myApp.name);
51 myApp.isChanged = true;
52 this.anyChanges = true;
55 this.deleteApp = (app) => {
56 let appMessage = this.selectedUser.firstName + ' ' + this.selectedUser.lastName;
57 confirmBoxService.deleteItem(appMessage).then(isConfirmed => {
59 this.anyChanges = true;
61 app.isDeleted = true; // use this to hide the app in the display
62 app.appRoles.forEach(function(role){
63 role.isApplied = false;
67 $log.error('NewUserModalCtrl::deleteApp error: ',err);
68 confirmBoxService.showInformation('There was a problem deleting the the applications. ' +
69 'Please try again later. Error: ' + err.status).then(isConfirmed => {});
73 this.getUserAppsRoles = () => {
74 if (!this.selectedUser || !this.selectedUser.orgUserId) {
75 $log.error('NewUserModalCtrl::getUserAppsRoles error: No user is selected');
79 //$log.debug('NewUserModalCtrl::getUserAppsRoles: about to call getAdminAppsSimpler');
80 this.isGettingAdminApps = true;
81 applicationsService.getAdminAppsSimpler().then((apps) => {
82 //$log.debug('NewUserModalCtrl::getUserAppsRoles: beginning of then for getAdminAppsSimpler');
83 this.isGettingAdminApps = false;
84 if (!apps || !apps.length) {
85 $log.error('NewUserModalCtrl::getUserAppsRoles error: no admin apps found');
88 //$log.debug('NewUserModalCtrl::getUserAppsRoles: then for getAdminAppsSimpler: step 2');
89 //$log.debug('NewUserModalCtrl::getUserAppsRoles: admin apps: ', apps);
90 this.adminApps = apps;
92 this.userAppRoles = {};
93 this.numberAppsProcessed = 0;
94 this.isLoading = true;
96 //$log.debug('NewUserModalCtrl::getUserAppsRoles: app: id: ', app.id, 'name: ',app.name);
97 // Keep track of which app has changed, so we know which apps to update using a BE API
98 app.isChanged = false;
99 // Each of these specifies a state, which corresponds to a different message and style that gets displayed
100 app.isLoading = true;
102 app.isDeleted = false;
103 app.printNoChanges = false;
104 app.isUpdating = false;
105 app.isErrorUpdating = false;
106 app.isDoneUpdating = false;
107 app.errorMessage = "";
108 usersService.getUserAppRoles(app.id, this.selectedUser.orgUserId, extRequestValue).promise().then((userAppRolesResult) => {
109 //$log.debug('NewUserModalCtrl::getUserAppsRoles: got a result for app: ',app.id,': ',app.name,': ',userAppRolesResult);
110 app.appRoles = userAppRolesResult;
111 app.isLoading = false;
116 app.isLoading = false;
117 app.errorMessage = err.headers('FEErrorString');
118 //$log.debug('NewUserModalCtrl::getUserAppsRoles: in new-user.controller: response header: '+err.headers('FEErrorString'));
120 this.numberAppsProcessed++;
121 if (this.numberAppsProcessed === this.adminApps.length) {
122 this.isLoading = false;
134 * Update the selected user apps with the new roles.
135 * If no roles remain, set the user to inactive.
137 this.updateUserAppsRoles = () => {
138 // $log.debug('NewUserModalCtrl::updateUserAppsRoles: entering updateUserAppsRoles');
139 if(!this.selectedUser || !this.selectedUser.orgUserId || !this.adminApps){
140 $log.error('NewUserModalCtrl::updateUserAppsRoles: mmissing arguments');
143 this.isSaving = true;
144 //$log.debug('NewUserModalCtrl::updateUserAppsRoles: going to update user: ' + this.selectedUser.orgUserId);
145 this.numberAppsProcessed = 0;
146 this.numberAppsSucceeded = 0;
147 this.adminApps.forEach(app => {
149 //$log.debug('NewUserModalCtrl::updateUserAppsRoles: app roles have changed; going to update: id: ', app.id, '; name: ', app.name);
150 app.isUpdating = true;
151 var newUserAppRoles = {
152 orgUserId: this.selectedUser.orgUserId,
154 appRoles: app.appRoles,
157 usersService.updateUserAppRoles(newUserAppRoles).promise()
159 //$log.debug('NewUserModalCtrl::updateUserAppsRoles: User app roles updated successfully on app: ',app.id);
160 app.isUpdating = false;
161 app.isDoneUpdating = true;
162 this.numberAppsSucceeded++;
165 app.isErrorUpdating = true;
166 confirmBoxService.showInformation(
167 'Failed to update the user application roles: ' + err.status)
168 .then(isConfirmed => {});
170 this.numberAppsProcessed++;
171 if (this.numberAppsProcessed === this.adminApps.length) {
172 this.isSaving = false; // hide the spinner
174 if (this.numberAppsSucceeded === this.adminApps.length) {
175 $scope.closeThisDialog(true);//close and resolve dialog promise with true (to update the table)
179 //$log.debug('NewUserModalCtrl::updateUserAppsRoles: app roles have NOT changed; NOT going to update: id: ', app.id, '; name: ', app.name);
180 app.noChanges = true;
181 app.isError = false; //remove the error message; just show the No Changes messages
182 this.numberAppsProcessed++;
183 this.numberAppsSucceeded++;
184 if (this.numberAppsProcessed === this.adminApps.length) {
185 this.isSaving = false; // hide the spinner
187 if (this.numberAppsSucceeded === this.adminApps.length) {
188 $scope.closeThisDialog(true);//close and resolve dialog promise with true (to update the table)
195 * Navigate between dialog screens using step number: 1,2,...
197 this.navigateBack = () => {
198 if (this.dialogState === 1) {
199 //back from 1st screen?
201 if (this.dialogState === 3) {
202 this.dialogState = 1;
208 $scope.$on('$stateChangeStart', e => {
209 //Disable navigation when modal is opened
210 //**Nabil - note: this will cause the history back state to be replaced with current state
215 NewUserModalCtrl.$inject = ['$scope', '$log', 'usersService', 'applicationsService', 'confirmBoxService'];
216 angular.module('ecompApp').controller('NewUserModalCtrl', NewUserModalCtrl);