Added new componetnts inside page modules
[portal.git] / portal-FE-common / src / app / pages / admins / new-admin / new-admin.component.ts
1 /*-
2  * ============LICENSE_START==========================================
3  * ONAP Portal
4  * ===================================================================
5  * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
6  * ===================================================================
7  *
8  * Unless otherwise specified, all software contained herein is licensed
9  * under the Apache License, Version 2.0 (the "License");
10  * you may not use this software except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  *             http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  *
21  * Unless otherwise specified, all documentation contained herein is licensed
22  * under the Creative Commons License, Attribution 4.0 Intl. (the "License");
23  * you may not use this documentation except in compliance with the License.
24  * You may obtain a copy of the License at
25  *
26  *             https://creativecommons.org/licenses/by/4.0/
27  *
28  * Unless required by applicable law or agreed to in writing, documentation
29  * distributed under the License is distributed on an "AS IS" BASIS,
30  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
31  * See the License for the specific language governing permissions and
32  * limitations under the License.
33  *
34  * ============LICENSE_END============================================
35  *
36  * 
37  */
38 import { Component, OnInit, Output, EventEmitter, Input } from '@angular/core';
39 import { AdminsService } from 'src/app/shared/services';
40 import { HttpErrorResponse } from '@angular/common/http';
41 import { ConfirmationModalComponent } from 'src/app/modals/confirmation-modal/confirmation-modal.component';
42 import { NgbModal, NgbActiveModal } from '@ng-bootstrap/ng-bootstrap';
43 import { PortalAdmin } from 'src/app/shared/model';
44 import { MatTableDataSource } from '@angular/material';
45 import { InformationModalComponent } from 'src/app/modals/information-modal/information-modal.component';
46 import { Router } from '@angular/router';
47
48 @Component({
49   selector: 'app-new-admin',
50   templateUrl: './new-admin.component.html',
51   styleUrls: ['./new-admin.component.scss']
52 })
53 export class NewAdminComponent implements OnInit {
54
55   @Input() dialogState: number;
56   @Input() userTitle: string;
57   @Input() disableBack: boolean;
58   @Input() adminModalData: any;
59   @Output() passBackNewAdminPopup: EventEmitter<any> = new EventEmitter();
60   searchTitleText = 'Enter First Name, Last Name or Org User Id';
61   placeholderText = 'Search';
62   changedSelectedUser: PortalAdmin;
63   adminAppsRoles: any;
64   adminDropdownApps: any;
65   isLoading: boolean;
66   newAppSelected: boolean;
67   adminAppSelectAndUnselectData: any;
68   displayedColumns: string[] = ['applications'];
69   adminsAppsSource = new MatTableDataSource(this.adminAppsRoles);
70   constructor(public router: Router, private adminsService: AdminsService, public ngModal: NgbModal, public activeModal: NgbActiveModal) { }
71
72   ngOnInit() {
73     this.adminAppsRoles = [];
74     this.changedSelectedUser = null;
75     if (this.disableBack){
76       this.changedSelectedUser = this.adminModalData;
77       this.getAdminAppsRoles();
78     }
79     this.adminDropdownApps = [];
80     this.adminAppSelectAndUnselectData = [];
81   }
82
83   changeSelectedUser(user: PortalAdmin) {
84     this.changedSelectedUser = user;
85     this.userTitle = `${this.changedSelectedUser.firstName}, ` + ` ${this.changedSelectedUser.lastName} ` + ` (${this.changedSelectedUser.orgUserId})`;
86   }
87
88   getAdminAppsRoles() {
89     this.isLoading = true;
90     this.adminsService.getAdminAppsRoles(this.changedSelectedUser.orgUserId).subscribe((_res: any) => {
91       JSON.stringify(_res);
92       if (!_res.appsRoles) {
93         return;
94       }
95       this.adminAppsRoles = [];
96       for (var i = 0; i < _res.appsRoles.length; i++) {
97         if (!_res.appsRoles[i].restrictedApp && _res.appsRoles[i].isAdmin) {
98           this.adminAppsRoles.push({
99             id: _res.appsRoles[i].id,
100             appName: _res.appsRoles[i].appName,
101             isAdmin: _res.appsRoles[i].isAdmin,
102             restrictedApp: _res.appsRoles[i].restrictedApp
103           });
104         } else if (!_res.appsRoles[i].restrictedApp) {
105           this.adminDropdownApps.push({
106             id: _res.appsRoles[i].id,
107             appName: _res.appsRoles[i].appName,
108             isAdmin: _res.appsRoles[i].isAdmin,
109             restrictedApp: _res.appsRoles[i].restrictedApp
110           });
111         }
112       }
113       this.isLoading = false;
114       this.newAppSelected = false;
115       this.dialogState = 2;
116       this.adminsAppsSource = new MatTableDataSource(this.adminAppsRoles);
117     });
118   }
119
120   navigateBack() {
121     this.dialogState = 1;
122     this.changedSelectedUser = null;
123   }
124
125   removeAdminApp(app: any) {
126     const modalRef = this.ngModal.open(InformationModalComponent);
127     modalRef.componentInstance.title = "Confirmation";
128     modalRef.componentInstance.message = `Are you sure you want to delete ${app.appName}?`;
129     modalRef.result.then((result) => {
130       if (result === 'Ok') {
131         this.adminAppsRoles.forEach((item: any, index: any) => {
132           if (item === app) this.adminAppsRoles.splice(index, 1);
133         });
134         //call from delete admin app
135         this.updateDropdown(app, false);
136         this.adminsAppsSource = new MatTableDataSource(this.adminAppsRoles);
137       }
138     }, (resut) => {
139       return;
140     })
141   }
142
143   updateDropdown(_newValue: any, isDropdownCall: boolean) {
144     // app is selected from dropdown
145     if (isDropdownCall) {
146       this.adminDropdownApps.forEach((item: any, index: any) => {
147         if (item === _newValue) this.adminDropdownApps.splice(index, 1);
148       });
149       this.getadminAppSelectAndUnselectedData(_newValue);
150       _newValue.isAdmin = true;
151       this.adminAppsRoles.push(_newValue);
152       this.adminsAppsSource = new MatTableDataSource(this.adminAppsRoles);
153     } else {    // app is removed from the admin list
154       this.getadminAppSelectAndUnselectedData(_newValue);
155       _newValue.isAdmin = false;
156       this.adminDropdownApps.push(_newValue);
157     }
158
159     // disable save button if nothing new in the admin list
160     if (this.adminAppSelectAndUnselectData.length > 0)
161       this.newAppSelected = true;
162     else
163       this.newAppSelected = false;
164
165   }
166
167   private getadminAppSelectAndUnselectedData(_newValue: any) {
168     const index: number = this.adminAppSelectAndUnselectData.indexOf(_newValue);
169     if (index !== -1) {
170       this.adminAppSelectAndUnselectData.splice(index, 1); // if found, remove selected app from dropdown in the list
171     }
172     else {
173       this.adminAppSelectAndUnselectData.push(_newValue);
174     }
175   }
176
177   remindToAddUserIfNecessary() {
178     let adminAddedToNewApp = true;
179     if ((this.adminAppsRoles != null) && (this.adminAppsRoles.length > 0)) {
180       for (var i = 0; i < this.adminAppSelectAndUnselectData.length; i++) {
181         var foundApp = false;
182         for (var j = 0; j < this.adminAppsRoles.length; j++) {
183           if (this.adminAppsRoles[j] === this.adminAppSelectAndUnselectData[i]) {
184             foundApp = true;
185           }
186         }
187         if (foundApp === false) {
188           adminAddedToNewApp = true;
189           break;
190         }
191       }
192     } else {
193       adminAddedToNewApp = true;
194     }
195     if (adminAddedToNewApp === true) {
196       const modalRef = this.ngModal.open(InformationModalComponent);
197       modalRef.componentInstance.title = "Confirmation";
198       modalRef.componentInstance.message = 'Add this person as an application user? This allows them to access the application from ONAP Portal. Press OK to go to the Add Users page.';
199       modalRef.result.then((_res) => {
200         if (_res === 'Ok') {
201           this.router.navigate(['/users']);
202         }
203       });
204     }
205   }
206
207   updateAdminAppsRoles() {
208     this.isLoading = true;
209     const modalRef = this.ngModal.open(InformationModalComponent);
210     modalRef.componentInstance.title = "Admin Update";
211     modalRef.componentInstance.message = 'Are you sure you want to make these admin changes?';
212     modalRef.result.then((result) => {
213       if (result === 'Ok') {
214         this.adminsService.updateAdminAppsRoles({ orgUserId: this.changedSelectedUser.orgUserId, appsRoles: this.adminAppsRoles }).subscribe(_data => {
215           this.passBackNewAdminPopup.emit(_data);
216           this.remindToAddUserIfNecessary();
217           this.isLoading = false;
218         }, (_err: HttpErrorResponse) => {
219           this.isLoading = false;
220           this.passBackNewAdminPopup.emit(_err);
221           const modalErrorRef = this.ngModal.open(ConfirmationModalComponent);
222           modalErrorRef.componentInstance.title = "Error";
223           if (_err.status) {
224             modalErrorRef.componentInstance.message = "There was a unknown problem while adding admin to selected application(s)." + "Please try again later. Error Status: " + _err.status;
225           }
226         });
227       }
228       this.isLoading = false;
229     }, (reason) => {
230       return;
231     });
232   }
233 }