Skip to content

Commit

Permalink
Merge dcaa248 into 67c9ddf
Browse files Browse the repository at this point in the history
  • Loading branch information
timi-codes committed Jul 21, 2019
2 parents 67c9ddf + dcaa248 commit 2eb8e8d
Show file tree
Hide file tree
Showing 17 changed files with 790 additions and 13 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
"passport-google-oauth20": "^2.0.0",
"pg": "^7.11.0",
"pg-hstore": "^2.3.3",
"pusher": "^2.2.1",
"sequelize": "^5.8.12",
"sequelize-cli": "^5.5.0",
"sinon-chai": "^3.3.0",
Expand Down
124 changes: 124 additions & 0 deletions src/controllers/notification.controller.js
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;
5 changes: 4 additions & 1 deletion src/controllers/user.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ import utility from '../helpers/utils';
import db from '../database/models';
import Pagination from '../helpers/pagination';
import mailer from '../helpers/mailer';
import NotificationHelper from '../helpers/notifications';

const { jwtSigner, verifyPassword } = utility;

const { onFollowNotification } = NotificationHelper;
/**
* @class UserController
*/
Expand Down Expand Up @@ -289,6 +290,8 @@ class UserController {
const [user, created] = followedUser;

if (created) {
await onFollowNotification({ followerId, followeeId });

return responseGenerator.sendSuccess(
res,
200,
Expand Down
4 changes: 4 additions & 0 deletions src/database/migrations/20190702165504-create-user.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ module.exports = {
values: ['unverified', 'active', 'inactive'],
defaultValue: 'unverified'
},
emailNotify: {
type: Sequelize.BOOLEAN,
defaultValue: false
},
createdAt: {
allowNull: false,
defaultValue: new Date(),
Expand Down
38 changes: 38 additions & 0 deletions src/database/migrations/20190717203358-create-notification.js
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');
}
};
8 changes: 4 additions & 4 deletions src/database/models/article.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,13 @@ export default (sequelize, DataTypes) => {
},
{}
);

Article.associate = models => {
Article.belongsTo(models.User, {
through: 'Articles',
foreignKey: 'authorId'
foreignKey: 'authorId',
as: 'author'
});
};
Article.associate = models => {

Article.belongsToMany(models.User, {
through: 'Bookmarks',
foreignKey: 'articleId',
Expand Down
2 changes: 0 additions & 2 deletions src/database/models/follower.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,6 @@ module.exports = (sequelize, DataTypes) => {
foreignKey: 'followerId',
as: 'follower'
});
};
Follower.associate = models => {
Follower.belongsTo(models.User, {
foreignKey: 'followeeId',
as: 'followee'
Expand Down
32 changes: 32 additions & 0 deletions src/database/models/notification.js
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;
};
21 changes: 18 additions & 3 deletions src/database/models/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ module.exports = (sequelize, DataTypes) => {
type: DataTypes.ENUM,
values: ['unverified', 'active', 'inactive']
},
emailNotify: {
type: DataTypes.BOOLEAN,
defaultValue: false
},
createdAt: {
allowNull: false,
defaultValue: new Date(),
Expand Down Expand Up @@ -50,25 +54,36 @@ module.exports = (sequelize, DataTypes) => {
as: 'follower',
foreignKey: 'followerId'
});
};
User.associate = models => {
User.belongsToMany(models.User, {
through: 'Followers',
as: 'followee',
foreignKey: 'followeeId'
});

};
User.associate = models => {
User.hasMany(models.Rating, {
foreignKey: 'userId',
as: 'rating'
});
};

User.associate = models => {
User.hasMany(models.Article, {
foreignKey: 'authorId',
as: 'article'
});
User.belongsToMany(models.Article, {
through: 'Bookmarks',
foreignKey: 'userId',
as: 'articleId'
});
};

User.associate = models => {
User.hasMany(models.Notification, {
as: 'notifications',
foreignKey: 'receiverId'
});
};
return User;
};
Loading

0 comments on commit 2eb8e8d

Please sign in to comment.