X-Git-Url: https://gerrit.o-ran-sc.org/r/gitweb?p=it%2Fotf.git;a=blobdiff_plain;f=otf-frontend%2Fclient%2Fsrc%2Fapp%2Fshared%2Fmodules%2Fuser-select%2Fuser-select.component.ts;fp=otf-frontend%2Fclient%2Fsrc%2Fapp%2Fshared%2Fmodules%2Fuser-select%2Fuser-select.component.ts;h=5cbfa0bd1709aea92baf97587dfe9381a50889af;hp=0000000000000000000000000000000000000000;hb=14f6f95c84a4a1fa8774190db4a03fd0214ec55f;hpb=f49bd1efeaaddd4891c1f329b18d8cfb28b3e75b diff --git a/otf-frontend/client/src/app/shared/modules/user-select/user-select.component.ts b/otf-frontend/client/src/app/shared/modules/user-select/user-select.component.ts new file mode 100644 index 0000000..5cbfa0b --- /dev/null +++ b/otf-frontend/client/src/app/shared/modules/user-select/user-select.component.ts @@ -0,0 +1,117 @@ +/* Copyright (c) 2019 AT&T Intellectual Property. # +# # +# Licensed under the Apache License, Version 2.0 (the "License"); # +# you may not use this file except in compliance with the License. # +# You may obtain a copy of the License at # +# # +# http://www.apache.org/licenses/LICENSE-2.0 # +# # +# Unless required by applicable law or agreed to in writing, software # +# distributed under the License is distributed on an "AS IS" BASIS, # +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # +# See the License for the specific language governing permissions and # +# limitations under the License. # +##############################################################################*/ + + +import { Component, OnInit, Inject } from '@angular/core'; +import { MAT_DIALOG_DATA, MatDialogRef } from '@angular/material'; +import { UserService } from 'app/shared/services/user.service'; +import { GroupService } from 'app/shared/services/group.service'; + +@Component({ + selector: 'app-user-select', + templateUrl: './user-select.component.pug', + styleUrls: ['./user-select.component.scss'] +}) +export class UserSelectComponent implements OnInit { + + + public data; + public users; + public group; + public search; + public selectedUsers; + + constructor( + public dialogRef: MatDialogRef, + private userService: UserService, + private groupService: GroupService, + @Inject(MAT_DIALOG_DATA) public input_data + ) { + this.data = {}; + } + + onNoClick(): void { + this.dialogRef.close(); + } + + selectUser(user){ + //this.unselectUser(); + if(user.isSelected){ + this.selectedUsers.push(user); + }else{ + //user.isSelected = false; + this.unselectUser(user); + } + + + } + + unselectUser(user){ + // this.selectedUsers = this.selectedUsers.filter(user => user.isSelected); + this.selectedUsers.splice(this.selectedUsers.findIndex(function(userN){ return userN._id.toString() === user._id.toString(); }), 1); + + } + + addUsers(){ + let usersToAdd = this.selectedUsers; + + //filters users that are already in the group to avoid duplicates + if(this.group.members){ + usersToAdd = this.selectedUsers.filter(user => + this.group.members.filter(member => member.userId.toString() == user._id.toString()).length <= 0 + ); + } + + //populates the users roles and userId field + for(let i = 0; i < usersToAdd.length; i++){ + usersToAdd[i] = { + userId : usersToAdd[i]._id, + roles : ["user"] + } + } + //sets up patch object + + let groupPatch = { + _id : this.input_data.groupId, + $push : { members: { $each : usersToAdd } } + + } + this.groupService.patch(groupPatch).subscribe((results) => { + this.dialogRef.close(usersToAdd); + }); + + } + + ngOnInit() { + this.users = []; + this.selectedUsers = []; + this.userService.find({$limit: -1, $select: ['firstName', 'lastName', 'email']}) + .subscribe( + (result) => { + this.users = result; + }, + (error) => { + console.log(error); + }); + this.groupService.get(this.input_data.groupId).subscribe((res) => { + this.group = res; + }) + + this.search = {}; + this.search.email = ""; + this.input_data.testDefinition = {}; + } + +}