| 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 Service |
| 20 | */ |
| 21 | import { Injectable, Injector } from '@angular/core'; |
| 22 | import { NgbModal } from '@ng-bootstrap/ng-bootstrap'; |
| 23 | import { AuthenticationService } from 'AuthenticationService'; |
| 24 | import { environment } from 'environment'; |
| 25 | import { ProjectData } from 'ProjectModel'; |
| 26 | import { BehaviorSubject, Observable, Subscription } from 'rxjs'; |
| 27 | import { SwitchProjectComponent } from 'SwitchProjectComponent'; |
| 28 | import { ProjectRoleMappings, UserDetail } from 'UserModel'; |
| 29 | import { ProjectModel } from 'VNFDModel'; |
| 30 | import { RestService } from './RestService'; |
| 31 | |
| 32 | /** |
| 33 | * An Injectable is a class adorned with the @Injectable decorator function. |
| 34 | * @Injectable takes a metadata object that tells Angular how to compile and run module code |
| 35 | */ |
| 36 | @Injectable({ |
| 37 | providedIn: 'root' |
| 38 | }) |
| 39 | /** Exporting a class @exports ProjectService */ |
| 40 | export class ProjectService { |
| 41 | /** Get method for project list */ |
| 42 | get projectList(): Observable<{}[]> { |
| 43 | return this.projectList$.asObservable(); |
| 44 | } |
| 45 | /** To inject services @public */ |
| 46 | public injector: Injector; |
| 47 | |
| 48 | /** Holds all the projects details */ |
| 49 | public allProjectList: string[]; |
| 50 | |
| 51 | /** Observable holds logined value @public */ |
| 52 | public username$: Observable<string>; |
| 53 | |
| 54 | /** Hold Rest Service Objects */ |
| 55 | private restService: RestService; |
| 56 | |
| 57 | /** Instance of the modal service @private */ |
| 58 | private modalService: NgbModal; |
| 59 | |
| 60 | /** Utilizes auth service for any auth operations @private */ |
| 61 | private authService: AuthenticationService; |
| 62 | |
| 63 | /** Holds the username in condition of type BehaviorSubject<string> @private */ |
| 64 | private projectList$: BehaviorSubject<{}[]> = new BehaviorSubject<{}[]>([]); |
| 65 | |
| 66 | constructor(injector: Injector) { |
| 67 | this.injector = injector; |
| 68 | this.restService = this.injector.get(RestService); |
| 69 | this.modalService = this.injector.get(NgbModal); |
| 70 | this.authService = this.injector.get(AuthenticationService); |
| 71 | } |
| 72 | |
| 73 | /** List all the projects @public */ |
| 74 | public getAllProjects(): Observable<{}> { |
| 75 | return this.restService.getResource(environment.PROJECTS_URL); |
| 76 | } |
| 77 | |
| 78 | /** Get current project details from local storage @public */ |
| 79 | public getCurrentProjectDetails(): Observable<{}> { |
| 80 | const project: string = localStorage.getItem('project_id'); |
| 81 | return this.restService.getResource(environment.PROJECTS_URL + '/' + project); |
| 82 | } |
| 83 | |
| 84 | /** Returns all the projects for a particular users @public */ |
| 85 | public getUserProjects(): Observable<{}> { |
| 86 | const username: string = localStorage.getItem('username'); |
| 87 | return this.restService.getResource(environment.USERS_URL + '/' + username); |
| 88 | } |
| 89 | |
| 90 | /** Set header projects @public */ |
| 91 | public setHeaderProjects(): void { |
| 92 | this.getUserProjects().subscribe((projects: UserDetail) => { |
| 93 | const projectList: {}[] = projects.project_role_mappings; |
| 94 | projectList.filter((list: ProjectModel) => { |
| 95 | if (list.project === localStorage.getItem('project_id')) { |
| 96 | localStorage.setItem('project', list.project_name); |
| 97 | this.authService.projectName$.next(list.project_name); |
| 98 | } |
| 99 | }); |
| 100 | const projectDistinctList: {}[] = projectList.filter( |
| 101 | (thing: ProjectRoleMappings, i: number, arr: []) => arr |
| 102 | .findIndex((t: ProjectRoleMappings) => t.project_name === thing.project_name) === i |
| 103 | ); |
| 104 | this.projectList$.next(projectDistinctList); |
| 105 | }); |
| 106 | } |
| 107 | |
| 108 | /** Toggle projects on selection @public */ |
| 109 | public switchProjectModal(list: ProjectData): void { |
| 110 | const username: string = localStorage.getItem('username'); |
| 111 | this.modalService.open(SwitchProjectComponent, { backdrop: 'static' }) |
| 112 | .componentInstance.params = { projectID: list.project, username }; |
| 113 | } |
| 114 | } |