leave room

This commit is contained in:
Peter Maquiran
2024-06-12 11:49:41 +01:00
parent 751ebb7731
commit 295fe96da6
4 changed files with 53 additions and 39 deletions
@@ -3,11 +3,17 @@ import { RoomListItemOutPutDTO, RoomListOutPutDTO } from '../../dto/room/roomLis
import { Dexie, EntityTable, liveQuery, Observable } from 'Dexie';
import { err, ok, Result } from 'neverthrow';
import { z } from 'zod';
import { ValidateSchema } from 'src/app/services/decorators/validate-schema.decorator';
const tableSchema = z.object({
id: z.string(),
roomName: z.string(),
createdBy: z.any(),
createdBy: z.object({
wxUserId: z.number(),
wxFullName: z.string(),
wxeMail: z.string().email(),
userPhoto: z.string().nullable()// api check
}),
createdAt: z.any(),
expirationDate: z.any(),
roomType: z.any()
@@ -49,6 +55,7 @@ export class RoomLocalDataSourceService {
constructor() {}
@ValidateSchema(tableSchema)
async createRoom(data: TableRoom) {
try {
const result = await roomDataSource.room.add(data)
@@ -68,6 +75,11 @@ export class RoomLocalDataSourceService {
}
}
async leaveRoom(id: string) {
return this.deleteRoomById(id)
}
async updateRoom(data: TableRoom) {
try {
const result = await roomDataSource.room.update(data.id, data);
@@ -58,6 +58,10 @@ export class RoomRepositoryService {
const { roomsToDelete, roomsToInsert, roomsToUpdate } = roomListDetermineChanges([result.value.data], localList)
for( const roomData of roomsToUpdate) {
if(!roomData.createdBy?.wxUserId) {
delete roomData.createdBy;
}
this.roomLocalDataSourceService.updateRoom(roomData)
}
@@ -132,6 +136,17 @@ export class RoomRepositoryService {
return result
}
async leaveRoom(data: UserRemoveListInputDTO) {
const result = await this.roomRemoteDataSourceService.removeMemberFromRoom(data)
if(result.isOk()) {
return this.roomLocalDataSourceService.leaveRoom(data.id)
}
return result
}
getItemsLive() {
return this.roomLocalDataSourceService.getItemsLive()
}
@@ -17,7 +17,8 @@
<button *ngIf="isAdmin" (click)="deleteGroup()" class="btn-delete" shape="round">Apagar grupo</button>
-->
<ion-progress-bar type="indeterminate" *ngIf="showLoader"></ion-progress-bar>
<br *ngIf="showLoader">
<button (click)="addUser()" class="btn-cancel" shape="round">Adicionar</button>
<button (click)="leaveGroup()" class="btn-cancel" shape="round">Sair do Grupo</button>
<button (click)="openChangeGroupName()" class="btn-cancel btn-cancel mt-10" shape="round" style="min-width: 192px;">Alterar
@@ -4,10 +4,12 @@ import { ChatService } from 'src/app/services/chat.service';
import { ToastService } from 'src/app/services/toast.service';
import { ThemeService } from 'src/app/services/theme.service'
import { SetRoomOwnerPage } from 'src/app/modals/set-room-owner/set-room-owner.page';
import { RoomRepositoryService } from 'src/app/services/Repositorys/chat/repository/room-repository.service'
import { ChatSystemService } from 'src/app/services/chat/chat-system.service';
import { HttpErrorResponse } from '@angular/common/http';
import { HttpErrorResponse, HttpResponse } from '@angular/common/http';
import { SessionStore } from 'src/app/store/session.service';
import { GroupContactsPage } from '../../chat/group-messages/group-contacts/group-contacts.page';
import { ZodError } from 'zod';
@Component({
@@ -21,6 +23,7 @@ export class ChatPopoverPage implements OnInit {
members:any;
isAdmin = false;
isGroupCreated: boolean;
showLoader = false
constructor(
private popoverController: PopoverController,
@@ -29,7 +32,7 @@ export class ChatPopoverPage implements OnInit {
private chatService: ChatService,
private toastService: ToastService,
public ThemeService: ThemeService,
public ChatSystemService: ChatSystemService,
private RoomRepositoryService: RoomRepositoryService
) {
this.roomId = this.navParams.get('roomId');
this.members = this.navParams.get('members');
@@ -81,53 +84,36 @@ export class ChatPopoverPage implements OnInit {
});
}
async leaveGroup(){
async leaveGroup() {
let body = { "roomId":this.roomId, }
this.showLoader = true
const result = await this.RoomRepositoryService.leaveRoom({
id: this.roomId,
members: [SessionStore.user.UserId]
})
let res:any;
try {
res = await this.ChatSystemService.leaveRoom(this.roomId);
} catch (error) {
console.error(error)
}
if(res.error){
if(res.error.error = "error-you-are-last-owner"){
this.toastService._badRequest("Você é o último administrador do grupo. Por favor, defina o novo administrador antes de sair da grupo.");
this.setRoomOwner();
}
else if(res.error.error == 'error-user-not-in-room'){
this.toastService._badRequest("Você já não esta nesta conversa");
}
else{
this.toastService._badRequest("Não foi possível sair do grupo");
}
}
else {
this.ChatSystemService.deleteRoom(this.roomId)
if(result.isOk()) {
this.close('leave');
// this.openGroupMessage.emit(this.roomId);
} else if (result.error instanceof HttpResponse) {
// this.httpErrorHandle.httpStatusHandle(result.error)
} else if (result.error instanceof ZodError) {
this.toastService._badRequest("Pedimos desculpa mas não foi possível executar a acção. Por favor, contacte o apoio técnico.")
console.log(result.error.errors)
} else {
this.toastService._badRequest("Pedimos desculpa mas não foi possível executar a acção. Por favor, contacte o apoio técnico.")
console.log(result.error)
}
this.showLoader = false
}
//Delete
deleteGroup() {
let body = { "roomId":this.roomId }
this.chatService.getRoomInfo(this.roomId).subscribe(room => {
this.room = room['room'];
if(this.room.t === 'p') {
this.chatService.deleteGroup(body).subscribe(res=>{
this.ChatSystemService.deleteRoom(this.roomId)
});
}
else {
this.chatService.deleteChannel(body).subscribe(res=>{
this.ChatSystemService.deleteRoom(this.roomId)
});
}
});
this.close('delete');
}