Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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');
*/
}
};
5 changes: 4 additions & 1 deletion api/models/notification.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
10 changes: 5 additions & 5 deletions api/routes/notification.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
});
Expand Down
2 changes: 1 addition & 1 deletion api/routes/userAuth.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
4 changes: 2 additions & 2 deletions api/utils/match.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions api/utils/notifications.js
Original file line number Diff line number Diff line change
@@ -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) {
Expand Down
9 changes: 5 additions & 4 deletions test/notification.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down