added svcapi ui and camunda code
[it/otf.git] / otf-frontend / client / src / app / shared / services / group.service.ts
diff --git a/otf-frontend/client/src/app/shared/services/group.service.ts b/otf-frontend/client/src/app/shared/services/group.service.ts
new file mode 100644 (file)
index 0000000..477ae92
--- /dev/null
@@ -0,0 +1,145 @@
+/*  Copyright (c) 2019 AT&T Intellectual Property.                             #\r
+#                                                                              #\r
+#   Licensed under the Apache License, Version 2.0 (the "License");            #\r
+#   you may not use this file except in compliance with the License.           #\r
+#   You may obtain a copy of the License at                                    #\r
+#                                                                              #\r
+#       http://www.apache.org/licenses/LICENSE-2.0                             #\r
+#                                                                              #\r
+#   Unless required by applicable law or agreed to in writing, software        #\r
+#   distributed under the License is distributed on an "AS IS" BASIS,          #\r
+#   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.   #\r
+#   See the License for the specific language governing permissions and        #\r
+#   limitations under the License.                                             #\r
+##############################################################################*/\r
+\r
+\r
+import { Injectable } from '@angular/core';\r
+import { HttpClient } from '@angular/common/http';\r
+import { ParamsService } from './params.service';\r
+import { ModelService } from './model.service';\r
+import { CookieService } from 'ngx-cookie-service';\r
+import { FeathersService } from './feathers.service';\r
+import { Observable, Subject } from 'rxjs';\r
+import * as organizeGroups from '../../../../../server/src/feathers/hooks/permissions/get-permissions';\r
+import { UserService } from './user.service';\r
+\r
+@Injectable({\r
+  providedIn: 'root'\r
+})\r
+export class GroupService extends ModelService {\r
+\r
+  protected groupList;\r
+  protected selectedGroup;\r
+\r
+  protected groupListChange: Subject<Array<any>> = new Subject<Array<any>>();\r
+  protected selectedGroupChange: Subject<Object> = new Subject<Object>();\r
+\r
+  constructor(http: HttpClient, Params: ParamsService, cookie: CookieService, feathers: FeathersService, private _users: UserService) {\r
+    super('groups', http, Params, cookie, feathers);\r
+    this.setUp();\r
+  }\r
+  //new edit:\r
+  public currentUser;\r
+\r
+  setUp() {\r
+    let currentId = window.localStorage.getItem('currentGroupId');\r
+\r
+    this.currentUser = JSON.parse(this.cookie.get('currentUser'));\r
+\r
+    this.find({ $limit: -1, lookup: 'both' }).subscribe(res => {\r
+      this.setGroupList(res);\r
+\r
+      if (currentId) {\r
+        this.setGroup(this.groupList.filter(elem => elem._id == currentId)[0]);\r
+      } else if (this.currentUser.defaultGroup) {\r
+        this.setGroup(this.groupList.filter(elem => elem._id == this.currentUser.defaultGroup)[0]);\r
+      }else {\r
+        //set to first group\r
+      }\r
+    },\r
+      err => {\r
+        console.log(err);\r
+      })\r
+  }\r
+\r
+\r
+\r
+  getGroup() {\r
+    return this.selectedGroup;\r
+  }\r
+\r
+  getGroupList() {\r
+    return this.groupList;\r
+  }\r
+\r
+  setGroup(group: any) {\r
+    this.selectedGroup = group;\r
+    window.localStorage.setItem('currentGroupId', group._id);\r
+    this.selectedGroupChange.next(this.selectedGroup);\r
+    if (!this.currentUser.defaultGroupEnabled) {\r
+      let userPatch = {\r
+        _id: this.currentUser._id,\r
+        defaultGroup: group._id,\r
+        defaultGroupEnabled: false\r
+      };\r
+\r
+      this._users.patch(userPatch).subscribe((res) => {\r
+        \r
+      });\r
+    }\r
+  }\r
+\r
+  organizeGroups(groups){\r
+    return organizeGroups(this.currentUser, groups);\r
+  }\r
+\r
+  setGroupList(groups) {\r
+    this.groupList = organizeGroups(this.currentUser, groups);\r
+    this.groupListChange.next(this.groupList);\r
+  }\r
+\r
+  listChange(): Subject<Array<any>> {\r
+    return this.groupListChange;\r
+  }\r
+\r
+  groupChange(): Subject<Object> {\r
+    return this.selectedGroupChange;\r
+  }\r
+\r
+  format(arr: Array<any>) {\r
+\r
+    //puts all groups in a single level array\r
+    // arr = organizeGroups(this.currentUser, arr);\r
+\r
+    var tree = [],\r
+      mappedArr = {},\r
+      arrElem,\r
+      mappedElem;\r
+\r
+    // First map the nodes of the array to an object -> create a hash table.\r
+    for (var i = 0, len = arr.length; i < len; i++) {\r
+      arrElem = arr[i];\r
+      mappedArr[arrElem._id] = arrElem;\r
+      mappedArr[arrElem._id]['children'] = [];\r
+    }\r
+\r
+\r
+    for (var _id in mappedArr) {\r
+      if (mappedArr.hasOwnProperty(_id)) {\r
+        mappedElem = mappedArr[_id];\r
+        // If the element is not at the root level, add it to its parent array of children.\r
+        if (mappedElem.parentGroupId) {\r
+          mappedArr[mappedElem['parentGroupId']]['children'].push(mappedElem);\r
+        }\r
+        // If the element is at the root level, add it to first level elements array.\r
+        else {\r
+          tree.push(mappedElem);\r
+        }\r
+      }\r
+    }\r
+    return tree;\r
+  }\r
+\r
+\r
+}\r