-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(notification): implement user notification
- create notification modules - setup in-app, email and push notifications - write endpoints to manage notifications [Delivers #167190447]
- Loading branch information
Showing
22 changed files
with
791 additions
and
15 deletions.
There are no files selected for viewing
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
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 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,76 @@ | ||
import sequelize from 'sequelize'; | ||
import models from '../db/models'; | ||
import helpers from '../helpers'; | ||
|
||
const { Op } = sequelize; | ||
const { successStat } = helpers; | ||
|
||
/** | ||
* @Module NotificationController | ||
* @description Controlls comments made by users | ||
*/ | ||
class NotificationController { | ||
/** | ||
* @static | ||
* @param {*} req Request object | ||
* @param {*} res Response object | ||
* @returns {Object} server response | ||
* @memberof NotificationController | ||
*/ | ||
static async getNotifications(req, res) { | ||
const { user: { id } } = req; | ||
const user = await models.User.findByPk(id); | ||
return successStat(res, 200, 'notifications', await user.getNotifications()); | ||
} | ||
|
||
/** | ||
* @static | ||
* @param {*} req Request object | ||
* @param {*} res Response object | ||
* @returns {Object} server response | ||
* @memberof NotificationController | ||
*/ | ||
static async markAsRead(req, res) { | ||
const { user: { id }, params, } = req; | ||
await models.Notification.update({ | ||
read: true, | ||
}, { | ||
where: { | ||
[Op.and]: [{ id: params.id }, { userId: id }] | ||
} | ||
}); | ||
return successStat(res, 200, 'message', 'marked as read'); | ||
} | ||
|
||
/** | ||
* @static | ||
* @param {*} req Request object | ||
* @param {*} res Response object | ||
* @returns {Object} server response | ||
* @memberof NotificationController | ||
*/ | ||
static async markAllAsRead(req, res) { | ||
const { id } = req.user; | ||
await models.Notification.update({ read: true }, { | ||
where: { | ||
userId: id, | ||
} | ||
}); | ||
return successStat(res, 200, 'message', 'all notifications marked as read'); | ||
} | ||
|
||
/** | ||
* @static | ||
* @param {*} req Request object | ||
* @param {*} res Response object | ||
* @returns {Object} server response | ||
* @memberof NotificationController | ||
*/ | ||
static async emailSubscribe(req, res) { | ||
const { user: { id }, method } = req; | ||
await models.User.update({ newPostEmailSub: method === 'PATCH', }, { where: { id } }); | ||
return successStat(res, 200, 'message', `email notification ${method === 'PATCH' ? 'enabled' : 'disabled'}`); | ||
} | ||
} | ||
|
||
export default NotificationController; |
45 changes: 45 additions & 0 deletions
45
server/db/migrations/20190806103204-create-notification.js
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,45 @@ | ||
module.exports = { | ||
up: (queryInterface, Sequelize) => queryInterface.createTable('Notifications', { | ||
id: { | ||
allowNull: false, | ||
autoIncrement: true, | ||
primaryKey: true, | ||
type: Sequelize.INTEGER | ||
}, | ||
userId: { | ||
type: Sequelize.INTEGER, | ||
allowNull: false, | ||
references: { | ||
model: 'Users', | ||
key: 'id', | ||
}, | ||
onDelete: 'CASCADE' | ||
}, | ||
resourceType: { | ||
type: Sequelize.STRING, | ||
allowNull: false, | ||
}, | ||
resourceId: { | ||
type: Sequelize.STRING, | ||
allowNull: false, | ||
}, | ||
message: { | ||
type: Sequelize.STRING, | ||
allowNull: false, | ||
}, | ||
read: { | ||
type: Sequelize.BOOLEAN, | ||
allowNull: false, | ||
default: false, | ||
}, | ||
createdAt: { | ||
allowNull: false, | ||
type: Sequelize.DATE | ||
}, | ||
updatedAt: { | ||
allowNull: false, | ||
type: Sequelize.DATE | ||
} | ||
}), | ||
down: queryInterface => queryInterface.dropTable('Notifications') | ||
}; |
8 changes: 8 additions & 0 deletions
8
server/db/migrations/20190806161700-post-email-sub-column-user-table.js
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,8 @@ | ||
module.exports = { | ||
up: (queryInterface, Sequelize) => queryInterface.addColumn('Users', 'newPostEmailSub', { | ||
type: Sequelize.BOOLEAN, | ||
allowNull: false, | ||
defaultValue: true | ||
}), | ||
down: queryInterface => queryInterface.removeColumn('Users', 'newPostEmailSub') | ||
}; |
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,16 @@ | ||
module.exports = (sequelize, DataTypes) => { | ||
const Notification = sequelize.define('Notification', { | ||
userId: DataTypes.INTEGER, | ||
resourceType: DataTypes.STRING, | ||
resourceId: DataTypes.STRING, | ||
message: DataTypes.STRING, | ||
read: DataTypes.BOOLEAN, | ||
}, {}); | ||
Notification.associate = (models) => { | ||
Notification.belongsTo(models.User, { | ||
as: 'userNotification', | ||
foreignKey: 'userId', | ||
}); | ||
}; | ||
return Notification; | ||
}; |
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
Oops, something went wrong.