blob: 600cecd6dceb61a5b98095a4f201973f04d68c03 [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 Role Component.
20 */
21import { HttpHeaders } from '@angular/common/http';
22import { Component, Injector, Input, OnInit } from '@angular/core';
23import { AbstractControl, FormArray, 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 { APIURLHEADER, ERRORDATA, MODALCLOSERESPONSEDATA } from 'CommonModel';
28import { environment } from 'environment';
29import { ProjectData } from 'ProjectModel';
30import { ProjectService } from 'ProjectService';
31import { RestService } from 'RestService';
32import { RoleData } from 'RolesModel';
33import { ProjectRoleMappings, UserDetail, UserRoleMap } from 'UserModel';
34
35/**
36 * Creating component
37 * @Component takes ProjectRole.html as template url
38 */
39@Component({
Barath Kumar R42fe05d2021-01-29 16:02:34 +053040 templateUrl: './ProjectRoleComponent.html',
41 styleUrls: ['./ProjectRoleComponent.scss']
kumaran.m3b4814a2020-05-01 19:48:54 +053042})
43/** Exporting a class @exports ProjectRoleComponent */
44export class ProjectRoleComponent implements OnInit {
Barath Kumar R42fe05d2021-01-29 16:02:34 +053045 /** To inject services @public */
46 public injector: Injector;
kumaran.m3b4814a2020-05-01 19:48:54 +053047
Barath Kumar R42fe05d2021-01-29 16:02:34 +053048 /** Instance for active modal service @public */
49 public activeModal: NgbActiveModal;
kumaran.m3b4814a2020-05-01 19:48:54 +053050
Barath Kumar R42fe05d2021-01-29 16:02:34 +053051 /** FormGroup user Edit Account added to the form @ html @public */
52 public projectRoleForm: FormGroup;
kumaran.m3b4814a2020-05-01 19:48:54 +053053
Barath Kumar R42fe05d2021-01-29 16:02:34 +053054 /** Form submission Add */
55 public submitted: boolean = false;
kumaran.m3b4814a2020-05-01 19:48:54 +053056
Barath Kumar R42fe05d2021-01-29 16:02:34 +053057 /** Input contains Modal dialog component Instance @private */
58 @Input() public userTitle: string;
kumaran.m3b4814a2020-05-01 19:48:54 +053059
Barath Kumar R42fe05d2021-01-29 16:02:34 +053060 /** Input contains Modal dialog component Instance @private */
61 @Input() public userID: string;
kumaran.m3b4814a2020-05-01 19:48:54 +053062
Barath Kumar R42fe05d2021-01-29 16:02:34 +053063 /** Contains user details information @public */
64 public userDetails: UserDetail;
kumaran.m3b4814a2020-05-01 19:48:54 +053065
Barath Kumar R42fe05d2021-01-29 16:02:34 +053066 /** Project Role Mapping @public */
67 public projectRoleMap: UserRoleMap = {};
kumaran.m3b4814a2020-05-01 19:48:54 +053068
Barath Kumar R42fe05d2021-01-29 16:02:34 +053069 /** Check the loading results @public */
70 public isLoadingResults: boolean = false;
kumaran.m3b4814a2020-05-01 19:48:54 +053071
Barath Kumar R42fe05d2021-01-29 16:02:34 +053072 /** Give the message for the loading @public */
73 public message: string = 'PLEASEWAIT';
kumaran.m3b4814a2020-05-01 19:48:54 +053074
Barath Kumar R42fe05d2021-01-29 16:02:34 +053075 /** Contains project information @public */
76 public projects: ProjectData[] = [];
kumaran.m3b4814a2020-05-01 19:48:54 +053077
Barath Kumar R42fe05d2021-01-29 16:02:34 +053078 /** Contains roles information @public */
79 public roles: RoleData[] = [];
kumaran.m3b4814a2020-05-01 19:48:54 +053080
Barath Kumar R42fe05d2021-01-29 16:02:34 +053081 /** Instance of the rest service @private */
82 private restService: RestService;
kumaran.m3b4814a2020-05-01 19:48:54 +053083
Barath Kumar R42fe05d2021-01-29 16:02:34 +053084 /** FormBuilder instance added to the formBuilder @private */
85 private formBuilder: FormBuilder;
kumaran.m3b4814a2020-05-01 19:48:54 +053086
Barath Kumar R42fe05d2021-01-29 16:02:34 +053087 /** Controls the header form @private */
88 private headers: HttpHeaders;
kumaran.m3b4814a2020-05-01 19:48:54 +053089
Barath Kumar R42fe05d2021-01-29 16:02:34 +053090 /** Notifier service to popup notification @private */
91 private notifierService: NotifierService;
kumaran.m3b4814a2020-05-01 19:48:54 +053092
Barath Kumar R42fe05d2021-01-29 16:02:34 +053093 /** Contains tranlsate instance @private */
94 private translateService: TranslateService;
kumaran.m3b4814a2020-05-01 19:48:54 +053095
Barath Kumar R42fe05d2021-01-29 16:02:34 +053096 /** Project Role Form array @private */
97 private projectRoleFormArray: FormArray;
kumaran.m3b4814a2020-05-01 19:48:54 +053098
Barath Kumar R42fe05d2021-01-29 16:02:34 +053099 /** Holds all project details @private */
100 private projectService: ProjectService;
kumaran.m3b4814a2020-05-01 19:48:54 +0530101
Barath Kumar R42fe05d2021-01-29 16:02:34 +0530102 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.m3b4814a2020-05-01 19:48:54 +0530110 }
kumaran.m3b4814a2020-05-01 19:48:54 +0530111
Barath Kumar R42fe05d2021-01-29 16:02:34 +0530112 /** 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.m3b4814a2020-05-01 19:48:54 +0530118 }
kumaran.m3b4814a2020-05-01 19:48:54 +0530119
Barath Kumar R42fe05d2021-01-29 16:02:34 +0530120 /** 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[] {
144 // tslint:disable-next-line:no-backbone-get-set-outside-model
145 return (this.projectRoleForm.get('project_role_mappings') as FormArray).controls;
146 }
147
148 /** Fetching the data from server to Load in the smarttable @public */
149 public generateData(): void {
150 if (this.userID !== '') {
151 this.isLoadingResults = true;
152 this.restService.getResource(environment.USERS_URL + '/' + this.userID).subscribe((userDetails: UserDetail): void => {
153 this.userDetails = userDetails;
154 this.loadMapping();
155 this.isLoadingResults = false;
156 }, (error: ERRORDATA): void => {
157 this.isLoadingResults = false;
158 this.restService.handleError(error, 'get');
159 });
160 }
161 }
162 /** Fetching the projects information @public */
163 public getProjects(): void {
164 this.isLoadingResults = true;
165 this.restService.getResource(environment.PROJECTS_URL).subscribe((projectsData: ProjectData[]): void => {
166 this.projects = projectsData;
167 this.getRoles();
168 }, (error: ERRORDATA): void => {
169 this.isLoadingResults = false;
170 this.restService.handleError(error, 'get');
171 });
172 }
173
174 /** Fetching the Roles information @public */
175 public getRoles(): void {
176 this.restService.getResource(environment.ROLES_URL).subscribe((rolesData: RoleData[]): void => {
177 this.roles = rolesData;
178 this.isLoadingResults = false;
179 }, (error: ERRORDATA): void => {
180 this.isLoadingResults = false;
181 this.restService.handleError(error, 'get');
182 });
183 }
184
185 /** Set all roles and project values to the form @public */
186 public loadMapping(): void {
187 this.userDetails.project_role_mappings.forEach((data: ProjectRoleMappings): void => {
188 // tslint:disable-next-line:no-backbone-get-set-outside-model
189 this.projectRoleFormArray = this.projectRoleForm.get('project_role_mappings') as FormArray;
190 this.projectRoleFormArray.push(this.projectRoleParamsBuilder);
191 });
192 this.projectRoleForm.patchValue(this.userDetails);
193 }
194
195 /** Remove project and roles from the list @public */
196 public removeMapping(index: number): void {
197 this.projectRoleFormArray.removeAt(index);
198 }
199
200 /** Submit project and roles @public */
201 public addProjectRole(): void {
202 this.submitted = true;
203 const modalData: MODALCLOSERESPONSEDATA = {
204 message: 'Done'
205 };
206 if (this.projectRoleForm.invalid) { return; }
207 const apiURLHeader: APIURLHEADER = {
208 url: environment.USERS_URL + '/' + this.userID
209 };
210 this.projectRoleMap.project_role_mappings = [];
211 this.projectRoleForm.value.project_role_mappings.forEach((res: ProjectRoleMappings): void => {
212 this.projectRoleMap.project_role_mappings.push({ project: res.project_name, role: res.role_name });
213 });
214 if (this.projectRoleMap.project_role_mappings.length !== 0) {
215 this.isLoadingResults = true;
216 this.restService.patchResource(apiURLHeader, this.projectRoleMap).subscribe((result: {}): void => {
217 this.isLoadingResults = false;
218 this.activeModal.close(modalData);
219 this.projectService.setHeaderProjects();
220 this.notifierService.notify('success', this.translateService.instant('PAGE.USERS.EDITEDSUCCESSFULLY'));
221 }, (error: ERRORDATA): void => {
222 this.isLoadingResults = false;
223 this.restService.handleError(error, 'patch');
224 });
225 } else {
226 this.notifierService.notify('error', this.translateService.instant('PAGE.USERS.EDITPROJECTROLEERROR'));
227 }
228 }
229
230 /** Add extra mapping and set empty project and roles @public */
231 public addMapping(): void {
232 // tslint:disable-next-line:no-backbone-get-set-outside-model
233 this.projectRoleFormArray = this.projectRoleForm.get('project_role_mappings') as FormArray;
234 this.projectRoleFormArray.push(this.projectRoleParamsBuilder);
235 }
236
237 /** Remove project and roles for the user @public */
238 public deleteProjectAndRoleMapping(getProjectRoles: ProjectRoleMappings): void {
239 const modalData: MODALCLOSERESPONSEDATA = {
240 message: 'Done'
241 };
242 const removeProjectRole: UserRoleMap = { remove_project_role_mappings: [] };
243 removeProjectRole.remove_project_role_mappings = [{ project: getProjectRoles.project_name, role: getProjectRoles.role_name }];
244 const apiURLHeader: APIURLHEADER = {
245 url: environment.USERS_URL + '/' + this.userID
246 };
247 this.isLoadingResults = true;
248 this.restService.patchResource(apiURLHeader, removeProjectRole).subscribe((result: {}): void => {
249 this.isLoadingResults = false;
250 this.activeModal.close(modalData);
251 this.projectService.setHeaderProjects();
252 this.notifierService.notify('success', this.translateService.instant('PAGE.USERS.EDITEDSUCCESSFULLY'));
253 }, (error: ERRORDATA): void => {
254 this.isLoadingResults = false;
255 this.restService.handleError(error, 'patch');
256 });
257 }
kumaran.m3b4814a2020-05-01 19:48:54 +0530258}