Skip to content

Commit

Permalink
Merge c61b25a into 77ca063
Browse files Browse the repository at this point in the history
  • Loading branch information
kevoese committed Aug 27, 2019
2 parents 77ca063 + c61b25a commit 19e5554
Show file tree
Hide file tree
Showing 7 changed files with 83 additions and 25 deletions.
2 changes: 1 addition & 1 deletion controllers/articles/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,6 @@ export default {
id: categoryId
}
});

if (!categoryExist) {
return res.status(400).send({
message: 'category does not Exist'
Expand Down Expand Up @@ -297,6 +296,7 @@ export default {
type: 'publish'
});


return res.status(201).json({
message: 'Article Created Successfully',
article
Expand Down
25 changes: 21 additions & 4 deletions controllers/users/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -226,15 +226,26 @@ export default {
}
},

getNotificationStatus: async (req, res) => {
const { user: { emailNotify, inAppNotify } } = req;
return res.status(200).send({
notificationStatus: {
emailNotify, inAppNotify
}
});
},

changeEmailNotification: async (req, res) => {
const { user } = req;
const notifyMe = user.emailNotify;
const result = await user.update({
emailNotify: !notifyMe,
});

const { emailNotify, inAppNotify } = result;
return res.status(200).send({
user: result
notificationStatus: {
emailNotify, inAppNotify
}
});
},

Expand All @@ -245,8 +256,11 @@ export default {
inAppNotify: !notifyMe,
});

const { emailNotify, inAppNotify } = result;
return res.status(200).send({
user: result
notificationStatus: {
emailNotify, inAppNotify
}
});
},

Expand All @@ -256,7 +270,10 @@ export default {
const notifications = await db.Notification.findAll({
where: {
receiverId: id,
}
},
order: [
['createdAt', 'DESC']
]
});

return res.status(200).send({
Expand Down
1 change: 1 addition & 0 deletions routes/v1/notifications.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const router = express.Router();
router.patch('/email-notify', Middleware.authenticate, Middleware.isblackListedToken, User.changeEmailNotification);
router.patch('/app-notify', Middleware.authenticate, Middleware.isblackListedToken, User.changeInAppNotification);
router.get('/', Middleware.authenticate, Middleware.isblackListedToken, User.getNotifications);
router.get('/status', Middleware.authenticate, Middleware.isblackListedToken, User.getNotificationStatus);
router.patch('/:id', Middleware.authenticate, Middleware.isblackListedToken, User.readNotification);

export default router;
38 changes: 32 additions & 6 deletions tests/functions/notification.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,14 @@ describe('Notification functions', () => {
});

const { message } = notificationContent.dataValues;
expect(message).to.equal(`${hamza.firstName} ${hamza.lastName} is following you`);
console.log(message);
const expectedMsg = {
name: `${hamza.firstName} ${hamza.lastName}`,
msg: 'is following you',
article: null,
};

expect((message)).to.equal(JSON.stringify(expectedMsg));
});

it('should create a new like notification message', async () => {
Expand All @@ -87,9 +94,13 @@ describe('Notification functions', () => {
receiverId: hamza.id
},
});

const expectedMsg = {
name: `${vincent.firstName} ${vincent.lastName}`,
msg: 'likes your article',
article: hamzaArticle.title,
};
const { message } = notificationContent.dataValues;
expect(message).to.equal(`${vincent.firstName} ${vincent.lastName} likes your article "${hamzaArticle.dataValues.title}"`);
expect((message)).to.equal(JSON.stringify(expectedMsg));
});

it('should create a new comment notification message', async () => {
Expand All @@ -106,8 +117,13 @@ describe('Notification functions', () => {
}
});

const expectedMsg = {
name: `${vincent.firstName} ${vincent.lastName}`,
msg: 'commented on your article',
article: hamzaArticle.title,
};
const { message } = notificationContent.dataValues;
expect(message).to.equal(`${vincent.firstName} ${vincent.lastName} commented on your article "${hamzaArticle.dataValues.title}"`);
expect((message)).to.equal(JSON.stringify(expectedMsg));
});

it('should create a new dislike notification message', async () => {
Expand All @@ -125,8 +141,13 @@ describe('Notification functions', () => {
}
});

const expectedMsg = {
name: `${vincent.firstName} ${vincent.lastName}`,
msg: 'dislikes your article',
article: hamzaArticle.title,
};
const { message } = notificationContent.dataValues;
expect(message).to.equal(`${vincent.firstName} ${vincent.lastName} dislikes your article "${hamzaArticle.dataValues.title}"`);
expect((message)).to.equal(JSON.stringify(expectedMsg));
});

