diff --git a/srcs/requirements/nestjs/api_back/src/chat/chat.service.ts b/srcs/requirements/nestjs/api_back/src/chat/chat.service.ts index 5de334f5..10ebc6ba 100644 --- a/srcs/requirements/nestjs/api_back/src/chat/chat.service.ts +++ b/srcs/requirements/nestjs/api_back/src/chat/chat.service.ts @@ -347,12 +347,18 @@ export class ChatService { let relation = await this.findOneRelationshipByUsername(to_block_username, username); if (relation) { - let blocked_friendship = await this.friendshipService.blockFriendship(relation.id, user); + await this.friendshipService.blockFriendship(relation.id, user); } else { const newFriendshipDto = {"receiverUsername": to_block_username, "status": FriendshipStatus.BLOCKED}; - await this.friendshipService.create(newFriendshipDto, user); + if (newFriendshipDto) + await this.friendshipService.create(newFriendshipDto, user); + else + { + printCaller(`throw error: error: true, code: 'BLOCK_USER_FAILED', message: 'we couldn't block the user because it was not found'`); + throw new HttpException({ error: true, code: 'BLOCK_USER_FAILED', message: `we couldn't block the user because it was not found` }, HttpStatus.UNAUTHORIZED); + } } printCaller("-- out "); @@ -363,8 +369,16 @@ export class ChatService { printCaller("-- in "); let user = await this.getUserByName(username); - let relation = await this.findOneRelationshipByUsername(to_unblock_username, username); - await this.friendshipService.removeFriendship(relation.id, user); + let relation: SendableFriendship; + if (user) + relation = await this.findOneRelationshipByUsername(to_unblock_username, username); + if (relation) + await this.friendshipService.removeFriendship(relation.id, user); + else + { + printCaller(`throw error: error: true, code: 'REMOVE_USER_FAILED', message: 'we couldn't unblock the user because it was not found'`); + throw new HttpException({ error: true, code: 'REMOVE_USER_FAILED', message: `we couldn't unblock the user because it was not found` }, HttpStatus.UNAUTHORIZED); + } printCaller("-- out "); } @@ -373,9 +387,11 @@ export class ChatService { { printCaller("-- in "); + let users: string[] = []; + let friends_users: SendableFriendship[]; let user = await this.getUserByName(username); - let friends_users = await this.friendshipService.findAllBlockedFriends(user.id); - let users: string[]; + if (user) + friends_users = await this.friendshipService.findAllBlockedFriends(user.id); if (friends_users) users = friends_users.map(user => user.receiverUsername);