Skip to content
Snippets Groups Projects
Commit 62b93a72 authored by Devin Alvaro's avatar Devin Alvaro
Browse files

Add addChat() method to ChatData

parent 19dc3b06
Branches
No related merge requests found
...@@ -12,4 +12,33 @@ class ChatData { ...@@ -12,4 +12,33 @@ class ChatData {
}) })
this.ChatModel = this.mongoose.model('Chat', this.chatSchema) this.ChatModel = this.mongoose.model('Chat', this.chatSchema)
} }
addChat (senderId, receiverId, content) {
var participantIds = [ senderId, receiverId ]
participantIds.sort()
var chat = {
owner_id: senderId,
content: content
}
this.ChatModel.findOneAndUpdate(
{ participant_ids: participantIds },
{ $push: { chats: chat } }, (err, done) => {
if (err) {
console.error(err)
}
if (!done) {
var chat = new this.ChatModel({
participant_ids: participantIds,
chats: [{
owner_id: senderId,
content: content
}]
})
chat.save()
}
})
}
} }
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment