Skip to content

Commit

Permalink
Merge fa1c4f7 into e12bb48
Browse files Browse the repository at this point in the history
  • Loading branch information
timi-codes committed Jul 30, 2019
2 parents e12bb48 + fa1c4f7 commit 9f702c2
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 17 deletions.
6 changes: 3 additions & 3 deletions src/controllers/user.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ class UserController {
receiver: email,
subject: 'Welcome to ErrorSwag',
templateName: 'confirm_account',
confirm_account_url: `${protocol}://${req.get(
buttonUrl: `${protocol}://${req.get(
'host'
)}/api/v1/auth/verify/${token}`
});
Expand Down Expand Up @@ -113,7 +113,7 @@ class UserController {
receiver: email,
subject: 'Welcome to ErrorSwag',
templateName: 'confirm_account',
confirm_account_url: `${protocol}://${req.get(
buttonUrl: `${protocol}://${req.get(
'host'
)}/api/v1/auth/verify/${token}`
});
Expand Down Expand Up @@ -297,7 +297,7 @@ class UserController {
const [user, created] = followedUser;

if (created) {
await onFollowNotification(req.currentUser, followeeId);
await onFollowNotification(req, followeeId);

return responseGenerator.sendSuccess(
res,
Expand Down
9 changes: 5 additions & 4 deletions src/helpers/mailer.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,21 @@ sgMail.setApiKey(secret);

const templates = {
password_reset: 'd-3fa45363fe634b418146f72cbe03942b',
confirm_account: 'd-84376bbc49e24647ab77848bcb926eb5'
confirm_account: 'd-84376bbc49e24647ab77848bcb926eb5',
new_notification: 'd-2a54c072315e4b45a215c74344f0cb4d'
};

const mailer = data => {
const { name, receiver, subject, templateName } = data;
const { name, receiver, subject, templateName, buttonUrl } = data;
const msg = {
to: receiver,
from: 'noreply@kifaru-ah.com',
subject,
templateId: templates[templateName],
dynamic_template_data: {
name,
confirm_account_url: data.confirm_account_url,
reset_password_url: data.reset_password_url
subject,
button_url: buttonUrl
}
};

Expand Down
58 changes: 54 additions & 4 deletions src/helpers/notifications.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import Pusher from 'pusher';
import dotenv from 'dotenv';
import BaseRepository from '../repository/base.repository';
import db from '../database/models';
import mailer from './mailer';

dotenv.config();

Expand Down Expand Up @@ -31,12 +32,31 @@ const createInAppNotification = async ({ receiverId, payload }) => {
};

// ON FOLLOW NOTICATION
const onFollowNotification = async (sender, followeeId) => {
const receiverId = followeeId;
const onFollowNotification = async (req, followeeId) => {
const { protocol, currentUser: sender } = req;

const receiver = await BaseRepository.findOneByField(db.User, {
id: followeeId
});

const payload = {
follower: sender.username,
type: 'new_follower'
};

const { id: receiverId, username, email, emailNotify } = receiver;

// SEND EMAIL TO THE FOLLOWED USER
if (emailNotify) {
mailer({
name: username,
receiver: email,
subject: `<b>${sender.username}</b> just followed you.`,
templateName: 'new_notification',
buttonUrl: `${protocol}://${req.get('host')}/profile/${sender.username}`
});
}

await createInAppNotification({
receiverId,
payload
Expand All @@ -45,7 +65,8 @@ const onFollowNotification = async (sender, followeeId) => {
};

// ON COMMENT NOTICATION
const onCommentNotification = async (sender, articleId) => {
const onCommentNotification = async (req, articleId) => {
const { protocol, currentUser: sender } = req;
const article = await BaseRepository.findAndInclude({
model: db.Article,
options: { id: articleId },
Expand All @@ -60,6 +81,19 @@ const onCommentNotification = async (sender, articleId) => {
type: 'new_comment'
};

// SEND EMAIL TO THE AUTHOR
if (article[0]['author.emailNotify']) {
mailer({
name: article[0]['author.username'],
receiver: article[0]['author.email'],
subject: `<b>${sender.username}</b> just commented on <b>${article[0].title}</b>`,
templateName: 'new_notification',
buttonUrl: `${protocol}://${req.get('host')}/article/${
article[0].slug
}/#commentId`
});
}

await createInAppNotification({
receiverId: article[0]['author.id'],
payload
Expand All @@ -69,7 +103,7 @@ const onCommentNotification = async (sender, articleId) => {
};

// ON PUBLISH NOTICATION
const onPublishArticleNotification = async ({ userId, articleId }) => {
const onPublishArticleNotification = async (req, { userId, articleId }) => {
const followers = await BaseRepository.findAndInclude({
model: db.Follower,
options: { followeeId: userId },
Expand All @@ -84,6 +118,8 @@ const onPublishArticleNotification = async ({ userId, articleId }) => {
alias: 'author'
});

const followerEmails = followers.filter(follower => follower.emailNotify);

const followerIds = followers.map(follower => follower.followerId);
const payload = {
author: article[0]['author.username'],
Expand All @@ -92,6 +128,20 @@ const onPublishArticleNotification = async ({ userId, articleId }) => {
type: 'new_article'
};

// SEND EMAIL TO ALL FOLLOWERS
const { protocol } = req || {};
followerEmails.forEach(email => {
mailer({
name: article[0]['author.username'],
receiver: email,
subject: `<b>${article[0]['author.username']}</b> just published <b>${
article[0].title
}</b>`,
templateName: 'new_notification',
buttonUrl: `${protocol}://${req.get('host')}/article/${article[0].slug}`
});
});

const data = followerIds.map(id => ({
receiverId: id,
payload
Expand Down
21 changes: 15 additions & 6 deletions src/test/notification.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,10 @@ describe('Notification helper functions', () => {
type: 'new_follower'
};

await onFollowNotification(follower, followee.id);
await onFollowNotification(
{ protocol: null, currentUser: follower },
followee.id
);
const notification = await BaseRepository.findAll(db.Notification, {
receiverId: followee.id
});
Expand All @@ -82,7 +85,10 @@ describe('Notification helper functions', () => {
type: 'new_comment'
};

await onCommentNotification(commenter, article.id);
await onCommentNotification(
{ protocol: null, currentUser: commenter },
article.id
);

const notification = await BaseRepository.findAll(db.Notification, {
receiverId: author.id
Expand Down Expand Up @@ -113,10 +119,13 @@ describe('Notification helper functions', () => {
type: 'new_article'
};

await onPublishArticleNotification({
userId: author.id,
articleId: article.id
});
await onPublishArticleNotification(
{ protocol: null },
{
userId: author.id,
articleId: article.id
}
);

const notification = await BaseRepository.findAll(db.Notification, {});

Expand Down

0 comments on commit 9f702c2

Please sign in to comment.