added svcapi ui and camunda code
[it/otf.git] / otf-frontend / client / src / app / shared / modules / create-group-modal / create-group-modal.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 { MatDialogRef, MAT_DIALOG_DATA, MatSnackBar, MatDialog } from '@angular/material';\r
19 import { GroupService } from 'app/shared/services/group.service';\r
20 import { CookieService } from 'ngx-cookie-service';\r
21 import { UserService } from 'app/shared/services/user.service';\r
22 import { AlertSnackbarComponent } from '../alert-snackbar/alert-snackbar.component';\r
23 import { AlertModalComponent } from 'app/shared/modules/alert-modal/alert-modal.component';\r
24 \r
25 \r
26 @Component({\r
27   selector: 'app-create-group-modal',\r
28   templateUrl: './create-group-modal.component.pug',\r
29   styleUrls: ['./create-group-modal.component.scss']\r
30 })\r
31 export class CreateGroupModalComponent implements OnInit {\r
32 \r
33   constructor(public dialogRef: MatDialogRef<CreateGroupModalComponent>, @Inject(MAT_DIALOG_DATA) public input_data, private userService: UserService, private groupService: GroupService, private cookieService: CookieService, private snack: MatSnackBar, private modal: MatDialog) { }\r
34 \r
35   public groups;\r
36   public newGroup;\r
37   public user;\r
38 \r
39   ngOnInit() {\r
40     this.newGroup = {};\r
41     this.user = {};\r
42     this.groups = [];\r
43     this.newGroup.groupName = '';\r
44     this.newGroup.parentGroupId = null;\r
45     this.user._id = this.userService.getId();\r
46     this.newGroup.ownerId = this.user["_id"];\r
47     //filter list of groups by the Admin permssion from the user\r
48     //Also add group onto active dropdown list when this dialog is closed\r
49     this.groupService.find({\r
50       $limit: -1\r
51       \r
52     }).subscribe((list) => {\r
53       //console.log(list);\r
54       for(let i in list){\r
55         //console.log(this.user._id + "     " + list[i]);\r
56         if(this.checkIsAdmin(list[i], this.user._id)){\r
57           this.groups.push(list[i]);\r
58         }\r
59       }\r
60       \r
61     });\r
62 \r
63   }\r
64 \r
65   checkIsAdmin(group, userId){\r
66     if(group.members){\r
67       let memberIndex =  group.members.findIndex(function(member){return member.userId.toString() == userId.toString()});\r
68       if(memberIndex >= 0){\r
69         if(group.members[memberIndex].roles.includes("admin")){\r
70           return true;\r
71         }\r
72       }\r
73     }\r
74     return false;\r
75   }\r
76 \r
77   close(){\r
78     \r
79     this.dialogRef.close(null);\r
80   }\r
81 \r
82   createGroup(){\r
83     \r
84     //console.log(this.newGroup);\r
85     if(this.newGroup.parentGroupId == "None"){\r
86       this.newGroup.parentGroupId = null;\r
87     }\r
88     \r
89     this.newGroup.roles = [{\r
90       roleName: "admin",\r
91       permissions: ["management", "write", "delete", "read", "execute"]\r
92     },\r
93     {\r
94       roleName: "user",\r
95       permissions: ["read"]\r
96     },\r
97     {\r
98       roleName: "developer",\r
99       permissions: ["write", "delete", "read", "execute"]\r
100     }];\r
101     this.newGroup.members = [{\r
102       userId: this.user._id,\r
103       roles: ["admin"]\r
104     }];\r
105     this.groupService.create(this.newGroup).subscribe(res => { \r
106       \r
107       let snackMessage = 'The group ' + this.newGroup.groupName + " has been created!";\r
108       this.snack.openFromComponent(AlertSnackbarComponent, {\r
109           duration: 1500,\r
110           data: {\r
111               message: snackMessage\r
112           }\r
113       });\r
114       if(res){\r
115         this.dialogRef.close(res)\r
116       }else{\r
117         this.close();\r
118       }\r
119     }, (error) => {\r
120         this.modal.open(AlertModalComponent, {\r
121           width: "250px",\r
122           data: {\r
123               type: "alert",\r
124               message: error\r
125           }\r
126         });\r
127     }); \r
128     \r
129   }\r
130 \r
131 }\r