-
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.
- Loading branch information
Showing
17 changed files
with
790 additions
and
13 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,124 @@ | ||
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 | ||
}); | ||
|
||
if (notification.length > 0) { | ||
return responseGenerator.sendSuccess(res, 200, notification); | ||
} | ||
|
||
return responseGenerator.sendError(res, 404, 'You have no notifications'); | ||
} 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 notification = await BaseRepository.findOneByField(Notification, { | ||
id: notificationId, | ||
receiverId | ||
}); | ||
|
||
if (notification) { | ||
const notificationRead = notification.read; | ||
if (notificationRead === true) { | ||
return responseGenerator.sendError( | ||
res, | ||
200, | ||
'Notification has been read' | ||
); | ||
} | ||
const markRead = await BaseRepository.update( | ||
Notification, | ||
{ read }, | ||
{ id: notificationId, receiverId } | ||
); | ||
|
||
if (markRead) { | ||
return responseGenerator.sendError( | ||
res, | ||
200, | ||
'Notification has been marked as read' | ||
); | ||
} | ||
} | ||
return responseGenerator.sendError( | ||
res, | ||
404, | ||
'Notification does not exist' | ||
); | ||
} 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.sendError( | ||
res, | ||
200, | ||
'Email notifications has been enabled' | ||
); | ||
} | ||
|
||
return responseGenerator.sendError( | ||
res, | ||
200, | ||
'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
38 changes: 38 additions & 0 deletions
38
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,38 @@ | ||
module.exports = { | ||
up: (queryInterface, Sequelize) => { | ||
return queryInterface.createTable('Notifications', { | ||
id: { | ||
allowNull: false, | ||
autoIncrement: true, | ||
primaryKey: true, | ||
type: Sequelize.INTEGER | ||
}, | ||
message: { | ||
type: Sequelize.TEXT | ||
}, | ||
read: { | ||
type: Sequelize.BOOLEAN, | ||
defaultValue: false | ||
}, | ||
receiverId: { | ||
type: Sequelize.INTEGER | ||
}, | ||
link: { | ||
type: Sequelize.STRING | ||
}, | ||
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'); | ||
} | ||
}; |
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,32 @@ | ||
module.exports = (sequelize, DataTypes) => { | ||
const Notification = sequelize.define( | ||
'Notification', | ||
{ | ||
message: DataTypes.TEXT, | ||
read: { | ||
type: DataTypes.BOOLEAN, | ||
defaultValue: false | ||
}, | ||
receiverId: DataTypes.INTEGER, | ||
link: DataTypes.STRING, | ||
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.