added svcapi ui and camunda code
[it/otf.git] / otf-frontend / client / src / app / shared / services / group.service.ts
1 /*  Copyright (c) 2019 AT&T Intellectual Property.                             #\r
2 #                                                                              #\r
3 #   Licensed under the Apache License, Version 2.0 (the "License");            #\r
4 #   you may not use this file except in compliance with the License.           #\r
5 #   You may obtain a copy of the License at                                    #\r
6 #                                                                              #\r
7 #       http://www.apache.org/licenses/LICENSE-2.0                             #\r
8 #                                                                              #\r
9 #   Unless required by applicable law or agreed to in writing, software        #\r
10 #   distributed under the License is distributed on an "AS IS" BASIS,          #\r
11 #   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.   #\r
12 #   See the License for the specific language governing permissions and        #\r
13 #   limitations under the License.                                             #\r
14 ##############################################################################*/\r
15 \r
16 \r
17 import { Injectable } from '@angular/core';\r
18 import { HttpClient } from '@angular/common/http';\r
19 import { ParamsService } from './params.service';\r
20 import { ModelService } from './model.service';\r
21 import { CookieService } from 'ngx-cookie-service';\r
22 import { FeathersService } from './feathers.service';\r
23 import { Observable, Subject } from 'rxjs';\r
24 import * as organizeGroups from '../../../../../server/src/feathers/hooks/permissions/get-permissions';\r
25 import { UserService } from './user.service';\r
26 \r
27 @Injectable({\r
28   providedIn: 'root'\r
29 })\r
30 export class GroupService extends ModelService {\r
31 \r
32   protected groupList;\r
33   protected selectedGroup;\r
34 \r
35   protected groupListChange: Subject<Array<any>> = new Subject<Array<any>>();\r
36   protected selectedGroupChange: Subject<Object> = new Subject<Object>();\r
37 \r
38   constructor(http: HttpClient, Params: ParamsService, cookie: CookieService, feathers: FeathersService, private _users: UserService) {\r
39     super('groups', http, Params, cookie, feathers);\r
40     this.setUp();\r
41   }\r
42   //new edit:\r
43   public currentUser;\r
44 \r
45   setUp() {\r
46     let currentId = window.localStorage.getItem('currentGroupId');\r
47 \r
48     this.currentUser = JSON.parse(this.cookie.get('currentUser'));\r
49 \r
50     this.find({ $limit: -1, lookup: 'both' }).subscribe(res => {\r
51       this.setGroupList(res);\r
52 \r
53       if (currentId) {\r
54         this.setGroup(this.groupList.filter(elem => elem._id == currentId)[0]);\r
55       } else if (this.currentUser.defaultGroup) {\r
56         this.setGroup(this.groupList.filter(elem => elem._id == this.currentUser.defaultGroup)[0]);\r
57       }else {\r
58         //set to first group\r
59       }\r
60     },\r
61       err => {\r
62         console.log(err);\r
63       })\r
64   }\r
65 \r
66 \r
67 \r
68   getGroup() {\r
69     return this.selectedGroup;\r
70   }\r
71 \r
72   getGroupList() {\r
73     return this.groupList;\r
74   }\r
75 \r
76   setGroup(group: any) {\r
77     this.selectedGroup = group;\r
78     window.localStorage.setItem('currentGroupId', group._id);\r
79     this.selectedGroupChange.next(this.selectedGroup);\r
80     if (!this.currentUser.defaultGroupEnabled) {\r
81       let userPatch = {\r
82         _id: this.currentUser._id,\r
83         defaultGroup: group._id,\r
84         defaultGroupEnabled: false\r
85       };\r
86 \r
87       this._users.patch(userPatch).subscribe((res) => {\r
88         \r
89       });\r
90     }\r
91   }\r
92 \r
93   organizeGroups(groups){\r
94     return organizeGroups(this.currentUser, groups);\r
95   }\r
96 \r
97   setGroupList(groups) {\r
98     this.groupList = organizeGroups(this.currentUser, groups);\r
99     this.groupListChange.next(this.groupList);\r
100   }\r
101 \r
102   listChange(): Subject<Array<any>> {\r
103     return this.groupListChange;\r
104   }\r
105 \r
106   groupChange(): Subject<Object> {\r
107     return this.selectedGroupChange;\r
108   }\r
109 \r
110   format(arr: Array<any>) {\r
111 \r
112     //puts all groups in a single level array\r
113     // arr = organizeGroups(this.currentUser, arr);\r
114 \r
115     var tree = [],\r
116       mappedArr = {},\r
117       arrElem,\r
118       mappedElem;\r
119 \r
120     // First map the nodes of the array to an object -> create a hash table.\r
121     for (var i = 0, len = arr.length; i < len; i++) {\r
122       arrElem = arr[i];\r
123       mappedArr[arrElem._id] = arrElem;\r
124       mappedArr[arrElem._id]['children'] = [];\r
125     }\r
126 \r
127 \r
128     for (var _id in mappedArr) {\r
129       if (mappedArr.hasOwnProperty(_id)) {\r
130         mappedElem = mappedArr[_id];\r
131         // If the element is not at the root level, add it to its parent array of children.\r
132         if (mappedElem.parentGroupId) {\r
133           mappedArr[mappedElem['parentGroupId']]['children'].push(mappedElem);\r
134         }\r
135         // If the element is at the root level, add it to first level elements array.\r
136         else {\r
137           tree.push(mappedElem);\r
138         }\r
139       }\r
140     }\r
141     return tree;\r
142   }\r
143 \r
144 \r
145 }\r