Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fetch user notifications #60

Merged
merged 1 commit into from Sep 19, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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;