added svcapi ui and camunda code
[it/otf.git] / otf-frontend / client / src / app / shared / modules / create-group-modal / create-group-modal.component.ts
diff --git a/otf-frontend/client/src/app/shared/modules/create-group-modal/create-group-modal.component.ts b/otf-frontend/client/src/app/shared/modules/create-group-modal/create-group-modal.component.ts
new file mode 100644 (file)
index 0000000..2d0befc
--- /dev/null
@@ -0,0 +1,131 @@
+/*  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 { Component, OnInit, Inject } from '@angular/core';\r
+import { MatDialogRef, MAT_DIALOG_DATA, MatSnackBar, MatDialog } from '@angular/material';\r
+import { GroupService } from 'app/shared/services/group.service';\r
+import { CookieService } from 'ngx-cookie-service';\r
+import { UserService } from 'app/shared/services/user.service';\r
+import { AlertSnackbarComponent } from '../alert-snackbar/alert-snackbar.component';\r
+import { AlertModalComponent } from 'app/shared/modules/alert-modal/alert-modal.component';\r
+\r
+\r
+@Component({\r
+  selector: 'app-create-group-modal',\r
+  templateUrl: './create-group-modal.component.pug',\r
+  styleUrls: ['./create-group-modal.component.scss']\r
+})\r
+export class CreateGroupModalComponent implements OnInit {\r
+\r
+  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
+\r
+  public groups;\r
+  public newGroup;\r
+  public user;\r
+\r
+  ngOnInit() {\r
+    this.newGroup = {};\r
+    this.user = {};\r
+    this.groups = [];\r
+    this.newGroup.groupName = '';\r
+    this.newGroup.parentGroupId = null;\r
+    this.user._id = this.userService.getId();\r
+    this.newGroup.ownerId = this.user["_id"];\r
+    //filter list of groups by the Admin permssion from the user\r
+    //Also add group onto active dropdown list when this dialog is closed\r
+    this.groupService.find({\r
+      $limit: -1\r
+      \r
+    }).subscribe((list) => {\r
+      //console.log(list);\r
+      for(let i in list){\r
+        //console.log(this.user._id + "     " + list[i]);\r
+        if(this.checkIsAdmin(list[i], this.user._id)){\r
+          this.groups.push(list[i]);\r
+        }\r
+      }\r
+      \r
+    });\r
+\r
+  }\r
+\r
+  checkIsAdmin(group, userId){\r
+    if(group.members){\r
+      let memberIndex =  group.members.findIndex(function(member){return member.userId.toString() == userId.toString()});\r
+      if(memberIndex >= 0){\r
+        if(group.members[memberIndex].roles.includes("admin")){\r
+          return true;\r
+        }\r
+      }\r
+    }\r
+    return false;\r
+  }\r
+\r
+  close(){\r
+    \r
+    this.dialogRef.close(null);\r
+  }\r
+\r
+  createGroup(){\r
+    \r
+    //console.log(this.newGroup);\r
+    if(this.newGroup.parentGroupId == "None"){\r
+      this.newGroup.parentGroupId = null;\r
+    }\r
+    \r
+    this.newGroup.roles = [{\r
+      roleName: "admin",\r
+      permissions: ["management", "write", "delete", "read", "execute"]\r
+    },\r
+    {\r
+      roleName: "user",\r
+      permissions: ["read"]\r
+    },\r
+    {\r
+      roleName: "developer",\r
+      permissions: ["write", "delete", "read", "execute"]\r
+    }];\r
+    this.newGroup.members = [{\r
+      userId: this.user._id,\r
+      roles: ["admin"]\r
+    }];\r
+    this.groupService.create(this.newGroup).subscribe(res => { \r
+      \r
+      let snackMessage = 'The group ' + this.newGroup.groupName + " has been created!";\r
+      this.snack.openFromComponent(AlertSnackbarComponent, {\r
+          duration: 1500,\r
+          data: {\r
+              message: snackMessage\r
+          }\r
+      });\r
+      if(res){\r
+        this.dialogRef.close(res)\r
+      }else{\r
+        this.close();\r
+      }\r
+    }, (error) => {\r
+        this.modal.open(AlertModalComponent, {\r
+          width: "250px",\r
+          data: {\r
+              type: "alert",\r
+              message: error\r
+          }\r
+        });\r
+    }); \r
+    \r
+  }\r
+\r
+}\r