Estoy construyendo un chat con salas privadas. Lo que estoy tratando de hacer es encontrar una habitación a la que también pertenezcan dos usuarios. Si no hay uno, cree uno.
esquema de chat
export const ChatSchema = new mongoose.Schema({ participants: [{ type: mongoose.Schema.Types.ObjectId, ref: "User" }], created_at: { type: Date, default: Date.now }, });Consulta
async findChatBetweenUsers(participantOneId, participantTwoId) { return await (await this.chatModel.findOne( { participants: [participantOneId, participantTwoId] } )).populate('participants'); }Controlador
async onJoinRoom(socket: Socket, reciever) { const authUser: User = await this.authUser(socket); const recievingUser: User = await this.userService.findOne(reciever.username); const chat = await this.chatService.findChatBetweenUsers(authUser, recievingUser); //Create new chat if doesn't exist if(Object.entries(chat).length === 0){ const newChat = await this.chatService.create(authUser, recievingUser); return; } console.log(chat) }el problema que tengo
El orden de la auth y el receiver cambia según quién haya iniciado sesión y produce un resultado de consulta diferente para el chat. Por ejemplo:
Ejemplo uno
const chat = await this.chatService.findChatBetweenUsers('KylesId', 'ChrisId');Producción
"chat" : { "_id": 'chatOneId', "participants": ['KylesId', 'ChrisId'] }Ejemplo dos
const chat = await this.chatService.findChatBetweenUsers('ChrisId','KylesId');Producción
"chat" : { "_id": 'chatTwoId', "participants": ['ChrisId', 'KylesId'] } ¿Cómo obtengo el mismo resultado a pesar de que se consulta el orden de los participants ?
this.chatModel.find({ participants: {$all: [ObjectId('61ce732e33c7e8a9ad80e151'), ObjectId('61ccf3251b9ba5c8a6ecf2a3')]} });