-
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.
feat(notification): User can get in app notification on being followed
- create database for in-app notification - Setup pusher to trigger notification to the listeners(frontend) - Implement algorithm for notification when a user is followed [Delivers #167323728 #167347852 #166840929 #167347716]
- Loading branch information
1 parent
eab73eb
commit 39cad00
Showing
20 changed files
with
1,174 additions
and
316 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
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,108 @@ | ||
import BaseRepository from '../repository/base.repository'; | ||
import responseGenerator from '../helpers/responseGenerator'; | ||
import db from '../database/models'; | ||
|
||
const { User, Notification } = db; | ||
|
||
/** | ||
* @class NotificationController | ||
*/ | ||
class NotificationController { | ||
/** | ||
* Get all notifications | ||
* @async | ||
* @param {Object} req - Object of the HTTP request | ||
* @param {Object} res - Object of the HTTP response | ||
* @returns {json} Returns json Object | ||
* @static | ||
*/ | ||
static async getNotifications(req, res) { | ||
const { id: recieverId } = req.currentUser; | ||
try { | ||
const notification = await BaseRepository.findAll(Notification, { | ||
recieverId | ||
}); | ||
|
||
return responseGenerator.sendSuccess(res, 200, notification); | ||
} catch (error) { | ||
return responseGenerator.sendError(res, 500, error.message); | ||
} | ||
} | ||
|
||
/** | ||
* Mark if the notification was read | ||
* @async | ||
* @param {Object} req - Object of the HTTP request | ||
* @param {Object} res - Object of the HTTP response | ||
* @returns {json} Returns json Object | ||
* @static | ||
*/ | ||
static async readNotification(req, res) { | ||
const read = true; | ||
const { notificationId } = req.params; | ||
const { id: receiverId } = req.currentUser; | ||
try { | ||
const markRead = await BaseRepository.update( | ||
Notification, | ||
{ read }, | ||
{ id: notificationId, receiverId } | ||
); | ||
|
||
if (markRead[0] > 0) { | ||
return responseGenerator.sendSuccess( | ||
res, | ||
200, | ||
null, | ||
'Notification has been read' | ||
); | ||
} | ||
return responseGenerator.sendError( | ||
res, | ||
404, | ||
'Failed to update notification read status' | ||
); | ||
} catch (error) { | ||
return responseGenerator.sendError(res, 500, error.message); | ||
} | ||
} | ||
|
||
/** | ||
* Mark if the notification was read | ||
* @async | ||
* @param {Object} req - Object of the HTTP request | ||
* @param {Object} res - Object of the HTTP response | ||
* @returns {json} Returns json Object | ||
* @static | ||
*/ | ||
static async toggleNotification(req, res) { | ||
const { emailNotify } = req.body; | ||
const { id } = req.currentUser; | ||
try { | ||
const emailNotification = await BaseRepository.update( | ||
User, | ||
{ emailNotify }, | ||
{ id } | ||
); | ||
|
||
if (emailNotification && emailNotify) { | ||
return responseGenerator.sendSuccess( | ||
res, | ||
200, | ||
null, | ||
'Email notifications has been enabled' | ||
); | ||
} | ||
|
||
return responseGenerator.sendSuccess( | ||
res, | ||
200, | ||
null, | ||
'Email notifications has been disabled' | ||
); | ||
} catch (error) { | ||
return responseGenerator.sendError(res, 500, error.message); | ||
} | ||
} | ||
} | ||
|
||
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
35 changes: 35 additions & 0 deletions
35
src/database/migrations/20190717203358-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,35 @@ | ||
module.exports = { | ||
up: (queryInterface, Sequelize) => { | ||
return queryInterface.createTable('Notifications', { | ||
id: { | ||
allowNull: false, | ||
autoIncrement: true, | ||
primaryKey: true, | ||
type: Sequelize.INTEGER | ||
}, | ||
payload: { | ||
type: Sequelize.JSON | ||
}, | ||
read: { | ||
type: Sequelize.BOOLEAN, | ||
defaultValue: false | ||
}, | ||
receiverId: { | ||
type: Sequelize.INTEGER | ||
}, | ||
createdAt: { | ||
allowNull: false, | ||
defaultValue: new Date(), | ||
type: Sequelize.DATE | ||
}, | ||
updatedAt: { | ||
allowNull: false, | ||
defaultValue: new Date(), | ||
type: Sequelize.DATE | ||
} | ||
}); | ||
}, | ||
down: (queryInterface /* , Sequelize */) => { | ||
return queryInterface.dropTable('Notifications'); | ||
} | ||
}; |
12 changes: 12 additions & 0 deletions
12
src/database/migrations/20190718124525-add-emailnotify-column-to-user.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,12 @@ | ||
module.exports = { | ||
up: (queryInterface, Sequelize) => { | ||
return queryInterface.addColumn('Users', 'emailNotify', { | ||
type: Sequelize.BOOLEAN, | ||
defaultValue: false | ||
}); | ||
}, | ||
|
||
down: (queryInterface /* , Sequelize */) => { | ||
return queryInterface.removeColumn('Users', 'emailNotify'); | ||
} | ||
}; |
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,31 @@ | ||
module.exports = (sequelize, DataTypes) => { | ||
const Notification = sequelize.define( | ||
'Notification', | ||
{ | ||
payload: DataTypes.JSON, | ||
read: { | ||
type: DataTypes.BOOLEAN, | ||
defaultValue: false | ||
}, | ||
receiverId: DataTypes.INTEGER, | ||
createdAt: { | ||
allowNull: false, | ||
defaultValue: new Date(), | ||
type: DataTypes.DATE | ||
}, | ||
updatedAt: { | ||
allowNull: false, | ||
defaultValue: new Date(), | ||
type: DataTypes.DATE | ||
} | ||
}, | ||
{} | ||
); | ||
Notification.associate = models => { | ||
Notification.belongsTo(models.User, { | ||
as: 'notifications', | ||
foreignKey: 'receiverId' | ||
}); | ||
}; | ||
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
Oops, something went wrong.