| kumaran.m | 3b4814a | 2020-05-01 19:48:54 +0530 | [diff] [blame] | 1 | /* |
| 2 | Copyright 2020 TATA ELXSI |
| 3 | |
| 4 | Licensed under the Apache License, Version 2.0 (the 'License'); |
| 5 | you may not use this file except in compliance with the License. |
| 6 | You may obtain a copy of the License at |
| 7 | |
| 8 | http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | |
| 10 | Unless required by applicable law or agreed to in writing, software |
| 11 | distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | See the License for the specific language governing permissions and |
| 14 | limitations under the License. |
| 15 | |
| 16 | Author: KUMARAN M (kumaran.m@tataelxsi.co.in), RAJESH S (rajesh.s@tataelxsi.co.in), BARATH KUMAR R (barath.r@tataelxsi.co.in) |
| 17 | */ |
| 18 | /** |
| 19 | * @file Project Role Component. |
| 20 | */ |
| 21 | import { HttpHeaders } from '@angular/common/http'; |
| 22 | import { Component, Injector, Input, OnInit } from '@angular/core'; |
| 23 | import { AbstractControl, FormArray, FormBuilder, FormGroup, Validators } from '@angular/forms'; |
| 24 | import { NgbActiveModal } from '@ng-bootstrap/ng-bootstrap'; |
| 25 | import { TranslateService } from '@ngx-translate/core'; |
| 26 | import { NotifierService } from 'angular-notifier'; |
| 27 | import { APIURLHEADER, ERRORDATA, MODALCLOSERESPONSEDATA } from 'CommonModel'; |
| 28 | import { environment } from 'environment'; |
| 29 | import { ProjectData } from 'ProjectModel'; |
| 30 | import { ProjectService } from 'ProjectService'; |
| 31 | import { RestService } from 'RestService'; |
| 32 | import { RoleData } from 'RolesModel'; |
| 33 | import { ProjectRoleMappings, UserDetail, UserRoleMap } from 'UserModel'; |
| 34 | |
| 35 | /** |
| 36 | * Creating component |
| 37 | * @Component takes ProjectRole.html as template url |
| 38 | */ |
| 39 | @Component({ |
| Barath Kumar R | 42fe05d | 2021-01-29 16:02:34 +0530 | [diff] [blame] | 40 | templateUrl: './ProjectRoleComponent.html', |
| 41 | styleUrls: ['./ProjectRoleComponent.scss'] |
| kumaran.m | 3b4814a | 2020-05-01 19:48:54 +0530 | [diff] [blame] | 42 | }) |
| 43 | /** Exporting a class @exports ProjectRoleComponent */ |
| 44 | export class ProjectRoleComponent implements OnInit { |
| Barath Kumar R | 42fe05d | 2021-01-29 16:02:34 +0530 | [diff] [blame] | 45 | /** To inject services @public */ |
| 46 | public injector: Injector; |
| kumaran.m | 3b4814a | 2020-05-01 19:48:54 +0530 | [diff] [blame] | 47 | |
| Barath Kumar R | 42fe05d | 2021-01-29 16:02:34 +0530 | [diff] [blame] | 48 | /** Instance for active modal service @public */ |
| 49 | public activeModal: NgbActiveModal; |
| kumaran.m | 3b4814a | 2020-05-01 19:48:54 +0530 | [diff] [blame] | 50 | |
| Barath Kumar R | 42fe05d | 2021-01-29 16:02:34 +0530 | [diff] [blame] | 51 | /** FormGroup user Edit Account added to the form @ html @public */ |
| 52 | public projectRoleForm: FormGroup; |
| kumaran.m | 3b4814a | 2020-05-01 19:48:54 +0530 | [diff] [blame] | 53 | |
| Barath Kumar R | 42fe05d | 2021-01-29 16:02:34 +0530 | [diff] [blame] | 54 | /** Form submission Add */ |
| 55 | public submitted: boolean = false; |
| kumaran.m | 3b4814a | 2020-05-01 19:48:54 +0530 | [diff] [blame] | 56 | |
| Barath Kumar R | 42fe05d | 2021-01-29 16:02:34 +0530 | [diff] [blame] | 57 | /** Input contains Modal dialog component Instance @private */ |
| 58 | @Input() public userTitle: string; |
| kumaran.m | 3b4814a | 2020-05-01 19:48:54 +0530 | [diff] [blame] | 59 | |
| Barath Kumar R | 42fe05d | 2021-01-29 16:02:34 +0530 | [diff] [blame] | 60 | /** Input contains Modal dialog component Instance @private */ |
| 61 | @Input() public userID: string; |
| kumaran.m | 3b4814a | 2020-05-01 19:48:54 +0530 | [diff] [blame] | 62 | |
| Barath Kumar R | 42fe05d | 2021-01-29 16:02:34 +0530 | [diff] [blame] | 63 | /** Contains user details information @public */ |
| 64 | public userDetails: UserDetail; |
| kumaran.m | 3b4814a | 2020-05-01 19:48:54 +0530 | [diff] [blame] | 65 | |
| Barath Kumar R | 42fe05d | 2021-01-29 16:02:34 +0530 | [diff] [blame] | 66 | /** Project Role Mapping @public */ |
| 67 | public projectRoleMap: UserRoleMap = {}; |
| kumaran.m | 3b4814a | 2020-05-01 19:48:54 +0530 | [diff] [blame] | 68 | |
| Barath Kumar R | 42fe05d | 2021-01-29 16:02:34 +0530 | [diff] [blame] | 69 | /** Check the loading results @public */ |
| 70 | public isLoadingResults: boolean = false; |
| kumaran.m | 3b4814a | 2020-05-01 19:48:54 +0530 | [diff] [blame] | 71 | |
| Barath Kumar R | 42fe05d | 2021-01-29 16:02:34 +0530 | [diff] [blame] | 72 | /** Give the message for the loading @public */ |
| 73 | public message: string = 'PLEASEWAIT'; |
| kumaran.m | 3b4814a | 2020-05-01 19:48:54 +0530 | [diff] [blame] | 74 | |
| Barath Kumar R | 42fe05d | 2021-01-29 16:02:34 +0530 | [diff] [blame] | 75 | /** Contains project information @public */ |
| 76 | public projects: ProjectData[] = []; |
| kumaran.m | 3b4814a | 2020-05-01 19:48:54 +0530 | [diff] [blame] | 77 | |
| Barath Kumar R | 42fe05d | 2021-01-29 16:02:34 +0530 | [diff] [blame] | 78 | /** Contains roles information @public */ |
| 79 | public roles: RoleData[] = []; |
| kumaran.m | 3b4814a | 2020-05-01 19:48:54 +0530 | [diff] [blame] | 80 | |
| Barath Kumar R | 42fe05d | 2021-01-29 16:02:34 +0530 | [diff] [blame] | 81 | /** Instance of the rest service @private */ |
| 82 | private restService: RestService; |
| kumaran.m | 3b4814a | 2020-05-01 19:48:54 +0530 | [diff] [blame] | 83 | |
| Barath Kumar R | 42fe05d | 2021-01-29 16:02:34 +0530 | [diff] [blame] | 84 | /** FormBuilder instance added to the formBuilder @private */ |
| 85 | private formBuilder: FormBuilder; |
| kumaran.m | 3b4814a | 2020-05-01 19:48:54 +0530 | [diff] [blame] | 86 | |
| Barath Kumar R | 42fe05d | 2021-01-29 16:02:34 +0530 | [diff] [blame] | 87 | /** Controls the header form @private */ |
| 88 | private headers: HttpHeaders; |
| kumaran.m | 3b4814a | 2020-05-01 19:48:54 +0530 | [diff] [blame] | 89 | |
| Barath Kumar R | 42fe05d | 2021-01-29 16:02:34 +0530 | [diff] [blame] | 90 | /** Notifier service to popup notification @private */ |
| 91 | private notifierService: NotifierService; |
| kumaran.m | 3b4814a | 2020-05-01 19:48:54 +0530 | [diff] [blame] | 92 | |
| Barath Kumar R | 42fe05d | 2021-01-29 16:02:34 +0530 | [diff] [blame] | 93 | /** Contains tranlsate instance @private */ |
| 94 | private translateService: TranslateService; |
| kumaran.m | 3b4814a | 2020-05-01 19:48:54 +0530 | [diff] [blame] | 95 | |
| Barath Kumar R | 42fe05d | 2021-01-29 16:02:34 +0530 | [diff] [blame] | 96 | /** Project Role Form array @private */ |
| 97 | private projectRoleFormArray: FormArray; |
| kumaran.m | 3b4814a | 2020-05-01 19:48:54 +0530 | [diff] [blame] | 98 | |
| Barath Kumar R | 42fe05d | 2021-01-29 16:02:34 +0530 | [diff] [blame] | 99 | /** Holds all project details @private */ |
| 100 | private projectService: ProjectService; |
| kumaran.m | 3b4814a | 2020-05-01 19:48:54 +0530 | [diff] [blame] | 101 | |
| Barath Kumar R | 42fe05d | 2021-01-29 16:02:34 +0530 | [diff] [blame] | 102 | constructor(injector: Injector) { |
| 103 | this.injector = injector; |
| 104 | this.formBuilder = this.injector.get(FormBuilder); |
| 105 | this.restService = this.injector.get(RestService); |
| 106 | this.activeModal = this.injector.get(NgbActiveModal); |
| 107 | this.notifierService = this.injector.get(NotifierService); |
| 108 | this.translateService = this.injector.get(TranslateService); |
| 109 | this.projectService = this.injector.get(ProjectService); |
| kumaran.m | 3b4814a | 2020-05-01 19:48:54 +0530 | [diff] [blame] | 110 | } |
| kumaran.m | 3b4814a | 2020-05-01 19:48:54 +0530 | [diff] [blame] | 111 | |
| Barath Kumar R | 42fe05d | 2021-01-29 16:02:34 +0530 | [diff] [blame] | 112 | /** Generate primitive params @public */ |
| 113 | get projectRoleParamsBuilder(): FormGroup { |
| 114 | return this.formBuilder.group({ |
| 115 | project_name: [null, [Validators.required]], |
| 116 | role_name: [null, [Validators.required]] |
| 117 | }); |
| kumaran.m | 3b4814a | 2020-05-01 19:48:54 +0530 | [diff] [blame] | 118 | } |
| kumaran.m | 3b4814a | 2020-05-01 19:48:54 +0530 | [diff] [blame] | 119 | |
| Barath Kumar R | 42fe05d | 2021-01-29 16:02:34 +0530 | [diff] [blame] | 120 | /** convenience getter for easy access to form fields */ |
| 121 | get f(): FormGroup['controls'] { return this.projectRoleForm.controls; } |
| 122 | |
| 123 | /** Lifecyle Hooks the trigger before component is instantiate @public */ |
| 124 | public ngOnInit(): void { |
| 125 | this.headers = new HttpHeaders({ |
| 126 | 'Content-Type': 'application/json', |
| 127 | Accept: 'application/json', |
| 128 | 'Cache-Control': 'no-cache, no-store, must-revalidate, max-age=0' |
| 129 | }); |
| 130 | this.initializeForm(); |
| 131 | this.getProjects(); |
| 132 | this.generateData(); |
| 133 | } |
| 134 | |
| 135 | /** Initializing Form Action @public */ |
| 136 | public initializeForm(): void { |
| 137 | this.projectRoleForm = this.formBuilder.group({ |
| 138 | project_role_mappings: this.formBuilder.array([]) |
| 139 | }); |
| 140 | } |
| 141 | |
| 142 | /** Handle FormArray Controls @public */ |
| 143 | public getControls(): AbstractControl[] { |
| Barath Kumar R | 42fe05d | 2021-01-29 16:02:34 +0530 | [diff] [blame] | 144 | return (this.projectRoleForm.get('project_role_mappings') as FormArray).controls; |
| 145 | } |
| 146 | |
| 147 | /** Fetching the data from server to Load in the smarttable @public */ |
| 148 | public generateData(): void { |
| 149 | if (this.userID !== '') { |
| 150 | this.isLoadingResults = true; |
| 151 | this.restService.getResource(environment.USERS_URL + '/' + this.userID).subscribe((userDetails: UserDetail): void => { |
| 152 | this.userDetails = userDetails; |
| 153 | this.loadMapping(); |
| 154 | this.isLoadingResults = false; |
| 155 | }, (error: ERRORDATA): void => { |
| 156 | this.isLoadingResults = false; |
| 157 | this.restService.handleError(error, 'get'); |
| 158 | }); |
| 159 | } |
| 160 | } |
| 161 | /** Fetching the projects information @public */ |
| 162 | public getProjects(): void { |
| 163 | this.isLoadingResults = true; |
| 164 | this.restService.getResource(environment.PROJECTS_URL).subscribe((projectsData: ProjectData[]): void => { |
| 165 | this.projects = projectsData; |
| 166 | this.getRoles(); |
| 167 | }, (error: ERRORDATA): void => { |
| 168 | this.isLoadingResults = false; |
| 169 | this.restService.handleError(error, 'get'); |
| 170 | }); |
| 171 | } |
| 172 | |
| 173 | /** Fetching the Roles information @public */ |
| 174 | public getRoles(): void { |
| 175 | this.restService.getResource(environment.ROLES_URL).subscribe((rolesData: RoleData[]): void => { |
| 176 | this.roles = rolesData; |
| 177 | this.isLoadingResults = false; |
| 178 | }, (error: ERRORDATA): void => { |
| 179 | this.isLoadingResults = false; |
| 180 | this.restService.handleError(error, 'get'); |
| 181 | }); |
| 182 | } |
| 183 | |
| 184 | /** Set all roles and project values to the form @public */ |
| 185 | public loadMapping(): void { |
| 186 | this.userDetails.project_role_mappings.forEach((data: ProjectRoleMappings): void => { |
| Barath Kumar R | 42fe05d | 2021-01-29 16:02:34 +0530 | [diff] [blame] | 187 | this.projectRoleFormArray = this.projectRoleForm.get('project_role_mappings') as FormArray; |
| 188 | this.projectRoleFormArray.push(this.projectRoleParamsBuilder); |
| 189 | }); |
| 190 | this.projectRoleForm.patchValue(this.userDetails); |
| 191 | } |
| 192 | |
| 193 | /** Remove project and roles from the list @public */ |
| 194 | public removeMapping(index: number): void { |
| 195 | this.projectRoleFormArray.removeAt(index); |
| 196 | } |
| 197 | |
| 198 | /** Submit project and roles @public */ |
| 199 | public addProjectRole(): void { |
| 200 | this.submitted = true; |
| 201 | const modalData: MODALCLOSERESPONSEDATA = { |
| 202 | message: 'Done' |
| 203 | }; |
| 204 | if (this.projectRoleForm.invalid) { return; } |
| 205 | const apiURLHeader: APIURLHEADER = { |
| 206 | url: environment.USERS_URL + '/' + this.userID |
| 207 | }; |
| 208 | this.projectRoleMap.project_role_mappings = []; |
| 209 | this.projectRoleForm.value.project_role_mappings.forEach((res: ProjectRoleMappings): void => { |
| 210 | this.projectRoleMap.project_role_mappings.push({ project: res.project_name, role: res.role_name }); |
| 211 | }); |
| 212 | if (this.projectRoleMap.project_role_mappings.length !== 0) { |
| 213 | this.isLoadingResults = true; |
| 214 | this.restService.patchResource(apiURLHeader, this.projectRoleMap).subscribe((result: {}): void => { |
| 215 | this.isLoadingResults = false; |
| 216 | this.activeModal.close(modalData); |
| 217 | this.projectService.setHeaderProjects(); |
| 218 | this.notifierService.notify('success', this.translateService.instant('PAGE.USERS.EDITEDSUCCESSFULLY')); |
| 219 | }, (error: ERRORDATA): void => { |
| 220 | this.isLoadingResults = false; |
| 221 | this.restService.handleError(error, 'patch'); |
| 222 | }); |
| 223 | } else { |
| 224 | this.notifierService.notify('error', this.translateService.instant('PAGE.USERS.EDITPROJECTROLEERROR')); |
| 225 | } |
| 226 | } |
| 227 | |
| 228 | /** Add extra mapping and set empty project and roles @public */ |
| 229 | public addMapping(): void { |
| Barath Kumar R | 42fe05d | 2021-01-29 16:02:34 +0530 | [diff] [blame] | 230 | this.projectRoleFormArray = this.projectRoleForm.get('project_role_mappings') as FormArray; |
| 231 | this.projectRoleFormArray.push(this.projectRoleParamsBuilder); |
| 232 | } |
| 233 | |
| 234 | /** Remove project and roles for the user @public */ |
| 235 | public deleteProjectAndRoleMapping(getProjectRoles: ProjectRoleMappings): void { |
| 236 | const modalData: MODALCLOSERESPONSEDATA = { |
| 237 | message: 'Done' |
| 238 | }; |
| 239 | const removeProjectRole: UserRoleMap = { remove_project_role_mappings: [] }; |
| 240 | removeProjectRole.remove_project_role_mappings = [{ project: getProjectRoles.project_name, role: getProjectRoles.role_name }]; |
| 241 | const apiURLHeader: APIURLHEADER = { |
| 242 | url: environment.USERS_URL + '/' + this.userID |
| 243 | }; |
| 244 | this.isLoadingResults = true; |
| 245 | this.restService.patchResource(apiURLHeader, removeProjectRole).subscribe((result: {}): void => { |
| 246 | this.isLoadingResults = false; |
| 247 | this.activeModal.close(modalData); |
| 248 | this.projectService.setHeaderProjects(); |
| 249 | this.notifierService.notify('success', this.translateService.instant('PAGE.USERS.EDITEDSUCCESSFULLY')); |
| 250 | }, (error: ERRORDATA): void => { |
| 251 | this.isLoadingResults = false; |
| 252 | this.restService.handleError(error, 'patch'); |
| 253 | }); |
| 254 | } |
| kumaran.m | 3b4814a | 2020-05-01 19:48:54 +0530 | [diff] [blame] | 255 | } |