Skip to content

Commit

Permalink
add follow function
Browse files Browse the repository at this point in the history
  • Loading branch information
manupacheco committed May 7, 2018
1 parent c872ad3 commit a798c7e
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 0 deletions.
1 change: 1 addition & 0 deletions models/company.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ const companySchema = new Schema({
messages: [Msg.schema],
send: [Msg.schema],
influencersFavs: [Schema.Types.ObjectId],
followers: [Schema.Types.ObjectId],
}, {
timestamps: { createdAt: 'created_at', updatedAt: 'updated_at' },
});
Expand Down
2 changes: 2 additions & 0 deletions models/influencer.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ const influencerSchema = new Schema({
messages: [Msg.schema],
send: [Msg.schema],
campaignsFavs: [Schema.Types.ObjectId],
companiesFavs: [Schema.Types.ObjectId],
followers: [Schema.Types.ObjectId],
instagram: {
username: String,
biography: String,
Expand Down
72 changes: 72 additions & 0 deletions routes/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -598,4 +598,76 @@ router.put('/messages/read/:idMessage', (req, res, next) => {
}
});

// Follow & Unfollow function

router.put('/follow/:userId', (req, res, next) => {
if (!req.session.currentUser) {
return res.status(401).json({ error: 'unauthorized' });
}
const options = {
new: true,
};

const { _id, role } = req.session.currentUser;
const { userId } = req.params;
const id = mongoose.Types.ObjectId(userId);

if (role === 'company') {
Company.findByIdAndUpdate({ _id }, { $push: { influencersFavs: userId } }, options)
.then(() => {
Influencer.findByIdAndUpdate({ _id: id }, { $push: { followers: _id } }, options)
.then((result) => {
res.status(200).json(result);
})
.catch(next);
})
.catch(next);
} else if (role === 'influencer') {
Influencer.findByIdAndUpdate({ _id }, { $push: { companiesFavs: userId } }, options)
.then(() => {
Company.findByIdAndUpdate({ _id: id }, { $push: { followers: _id } }, options)
.then((result) => {
res.status(200).json(result);
})
.catch(next);
})
.catch(next);
}
});

router.put('/unfollow/:userId', (req, res, next) => {
if (!req.session.currentUser) {
return res.status(401).json({ error: 'unauthorized' });
}
const options = {
new: true,
};

const { _id, role } = req.session.currentUser;
const { userId } = req.params;
const id = mongoose.Types.ObjectId(userId);

if (role === 'company') {
Company.findByIdAndUpdate({ _id }, { $pull: { influencersFavs: userId } }, options)
.then(() => {
Influencer.findByIdAndUpdate({ _id: id }, { $pull: { followers: _id } }, options)
.then((result) => {
res.status(200).json(result);
})
.catch(next);
})
.catch(next);
} else if (role === 'influencer') {
Influencer.findByIdAndUpdate({ _id }, { $pull: { companiesFavs: userId } }, options)
.then(() => {
Company.findByIdAndUpdate({ _id: id }, { $pull: { followers: _id } }, options)
.then((result) => {
res.status(200).json(result);
})
.catch(next);
})
.catch(next);
}
});

module.exports = router;

0 comments on commit a798c7e

Please sign in to comment.