-
+
Contactos selecção: - -
+ +
-

{{user.name }}

- +

{{user.wxFullName }}

+ @@ -55,10 +55,10 @@ {{ userContainer.key }}
-
- - {{user.name}} -
+
+ + {{user.wxFullName}} +
diff --git a/src/app/shared/chat/group-messages/group-contacts/group-contacts.page.ts b/src/app/shared/chat/group-messages/group-contacts/group-contacts.page.ts index c6e20fd9b..f41eb8ed8 100644 --- a/src/app/shared/chat/group-messages/group-contacts/group-contacts.page.ts +++ b/src/app/shared/chat/group-messages/group-contacts/group-contacts.page.ts @@ -1,13 +1,14 @@ -import { HttpHeaders } from '@angular/common/http'; import { Component, EventEmitter, Input, OnInit, Output } from '@angular/core'; -import { ModalController } from '@ionic/angular'; -import * as _ from 'lodash'; -import { ChatService } from 'src/app/services/chat.service'; -import { NewGroupPage } from '../../new-group/new-group.page'; -import { GroupMessagesPage } from '../group-messages.page'; import { ThemeService } from 'src/app/services/theme.service' -import { SessionStore } from 'src/app/store/session.service'; import { ChatSystemService } from 'src/app/services/chat/chat-system.service'; +import { ContactRepositoryService } from 'src/app/services/Repositorys/contacts/repository/contacts-repository.service'; +import { UserContacts } from 'src/app/services/Repositorys/contacts/data-source/contacts-data-source.service'; +import { RoomRepositoryService } from 'src/app/services/Repositorys/chat/repository/room-repository.service' + +import { HttpErrorHandle } from 'src/app/services/http-error-handle.service'; +import { SessionStore } from 'src/app/store/session.service'; +import { HttpRequest } from '@angular/common/http'; +import { ZodError } from 'zod'; @Component({ selector: 'app-group-contacts', @@ -15,357 +16,192 @@ import { ChatSystemService } from 'src/app/services/chat/chat-system.service'; styleUrls: ['./group-contacts.page.scss'], }) export class GroupContactsPage implements OnInit { + showLoader: boolean; - loggedUser: any; - users = []; - - contact: string[] = [" Ana M.", "Andre F.", "Bruno G.", "Catarina T", "Tiago"]; - - options:any; - listContacts: any[]; - contacts: any; textSearch:string; - room:any; - members:any; - dm:any; - isGroupCreated:boolean; - groupName:string; - selectedUserList:any; - sessionStore = SessionStore @Input() roomId:string; @Output() openGroupMessage:EventEmitter = new EventEmitter(); + currentMembers:UserContacts[]; + allChatUsers: UserContacts[] = []; + userContainer: {[key: string]: ( UserContacts & {isChecked: boolean})[] } = {} + + selectedUsers: number[] =[] + SessionStore = SessionStore - objectUserSingleStone = [] - userContainer = {} - constructor( - private modalController: ModalController, - private chatService: ChatService, public ThemeService: ThemeService, - public ChatSystemService: ChatSystemService + public ChatSystemService: ChatSystemService, + private contactsRepositoryService: ContactRepositoryService, + private RoomRepositoryService: RoomRepositoryService, + private httpErrorHandle: HttpErrorHandle, ) - { - this.loggedUser = SessionStore.user.ChatData['data']; - this.textSearch=""; - this.dm=null; - this.room=null; + {} + ngOnInit(): void { + this.loadUsers() } - ngOnInit() { - // this.chatService.refreshtoken(); - //this.getRoomInfo(); - this.loadUsers(); - - this.getChatInfo(); - // + get hasMemberToUpload() { + return this.selectedUsers.length >= 1 } - getChatInfo(){ - - this.chatService.getRoomInfo(this.roomId).subscribe(room=>{ - - - this.getGroupContacts(room['room']); - }); - } - deleteMember(data:any){ - + async updateGroup() { - let body = { - "roomId": this.roomId, - "userId": data._id, + if(this.hasMemberToUpload) { + this.showLoader = true; + const addMembers = await this.RoomRepositoryService.addMemberToRoom({ + id: this.roomId, + members: this.selectedUsers + }) + if(addMembers.isOk()) { + // this.addContacts(this.roomId); + this.openGroupMessage.emit(this.roomId); + this.RoomRepositoryService.getRoomById(this.roomId) + } else if(addMembers.error instanceof HttpRequest) { + this.httpErrorHandle.httpStatusHandle(addMembers.error) + } + } else { + this.openGroupMessage.emit(this.roomId); } - - this.chatService.getRoomInfo(this.roomId).subscribe(room=>{ - if(room['room'].t == "p"){ - this.chatService.removeGroupMember(body).subscribe(res=>{ - - this.getMembers(); - this.getChatInfo(); - }); - } - else if(room['room'].t == "c"){ - this.chatService.removeChannelMember(body).subscribe(res=>{ - - this.getMembers(); - this.getChatInfo(); - }); - } - }); - } - getMembers(){ - this.chatService.getRoomInfo(this.roomId).subscribe(res=>{ - - let room = res['room']; - - if(room.t == "p"){ - this.chatService.getGroupMembers(this.roomId).subscribe(res=>{ - this.members = res['members']; - }); - } - else if(room.t == "c"){ - this.chatService.getChannelMembers(this.roomId).subscribe(res=>{ - this.members = res['members']; - }); - } - }); - } - - getGroupContacts(room:any){ - this.showLoader = true; - if(room.t === 'p'){ - this.chatService.getGroupMembers(this.roomId).subscribe(res=>{ - this.members = res['members']; - this.loadUsers1(this.members); - this.showLoader = false; - }); - } - else{ - this.chatService.getChannelMembers(this.roomId).subscribe(res=>{ - this.members = res['members']; - this.loadUsers1(this.members); - this.showLoader = false; - }); - } - } - - updateGroup(){ - this.showLoader = true; - this.addContacts(this.roomId); - this.openGroupMessage.emit(this.roomId); this.showLoader = false; } - openGroupMessagesPage(){ - this.showLoader = true; + + openGroupMessagesPage() { this.openGroupMessage.emit(this.roomId) + } + + async loadUsers() { + + const getallChatUsers = await this.contactsRepositoryService.getUsers() + const getRoomById = await this.RoomRepositoryService.getRoomById(this.roomId) + + if(getallChatUsers.isOk() && getRoomById.isOk()) { + + this.allChatUsers = getallChatUsers.value.data.result.sort((a,b) => { + if(a.wxFullName < b.wxFullName) { + return -1; + } + if(a.wxFullName > b.wxFullName) { + return 1; + } + return 0; + }); + + const currentMemberToMap = await this.RoomRepositoryService.getRoomMemberById(this.roomId) + + this.currentMembers = currentMemberToMap.map((e)=> ({ + userPhoto: e.user.userPhoto, + wxeMail: e.user.wxeMail, + wxFullName: e.user.wxFullName, + wxUserId: e.user.wxUserId + })) + + } + else if (getRoomById.isErr()) { + this.httpErrorHandle.httpStatusHandle(getRoomById.error) + } else if (getallChatUsers.isErr()) { + this.httpErrorHandle.httpStatusHandle(getallChatUsers.error) + } + + const currentMemberIds = this.currentMembers.map(e => e.wxUserId) + + const allSelectableUsers = this.allChatUsers.filter(e => !currentMemberIds.includes(e.wxUserId)) + + for(const user of allSelectableUsers) { + const firstLetter = user.wxFullName.charAt(0) + + if(!this.userContainer[firstLetter]) { + user['isChecked'] = false + this.userContainer[firstLetter] = [user as any] + } else { + const userIds = this.userContainer[firstLetter].map( e => e.wxUserId) + if(!userIds.includes(user.wxUserId)) { + user['isChecked'] = false + this.userContainer[firstLetter].push(user as any) + } + } + } this.showLoader = false; } - loadUsers1(members:any) { - - this.chatService.getAllUsers().subscribe((res:any)=>{ - - - - if(members){ - this.contacts = res.users.filter(f => !this.members.some(item => item._id === f._id)); - } - else{ - this.contacts = res.users.filter(data => data.username != this.sessionStore.user.UserName); - } - - this.users = this.contacts.sort((a,b) => { - if(a.name < b.name){ - return -1; - } - if(a.name > b.name){ - return 1; - } - return 0; - }); - - - this.showLoader = false; - }); - } - - loadUsers() { - - this.chatService.getAllUsers().subscribe((res:any)=>{ - - - - if(this.members){ - this.contacts = res.users.filter(f => !this.members.some(item => item._id === f._id)); - } - else{ - this.contacts = res.users.filter(data => data.username != this.sessionStore.user.UserName); - } - - this.users = this.contacts.sort((a,b) => { - if(a.name < b.name){ - return -1; - } - if(a.name > b.name){ - return 1; - } - return 0; - }); - - - for( const user of this.users) { - - const foundUser = this.objectUserSingleStone.find( e => e.name == user.name) - - if(!foundUser) { - this.objectUserSingleStone.push(user) - } else { - console.log('not found') - } - - } - - const userContainer = {} - - for(const user of this.objectUserSingleStone) { - const firstLetter = user.name.charAt(0) - - if(!userContainer[firstLetter]) { - userContainer[firstLetter] = [user] - } else { - userContainer[firstLetter].push(user) - } - } - - - this.userContainer = userContainer - - this.showLoader = false; - }); - } - - doRefresh(ev){ + doRefresh(ev) { ev.target.complete(); } - async close(){ - this.modalController.dismiss(); - if(this.isGroupCreated){ - - } - else{ - this.modalController.dismiss(); - - const modal = await this.modalController.create({ - component: NewGroupPage, - componentProps: { - name:this.groupName, - duration:'', - }, - cssClass: 'new-group modal-desktop', - backdropDismiss: false, - }); - await modal.present(); + FilterUserListedByTextSearch() { + return this.allChatUsers.filter( e => e.wxFullName.toLowerCase().includes(this.textSearch.toLowerCase())).sort((a,b) => { + if(a.wxFullName < b.wxFullName) { + return -1; + } + if(a.wxFullName > b.wxFullName) { + return 1; + } + return 0; + }); + } + + + onChangeCheckBox(user: UserContacts & {isChecked: boolean}) { + if(user.isChecked) { + this.selectedUsers.push(user.wxUserId) + } else { + this.selectedUsers = this.selectedUsers.filter(e => e!= user.wxUserId) } } onChange(event) { this.textSearch = event.detail.value; - const users: any[] = this.contacts.filter( e => e.name.toLowerCase().includes(this.textSearch.toLowerCase())).sort((a,b) => { - if(a.name < b.name) { - return -1; - } - if(a.name > b.name) { - return 1; - } - return 0; - }); + const filteredUserList: (UserContacts & {isChecked: boolean})[] = this.FilterUserListedByTextSearch() as any - const selectedUsers = this.users.filter( e => e?.isChecked == true) - - users.forEach( (user, index) => { - if(user[index]) { - console.log({user, index}) - const isCheck = selectedUsers.find( e => e._id == user._id)?.isChecked - if(isCheck) { - user[index].isChecked = isCheck - } + const userContainer = {} + for(const user of filteredUserList) { + const firstLetter = user.wxFullName.charAt(0) - // if(user[index].isChecked) { - // console.log('user[index].isChecked', user[index].isChecked) - // } + if(!userContainer[firstLetter]) { + user.isChecked = this.selectedUsers.includes(user.wxUserId) + userContainer[firstLetter] = [user] + } else { + user.isChecked = this.selectedUsers.includes(user.wxUserId) + userContainer[firstLetter].push(user) } + } + this.userContainer = userContainer + } + + + async deleteMember(user: UserContacts) { + this.showLoader = true; + + const result = await this.RoomRepositoryService.removeMemberToRoom({ + id: this.roomId, + members: [ user.wxUserId] }) - this.users = users + if(result.isOk()) { + this.currentMembers = this.currentMembers.filter( e => e.wxUserId != user.wxUserId) + const firstLetter = user.wxFullName.charAt(0) - let a = this.objectUserSingleStone.filter( e => e.name.toLowerCase().includes(this.textSearch.toLowerCase())) - - let b = {} - for(const user of a) { - const firstLetter = user.name.charAt(0) - - if(!b[firstLetter]) { - b[firstLetter] = [user] + if(!this.userContainer[firstLetter]) { + user['isChecked'] = false + this.userContainer[firstLetter] = [user as any] } else { - b[firstLetter].push(user) + user['isChecked'] = false + this.userContainer[firstLetter].push(user as any) } - + + } else if(result.error instanceof HttpRequest) { + this.httpErrorHandle.httpStatusHandle(result.error) + } else if(result.error instanceof ZodError) { + console.log(result.error.issues) } + this.showLoader = false; - this.userContainer = b - - // console.log('this.users', this.users) - } - - clicked(){ - - } - - selectedContact(user:any) { - /* this.groupName = this.room.name; */ - if(user.isChecked != true ) { - user.isChecked = false - } else { - user.isChecked = true - } - - const userIndex = this.objectUserSingleStone.findIndex((e) => e._id == user._id) - this.objectUserSingleStone[userIndex].isChecked = user.isChecked - - } - - addContacts(roomId:any) { - - this.selectedUserList = this.objectUserSingleStone.filter(function(contact) { - return contact.isChecked == true; - }); - - this.selectedUserList.forEach(user=>{ - let body ={ - "roomId":roomId, - "userId":user._id, - } - this.chatService.addUserToGroup(body).subscribe(res=>{ - this.ChatSystemService.getGroupRoom(roomId).updateContacts() - }); - }); - } - - - async newGroup(){ - this.close(); - const modal = await this.modalController.create({ - component: NewGroupPage, - cssClass: 'new-group modal-desktop', - backdropDismiss: false, - }); - - modal.onDidDismiss(); - await modal.present(); - } - - async openGroupMessages(room:any){ - this.close(); - const modal = await this.modalController.create({ - component: GroupMessagesPage, - componentProps: { - room: room, - }, - cssClass: 'group-messages', - backdropDismiss: false - }); - - - modal.onDidDismiss(); - await modal.present(); } } diff --git a/src/app/shared/chat/group-messages/group-messages.page.ts b/src/app/shared/chat/group-messages/group-messages.page.ts index 041ae5845..d84c01f6d 100644 --- a/src/app/shared/chat/group-messages/group-messages.page.ts +++ b/src/app/shared/chat/group-messages/group-messages.page.ts @@ -521,7 +521,6 @@ export class GroupMessagesPage implements OnInit, OnChanges, AfterViewInit, OnDe }); - } openSendGroupMessageOptions(ev?: any) { diff --git a/src/app/shared/chat/messages/contacts/contacts.page.ts b/src/app/shared/chat/messages/contacts/contacts.page.ts index 965520619..f955cd0c7 100644 --- a/src/app/shared/chat/messages/contacts/contacts.page.ts +++ b/src/app/shared/chat/messages/contacts/contacts.page.ts @@ -12,7 +12,6 @@ import { RoomRepositoryService } from 'src/app/services/Repositorys/chat/reposit import { HttpErrorResponse } from '@angular/common/http'; - class UserToSelect { } @@ -95,7 +94,7 @@ export class ContactsPage implements OnInit { }, () => { this.loading = false - }); + }); }*/ } @@ -170,7 +169,7 @@ export class ContactsPage implements OnInit { } /* this.chatService.createRoom(body).subscribe(res => { - + this.room = res['room']; this.openMessagesModal(this.room._id); this.ChatSystemService.getAllRooms() diff --git a/src/app/shared/chat/messages/messages.page.html b/src/app/shared/chat/messages/messages.page.html index 502db2cad..4f4b64ccb 100644 --- a/src/app/shared/chat/messages/messages.page.html +++ b/src/app/shared/chat/messages/messages.page.html @@ -7,7 +7,7 @@
-