blob: 2d45ef6d2d37ad7c2c8021b0a9eea99d78af07f5 [file] [log] [blame]
kumaran.m3b4814a2020-05-01 19:48:54 +05301/*
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 */
21import { Component, Injector, Input, OnInit } from '@angular/core';
Barath Kumar R09cd4ec2020-07-07 16:12:32 +053022import { AbstractControl, FormBuilder, FormControl, FormGroup, Validators } from '@angular/forms';
kumaran.m3b4814a2020-05-01 19:48:54 +053023import { NgbActiveModal } from '@ng-bootstrap/ng-bootstrap';
24import { TranslateService } from '@ngx-translate/core';
25import { NotifierService } from 'angular-notifier';
Barath Kumar R16070582021-02-08 18:19:35 +053026import { APIURLHEADER, ERRORDATA, MODALCLOSERESPONSEDATA, TYPESECTION } from 'CommonModel';
kumaran.m3b4814a2020-05-01 19:48:54 +053027import { DataService } from 'DataService';
28import { environment } from 'environment';
Barath Kumar R16070582021-02-08 18:19:35 +053029import { ProjectData, ProjectDetails, QUOTAITEM, QUOTA_ITEMS } from 'ProjectModel';
kumaran.m3b4814a2020-05-01 19:48:54 +053030import { ProjectService } from 'ProjectService';
31import { RestService } from 'RestService';
SANDHYA.JSc84f1122024-06-04 21:50:03 +053032import { SharedService, isNullOrUndefined } from 'SharedService';
kumaran.m3b4814a2020-05-01 19:48:54 +053033
34/**
35 * Creating component
36 * @Component takes ProjectCreateUpdateComponent.html as template url
37 */
38@Component({
Barath Kumar R16070582021-02-08 18:19:35 +053039 selector: 'app-project-create-update',
40 templateUrl: './ProjectCreateUpdateComponent.html',
41 styleUrls: ['./ProjectCreateUpdateComponent.scss']
kumaran.m3b4814a2020-05-01 19:48:54 +053042})
43/** Exporting a class @exports ProjectCreateUpdateComponent */
44export class ProjectCreateUpdateComponent implements OnInit {
Barath Kumar R16070582021-02-08 18:19:35 +053045 /** To inject services @public */
46 public injector: Injector;
kumaran.m3b4814a2020-05-01 19:48:54 +053047
Barath Kumar R16070582021-02-08 18:19:35 +053048 /** Instance of the rest service @public */
49 public restService: RestService;
kumaran.m3b4814a2020-05-01 19:48:54 +053050
Barath Kumar R16070582021-02-08 18:19:35 +053051 /** Instance for active modal service @public */
52 public activeModal: NgbActiveModal;
kumaran.m3b4814a2020-05-01 19:48:54 +053053
Barath Kumar R16070582021-02-08 18:19:35 +053054 /** Contains the recently created project details @public */
55 public recentProject: ProjectDetails;
kumaran.m3b4814a2020-05-01 19:48:54 +053056
Barath Kumar R16070582021-02-08 18:19:35 +053057 /** Contains project create or edit @public */
58 public getProjectType: string;
kumaran.m3b4814a2020-05-01 19:48:54 +053059
Barath Kumar R16070582021-02-08 18:19:35 +053060 /** To inject input type services @public */
61 @Input() public projectType: string;
kumaran.m3b4814a2020-05-01 19:48:54 +053062
Barath Kumar R16070582021-02-08 18:19:35 +053063 /** FormGroup user Edit Account added to the form @ html @public */
64 public projectForm: FormGroup;
kumaran.m3b4814a2020-05-01 19:48:54 +053065
Barath Kumar R16070582021-02-08 18:19:35 +053066 /** Form submission Add */
67 public submitted: boolean = false;
kumaran.m3b4814a2020-05-01 19:48:54 +053068
Barath Kumar R16070582021-02-08 18:19:35 +053069 /** Check the loading results for loader status @public */
70 public isLoadingResults: boolean = false;
kumaran.m3b4814a2020-05-01 19:48:54 +053071
Barath Kumar R16070582021-02-08 18:19:35 +053072 /** Give the message for the loading @public */
73 public message: string = 'PLEASEWAIT';
kumaran.m3b4814a2020-05-01 19:48:54 +053074
Barath Kumar R16070582021-02-08 18:19:35 +053075 /** Holds list of domains @public */
76 public domains: TYPESECTION[] = [];
kumaran.m3b4814a2020-05-01 19:48:54 +053077
Barath Kumar R16070582021-02-08 18:19:35 +053078 /** Holds list of quota items @public */
79 public quotaItems: QUOTAITEM[] = QUOTA_ITEMS;
Barath Kumar R09cd4ec2020-07-07 16:12:32 +053080
Barath Kumar R16070582021-02-08 18:19:35 +053081 /** Holds project reference from response @public */
82 public quotaRefs: {} = null;
Barath Kumar R09cd4ec2020-07-07 16:12:32 +053083
Barath Kumar R16070582021-02-08 18:19:35 +053084 /** FormBuilder instance added to the formBuilder @private */
85 private formBuilder: FormBuilder;
kumaran.m3b4814a2020-05-01 19:48:54 +053086
Barath Kumar R16070582021-02-08 18:19:35 +053087 /** DataService to pass the data from one component to another @private */
88 private dataService: DataService;
kumaran.m3b4814a2020-05-01 19:48:54 +053089
Barath Kumar R16070582021-02-08 18:19:35 +053090 /** Contains project name ref @private */
91 private projectRef: string;
kumaran.m3b4814a2020-05-01 19:48:54 +053092
Barath Kumar R16070582021-02-08 18:19:35 +053093 /** Notifier service to popup notification @private */
94 private notifierService: NotifierService;
kumaran.m3b4814a2020-05-01 19:48:54 +053095
Barath Kumar R16070582021-02-08 18:19:35 +053096 /** Contains tranlsate instance @private */
97 private translateService: TranslateService;
kumaran.m3b4814a2020-05-01 19:48:54 +053098
Barath Kumar R16070582021-02-08 18:19:35 +053099 /** Contains all methods related to shared @private */
100 private sharedService: SharedService;
kumaran.m3b4814a2020-05-01 19:48:54 +0530101
Barath Kumar R16070582021-02-08 18:19:35 +0530102 /** ModalData instance of modal @private */
103 private modalData: MODALCLOSERESPONSEDATA;
kumaran.m3b4814a2020-05-01 19:48:54 +0530104
Barath Kumar R16070582021-02-08 18:19:35 +0530105 /** Holds all project details @private */
106 private projectService: ProjectService;
kumaran.m3b4814a2020-05-01 19:48:54 +0530107
Barath Kumar R16070582021-02-08 18:19:35 +0530108 constructor(injector: Injector) {
109 this.injector = injector;
110 this.formBuilder = this.injector.get(FormBuilder);
111 this.restService = this.injector.get(RestService);
112 this.activeModal = this.injector.get(NgbActiveModal);
113 this.dataService = this.injector.get(DataService);
114 this.notifierService = this.injector.get(NotifierService);
115 this.translateService = this.injector.get(TranslateService);
116 this.sharedService = this.injector.get(SharedService);
117 this.projectService = this.injector.get(ProjectService);
118 /** Initializing Form Action */
119 this.projectForm = this.formBuilder.group({
120 project_name: ['', Validators.required],
121 domain_name: [null],
122 enable_quota: [false, Validators.required]
123 });
kumaran.m3b4814a2020-05-01 19:48:54 +0530124 }
kumaran.m3b4814a2020-05-01 19:48:54 +0530125
Barath Kumar R16070582021-02-08 18:19:35 +0530126 /** convenience getter for easy access to form fields */
127 get f(): FormGroup['controls'] { return this.projectForm.controls; }
kumaran.m3b4814a2020-05-01 19:48:54 +0530128
Barath Kumar R16070582021-02-08 18:19:35 +0530129 /** Lifecyle Hooks the trigger before component is instantiate @public */
130 public ngOnInit(): void {
131 this.getProjectType = this.projectType;
132 if (this.getProjectType === 'Edit') {
133 this.dataService.currentMessage.subscribe((data: ProjectData): void => {
134 if (data.projectName !== undefined || data.projectName !== '' || data.projectName !== null) {
135 this.projectForm.patchValue({ project_name: data.projectName });
136 this.projectRef = data.id;
137 this.quotaRefs = data.quotas;
138 this.patchQuotaInfo(this.quotaRefs);
139 }
140 });
Barath Kumar R09cd4ec2020-07-07 16:12:32 +0530141 } else {
Barath Kumar R16070582021-02-08 18:19:35 +0530142 this.patchQuotaInfo();
143 this.getProjects();
Barath Kumar R09cd4ec2020-07-07 16:12:32 +0530144 }
Barath Kumar R09cd4ec2020-07-07 16:12:32 +0530145 }
Barath Kumar R16070582021-02-08 18:19:35 +0530146
147 /** Get the last project name @public */
148 public getProjects(): void {
149 this.isLoadingResults = true;
150 this.restService.getResource(environment.PROJECTS_URL).subscribe((projects: ProjectDetails[]): void => {
151 this.recentProject = projects.slice(-1).pop();
152 this.getDomainList();
153 }, (error: ERRORDATA): void => {
154 this.restService.handleError(error, 'get');
155 this.isLoadingResults = false;
156 });
157 }
158
159 /** On modal submit users acction will called @public */
160 public projectAction(userType: string): void {
161 this.submitted = true;
162 this.modalData = {
163 message: 'Done'
164 };
165 this.sharedService.cleanForm(this.projectForm);
SANDHYA.JSb1ec4c32022-04-05 08:58:00 +0530166 for (const data of this.quotaItems) {
167 if (this.getFormControl(data.value).value > data.maxValue) {
168 this.notifierService.notify('error', this.translateService.instant('PAGE.PROJECT.QUOTALIMIT'));
169 break;
170 }
171 }
Barath Kumar R16070582021-02-08 18:19:35 +0530172 if (!this.projectForm.invalid) {
173 if (userType === 'Add') {
174 this.createProject();
175 } else if (userType === 'Edit') {
176 this.editProject();
177 }
178 }
179 }
180
181 /** Create project @public */
182 public createProject(): void {
183 this.isLoadingResults = true;
184 const apiURLHeader: APIURLHEADER = {
185 url: environment.PROJECTS_URL
186 };
187 const projectPayload: ProjectDetails = {
188 name: this.projectForm.value.project_name,
189 domain_name: !isNullOrUndefined(this.projectForm.value.domain_name) ? this.projectForm.value.domain_name : undefined
190 };
191 this.addQuotaLimit(projectPayload);
192 this.restService.postResource(apiURLHeader, projectPayload).subscribe((): void => {
193 this.activeModal.close(this.modalData);
194 this.isLoadingResults = false;
195 this.notifierService.notify('success', this.translateService.instant('PAGE.PROJECT.CREATEDSUCCESSFULLY'));
196 }, (error: ERRORDATA): void => {
197 this.restService.handleError(error, 'post');
198 this.isLoadingResults = false;
199 });
200 }
201 /** Handle enable quota limit checkbox event @public */
202 public checkQuota(): void {
203 if (this.getFormControl('enable_quota').value) {
204 this.quotaItems.forEach((quotaItem: QUOTAITEM): void => {
Barath Kumar R2e02e7d2021-06-09 09:18:17 +0530205 this.projectForm.addControl(quotaItem.value, new FormControl(''));
Barath Kumar R16070582021-02-08 18:19:35 +0530206 });
207 } else {
208 this.quotaItems.forEach((quotaItem: QUOTAITEM): void => {
Barath Kumar R2e02e7d2021-06-09 09:18:17 +0530209 this.getFormControl(quotaItem.value).setValue('');
Barath Kumar R16070582021-02-08 18:19:35 +0530210 });
211 }
212 }
213 /** Edit project @public */
214 public editProject(): void {
SANDHYA.JS382448f2023-10-12 14:31:00 +0530215 if (!this.projectForm.dirty) {
216 this.notifierService.notify('warning', this.translateService.instant('PAGE.TOPOLOGY.DATAEMPTY'));
217 return;
218 }
Barath Kumar R16070582021-02-08 18:19:35 +0530219 this.isLoadingResults = true;
220 const apiURLHeader: APIURLHEADER = {
221 url: environment.PROJECTS_URL + '/' + this.projectRef
222 };
223 const projectPayload: ProjectDetails = {
224 name: this.projectForm.value.project_name
225 };
226 this.addQuotaLimit(projectPayload);
227 this.restService.patchResource(apiURLHeader, projectPayload).subscribe((): void => {
228 this.activeModal.close(this.modalData);
229 this.isLoadingResults = false;
230 this.projectService.setHeaderProjects();
231 this.notifierService.notify('success', this.translateService.instant('PAGE.PROJECT.UPDATEDSUCCESSFULLY'));
SANDHYA.JS382448f2023-10-12 14:31:00 +0530232 this.activeModal.close(this.modalData);
Barath Kumar R16070582021-02-08 18:19:35 +0530233 }, (error: ERRORDATA): void => {
234 this.restService.handleError(error, 'patch');
235 this.isLoadingResults = false;
236 });
237 }
238 /** Get domain name list @private */
239 private getDomainList(): void {
240 this.isLoadingResults = true;
241 this.sharedService.getDomainName().subscribe((domainList: TYPESECTION[]): void => {
242 this.domains = domainList;
243 this.isLoadingResults = false;
244 }, (error: ERRORDATA): void => {
245 this.isLoadingResults = false;
246 this.restService.handleError(error, 'get');
247 });
248 }
249
250 /** Used to get the AbstractControl of controlName passed @private */
251 private getFormControl(controlName: string): AbstractControl {
SANDHYA.JS0a34dfa2023-04-25 23:59:41 +0530252 // eslint-disable-next-line security/detect-object-injection
Barath Kumar R16070582021-02-08 18:19:35 +0530253 return this.projectForm.controls[controlName];
254 }
255
256 /** Add quota information to payload @private */
257 private addQuotaLimit(payload: ProjectDetails): void {
258 if (this.getFormControl('enable_quota').value) {
259 payload.quotas = {};
260 this.quotaItems.forEach((quotaItem: QUOTAITEM): void => {
Barath Kumar R2e02e7d2021-06-09 09:18:17 +0530261 if (this.getFormControl(quotaItem.value).value !== '') {
262 payload.quotas[quotaItem.value] = this.getFormControl(quotaItem.value).value;
263 }
Barath Kumar R16070582021-02-08 18:19:35 +0530264 });
Barath Kumar R2e02e7d2021-06-09 09:18:17 +0530265 if (Object.keys(payload.quotas).length === 0) {
266 delete payload.quotas;
267 }
Barath Kumar R16070582021-02-08 18:19:35 +0530268 }
269 }
270
271 /** Set quota information in project form model @private */
272 private patchQuotaInfo(quotaRef?: {}): void {
273 if (quotaRef !== null && this.getProjectType === 'Edit') {
274 this.getFormControl('enable_quota').setValue(true);
275 this.quotaItems.forEach((quotaItem: QUOTAITEM): void => {
276 if (!isNullOrUndefined(quotaRef[quotaItem.value])) {
277 this.projectForm.addControl(quotaItem.value, new FormControl(quotaRef[quotaItem.value],
Barath Kumar R2e02e7d2021-06-09 09:18:17 +0530278 [Validators.max(quotaItem.maxValue)]));
Barath Kumar R16070582021-02-08 18:19:35 +0530279 } else {
Barath Kumar R2e02e7d2021-06-09 09:18:17 +0530280 this.projectForm.addControl(quotaItem.value, new FormControl('', [Validators.max(quotaItem.maxValue)]));
Barath Kumar R16070582021-02-08 18:19:35 +0530281 }
282 });
283 } else {
284 this.quotaItems.forEach((quotaItem: QUOTAITEM): void => {
Barath Kumar R2e02e7d2021-06-09 09:18:17 +0530285 this.projectForm.addControl(quotaItem.value, new FormControl('', [Validators.min(quotaItem.minValue),
286 Validators.max(quotaItem.maxValue)]));
Barath Kumar R16070582021-02-08 18:19:35 +0530287 });
288 }
289 }
kumaran.m3b4814a2020-05-01 19:48:54 +0530290}