it('should get a new publish notification message', async () => {
Expand All @@ -149,8 +170,13 @@ describe('Notification functions', () => {
}
});

const expectedMsg = {
name: `${hamza.firstName} ${hamza.lastName}`,
msg: 'published a new article titled',
article: hamzaArticle.title,
};
const { message } = notificationContent.dataValues;
expect(message).to.equal(`${hamza.firstName} ${hamza.lastName} published a new article titled "${hamzaArticle.dataValues.title}"`);
expect((message)).to.equal(JSON.stringify(expectedMsg));
});
});
});
8 changes: 4 additions & 4 deletions tests/routes/notifications.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ describe('USER NOTIFICATIONS', () => {
.patch('/api/v1/notifications/email-notify')
.set('x-access-token', token);
expect(res.statusCode).to.equal(200);
expect(res.body.user.emailNotify).to.equal(false);
expect(res.body.notificationStatus.emailNotify).to.equal(false);
});

it('should return 200 and true if user opt in to email notification', async () => {
Expand All @@ -54,7 +54,7 @@ describe('USER NOTIFICATIONS', () => {
.patch('/api/v1/notifications/email-notify')
.set('x-access-token', token);
expect(res.statusCode).to.equal(200);
expect(res.body.user.emailNotify).to.equal(true);
expect(res.body.notificationStatus.emailNotify).to.equal(true);
});

it('should return all the users notifications if any', async () => {
Expand Down Expand Up @@ -152,7 +152,7 @@ describe('USER NOTIFICATIONS', () => {
.patch('/api/v1/notifications/app-notify')
.set('x-access-token', token);
expect(res.statusCode).to.equal(200);
expect(res.body.user.inAppNotify).to.equal(false);
expect(res.body.notificationStatus.inAppNotify).to.equal(false);
});

it('should return 200 and true if user opt in to an app notification', async () => {
Expand All @@ -171,6 +171,6 @@ describe('USER NOTIFICATIONS', () => {
.patch('/api/v1/notifications/app-notify')
.set('x-access-token', token);
expect(res.statusCode).to.equal(200);
expect(res.body.user.inAppNotify).to.equal(true);
expect(res.body.notificationStatus.inAppNotify).to.equal(true);
});
});
33 changes: 24 additions & 9 deletions utils/notifications/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ const pusher = new Pusher({
useTLS: true
});

const pushNotification = (users) => {
users.forEach((user) => {
pusher.trigger('notifications', `event-${user.id}`, 'You have a notification');
const pushNotification = (userIds) => {
userIds.forEach((id) => {
pusher.trigger('notifications', `event-${id}`, 'You have a new notification');
});
};

Expand Down Expand Up @@ -53,7 +53,11 @@ const followNotification = async ({ userId, followedUserId }) => {
});

const { emailNotify, inAppNotify, email } = followedUser;
const message = `${user.firstName} ${user.lastName} is following you`;
const message = JSON.stringify({
name: `${user.firstName} ${user.lastName}`,
msg: 'is following you',
article: null
});
const emailMsg = `<strong>${user.firstName} ${user.lastName}</strong> is following you`;
const link = `/profile/${user.username}`;

Expand Down Expand Up @@ -135,31 +139,42 @@ const articleNotification = async ({ userId, articleId, type }) => {
const link = `/articles/${slug}`;

if (type === 'publish') {
const publishMsg = `${author.firstName} ${author.lastName} published a new article titled "${title}"`;
const publishEmailMsg = `${author.firstName} ${author.lastName} published a new article titled <strong>${title}</strong>`;
const publishMsg = JSON.stringify({
name: `${author.firstName} ${author.lastName}`,
msg: 'published a new article titled',
article: title
});
const publishEmailMsg = `<strong>${author.firstName} ${author.lastName}</strong> published a new article titled <strong>${title}</strong>`;
await bulkNotify({
senderId: userId,
message: publishMsg,
emailMsg: publishEmailMsg,
link
});
} else {
if (user.id === author.id) {
return;
}
let msgType;
if (type === 'like') msgType = 'likes';
else if (type === 'dislike') msgType = 'dislikes';
else msgType = 'commented on';

const message = `${user.firstName} ${user.lastName} ${msgType} your article "${title}"`;
const message = JSON.stringify({
name: `${user.firstName} ${user.lastName}`,
msg: `${msgType} your article`,
article: title
});
const emailMsg = `${user.firstName} ${user.lastName} ${msgType} your article <strong>${title}</strong>`;

if (emailNotify) await sendEmailNotification(email, emailMsg, link);
if (emailNotify) sendEmailNotification(email, emailMsg, link);


if (inAppNotify) {
await createNotificationMsg({
senderId: userId, receiverId: authorId, message, link
});
await pushNotification([userId]);
pushNotification([authorId]);
}
}
};
Expand Down
1 change: 0 additions & 1 deletion validators/articles/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ export default {
};

const data = { ...req.body, ...req.files };

try {
await validatorInstance.validateAll(data, rules, messages);
next();
Expand Down

0 comments on commit 19e5554

Please sign in to comment.