1 /* Copyright (c) 2019 AT&T Intellectual Property. #
\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
7 # http://www.apache.org/licenses/LICENSE-2.0 #
\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
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
30 export class GroupService extends ModelService {
\r
32 protected groupList;
\r
33 protected selectedGroup;
\r
35 protected groupListChange: Subject<Array<any>> = new Subject<Array<any>>();
\r
36 protected selectedGroupChange: Subject<Object> = new Subject<Object>();
\r
38 constructor(http: HttpClient, Params: ParamsService, cookie: CookieService, feathers: FeathersService, private _users: UserService) {
\r
39 super('groups', http, Params, cookie, feathers);
\r
46 let currentId = window.localStorage.getItem('currentGroupId');
\r
48 this.currentUser = JSON.parse(this.cookie.get('currentUser'));
\r
50 this.find({ $limit: -1, lookup: 'both' }).subscribe(res => {
\r
51 this.setGroupList(res);
\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
58 //set to first group
\r
69 return this.selectedGroup;
\r
73 return this.groupList;
\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
82 _id: this.currentUser._id,
\r
83 defaultGroup: group._id,
\r
84 defaultGroupEnabled: false
\r
87 this._users.patch(userPatch).subscribe((res) => {
\r
93 organizeGroups(groups){
\r
94 return organizeGroups(this.currentUser, groups);
\r
97 setGroupList(groups) {
\r
98 this.groupList = organizeGroups(this.currentUser, groups);
\r
99 this.groupListChange.next(this.groupList);
\r
102 listChange(): Subject<Array<any>> {
\r
103 return this.groupListChange;
\r
106 groupChange(): Subject<Object> {
\r
107 return this.selectedGroupChange;
\r
110 format(arr: Array<any>) {
\r
112 //puts all groups in a single level array
\r
113 // arr = organizeGroups(this.currentUser, arr);
\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
123 mappedArr[arrElem._id] = arrElem;
\r
124 mappedArr[arrElem._id]['children'] = [];
\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
135 // If the element is at the root level, add it to first level elements array.
\r
137 tree.push(mappedElem);
\r