CSIT Fix for SDC-2585
[sdc.git] / catalog-ui / src / app / view-models / workspace / tabs / activity-log / activity-log.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 {IWorkspaceViewModelScope} from "app/view-models/workspace/workspace-view-model";
24 import {Activity} from "app/models";
25 import {ActivityLogService} from "app/services";
26
27 export interface IActivityLogViewModelScope extends IWorkspaceViewModelScope {
28     activityDateArray:Array<any>; //this is in order to sort the dates
29     activityLog:Array<Activity>;
30     preVersion:string;
31
32     tableHeadersList:Array<any>;
33     reverse:boolean;
34     sortBy:string;
35     searchBind:string;
36
37     getActivityLog(uniqueId:string):void;
38     onVersionChanged(version:any):void;
39     parseAction(action:string):string;
40     sort(sortBy:string):void;
41 }
42
43 export class ActivityLogViewModel {
44
45     static '$inject' = [
46         '$scope',
47         '$state',
48         'Sdc.Services.ActivityLogService'
49     ];
50
51     constructor(private $scope:IActivityLogViewModelScope,
52                 private $state:ng.ui.IStateService,
53                 private activityLogService:ActivityLogService) {
54
55         this.initScope();
56         this.$scope.setValidState(true);
57         this.initSortedTableScope();
58
59         // Set default sorting
60         this.$scope.sortBy = 'logDate';
61     }
62
63     private initScope():void {
64
65         this.$scope.preVersion = this.$scope.component.version;
66
67         this.$scope.onVersionChanged = (version:any):void => {
68             if (version.versionNumber != this.$scope.component.version) {
69                 this.$scope.isLoading = true;
70                 this.$scope.getActivityLog(version.versionId);
71             }
72         };
73
74         this.$scope.getActivityLog = (uniqueId:any):void => {
75
76             let onError = (response) => {
77                 this.$scope.isLoading = false;
78                 console.info('onFaild', response);
79
80             };
81
82             let onSuccess = (response:Array<Activity>) => {
83                 this.$scope.activityLog = _.sortBy(response, function (o) {
84                     return o.TIMESTAMP;
85                 }); //response; //
86                 this.$scope.isLoading = false;
87             };
88
89             this.$scope.isLoading = true;
90             if (this.$scope.component.isResource()) {
91                 this.activityLogService.getActivityLogService('resources', uniqueId).then(onSuccess, onError);
92             }
93             if (this.$scope.component.isService()) {
94                 this.activityLogService.getActivityLogService('services', uniqueId).then(onSuccess, onError);
95             }
96
97         };
98
99         if (!this.$scope.activityLog || this.$scope.preVersion != this.$scope.component.version) {
100             this.$scope.getActivityLog(this.$scope.component.uniqueId);
101         }
102
103         this.$scope.parseAction = (action:string) => {
104             return action ? action.split(/(?=[A-Z])/).join(' ') : '';
105         };
106
107     }
108
109     private initSortedTableScope = ():void => {
110         this.$scope.tableHeadersList = [
111             {title: 'Date', property: 'dateFormat'},
112             {title: 'Action', property: 'ACTION'},
113             {title: 'Comment', property: 'COMMENT'},
114             {title: 'Username', property: 'MODIFIER'},
115             {title: 'Status', property: 'STATUS'}
116         ];
117
118         this.$scope.sort = (sortBy:string):void => {
119             this.$scope.reverse = (this.$scope.sortBy === sortBy) ? !this.$scope.reverse : false;
120             this.$scope.sortBy = sortBy;
121         };
122     };
123 }