diff --git a/models/chat_model.js b/models/chat_model.js index 123f00ae..a90bf53b 100644 --- a/models/chat_model.js +++ b/models/chat_model.js @@ -1,16 +1,16 @@ let mongoose = require('mongoose'); var Schema = mongoose.Schema; +var itemData = require('../models/item_model'); var chatDataSchema= new Schema({ // Initial info - users:[{email:String}], + item: { type: Schema.Types.ObjectId, ref: 'itemData'}, + interestedBuyer: String, + seller: String, messages:[{message:String, byWho: String, timestamp:Number}], + allParticipants:[{email:String}], dateChatCreated: Number, - dateChatCreatedComputed: String, - dateChatStartedBy: String, - selectedImageUrl: String, - selectedPostTitle: String, - selectedPersonSelling: String + dateChatCreatedComputed: String },{collection:'chats'}); diff --git a/routes/chat.js b/routes/chat.js index 9ac3140f..86a1a91e 100644 --- a/routes/chat.js +++ b/routes/chat.js @@ -23,15 +23,15 @@ router.use(bodyParser.urlencoded({ extended: false })) /* GET home page. */ router.get('/', checkAuthentication, function(req, res, next) { preselectedThread = req.query.preselectedThread ? req.query.preselectedThread : ''; - chatData.find({users: + chatData.find({allParticipants: {$elemMatch : { email: req.session.passport.user._json.email } } } - ).sort({ dateChatCreated: -1 }).then(function(doc) { - res.render('chat', { title: 'DrewUse', currentSession: req.session, chats:doc, preselectedThread:preselectedThread}); + ).populate('item').sort({ dateChatCreated: -1 }).then(function(doc) { + res.render('chat', { title: 'DrewUse', currentUser: req.session.passport.user._json.email, chats:doc, preselectedThread:preselectedThread}); }); }); @@ -39,14 +39,13 @@ router.post('/newMessage', checkAuthentication, function(req,res){ var current_timestamp = Long.fromNumber(current_millies); var date = new Date(current_millies); var dateReadable = date.toString(); - var chat ={ - users:[{email: req.session.passport.user._json.email},{email: req.body.selectedPersonSelling} ], + var chat = { + item: mongoose.Types.ObjectId(req.body.itemId), + seller: req.body.seller, + interestedBuyer: req.session.passport.user._json.email, + allParticipants:[ {email: req.session.passport.user._json.email}, {email: req.body.seller} ], dateChatCreated: current_timestamp, - dateChatCreatedComputed: dateReadable, - dateChatStartedBy: req.session.passport.user._json.email, - selectedImageUrl: req.body.selectedImageUrl, - selectedPostTitle: req.body.selectedPostTitle, - selectedPersonSelling: req.body.selectedPersonSelling + dateChatCreatedComputed: dateReadable } var data = new chatData(chat); data.save((err, doc) => { @@ -59,7 +58,7 @@ router.post('/messages/getInfo', checkAuthentication, (req, res) => { console.log(req.body.threadId); chatData.find({_id: req.body.threadId}) .then(function(doc) { - console.log(doc); + // console.log(doc); res.io.emit('allMessages', doc); }); diff --git a/views/chat.hbs b/views/chat.hbs index c124056a..1e85a565 100644 --- a/views/chat.hbs +++ b/views/chat.hbs @@ -188,14 +188,14 @@ img{ max-width:100%;}
{{# each chats }}
- sunil + Image for product '{{this.item.title}}' -
{{this.selectedPostTitle}}
- {{#ifEquals this.selectedPersonSelling ../currentSession.passport.user._json.email}} +
{{this.item.title}}
+ {{#ifEquals this.seller ../currentUser}} Interested Buyer {{/ifEquals}} - {{#ifEquals this.dateChatStartedBy ../currentSession.passport.user._json.email}} + {{#ifEquals this.interestedBuyer ../currentUser}} Product Seller {{/ifEquals}}

Last chatted:

diff --git a/views/index.hbs b/views/index.hbs index 694072c0..9095b02b 100644 --- a/views/index.hbs +++ b/views/index.hbs @@ -131,9 +131,8 @@ {{else}}
- - - + +
{{/if}}