| 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 Add Modal |
| 20 | */ |
| SANDHYA.JS | 0a34dfa | 2023-04-25 23:59:41 +0530 | [diff] [blame] | 21 | import { isNullOrUndefined } from 'util'; |
| kumaran.m | 3b4814a | 2020-05-01 19:48:54 +0530 | [diff] [blame] | 22 | import { Component, Injector, Input, OnInit } from '@angular/core'; |
| Barath Kumar R | 09cd4ec | 2020-07-07 16:12:32 +0530 | [diff] [blame] | 23 | import { AbstractControl, FormBuilder, FormControl, FormGroup, Validators } from '@angular/forms'; |
| kumaran.m | 3b4814a | 2020-05-01 19:48:54 +0530 | [diff] [blame] | 24 | import { NgbActiveModal } from '@ng-bootstrap/ng-bootstrap'; |
| 25 | import { TranslateService } from '@ngx-translate/core'; |
| 26 | import { NotifierService } from 'angular-notifier'; |
| Barath Kumar R | 1607058 | 2021-02-08 18:19:35 +0530 | [diff] [blame] | 27 | import { APIURLHEADER, ERRORDATA, MODALCLOSERESPONSEDATA, TYPESECTION } from 'CommonModel'; |
| kumaran.m | 3b4814a | 2020-05-01 19:48:54 +0530 | [diff] [blame] | 28 | import { DataService } from 'DataService'; |
| 29 | import { environment } from 'environment'; |
| Barath Kumar R | 1607058 | 2021-02-08 18:19:35 +0530 | [diff] [blame] | 30 | import { ProjectData, ProjectDetails, QUOTAITEM, QUOTA_ITEMS } from 'ProjectModel'; |
| kumaran.m | 3b4814a | 2020-05-01 19:48:54 +0530 | [diff] [blame] | 31 | import { ProjectService } from 'ProjectService'; |
| 32 | import { RestService } from 'RestService'; |
| 33 | import { SharedService } from 'SharedService'; |
| kumaran.m | 3b4814a | 2020-05-01 19:48:54 +0530 | [diff] [blame] | 34 | |
| 35 | /** |
| 36 | * Creating component |
| 37 | * @Component takes ProjectCreateUpdateComponent.html as template url |
| 38 | */ |
| 39 | @Component({ |
| Barath Kumar R | 1607058 | 2021-02-08 18:19:35 +0530 | [diff] [blame] | 40 | selector: 'app-project-create-update', |
| 41 | templateUrl: './ProjectCreateUpdateComponent.html', |
| 42 | styleUrls: ['./ProjectCreateUpdateComponent.scss'] |
| kumaran.m | 3b4814a | 2020-05-01 19:48:54 +0530 | [diff] [blame] | 43 | }) |
| 44 | /** Exporting a class @exports ProjectCreateUpdateComponent */ |
| 45 | export class ProjectCreateUpdateComponent implements OnInit { |
| Barath Kumar R | 1607058 | 2021-02-08 18:19:35 +0530 | [diff] [blame] | 46 | /** To inject services @public */ |
| 47 | public injector: Injector; |
| kumaran.m | 3b4814a | 2020-05-01 19:48:54 +0530 | [diff] [blame] | 48 | |
| Barath Kumar R | 1607058 | 2021-02-08 18:19:35 +0530 | [diff] [blame] | 49 | /** Instance of the rest service @public */ |
| 50 | public restService: RestService; |
| kumaran.m | 3b4814a | 2020-05-01 19:48:54 +0530 | [diff] [blame] | 51 | |
| Barath Kumar R | 1607058 | 2021-02-08 18:19:35 +0530 | [diff] [blame] | 52 | /** Instance for active modal service @public */ |
| 53 | public activeModal: NgbActiveModal; |
| kumaran.m | 3b4814a | 2020-05-01 19:48:54 +0530 | [diff] [blame] | 54 | |
| Barath Kumar R | 1607058 | 2021-02-08 18:19:35 +0530 | [diff] [blame] | 55 | /** Contains the recently created project details @public */ |
| 56 | public recentProject: ProjectDetails; |
| kumaran.m | 3b4814a | 2020-05-01 19:48:54 +0530 | [diff] [blame] | 57 | |
| Barath Kumar R | 1607058 | 2021-02-08 18:19:35 +0530 | [diff] [blame] | 58 | /** Contains project create or edit @public */ |
| 59 | public getProjectType: string; |
| kumaran.m | 3b4814a | 2020-05-01 19:48:54 +0530 | [diff] [blame] | 60 | |
| Barath Kumar R | 1607058 | 2021-02-08 18:19:35 +0530 | [diff] [blame] | 61 | /** To inject input type services @public */ |
| 62 | @Input() public projectType: string; |
| kumaran.m | 3b4814a | 2020-05-01 19:48:54 +0530 | [diff] [blame] | 63 | |
| Barath Kumar R | 1607058 | 2021-02-08 18:19:35 +0530 | [diff] [blame] | 64 | /** FormGroup user Edit Account added to the form @ html @public */ |
| 65 | public projectForm: FormGroup; |
| kumaran.m | 3b4814a | 2020-05-01 19:48:54 +0530 | [diff] [blame] | 66 | |
| Barath Kumar R | 1607058 | 2021-02-08 18:19:35 +0530 | [diff] [blame] | 67 | /** Form submission Add */ |
| 68 | public submitted: boolean = false; |
| kumaran.m | 3b4814a | 2020-05-01 19:48:54 +0530 | [diff] [blame] | 69 | |
| Barath Kumar R | 1607058 | 2021-02-08 18:19:35 +0530 | [diff] [blame] | 70 | /** Check the loading results for loader status @public */ |
| 71 | public isLoadingResults: boolean = false; |
| kumaran.m | 3b4814a | 2020-05-01 19:48:54 +0530 | [diff] [blame] | 72 | |
| Barath Kumar R | 1607058 | 2021-02-08 18:19:35 +0530 | [diff] [blame] | 73 | /** Give the message for the loading @public */ |
| 74 | public message: string = 'PLEASEWAIT'; |
| kumaran.m | 3b4814a | 2020-05-01 19:48:54 +0530 | [diff] [blame] | 75 | |
| Barath Kumar R | 1607058 | 2021-02-08 18:19:35 +0530 | [diff] [blame] | 76 | /** Holds list of domains @public */ |
| 77 | public domains: TYPESECTION[] = []; |
| kumaran.m | 3b4814a | 2020-05-01 19:48:54 +0530 | [diff] [blame] | 78 | |
| Barath Kumar R | 1607058 | 2021-02-08 18:19:35 +0530 | [diff] [blame] | 79 | /** Holds list of quota items @public */ |
| 80 | public quotaItems: QUOTAITEM[] = QUOTA_ITEMS; |
| Barath Kumar R | 09cd4ec | 2020-07-07 16:12:32 +0530 | [diff] [blame] | 81 | |
| Barath Kumar R | 1607058 | 2021-02-08 18:19:35 +0530 | [diff] [blame] | 82 | /** Holds project reference from response @public */ |
| 83 | public quotaRefs: {} = null; |
| Barath Kumar R | 09cd4ec | 2020-07-07 16:12:32 +0530 | [diff] [blame] | 84 | |
| Barath Kumar R | 1607058 | 2021-02-08 18:19:35 +0530 | [diff] [blame] | 85 | /** FormBuilder instance added to the formBuilder @private */ |
| 86 | private formBuilder: FormBuilder; |
| kumaran.m | 3b4814a | 2020-05-01 19:48:54 +0530 | [diff] [blame] | 87 | |
| Barath Kumar R | 1607058 | 2021-02-08 18:19:35 +0530 | [diff] [blame] | 88 | /** DataService to pass the data from one component to another @private */ |
| 89 | private dataService: DataService; |
| kumaran.m | 3b4814a | 2020-05-01 19:48:54 +0530 | [diff] [blame] | 90 | |
| Barath Kumar R | 1607058 | 2021-02-08 18:19:35 +0530 | [diff] [blame] | 91 | /** Contains project name ref @private */ |
| 92 | private projectRef: string; |
| kumaran.m | 3b4814a | 2020-05-01 19:48:54 +0530 | [diff] [blame] | 93 | |
| Barath Kumar R | 1607058 | 2021-02-08 18:19:35 +0530 | [diff] [blame] | 94 | /** Notifier service to popup notification @private */ |
| 95 | private notifierService: NotifierService; |
| kumaran.m | 3b4814a | 2020-05-01 19:48:54 +0530 | [diff] [blame] | 96 | |
| Barath Kumar R | 1607058 | 2021-02-08 18:19:35 +0530 | [diff] [blame] | 97 | /** Contains tranlsate instance @private */ |
| 98 | private translateService: TranslateService; |
| kumaran.m | 3b4814a | 2020-05-01 19:48:54 +0530 | [diff] [blame] | 99 | |
| Barath Kumar R | 1607058 | 2021-02-08 18:19:35 +0530 | [diff] [blame] | 100 | /** Contains all methods related to shared @private */ |
| 101 | private sharedService: SharedService; |
| kumaran.m | 3b4814a | 2020-05-01 19:48:54 +0530 | [diff] [blame] | 102 | |
| Barath Kumar R | 1607058 | 2021-02-08 18:19:35 +0530 | [diff] [blame] | 103 | /** ModalData instance of modal @private */ |
| 104 | private modalData: MODALCLOSERESPONSEDATA; |
| kumaran.m | 3b4814a | 2020-05-01 19:48:54 +0530 | [diff] [blame] | 105 | |
| Barath Kumar R | 1607058 | 2021-02-08 18:19:35 +0530 | [diff] [blame] | 106 | /** Holds all project details @private */ |
| 107 | private projectService: ProjectService; |
| kumaran.m | 3b4814a | 2020-05-01 19:48:54 +0530 | [diff] [blame] | 108 | |
| Barath Kumar R | 1607058 | 2021-02-08 18:19:35 +0530 | [diff] [blame] | 109 | constructor(injector: Injector) { |
| 110 | this.injector = injector; |
| 111 | this.formBuilder = this.injector.get(FormBuilder); |
| 112 | this.restService = this.injector.get(RestService); |
| 113 | this.activeModal = this.injector.get(NgbActiveModal); |
| 114 | this.dataService = this.injector.get(DataService); |
| 115 | this.notifierService = this.injector.get(NotifierService); |
| 116 | this.translateService = this.injector.get(TranslateService); |
| 117 | this.sharedService = this.injector.get(SharedService); |
| 118 | this.projectService = this.injector.get(ProjectService); |
| 119 | /** Initializing Form Action */ |
| 120 | this.projectForm = this.formBuilder.group({ |
| 121 | project_name: ['', Validators.required], |
| 122 | domain_name: [null], |
| 123 | enable_quota: [false, Validators.required] |
| 124 | }); |
| kumaran.m | 3b4814a | 2020-05-01 19:48:54 +0530 | [diff] [blame] | 125 | } |
| kumaran.m | 3b4814a | 2020-05-01 19:48:54 +0530 | [diff] [blame] | 126 | |
| Barath Kumar R | 1607058 | 2021-02-08 18:19:35 +0530 | [diff] [blame] | 127 | /** convenience getter for easy access to form fields */ |
| 128 | get f(): FormGroup['controls'] { return this.projectForm.controls; } |
| kumaran.m | 3b4814a | 2020-05-01 19:48:54 +0530 | [diff] [blame] | 129 | |
| Barath Kumar R | 1607058 | 2021-02-08 18:19:35 +0530 | [diff] [blame] | 130 | /** Lifecyle Hooks the trigger before component is instantiate @public */ |
| 131 | public ngOnInit(): void { |
| 132 | this.getProjectType = this.projectType; |
| 133 | if (this.getProjectType === 'Edit') { |
| 134 | this.dataService.currentMessage.subscribe((data: ProjectData): void => { |
| 135 | if (data.projectName !== undefined || data.projectName !== '' || data.projectName !== null) { |
| 136 | this.projectForm.patchValue({ project_name: data.projectName }); |
| 137 | this.projectRef = data.id; |
| 138 | this.quotaRefs = data.quotas; |
| 139 | this.patchQuotaInfo(this.quotaRefs); |
| 140 | } |
| 141 | }); |
| Barath Kumar R | 09cd4ec | 2020-07-07 16:12:32 +0530 | [diff] [blame] | 142 | } else { |
| Barath Kumar R | 1607058 | 2021-02-08 18:19:35 +0530 | [diff] [blame] | 143 | this.patchQuotaInfo(); |
| 144 | this.getProjects(); |
| Barath Kumar R | 09cd4ec | 2020-07-07 16:12:32 +0530 | [diff] [blame] | 145 | } |
| Barath Kumar R | 09cd4ec | 2020-07-07 16:12:32 +0530 | [diff] [blame] | 146 | } |
| Barath Kumar R | 1607058 | 2021-02-08 18:19:35 +0530 | [diff] [blame] | 147 | |
| 148 | /** Get the last project name @public */ |
| 149 | public getProjects(): void { |
| 150 | this.isLoadingResults = true; |
| 151 | this.restService.getResource(environment.PROJECTS_URL).subscribe((projects: ProjectDetails[]): void => { |
| 152 | this.recentProject = projects.slice(-1).pop(); |
| 153 | this.getDomainList(); |
| 154 | }, (error: ERRORDATA): void => { |
| 155 | this.restService.handleError(error, 'get'); |
| 156 | this.isLoadingResults = false; |
| 157 | }); |
| 158 | } |
| 159 | |
| 160 | /** On modal submit users acction will called @public */ |
| 161 | public projectAction(userType: string): void { |
| 162 | this.submitted = true; |
| 163 | this.modalData = { |
| 164 | message: 'Done' |
| 165 | }; |
| 166 | this.sharedService.cleanForm(this.projectForm); |
| SANDHYA.JS | b1ec4c3 | 2022-04-05 08:58:00 +0530 | [diff] [blame] | 167 | for (const data of this.quotaItems) { |
| 168 | if (this.getFormControl(data.value).value > data.maxValue) { |
| 169 | this.notifierService.notify('error', this.translateService.instant('PAGE.PROJECT.QUOTALIMIT')); |
| 170 | break; |
| 171 | } |
| 172 | } |
| Barath Kumar R | 1607058 | 2021-02-08 18:19:35 +0530 | [diff] [blame] | 173 | if (!this.projectForm.invalid) { |
| 174 | if (userType === 'Add') { |
| 175 | this.createProject(); |
| 176 | } else if (userType === 'Edit') { |
| 177 | this.editProject(); |
| 178 | } |
| 179 | } |
| 180 | } |
| 181 | |
| 182 | /** Create project @public */ |
| 183 | public createProject(): void { |
| 184 | this.isLoadingResults = true; |
| 185 | const apiURLHeader: APIURLHEADER = { |
| 186 | url: environment.PROJECTS_URL |
| 187 | }; |
| 188 | const projectPayload: ProjectDetails = { |
| 189 | name: this.projectForm.value.project_name, |
| 190 | domain_name: !isNullOrUndefined(this.projectForm.value.domain_name) ? this.projectForm.value.domain_name : undefined |
| 191 | }; |
| 192 | this.addQuotaLimit(projectPayload); |
| 193 | this.restService.postResource(apiURLHeader, projectPayload).subscribe((): void => { |
| 194 | this.activeModal.close(this.modalData); |
| 195 | this.isLoadingResults = false; |
| 196 | this.notifierService.notify('success', this.translateService.instant('PAGE.PROJECT.CREATEDSUCCESSFULLY')); |
| 197 | }, (error: ERRORDATA): void => { |
| 198 | this.restService.handleError(error, 'post'); |
| 199 | this.isLoadingResults = false; |
| 200 | }); |
| 201 | } |
| 202 | /** Handle enable quota limit checkbox event @public */ |
| 203 | public checkQuota(): void { |
| 204 | if (this.getFormControl('enable_quota').value) { |
| 205 | this.quotaItems.forEach((quotaItem: QUOTAITEM): void => { |
| Barath Kumar R | 2e02e7d | 2021-06-09 09:18:17 +0530 | [diff] [blame] | 206 | this.projectForm.addControl(quotaItem.value, new FormControl('')); |
| Barath Kumar R | 1607058 | 2021-02-08 18:19:35 +0530 | [diff] [blame] | 207 | }); |
| 208 | } else { |
| 209 | this.quotaItems.forEach((quotaItem: QUOTAITEM): void => { |
| Barath Kumar R | 2e02e7d | 2021-06-09 09:18:17 +0530 | [diff] [blame] | 210 | this.getFormControl(quotaItem.value).setValue(''); |
| Barath Kumar R | 1607058 | 2021-02-08 18:19:35 +0530 | [diff] [blame] | 211 | }); |
| 212 | } |
| 213 | } |
| 214 | /** Edit project @public */ |
| 215 | public editProject(): void { |
| SANDHYA.JS | 382448f | 2023-10-12 14:31:00 +0530 | [diff] [blame^] | 216 | if (!this.projectForm.dirty) { |
| 217 | this.notifierService.notify('warning', this.translateService.instant('PAGE.TOPOLOGY.DATAEMPTY')); |
| 218 | return; |
| 219 | } |
| Barath Kumar R | 1607058 | 2021-02-08 18:19:35 +0530 | [diff] [blame] | 220 | this.isLoadingResults = true; |
| 221 | const apiURLHeader: APIURLHEADER = { |
| 222 | url: environment.PROJECTS_URL + '/' + this.projectRef |
| 223 | }; |
| 224 | const projectPayload: ProjectDetails = { |
| 225 | name: this.projectForm.value.project_name |
| 226 | }; |
| 227 | this.addQuotaLimit(projectPayload); |
| 228 | this.restService.patchResource(apiURLHeader, projectPayload).subscribe((): void => { |
| 229 | this.activeModal.close(this.modalData); |
| 230 | this.isLoadingResults = false; |
| 231 | this.projectService.setHeaderProjects(); |
| 232 | this.notifierService.notify('success', this.translateService.instant('PAGE.PROJECT.UPDATEDSUCCESSFULLY')); |
| SANDHYA.JS | 382448f | 2023-10-12 14:31:00 +0530 | [diff] [blame^] | 233 | this.activeModal.close(this.modalData); |
| Barath Kumar R | 1607058 | 2021-02-08 18:19:35 +0530 | [diff] [blame] | 234 | }, (error: ERRORDATA): void => { |
| 235 | this.restService.handleError(error, 'patch'); |
| 236 | this.isLoadingResults = false; |
| 237 | }); |
| 238 | } |
| 239 | /** Get domain name list @private */ |
| 240 | private getDomainList(): void { |
| 241 | this.isLoadingResults = true; |
| 242 | this.sharedService.getDomainName().subscribe((domainList: TYPESECTION[]): void => { |
| 243 | this.domains = domainList; |
| 244 | this.isLoadingResults = false; |
| 245 | }, (error: ERRORDATA): void => { |
| 246 | this.isLoadingResults = false; |
| 247 | this.restService.handleError(error, 'get'); |
| 248 | }); |
| 249 | } |
| 250 | |
| 251 | /** Used to get the AbstractControl of controlName passed @private */ |
| 252 | private getFormControl(controlName: string): AbstractControl { |
| SANDHYA.JS | 0a34dfa | 2023-04-25 23:59:41 +0530 | [diff] [blame] | 253 | // eslint-disable-next-line security/detect-object-injection |
| Barath Kumar R | 1607058 | 2021-02-08 18:19:35 +0530 | [diff] [blame] | 254 | return this.projectForm.controls[controlName]; |
| 255 | } |
| 256 | |
| 257 | /** Add quota information to payload @private */ |
| 258 | private addQuotaLimit(payload: ProjectDetails): void { |
| 259 | if (this.getFormControl('enable_quota').value) { |
| 260 | payload.quotas = {}; |
| 261 | this.quotaItems.forEach((quotaItem: QUOTAITEM): void => { |
| Barath Kumar R | 2e02e7d | 2021-06-09 09:18:17 +0530 | [diff] [blame] | 262 | if (this.getFormControl(quotaItem.value).value !== '') { |
| 263 | payload.quotas[quotaItem.value] = this.getFormControl(quotaItem.value).value; |
| 264 | } |
| Barath Kumar R | 1607058 | 2021-02-08 18:19:35 +0530 | [diff] [blame] | 265 | }); |
| Barath Kumar R | 2e02e7d | 2021-06-09 09:18:17 +0530 | [diff] [blame] | 266 | if (Object.keys(payload.quotas).length === 0) { |
| 267 | delete payload.quotas; |
| 268 | } |
| Barath Kumar R | 1607058 | 2021-02-08 18:19:35 +0530 | [diff] [blame] | 269 | } |
| 270 | } |
| 271 | |
| 272 | /** Set quota information in project form model @private */ |
| 273 | private patchQuotaInfo(quotaRef?: {}): void { |
| 274 | if (quotaRef !== null && this.getProjectType === 'Edit') { |
| 275 | this.getFormControl('enable_quota').setValue(true); |
| 276 | this.quotaItems.forEach((quotaItem: QUOTAITEM): void => { |
| 277 | if (!isNullOrUndefined(quotaRef[quotaItem.value])) { |
| 278 | this.projectForm.addControl(quotaItem.value, new FormControl(quotaRef[quotaItem.value], |
| Barath Kumar R | 2e02e7d | 2021-06-09 09:18:17 +0530 | [diff] [blame] | 279 | [Validators.max(quotaItem.maxValue)])); |
| Barath Kumar R | 1607058 | 2021-02-08 18:19:35 +0530 | [diff] [blame] | 280 | } else { |
| Barath Kumar R | 2e02e7d | 2021-06-09 09:18:17 +0530 | [diff] [blame] | 281 | this.projectForm.addControl(quotaItem.value, new FormControl('', [Validators.max(quotaItem.maxValue)])); |
| Barath Kumar R | 1607058 | 2021-02-08 18:19:35 +0530 | [diff] [blame] | 282 | } |
| 283 | }); |
| 284 | } else { |
| 285 | this.quotaItems.forEach((quotaItem: QUOTAITEM): void => { |
| Barath Kumar R | 2e02e7d | 2021-06-09 09:18:17 +0530 | [diff] [blame] | 286 | this.projectForm.addControl(quotaItem.value, new FormControl('', [Validators.min(quotaItem.minValue), |
| 287 | Validators.max(quotaItem.maxValue)])); |
| Barath Kumar R | 1607058 | 2021-02-08 18:19:35 +0530 | [diff] [blame] | 288 | }); |
| 289 | } |
| 290 | } |
| kumaran.m | 3b4814a | 2020-05-01 19:48:54 +0530 | [diff] [blame] | 291 | } |