added svcapi ui and camunda code
[it/otf.git] / otf-frontend / client / src / app / layout / manage-group / dropdown-multiselect.component.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 { Component, OnInit, Inject } from '@angular/core';\r
18 import { GroupService } from 'app/shared/services/group.service';\r
19 import { MatDialogRef, MAT_DIALOG_DATA } from '@angular/material';\r
20 import { UserService } from 'app/shared/services/user.service';\r
21 \r
22 \r
23 @Component({\r
24   selector: 'app-dropdown-multiselect',\r
25   templateUrl: './dropdown-multiselect.component.pug',\r
26   styleUrls: ['./dropdown-multiselect.component.scss']\r
27 })\r
28 export class DropdownMultiselectComponent implements OnInit {\r
29 \r
30   public group;\r
31   public memberRoles;\r
32   private params;\r
33   public roles;\r
34   public user;\r
35   public userId;\r
36   public search;\r
37   constructor(private groupService: GroupService, public dialogRef: MatDialogRef<DropdownMultiselectComponent>,\r
38     private userService: UserService,\r
39     @Inject(MAT_DIALOG_DATA) public input_data\r
40   ) {\r
41    \r
42    }\r
43 \r
44   ngOnInit() {\r
45     this.search = {};\r
46     this.userId = this.input_data["user"][0]["_id"];\r
47     this.group = this.input_data["group"];\r
48     this.memberRoles = this.group["members"].filter(member => member.userId == this.userId)["roles"];\r
49     this.userService.get(this.userId).subscribe((result) => {\r
50       this.user = result;\r
51     });\r
52     this.roles = this.group.roles;\r
53     \r
54     this.memberRoles = this.group.members.filter(member => member.userId.toString() == this.userId.toString())[0].roles;\r
55     if(this.memberRoles){\r
56       for(let i = 0; i < this.roles.length; i++){\r
57         this.roles[i].isSelected = false;\r
58         for(let j = 0; j < this.memberRoles.length; j++){\r
59           if(this.roles[i].roleName == this.memberRoles[j]){\r
60             this.roles[i].isSelected = true;\r
61           }\r
62         }\r
63       }\r
64     }\r
65   }\r
66 \r
67   saveRoles(){\r
68     let member = {\r
69       userId : this.userId,\r
70       roles : []\r
71     }\r
72     \r
73     member.roles = this.roles.filter(role => role.isSelected).map(item => {return item.roleName});\r
74     \r
75     // the logic to remove the one member from the array of members and then push the new member roles\r
76     this.groupService.get(this.group._id).subscribe((res) => {\r
77       let group = res;\r
78       \r
79       let newMembers = [];\r
80       if(group["members"]){\r
81         newMembers = group["members"].filter(member => member.userId.toString() != this.userId.toString());\r
82       }\r
83       newMembers.push(member)\r
84       let groupPatch = {\r
85         _id : this.group._id,\r
86         members : newMembers\r
87       }\r
88       this.groupService.patch(groupPatch).subscribe((response) => {\r
89         this.dialogRef.close();\r
90       });\r
91     });\r
92     \r
93     \r
94   }\r
95 \r
96 }\r