added svcapi ui and camunda code
[it/otf.git] / otf-frontend / client / src / app / shared / modules / user-select / user-select.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 { MAT_DIALOG_DATA, MatDialogRef } from '@angular/material';\r
19 import { UserService } from 'app/shared/services/user.service';\r
20 import { GroupService } from 'app/shared/services/group.service';\r
21 \r
22 @Component({\r
23   selector: 'app-user-select',\r
24   templateUrl: './user-select.component.pug',\r
25   styleUrls: ['./user-select.component.scss']\r
26 })\r
27 export class UserSelectComponent implements OnInit {\r
28 \r
29   \r
30   public data; \r
31   public users; \r
32   public group;\r
33   public search;\r
34   public selectedUsers;\r
35 \r
36   constructor(\r
37     public dialogRef: MatDialogRef<UserSelectComponent>,\r
38     private userService: UserService,\r
39     private groupService: GroupService,\r
40     @Inject(MAT_DIALOG_DATA) public input_data\r
41   ) {\r
42     this.data = {};\r
43    }\r
44   \r
45   onNoClick(): void {\r
46     this.dialogRef.close();\r
47   }\r
48 \r
49   selectUser(user){\r
50     //this.unselectUser();\r
51     if(user.isSelected){\r
52       this.selectedUsers.push(user);\r
53     }else{\r
54       //user.isSelected = false;\r
55       this.unselectUser(user);\r
56     }\r
57     \r
58 \r
59   }\r
60 \r
61   unselectUser(user){\r
62     // this.selectedUsers = this.selectedUsers.filter(user => user.isSelected);\r
63     this.selectedUsers.splice(this.selectedUsers.findIndex(function(userN){ return userN._id.toString() === user._id.toString(); }), 1);\r
64     \r
65   }\r
66 \r
67   addUsers(){\r
68     let usersToAdd = this.selectedUsers;\r
69 \r
70     //filters users that are already in the group to avoid duplicates\r
71     if(this.group.members){\r
72       usersToAdd = this.selectedUsers.filter(user =>\r
73         this.group.members.filter(member => member.userId.toString() == user._id.toString()).length <= 0\r
74       );\r
75     }\r
76    \r
77     //populates the users roles and userId field\r
78     for(let i = 0; i < usersToAdd.length; i++){\r
79         usersToAdd[i] = {\r
80           userId : usersToAdd[i]._id,\r
81           roles : ["user"]\r
82         }\r
83     }\r
84     //sets up patch object\r
85  \r
86     let groupPatch = {\r
87       _id : this.input_data.groupId,\r
88       $push : { members: { $each : usersToAdd } }\r
89       \r
90     }\r
91     this.groupService.patch(groupPatch).subscribe((results) => {\r
92       this.dialogRef.close(usersToAdd);\r
93     });\r
94     \r
95   }\r
96 \r
97   ngOnInit() {\r
98     this.users = [];\r
99     this.selectedUsers = [];\r
100     this.userService.find({$limit: -1, $select: ['firstName', 'lastName', 'email']})\r
101       .subscribe(\r
102         (result) => {\r
103             this.users = result;\r
104         },\r
105         (error) => {\r
106             console.log(error);\r
107       });\r
108       this.groupService.get(this.input_data.groupId).subscribe((res) => {\r
109         this.group = res;\r
110       })\r
111     \r
112     this.search = {};\r
113     this.search.email = ""; \r
114     this.input_data.testDefinition = {};\r
115   }\r
116 \r
117 }\r