diff --git a/api/migrations/20190223173124-update-notification-category-group.js b/api/migrations/20190223173124-update-notification-category-group.js new file mode 100644 index 0000000..3b69efb --- /dev/null +++ b/api/migrations/20190223173124-update-notification-category-group.js @@ -0,0 +1,55 @@ +'use strict'; + +module.exports = { + up: async (queryInterface, Sequelize) => { + await queryInterface.addColumn( + 'notifications', + 'category', { + type: Sequelize.STRING + } + ); + await queryInterface.addColumn( + 'notifications', + 'group', { + type: Sequelize.ENUM, + values: ['Info', 'Success', 'Error'], + } + ); + return queryInterface.removeColumn( + 'notifications', + 'type' + ); + /* + Add altering commands here. + Return a promise to correctly handle asynchronicity. + + Example: + return queryInterface.createTable('users', { id: Sequelize.INTEGER }); + */ + }, + + down: async (queryInterface, Sequelize) => { + await queryInterface.removeColumn( + 'notifications', + 'group' + ); + await queryInterface.addColumn( + 'notifications', + 'type', { + type: Sequelize.ENUM, + values: ['Info', 'Success', 'Error'], + } + ); + return queryInterface.removeColumn( + 'notifications', + 'category' + ); + /* + Add reverting commands here. + Return a promise to correctly handle asynchronicity. + + Example: + return queryInterface.dropTable('users'); + */ + } +}; diff --git a/api/models/notification.js b/api/models/notification.js index 66712f2..cf91bd5 100644 --- a/api/models/notification.js +++ b/api/models/notification.js @@ -10,10 +10,13 @@ module.exports = (sequelize, DataTypes) => { type: DataTypes.STRING, allowNull: false, }, - type: { + group: { type: DataTypes.ENUM, values: ['Info', 'Success', 'Error'], }, + category: { + type: DataTypes.STRING, + }, userId: { type: DataTypes.BIGINT, allowNull: false, diff --git a/api/routes/notification.js b/api/routes/notification.js index 8e7030d..f1e970d 100644 --- a/api/routes/notification.js +++ b/api/routes/notification.js @@ -41,17 +41,17 @@ router.delete('/delete/:id', [ } }); -router.delete('/delete/type/:type', [ - check('type') - .exists().withMessage('Notification type is required'), +router.delete('/delete/type/:group', [ + check('group') + .exists().withMessage('Notification group is required'), ], async (req, res) => { try { if (handleValidationErrors(req, res)) return null; - const { type } = req.params; + const { group } = req.params; const userId = req.user.id; const deletedRows = await Notification.destroy({ where: { - type, + group, userId, }, }); diff --git a/api/routes/userAuth.js b/api/routes/userAuth.js index 3cfd4f1..11217b7 100644 --- a/api/routes/userAuth.js +++ b/api/routes/userAuth.js @@ -232,7 +232,7 @@ router.get('/verify/:username/:tokenSource', async (req, res) => { if (await bcrypt.compare(tokenSource, user.activationToken)) { socket.sendMessage(user.id, 'Your account has been activated', 'Activation success'); await User.update({ activated: true }, { where: { id: user.id } }); - await notificationUtils.createNotification('Success', 'Email verification', 'Email verified successfully', user.id); + await notificationUtils.createNotification('PERSONAL', 'Success', 'Email verification', 'Email verified successfully', user.id); return res.status(200).redirect('https://code.pragyan.org'); } return res.status(400).redirect('https://code.pragyan.org'); diff --git a/api/utils/match.js b/api/utils/match.js index f77c914..ba89dc6 100644 --- a/api/utils/match.js +++ b/api/utils/match.js @@ -236,8 +236,8 @@ const updateMatchResults = async (matchId, score1, score2, interestingness) => { socket.sendMessage(match.userId1, user1Status, user1Type); socket.sendMessage(match.userId2, user2Status, user2Type); - await notificationUtils.createNotification(user1Type, user1Title, user1Status, match.userId1); - await notificationUtils.createNotification(user2Type, user2Title, user2Status, match.userId2); + await notificationUtils.createNotification('MATCH', user1Type, user1Title, user1Status, match.userId1); + await notificationUtils.createNotification('MATCH', user2Type, user2Title, user2Status, match.userId2); } } catch (err) { console.log(err); diff --git a/api/utils/notifications.js b/api/utils/notifications.js index d5683a4..6a896f9 100644 --- a/api/utils/notifications.js +++ b/api/utils/notifications.js @@ -1,9 +1,9 @@ const Notification = require('../models').notification; -const createNotification = async (type, title, content, userId) => { +const createNotification = async (category, group, title, content, userId) => { try { await Notification.create({ - type, title, content, userId, + category, group, title, content, userId, }); return true; } catch (err) { diff --git a/test/notification.js b/test/notification.js index 15a4207..98697e3 100644 --- a/test/notification.js +++ b/test/notification.js @@ -53,8 +53,9 @@ describe('Test Notification', async () => { id: index + 1, title: `notification_title_${index}`, content: `notification_content_${index}`, - type: getType(index), + group: getType(index), userId: user.id, + category: 'PERSONAL', }, }); notificationResults.push(notification); @@ -122,15 +123,15 @@ describe('Test Notification', async () => { await superAgent.post('/user/login') .set('content-type', 'application/json') .send(loginBody); - const type = ['Info', 'Success', 'Error']; + const group = ['Info', 'Success', 'Error']; const delPromises = []; for (let index = 0; index < 3; index += 1) { // eslint-disable-next-line no-await-in-loop - await superAgent.delete(`/notifications/delete/type/${type[index]}`); + await superAgent.delete(`/notifications/delete/type/${group[index]}`); const del = Notification.findAll({ where: { userId: user.id, - type: type[index], + group: group[index], }, }); delPromises.push(del);