Adding new components with portal-FE-common
[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   }
123
124   removeAdminApp(app: any) {
125     const modalRef = this.ngModal.open(InformationModalComponent);
126     modalRef.componentInstance.title = "Confirmation";
127     modalRef.componentInstance.message = `Are you sure you want to delete ${app.appName}?`;
128     modalRef.result.then((result) => {
129       if (result === 'Ok') {
130         this.adminAppsRoles.forEach((item: any, index: any) => {
131           if (item === app) this.adminAppsRoles.splice(index, 1);
132         });
133         //call from delete admin app
134         this.updateDropdown(app, false);
135         this.adminsAppsSource = new MatTableDataSource(this.adminAppsRoles);
136       }
137     }, (resut) => {
138       return;
139     })
140   }
141
142   updateDropdown(_newValue: any, isDropdownCall: boolean) {
143     // app is selected from dropdown
144     if (isDropdownCall) {
145       this.adminDropdownApps.forEach((item: any, index: any) => {
146         if (item === _newValue) this.adminDropdownApps.splice(index, 1);
147       });
148       this.getadminAppSelectAndUnselectedData(_newValue);
149       _newValue.isAdmin = true;
150       this.adminAppsRoles.push(_newValue);
151       this.adminsAppsSource = new MatTableDataSource(this.adminAppsRoles);
152     } else {    // app is removed from the admin list
153       this.getadminAppSelectAndUnselectedData(_newValue);
154       _newValue.isAdmin = false;
155       this.adminDropdownApps.push(_newValue);
156     }
157
158     // disable save button if nothing new in the admin list
159     if (this.adminAppSelectAndUnselectData.length > 0)
160       this.newAppSelected = true;
161     else
162       this.newAppSelected = false;
163
164   }
165
166   private getadminAppSelectAndUnselectedData(_newValue: any) {
167     const index: number = this.adminAppSelectAndUnselectData.indexOf(_newValue);
168     if (index !== -1) {
169       this.adminAppSelectAndUnselectData.splice(index, 1); // if found, remove selected app from dropdown in the list
170     }
171     else {
172       this.adminAppSelectAndUnselectData.push(_newValue);
173     }
174   }
175
176   remindToAddUserIfNecessary() {
177     let adminAddedToNewApp = true;
178     if ((this.adminAppsRoles != null) && (this.adminAppsRoles.length > 0)) {
179       for (var i = 0; i < this.adminAppSelectAndUnselectData.length; i++) {
180         var foundApp = false;
181         for (var j = 0; j < this.adminAppsRoles.length; j++) {
182           if (this.adminAppsRoles[j] === this.adminAppSelectAndUnselectData[i]) {
183             foundApp = true;
184           }
185         }
186         if (foundApp === false) {
187           adminAddedToNewApp = true;
188           break;
189         }
190       }
191     } else {
192       adminAddedToNewApp = true;
193     }
194     if (adminAddedToNewApp === true) {
195       const modalRef = this.ngModal.open(InformationModalComponent);
196       modalRef.componentInstance.title = "Confirmation";
197       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.';
198       modalRef.result.then((_res) => {
199         if (_res === 'Ok') {
200           this.router.navigate(['/users']);
201         }
202       });
203     }
204   }
205
206   updateAdminAppsRoles() {
207     const modalRef = this.ngModal.open(InformationModalComponent);
208     modalRef.componentInstance.title = "Admin Update";
209     modalRef.componentInstance.message = 'Are you sure you want to make these admin changes?';
210     modalRef.result.then((result) => {
211       if (result === 'Ok') {
212         this.adminsService.updateAdminAppsRoles({ orgUserId: this.changedSelectedUser.orgUserId, appsRoles: this.adminAppsRoles }).subscribe(_data => {
213           this.passBackNewAdminPopup.emit(_data);
214           this.remindToAddUserIfNecessary();
215         }, (_err: HttpErrorResponse) => {
216           this.passBackNewAdminPopup.emit(_err);
217           const modalErrorRef = this.ngModal.open(ConfirmationModalComponent);
218           modalErrorRef.componentInstance.title = "Error";
219           if (_err.status) {
220             modalErrorRef.componentInstance.message = "There was a unknown problem while adding admin to selected application(s)." + "Please try again later. Error Status: " + _err.status;
221           }
222         });
223       }
224     }, (reason) => {
225       return;
226     });
227   }
228 }