Skip to content

Commit

Permalink
seller can no longer open chat for his own listing; special case
Browse files Browse the repository at this point in the history
  • Loading branch information
KaranErry committed May 2, 2019
1 parent 2b6e57f commit cea35ce
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 23 deletions.
53 changes: 32 additions & 21 deletions routes/chat.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,32 +101,43 @@ function setupSocketListeners(req, res, next) {
router.get('/openThread', (req, res, next) => {
req.session.authorigin = 'chat/openThread?' + querystring.stringify(req.query);
next();
}, checkAuthentication, function(req,res){
}, checkAuthentication, function(req,res) {
var {itemId, seller} = req.query;
chatData.findOne({item: itemId, interestedBuyer:req.session.passport.user._json.email}).then(doc => {
var currentUser = req.session.passport.user._json.email;
// Ensure the seller is not opening a thread on their own listing
itemData.findOne({_id: itemId, postedBy:currentUser}).then(doc => {
if (!doc) {
// Start a new thread
var current_millies = new Date().getTime();
var current_timestamp = Long.fromNumber(current_millies);
var date = new Date(current_millies);
var dateReadable = date.toString();
var chat = {
item: mongoose.Types.ObjectId(itemId),
seller: seller,
interestedBuyer: req.session.passport.user._json.email,
allParticipants:[ {email: req.session.passport.user._json.email}, {email: seller} ],
dateChatCreated: current_timestamp,
dateChatCreatedComputed: dateReadable
}
var data = new chatData(chat);
data.save((err, doc) => {
res.redirect('/chat?' + querystring.stringify({'preselectedThread':doc.id}));
// The seller ≠ the buyer
// Create a new thread or open the existing one
chatData.findOne({item: itemId, interestedBuyer:currentUser}).then(doc => {
if (!doc) {
// Start a new thread
var current_millies = new Date().getTime();
var current_timestamp = Long.fromNumber(current_millies);
var date = new Date(current_millies);
var dateReadable = date.toString();
var chat = {
item: mongoose.Types.ObjectId(itemId),
seller: seller,
interestedBuyer: req.session.passport.user._json.email,
allParticipants:[ {email: currentUser}, {email: seller} ],
dateChatCreated: current_timestamp,
dateChatCreatedComputed: dateReadable
}
var data = new chatData(chat);
data.save((err, doc) => {
res.redirect('/chat?' + querystring.stringify({'preselectedThread':doc.id}));
});
} else {
// Open the existing thread
res.redirect('/chat?' + querystring.stringify({'preselectedThread':doc.id}));
}
});
} else {
// Open the existing thread
res.redirect('/chat?' + querystring.stringify({'preselectedThread':doc.id}));
// The seller = the buyer
res.redirect('/?' + querystring.stringify({'alert':"You own this listing! You cannot buy your own listing!"}));
}
})
});
});

//authenticate a user is logged in
Expand Down
2 changes: 1 addition & 1 deletion routes/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ router.get('/' ,function(req, res, next) {
}
results.find({sold:false}).sort( { datePosted: -1 } )
.then(function(doc) {
res.render('index', { title: 'DrewUse', items:doc, currentSession: req.session, filters:filters});
res.render('index', { title: 'DrewUse', items:doc, currentSession: req.session, filters:filters, queryParams:req.query});
});
});

Expand Down
7 changes: 6 additions & 1 deletion views/index.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -141,4 +141,9 @@
{{/ifNotEquals}}
{{/each}}
</div>
<br>

{{#if queryParams.alert}}
<script>
alert('{{queryParams.alert}}');
</script>
{{/if}}

0 comments on commit cea35ce

Please sign in to comment.