doesPrivateExist logic is wrong
Closed this issue · 2 comments
rkieboom commented
const chatExists = await this.doesPrivateExist(users[0], users[1]); console.log(chatExists); if (chatExists && chatExists.users.length > 1) { throw new BadRequestException("Private chat already exists"); }
private async doesPrivateExist(idOne: number, idTwo: number): Promise<Chat> { return await this.chatRepo.findOne({ relations: ['users'], where: { type: ChatType.PRIVATE, // users: In([idOne, idTwo]), users: { id: In([idOne, idTwo]), }, } }); }
rkieboom commented
Bob was so sure of himself that it was fixed. And here we are again! :)
bdekonin commented
I used this code first
private async doesPrivateExist(idOne: number, idTwo: number): Promise<Chat> {
return await this.chatRepo.findOne({
relations: ['users'],
where: {
type: ChatType.PRIVATE,
// users: In([idOne, idTwo]),
users: {
id: In([idOne, idTwo]),
},
}
});
}
I use id: In(). This doesnt work at all oops.
Switched to finding a chat by looping through every chat!
private async doesPrivateExist(idOne: number, idTwo: number): Promise<Chat> {
const userOne = await this.userService.findUserById(idOne)
const userTwo = await this.userService.findUserById(idTwo)
if (!userOne) {
throw new UserNotFoundException();
}
if (!userTwo) {
throw new UserNotFoundException();
}
const allchats = await this.chatRepo.find();
const foundChat = allchats.find((chat) => {
if (chat.type == ChatType.PRIVATE && chat.users.includes(userOne) && chat.users.includes(userTwo))
return chat;
});
return (foundChat);
}
But issue is Resolved!