-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#167313410 Users should be able to receive notifications (#35)
* updated develop * User should get notifications * feature(notifications): User should be able to get notifications [Finishes #167313410] * feature(notifications):Remove unnecessary files [Finishes #167313410] * feature(notifications):Remove all unnecessary files [Finishes #167313410] * feature(notifications):Remove nodemailer files [Finishes #167313410]
- Loading branch information
1 parent
9f9f8fa
commit f239c55
Showing
29 changed files
with
643 additions
and
14 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import models from '../models/index'; | ||
|
||
const { user, Favorites, Article } = models; | ||
|
||
/** | ||
* @description A collection of controller methods for handling favorited articles | ||
* @class ArticleController | ||
*/ | ||
class FavoritesController { | ||
/** | ||
* @description controller method for favoriting an article | ||
* @static | ||
* @param {object} req Request object | ||
* @param {object} res Response object | ||
* @returns {Object} a response object | ||
*/ | ||
static async createOrRemoveFavorite(req, res) { | ||
try { | ||
const userDetails = await user.findOne({ where: { email: req.auth.email } }); | ||
const userId = userDetails.id; | ||
const { articleId } = req.params; | ||
|
||
const foundArticle = await Article.findOne({ where: { id: articleId } }); | ||
if (!foundArticle) return res.status(404).json({ error: 'Article not found' }); | ||
|
||
await Favorites.findOrCreate({ | ||
where: { articleId, userId }, | ||
attributes: ['id', 'articleId', 'userId'] | ||
}).spread(async (favorite, created) => { | ||
if (created) { | ||
return res.status(200).json({ | ||
message: 'Article favorited successfully' | ||
}); | ||
} | ||
await favorite.destroy({ force: true }); | ||
return res.status(200).json({ message: 'Favorite removed successfully' }); | ||
}); | ||
} catch (error) { | ||
return (error); | ||
} | ||
} | ||
} | ||
export default FavoritesController; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import notificationHelper from '../helpers/notification.helper'; | ||
|
||
|
||
/** | ||
* @description Contains method to allow users opt in and out of notifications | ||
* @export | ||
* @class NotificationController | ||
*/ | ||
class NotificationController { | ||
/** | ||
* | ||
* @description Method to opt in and out of email and push notication | ||
* @static | ||
* @param {object} req client request | ||
* @param {object} res server response | ||
* @returns {Object} server response object | ||
* @param {Function} next passes control to the next middleware | ||
* @memberof NotificationController | ||
*/ | ||
static async notification(req, res, next) { | ||
const { emailOrInApp } = req.params; | ||
|
||
switch (emailOrInApp) { | ||
case 'email': | ||
notificationHelper(req, res, next, 'subscribed'); | ||
break; | ||
case 'inApp': | ||
notificationHelper(req, res, next, 'inAppNotification'); | ||
break; | ||
default: | ||
return res.status(404).json({ error: 'route does not exist' }); | ||
} | ||
} | ||
} | ||
export default NotificationController; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import models from '../models/index'; | ||
|
||
|
||
export default async (req, res, next, userColumn) => { | ||
const userDetails = await models.user.findOne({ where: { email: req.auth.email } }); | ||
const { id } = userDetails; | ||
try { | ||
const user = await models.user.findOne({ where: { id }, attributes: ['id', 'email', userColumn] }); | ||
|
||
// This toggles the bolean value | ||
const newNotificationStatus = !user.dataValues[userColumn]; | ||
|
||
const updatedUser = await models.user.update( | ||
{ | ||
[userColumn]: newNotificationStatus, | ||
}, | ||
{ | ||
where: { id }, | ||
returning: true | ||
} | ||
); | ||
|
||
if (userColumn === 'subscribed') { | ||
const { subscribed } = updatedUser[1][0].dataValues; | ||
if (subscribed === newNotificationStatus) { | ||
const message = subscribed === true ? 'You have successfully subscribed to our email notifications' : 'You will no longer receive email notifications from us'; | ||
return res.status(200).json({ message, statusCode: 200 }); | ||
} | ||
} | ||
if (userColumn === 'inAppNotification') { | ||
const { inAppNotification } = updatedUser[1][0].dataValues; | ||
if (inAppNotification === newNotificationStatus) { | ||
const message = inAppNotification === true ? 'You have successfully subscribed to in app notifications' : 'You will no longer receive in-app notifications from us'; | ||
return res.status(200).json({ message, statusCode: 200 }); | ||
} | ||
} | ||
} catch (error) { | ||
return next(error); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import Joi from 'joi'; | ||
import Util from '../../helpers/util'; | ||
|
||
const util = new Util(); | ||
|
||
export default (req, res, next) => { | ||
const { articleId } = req.params; | ||
|
||
const schema = { | ||
articleId: Joi.number().positive().required() | ||
}; | ||
const { error } = Joi.validate({ | ||
articleId | ||
}, schema); | ||
|
||
if (!error) return next(); | ||
const errorMessageFromJoi = error.details[0].message; | ||
switch (errorMessageFromJoi) { | ||
case '"articleId" must be a number': | ||
util.setError(400, 'articleId must be a non negative integer'); | ||
return util.send(res); | ||
default: | ||
util.setError(400, errorMessageFromJoi); | ||
return util.send(res); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
'use strict'; | ||
module.exports = { | ||
up: (queryInterface, Sequelize) => { | ||
return queryInterface.createTable('AppNotifications', { | ||
id: { | ||
allowNull: false, | ||
autoIncrement: true, | ||
primaryKey: true, | ||
type: Sequelize.INTEGER | ||
}, | ||
articleSlug: { | ||
type: Sequelize.STRING | ||
}, | ||
receiverId: { | ||
type: Sequelize.INTEGER | ||
}, | ||
category: { | ||
type: Sequelize.STRING | ||
}, | ||
read: { | ||
type: Sequelize.BOOLEAN | ||
}, | ||
message: { | ||
type: Sequelize.STRING | ||
}, | ||
createdAt: { | ||
allowNull: false, | ||
type: Sequelize.DATE | ||
}, | ||
updatedAt: { | ||
allowNull: false, | ||
type: Sequelize.DATE | ||
} | ||
}); | ||
}, | ||
down: (queryInterface, Sequelize) => { | ||
return queryInterface.dropTable('AppNotifications'); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
'use strict'; | ||
module.exports = { | ||
up: (queryInterface, Sequelize) => { | ||
return queryInterface.createTable('Favorites', { | ||
id: { | ||
allowNull: false, | ||
autoIncrement: true, | ||
primaryKey: true, | ||
type: Sequelize.INTEGER | ||
}, | ||
articleId: { | ||
type: Sequelize.INTEGER | ||
}, | ||
userId: { | ||
type: Sequelize.INTEGER | ||
}, | ||
createdAt: { | ||
allowNull: false, | ||
type: Sequelize.DATE | ||
}, | ||
updatedAt: { | ||
allowNull: false, | ||
type: Sequelize.DATE | ||
} | ||
}); | ||
}, | ||
down: (queryInterface, Sequelize) => { | ||
return queryInterface.dropTable('Favorites'); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
'use strict'; | ||
module.exports = (sequelize, DataTypes) => { | ||
const AppNotification = sequelize.define('AppNotification', { | ||
articleSlug: DataTypes.STRING, | ||
receiverId: DataTypes.INTEGER, | ||
category: DataTypes.STRING, | ||
read: DataTypes.BOOLEAN, | ||
message: DataTypes.STRING | ||
}, {}); | ||
AppNotification.associate = function(models) { | ||
// associations can be defined here | ||
}; | ||
return AppNotification; | ||
}; |
Oops, something went wrong.