From 39bc8979a8cfa2d96a71ab213d3e5fd208fdd34d Mon Sep 17 00:00:00 2001 From: Peter Maquiran Date: Tue, 11 Jun 2024 17:29:04 +0100 Subject: [PATCH] change detector for room list --- .../room/rooom-local-data-source.service.ts | 4 +- .../chat/dto/room/roomOutputDTO.ts | 2 +- .../repository/room-repository.service.ts | 25 +++++++--- .../group-contacts/group-contacts.page.html | 2 +- .../group-contacts/group-contacts.page.ts | 46 ++++++++++++------- 5 files changed, 51 insertions(+), 28 deletions(-) diff --git a/src/app/services/Repositorys/chat/data-source/room/rooom-local-data-source.service.ts b/src/app/services/Repositorys/chat/data-source/room/rooom-local-data-source.service.ts index db116fe0c..27e581588 100644 --- a/src/app/services/Repositorys/chat/data-source/room/rooom-local-data-source.service.ts +++ b/src/app/services/Repositorys/chat/data-source/room/rooom-local-data-source.service.ts @@ -82,9 +82,9 @@ export class RoomLocalDataSourceService { const createResult = await this.createRoom(data) if(createResult.isOk()) { - return this.updateRoom(data) - } else { return createResult + } else { + return this.updateRoom(data) } } diff --git a/src/app/services/Repositorys/chat/dto/room/roomOutputDTO.ts b/src/app/services/Repositorys/chat/dto/room/roomOutputDTO.ts index 1e099148a..a704a70b3 100644 --- a/src/app/services/Repositorys/chat/dto/room/roomOutputDTO.ts +++ b/src/app/services/Repositorys/chat/dto/room/roomOutputDTO.ts @@ -6,7 +6,7 @@ export const RoomOutPutDTOSchema = z.object({ data: z.object({ id: z.string(), roomName: z.string(), - createdBy: z.any(), + createdBy: z.any().nullable(), createdAt: z.string(), expirationDate: z.string().nullable(), roomType: z.any() diff --git a/src/app/services/Repositorys/chat/repository/room-repository.service.ts b/src/app/services/Repositorys/chat/repository/room-repository.service.ts index fcee03432..d18b9546d 100644 --- a/src/app/services/Repositorys/chat/repository/room-repository.service.ts +++ b/src/app/services/Repositorys/chat/repository/room-repository.service.ts @@ -11,6 +11,7 @@ import { UserRemoveListInputDTO } from '../dto/room/userRemoveListInputDTO'; import { roomMemberListDetermineChanges } from '../async/rooms/roomMembersChangeDetector'; import { captureAndReraiseAsync } from 'src/app/services/decorators/captureAndReraiseAsync'; import { RoomUpdateInputDTO } from '../dto/room/roomUpdateInputDTO'; +import { SessionStore } from 'src/app/store/session.service'; @Injectable({ providedIn: 'root' @@ -64,17 +65,19 @@ export class RoomRepositoryService { const result = await this.roomRemoteDataSourceService.getRoom(id) if(result.isOk()) { + + const localListRoom = await this.roomLocalDataSourceService.getRoomList() + const { roomsToDelete, roomsToInsert, roomsToUpdate } = roomListDetermineChanges([result.value.data], localListRoom) + + for( const roomData of roomsToUpdate) { + this.roomLocalDataSourceService.updateRoom(roomData) + } + + // ============================ const localList = await this.roomLocalDataSourceService.getRoomMemberById(id) const { membersToInsert, membersToUpdate, membersToDelete } = roomMemberListDetermineChanges(result.value.data.members, localList, id) - - const a = await this.roomLocalDataSourceService.createOrUpdateRoom(result.value.data) - - if(a.isErr()) { - return a - } - for (const user of membersToInsert) { this.roomLocalDataSourceService.addMember({...user, roomId:id}) } @@ -94,6 +97,14 @@ export class RoomRepositoryService { const result = await this.roomRemoteDataSourceService.createRoom(data) if(result.isOk()) { + if(!result.value.data.createdBy) { + result.value.data.createdBy = { + wxeMail: SessionStore.user.Email, + wxFullName: SessionStore.user.FullName, + wxUserId: SessionStore.user.UserId + } + } + this.roomMemoryDataSourceService.dispatch(addRoom(result.value)) this.roomLocalDataSourceService.createRoom(result.value.data) } diff --git a/src/app/shared/chat/group-messages/group-contacts/group-contacts.page.html b/src/app/shared/chat/group-messages/group-contacts/group-contacts.page.html index 0f8defe06..fafe6169d 100644 --- a/src/app/shared/chat/group-messages/group-contacts/group-contacts.page.html +++ b/src/app/shared/chat/group-messages/group-contacts/group-contacts.page.html @@ -14,7 +14,7 @@
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 153524bfe..76af907d5 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 @@ -82,6 +82,8 @@ export class GroupContactsPage implements OnInit { const getallChatUsers = await this.contactsRepositoryService.getUsers() const getRoomById = await this.RoomRepositoryService.getRoomById(this.roomId) + console.log({getallChatUsers, getRoomById}) + if(getallChatUsers.isOk() && getRoomById.isOk()) { this.allChatUsers = getallChatUsers.value.data.result.sort((a,b) => { @@ -96,6 +98,8 @@ export class GroupContactsPage implements OnInit { const currentMemberToMap = await this.RoomRepositoryService.getRoomMemberById(this.roomId) + console.log({currentMemberToMap}) + this.currentMembers = currentMemberToMap.map((e)=> ({ userPhoto: e.user.userPhoto, wxeMail: e.user.wxeMail, @@ -103,33 +107,41 @@ export class GroupContactsPage implements OnInit { wxUserId: e.user.wxUserId })) + + 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) + } + } + } + + } else if (getRoomById.isErr() && getRoomById.error instanceof HttpResponse) { this.httpErrorHandle.httpStatusHandle(getRoomById.error) } else if (getallChatUsers.isErr() && getallChatUsers.error instanceof HttpResponse) { this.httpErrorHandle.httpStatusHandle(getallChatUsers.error) + } else if (getRoomById.isErr() ) { + console.log(getRoomById.error) + } else if (getallChatUsers.isErr() ) { + console.log(getallChatUsers.error) } else { this.toastService._badRequest("Pedimos desculpa mas não foi possível executar a acção. Por favor, contacte o apoio técnico.") } - 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; }