blob: d9885489f434ec56716db3d7747776e112af29f5 [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 Add Edit Component.
20 */
21import { HttpHeaders } from '@angular/common/http';
22import { Component, Injector, Input, OnInit } from '@angular/core';
23import { AbstractControl, FormBuilder, FormGroup, Validators } from '@angular/forms';
24import { NgbActiveModal } from '@ng-bootstrap/ng-bootstrap';
25import { TranslateService } from '@ngx-translate/core';
26import { NotifierService } from 'angular-notifier';
27import { AuthenticationService } from 'AuthenticationService';
Barath Kumar R76e55f42021-02-08 18:19:35 +053028import { APIURLHEADER, ERRORDATA, LOGINPARAMS, MODALCLOSERESPONSEDATA, TYPESECTION } from 'CommonModel';
kumaran.m3b4814a2020-05-01 19:48:54 +053029import { environment } from 'environment';
30import { RestService } from 'RestService';
31import { SharedService } from 'SharedService';
32import { isNullOrUndefined } from 'util';
33
34/**
35 * Creating component
36 * @Component takes AddEditUserComponent.html as template url
37 */
38@Component({
Barath Kumar R76e55f42021-02-08 18:19:35 +053039 templateUrl: './AddEditUserComponent.html',
40 styleUrls: ['./AddEditUserComponent.scss']
kumaran.m3b4814a2020-05-01 19:48:54 +053041})
42/** Exporting a class @exports AddEditUserComponent */
43export class AddEditUserComponent implements OnInit {
Barath Kumar R76e55f42021-02-08 18:19:35 +053044 /** To inject services @public */
45 public injector: Injector;
kumaran.m3b4814a2020-05-01 19:48:54 +053046
Barath Kumar R76e55f42021-02-08 18:19:35 +053047 /** Instance for active modal service @public */
48 public activeModal: NgbActiveModal;
kumaran.m3b4814a2020-05-01 19:48:54 +053049
Barath Kumar R76e55f42021-02-08 18:19:35 +053050 /** FormGroup user Edit Account added to the form @ html @public */
51 public userForm: FormGroup;
kumaran.m3b4814a2020-05-01 19:48:54 +053052
Barath Kumar R76e55f42021-02-08 18:19:35 +053053 /** Form submission Add */
54 public submitted: boolean = false;
kumaran.m3b4814a2020-05-01 19:48:54 +053055
Barath Kumar R76e55f42021-02-08 18:19:35 +053056 /** Input contains Modal dialog component Instance @public */
57 @Input() public userTitle: string;
kumaran.m3b4814a2020-05-01 19:48:54 +053058
Barath Kumar R76e55f42021-02-08 18:19:35 +053059 /** Input contains Modal dialog component Instance @public */
60 @Input() public userType: string;
kumaran.m3b4814a2020-05-01 19:48:54 +053061
Barath Kumar R76e55f42021-02-08 18:19:35 +053062 /** Input contains Modal dialog component Instance @public */
63 @Input() public userID: string;
kumaran.m3b4814a2020-05-01 19:48:54 +053064
Barath Kumar R76e55f42021-02-08 18:19:35 +053065 /** Input contains Modal dialog component Instance @public */
66 @Input() public userName: string;
kumaran.m3b4814a2020-05-01 19:48:54 +053067
Barath Kumar R76e55f42021-02-08 18:19:35 +053068 /** Check the loading results for loader status @public */
69 public isLoadingResults: boolean = false;
kumaran.m3b4814a2020-05-01 19:48:54 +053070
Barath Kumar R76e55f42021-02-08 18:19:35 +053071 /** Give the message for the loading @public */
72 public message: string = 'PLEASEWAIT';
kumaran.m3b4814a2020-05-01 19:48:54 +053073
Barath Kumar R76e55f42021-02-08 18:19:35 +053074 /** Holds list of domains @public */
75 public domains: TYPESECTION[] = [];
kumaran.m3b4814a2020-05-01 19:48:54 +053076
Barath Kumar R76e55f42021-02-08 18:19:35 +053077 /** Instance of the rest service @private */
78 private restService: RestService;
kumaran.m3b4814a2020-05-01 19:48:54 +053079
Barath Kumar R76e55f42021-02-08 18:19:35 +053080 /** FormBuilder instance added to the formBuilder @private */
81 private formBuilder: FormBuilder;
kumaran.m3b4814a2020-05-01 19:48:54 +053082
Barath Kumar R76e55f42021-02-08 18:19:35 +053083 /** Controls the header form @private */
84 private headers: HttpHeaders;
kumaran.m3b4814a2020-05-01 19:48:54 +053085
Barath Kumar R76e55f42021-02-08 18:19:35 +053086 /** Notifier service to popup notification @private */
87 private notifierService: NotifierService;
kumaran.m3b4814a2020-05-01 19:48:54 +053088
Barath Kumar R76e55f42021-02-08 18:19:35 +053089 /** Contains tranlsate instance @private */
90 private translateService: TranslateService;
kumaran.m3b4814a2020-05-01 19:48:54 +053091
Barath Kumar R76e55f42021-02-08 18:19:35 +053092 /** Contains all methods related to shared @private */
93 private sharedService: SharedService;
kumaran.m3b4814a2020-05-01 19:48:54 +053094
Barath Kumar R76e55f42021-02-08 18:19:35 +053095 /** ModalData instance of modal @private */
96 private modalData: MODALCLOSERESPONSEDATA;
kumaran.m3b4814a2020-05-01 19:48:54 +053097
Barath Kumar R76e55f42021-02-08 18:19:35 +053098 /** Utilizes auth service for any auth operations @private */
99 private authService: AuthenticationService;
kumaran.m3b4814a2020-05-01 19:48:54 +0530100
Barath Kumar R76e55f42021-02-08 18:19:35 +0530101 constructor(injector: Injector) {
102 this.injector = injector;
103 this.formBuilder = this.injector.get(FormBuilder);
104 this.restService = this.injector.get(RestService);
105 this.activeModal = this.injector.get(NgbActiveModal);
106 this.notifierService = this.injector.get(NotifierService);
107 this.translateService = this.injector.get(TranslateService);
108 this.sharedService = this.injector.get(SharedService);
109 this.authService = this.injector.get(AuthenticationService);
kumaran.m3b4814a2020-05-01 19:48:54 +0530110
Barath Kumar R76e55f42021-02-08 18:19:35 +0530111 /** Initializing Form Action */
112 this.userForm = this.formBuilder.group({
113 userName: ['', Validators.required],
114 password: [null, [Validators.required, Validators.pattern(this.sharedService.REGX_PASSWORD_PATTERN)]],
115 password2: [null, Validators.required],
116 domain_name: [null]
117 });
kumaran.m3b4814a2020-05-01 19:48:54 +0530118 }
kumaran.m3b4814a2020-05-01 19:48:54 +0530119
Barath Kumar R76e55f42021-02-08 18:19:35 +0530120 /** convenience getter for easy access to form fields */
121 get f(): FormGroup['controls'] { return this.userForm.controls; }
kumaran.m3b4814a2020-05-01 19:48:54 +0530122
Barath Kumar R76e55f42021-02-08 18:19:35 +0530123 /** 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 if (this.userType === 'add') {
131 this.getDomainList();
132 } else if (this.userType === 'editUserName') {
133 this.userForm.patchValue({ userName: this.userName });
kumaran.m3b4814a2020-05-01 19:48:54 +0530134 }
kumaran.m3b4814a2020-05-01 19:48:54 +0530135 }
kumaran.m3b4814a2020-05-01 19:48:54 +0530136
Barath Kumar R76e55f42021-02-08 18:19:35 +0530137 /** On modal submit users acction will called @public */
138 public userAction(userType: string): void {
139 if (userType === 'editPassword') {
140 this.getFormControl('userName').setValidators([]);
141 this.getFormControl('userName').updateValueAndValidity();
142 } else if (userType === 'editUserName') {
143 this.getFormControl('password').setValidators([]);
144 this.getFormControl('password').updateValueAndValidity();
145 this.getFormControl('password2').setValidators([]);
146 this.getFormControl('password2').updateValueAndValidity();
147 }
148 this.submitted = true;
149 this.modalData = {
150 message: 'Done'
151 };
152 this.sharedService.cleanForm(this.userForm);
153 if (!this.userForm.invalid) {
154 if (this.userForm.value.password !== this.userForm.value.password2) {
155 this.notifierService.notify('error', this.translateService.instant('PAGE.USERS.PASSWORDCONFLICT'));
156 return;
157 }
158 if (userType === 'add') {
159 this.addUser();
160 } else if (userType === 'editUserName' || userType === 'editPassword') {
161 this.editUser();
162 }
163 }
kumaran.m3b4814a2020-05-01 19:48:54 +0530164 }
Barath Kumar R76e55f42021-02-08 18:19:35 +0530165
166 /** Add user @public */
167 public addUser(): void {
168 this.isLoadingResults = true;
169 const payLoad: {} = JSON.stringify({
170 username: (this.userForm.value.userName).toLowerCase(),
171 password: (this.userForm.value.password),
172 domain_name: !isNullOrUndefined(this.userForm.value.domain_name) ? this.userForm.value.domain_name : undefined
173 });
174 const apiURLHeader: APIURLHEADER = {
175 url: environment.USERS_URL,
176 httpOptions: { headers: this.headers }
177 };
178 this.restService.postResource(apiURLHeader, payLoad).subscribe((result: {}): void => {
179 this.activeModal.close(this.modalData);
180 this.isLoadingResults = false;
181 this.notifierService.notify('success', this.translateService.instant('PAGE.USERS.CREATEDSUCCESSFULLY'));
182 }, (error: ERRORDATA): void => {
183 this.restService.handleError(error, 'post');
184 this.isLoadingResults = false;
185 });
186 }
187
188 /** Edit user @public */
189 public editUser(): void {
190 this.isLoadingResults = true;
191 const payLoad: LOGINPARAMS = {};
192 if (this.userType === 'editPassword') {
193 payLoad.password = (this.userForm.value.password);
194 } else {
195 payLoad.username = this.userForm.value.userName.toLowerCase();
196 }
197 const apiURLHeader: APIURLHEADER = {
198 url: environment.USERS_URL + '/' + this.userID,
199 httpOptions: { headers: this.headers }
200 };
201 this.restService.patchResource(apiURLHeader, payLoad).subscribe((result: {}): void => {
202 this.checkUsername(payLoad);
203 this.activeModal.close(this.modalData);
204 this.isLoadingResults = false;
205 this.notifierService.notify('success', this.translateService.instant('PAGE.USERS.EDITEDSUCCESSFULLY'));
206 }, (error: ERRORDATA): void => {
207 this.restService.handleError(error, 'put');
208 this.isLoadingResults = false;
209 });
210 }
211 /** Get domain name list @private */
212 private getDomainList(): void {
213 this.isLoadingResults = true;
214 this.sharedService.getDomainName().subscribe((domainList: TYPESECTION[]): void => {
215 this.domains = domainList;
216 this.isLoadingResults = false;
217 }, (error: ERRORDATA): void => {
218 this.isLoadingResults = false;
219 this.restService.handleError(error, 'get');
220 });
221 }
222
223 /** Used to get the AbstractControl of controlName passed @private */
224 private getFormControl(controlName: string): AbstractControl {
225 return this.userForm.controls[controlName];
226 }
227
228 /** Method to check loggedin username and update @private */
229 private checkUsername(payLoad: LOGINPARAMS): void {
230 const logUsername: string = localStorage.getItem('username');
231 if (this.userType === 'editUserName' && logUsername === this.userName) {
232 this.authService.userName.next(payLoad.username);
233 localStorage.setItem('username', payLoad.username);
234 }
235 }
kumaran.m3b4814a2020-05-01 19:48:54 +0530236}