Skip to content

Commit

Permalink
Merge pull request #60 from andela/ft-fetch-user-notifications
Browse files Browse the repository at this point in the history
Fetch user notifications
  • Loading branch information
j4l13n committed Sep 19, 2019
2 parents a8fd7de + 0a98dec commit 4fc1162
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 16 deletions.
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -12,7 +12,7 @@
"create_tables": "sequelize db:migrate",
"start": "node dist/index.js",
"prod_seeds": "sequelize db:seed --seed 20190722102843-create-admin && sequelize db:seed --seed 20190723161730-create-permission",
"build": "sequelize db:migrate:undo:all && sequelize db:migrate && npm run prod_seeds && babel src --out-dir dist",
"build": "babel src --out-dir dist",
"dev": "nodemon --exec babel-node ./src/index.js"
},
"engines": {
Expand Down
15 changes: 10 additions & 5 deletions src/controllers/articleRatingControllers.js
Expand Up @@ -20,11 +20,6 @@ class ArticleRatingManager {

try {
const findArticle = await Articles.findOne({ where: { slug } });
if (!findArticle) {
return res.status(404).send({
error: 'Article not found!'
});
}
const findUser = await Users.findOne({ where: { email } });

if (findUser.id === findArticle.postedBy) {
Expand All @@ -33,6 +28,16 @@ class ArticleRatingManager {
});
}

const hasRated = await Rating.findOne({ where: { user: findUser.id } });
if (hasRated) {
await hasRated.update({
rating,
where: { user: findUser.id }
});
return res.status(200).json({
message: `Article ratings changed to ${rating}`
});
}
const saveRating = await Rating.create({
rating,
user: findUser.id,
Expand Down
20 changes: 16 additions & 4 deletions src/controllers/notificationController.js
Expand Up @@ -17,10 +17,22 @@ class NotificationManager {
if (notifications.length === 0) {
return res.status(404).json({ message: 'You are all caught up, you have zero notifications' });
}
notifications.map(async (notification) => {
await notification.update({
status: 'seen'
});
return res.status(200).json({ number: notifications.length, notifications });
}

/**
*
* @param {object} req
* @param {object} res
* @returns {object} article
*/
static async readNotification(req, res) {
const notifications = await Notifications.findOne({ where: { id: req.params.id } });
if (notifications.length === 0) {
return res.status(404).json({ message: 'You are all caught up, you have zero notifications' });
}
await notifications.update({
status: req.body.status
});
return res.status(200).json({ number: notifications.length, notifications });
}
Expand Down
5 changes: 2 additions & 3 deletions src/controllers/userControllers.js
@@ -1,5 +1,6 @@
import bcrypt from 'bcrypt';
import dotenv from 'dotenv';
import open from 'open';
import model from '../db/models/index';
import mail from '../helpers/mail';
import validations from '../helpers/validations';
Expand Down Expand Up @@ -68,9 +69,7 @@ class UserManager {
});
}
await Users.update({ isVerified: true }, { where: { id: findUser.id } });
return res.status(403).json({
message: `User with ${findUser.email} has been verified, use this link to login: https://ah-lobos-backend-swagger.herokuapp.com`,
});
return open('https://ah-lobos-frontend.herokuapp.com/login');
}
} catch (error) {
return res.status(500).json({
Expand Down
6 changes: 3 additions & 3 deletions src/helpers/notifications/newArticle.js
Expand Up @@ -23,13 +23,13 @@ class articleNotification {

const { APP_URL } = process.env;
return followers.map(async (follower) => {
const url = `${APP_URL}/api/articles/${slug}`;
const url = `${APP_URL}/articles/${slug}`;

const user = await Users.findOne({ where: { id: follower } });
const config = await userConfig.get(user.dataValues.id);

const inAppMessage = `Hello ${user.dataValues.username}, ${author.dataValues.username} has just published a new article. Click on this link ${url} to read this article`;
const emailMessage = `Hello ${user.dataValues.username}, ${author.dataValues.username} has just published a new article. Click on this link ${url} to read this article`;
const inAppMessage = `Hello ${user.dataValues.username}, ${author.dataValues.username} has just published a new article.`;
const emailMessage = `Hello ${user.dataValues.username}, ${author.dataValues.username} has just published a new article.`;

const action = 'publish';

Expand Down
1 change: 1 addition & 0 deletions src/routes/api/notifications.js
Expand Up @@ -8,5 +8,6 @@ const router = express.Router();
router.get('/', auth.checkAuthentication, notificationManager.getNotifications);
router.get('/config', auth.checkAuthentication, notificationManager.getNotificationsConfig);
router.put('/config', auth.checkAuthentication, ValidateConfig.notificationConfigSchema, notificationManager.updateConfig);
router.put('/read/:id', auth.checkAuthentication, notificationManager.readNotification);

export default router;

0 comments on commit 4fc1162

Please sign in to comment.