From a798c7e8ae1e88bfc726ebe60c38933401a47681 Mon Sep 17 00:00:00 2001 From: Manu Pacheco Date: Mon, 7 May 2018 12:01:58 +0200 Subject: [PATCH] add follow function --- models/company.js | 1 + models/influencer.js | 2 ++ routes/api.js | 72 ++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 75 insertions(+) diff --git a/models/company.js b/models/company.js index 5c3f949e..f2ab3e2c 100644 --- a/models/company.js +++ b/models/company.js @@ -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' }, }); diff --git a/models/influencer.js b/models/influencer.js index b56f836c..2ae172ce 100644 --- a/models/influencer.js +++ b/models/influencer.js @@ -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, diff --git a/routes/api.js b/routes/api.js index aa176b5b..bab88db3 100644 --- a/routes/api.js +++ b/routes/api.js @@ -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;