Catalog alignment
[sdc.git] / catalog-ui / src / app / view-models / admin-dashboard / user-management / user-management-view-model.ts
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 'use strict';
22 import * as _ from "lodash";
23 import { User, IUserProperties, IUser, IAppConfigurtaion } from "app/models";
24 import { UserService } from "../../../ng2/services/user.service";
25 import { SdcUiCommon, SdcUiServices, SdcUiComponents } from "onap-ui-angular";
26 import { AuthenticationService } from "app/ng2/services/authentication.service";
27
28 interface IUserManagementViewModelScope extends ng.IScope {
29     usersList:Array<IUserProperties>;
30     isLoading:boolean;
31     sortBy:string;
32     reverse:boolean;
33     tableHeadersList:any;
34     getAllUsers():void;
35     sort(sortBy:string):void;
36     getTitle(role:string):string;
37 }
38
39
40 export class UserManagementViewModel {
41     static '$inject' = [
42         '$scope',
43         'UserServiceNg2',
44         'AuthenticationServiceNg2',
45         '$filter',
46         'ModalServiceSdcUI'
47     ];
48
49     constructor(private $scope:IUserManagementViewModelScope,
50                 private userService:UserService,
51                 private authService:AuthenticationService,
52                 private $filter:ng.IFilterService,
53                 private modalService:SdcUiServices.ModalService) {
54
55
56         setTimeout(this.initScope, 1000);
57     }
58
59
60     private getAllUsers = ():void => {
61         this.$scope.isLoading = true;
62
63         let onError = (response) => {
64             this.$scope.isLoading = false;
65             console.info('onFaild', response);
66         };
67         let onSuccess = (response:Array<IUserProperties>) => {
68             this.$scope.usersList = response;
69             _.forEach(this.$scope.usersList, (user:any, i:number)=> {
70                 user.index = i;
71             });
72             this.$scope.isLoading = false;
73         };
74         this.userService.getAllUsers().subscribe(onSuccess, onError);
75     };
76
77     private initScope = ():void => {
78         let self = this;
79         this.$scope.tableHeadersList = [{title: "First Name", property: 'firstName'}, {
80             title: "Last Name",
81             property: 'lastName'
82         },
83             {
84                 title: this.$filter('translate')("USER_MANAGEMENT_TABLE_HEADER_USER_ID"),
85                 property: 'userId'
86             }, {title: "Email", property: 'email'}, {title: "Role", property: 'role'}, {
87                 title: "Last Active",
88                 property: 'lastLoginTime'
89             }];
90         this.$scope.sortBy = 'lastLoginTime';
91         this.$scope.reverse = false;
92         this.getAllUsers();
93
94         this.$scope.sort = (sortBy:string):void => {//default sort by descending last update. default for alphabetical = ascending
95             this.$scope.reverse = (this.$scope.sortBy === sortBy) ? ( !this.$scope.reverse) : this.$scope.reverse = false;
96             this.$scope.sortBy = sortBy;
97         };
98
99         this.$scope.getTitle = (role:string):string => {
100             return role.toLowerCase().replace('governor', 'governance_Rep').replace('_', ' ');
101         };
102
103     }
104 }