Skip to content

Commit

Permalink
Merge ebb0668 into f10978a
Browse files Browse the repository at this point in the history
  • Loading branch information
EmyRukundo committed Jul 4, 2019
2 parents f10978a + ebb0668 commit 910a8d3
Show file tree
Hide file tree
Showing 18 changed files with 279 additions and 201 deletions.
18 changes: 16 additions & 2 deletions src/api/controllers/articlesController.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,15 @@ import models from '../../sequelize/models';
import readTime from '../../helpers/ReadTime.helper';
import eventEmitter from '../../helpers/notifications/EventEmitter';
import findUser from '../../helpers/FindUser';
import AuthorNotifier from '../../helpers/NotifyAuthorOnArticleBlock';

const {
notifyAuthorblock, notifyAuthorUnblock
} = AuthorNotifier;

const {
Article,
User,
Article,
LikeDislike,
ReportedArticles,
BlockedArticles,
Expand Down Expand Up @@ -431,6 +435,9 @@ class articlesController {
const reporterUsername = await ReportedArticles.findOne({
where: { slug }
});
const { dataValues: { email, lastName } } = await User.findOne({
where: { id: article.authorId }
});
const username = !reporterUsername
? null
: reporterUsername.dataValues.username;
Expand All @@ -449,6 +456,7 @@ class articlesController {
{ blocked: true },
{ where: { id: responce.articleId } }
);
await notifyAuthorblock({ email, lastName });
res.status(201).send({
status: 201,
data: {
Expand All @@ -466,9 +474,15 @@ class articlesController {
*/
static async unBlockArticle(req, res) {
const { slug } = req.params;
const { id } = await Article.findOne({ where: { slug } });
const { id, authorId } = await Article.findOne({ where: { slug } });

const { dataValues: { email, lastName } } = await User.findOne({
where: { id: authorId }
});

BlockedArticles.destroy({ where: { articleId: id } }).then(async () => {
await Article.update({ blocked: false }, { where: { slug } });
await notifyAuthorUnblock({ email, lastName, slug });
res.status(200).send({
status: 200,
data: {
Expand Down
43 changes: 14 additions & 29 deletions src/api/controllers/auth.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import jwt from 'jsonwebtoken';
import dotenv from 'dotenv';
import nodemailer from 'nodemailer';
import { omit } from 'lodash';
import tokenHelper from '../../helpers/Token.helper';
import Mailhelper from '../../helpers/SendMail.helper';
import HashHelper from '../../helpers/hashHelper';
import db from '../../sequelize/models/index';
import templete from '../../helpers/emailTemplete';
import verifyTemplete from '../../helpers/emailverifiyTemplete';
import sendEmail from '../../helpers/mailer/SendAnyEmail';

const { generateToken, decodeToken } = tokenHelper;
const { User, Blacklist, Opt } = db;
Expand Down Expand Up @@ -59,14 +59,17 @@ class AuthController {
userId: newUser.id,
type: 'inapp'
});
Mailhelper.sendMail({
to: newUser.email,
names: `${newUser.firstName} ${newUser.lastName}`,
subject: 'Welcome to Authorshaven',
message: 'Thank you for choosing Authorshaven',
token
const htmlToSend = verifyTemplete.sendVerification(`${newUser.firstName} ${newUser.lastName}`, newUser.email, token);
sendEmail({ email: newUser.email }, htmlToSend, 'Welcome to Authorshaven').then(() => {
res.status(201).send({
status: 201,
data: {
message: `Reset link sent to your email <${newUser.email}>`,
email: `${newUser.email}`,
token
}
});
});

res.status(201).json({
status: 201,
message: 'We have sent an email to you to verify your account',
Expand Down Expand Up @@ -210,32 +213,14 @@ class AuthController {
const { firstName, lastName, email } = user.dataValues;
const link = `${process.env.BASE_URL}/api/auth/reset/${token}`;
const mail = {
firstName,
lastName,
link,
email
firstName, lastName, link, email
};

const transport = nodemailer.createTransport({
service: 'gmail',
auth: {
user: process.env.AUTHOSHAVEN_USER,
pass: process.env.AUTHOSHAVEN_PASS
}
});
const htmlToSend = templete.getPasswordResetTemplete(
mail.firstName,
mail.lastName,
mail.link
);
const mailOptions = {
from: 'Authors Haven',
to: `${mail.email}`,
subject: ' Password Reset',
text: 'Hello there',
html: htmlToSend
};
transport.sendMail(mailOptions, async () => {
sendEmail(mail, htmlToSend, 'Password Reset').then(() => {
res.status(201).send({
status: 201,
data: {
Expand Down
44 changes: 0 additions & 44 deletions src/helpers/MailTemplate.helper.js

This file was deleted.

40 changes: 40 additions & 0 deletions src/helpers/NotifyAuthorOnArticleBlock.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import sendEmail from './mailer/SendAnyEmail';
import Template from './emailTempleteblock';
import TemplateUnblock from './emailTempleteUnBlock';

/**
* @author EmyRukundo
* @class AuthController
* @description this class performs the whole authentication
*/
class notifyAuthor {
/**
*
* @param {Object} data - Request object
* @param {Object} res - Response object
* @returns {Object} - Response object
*/
static async notifyAuthorblock(data) {
const mail = {
lastName: data.lastName, email: data.email
};
const htmlToSend = Template.articleBlockedTemplate(mail.lastName);
await sendEmail(mail, htmlToSend, 'Notification');
}

/**
*
* @param {Object} data - Request object
* @param {Object} res - Response object
* @returns {Object} - Response object
*/
static async notifyAuthorUnblock(data) {
const link = `${process.env.BASE_URL}/api/articles/${data.slug}`;
const mailUnblock = {
lastName: data.lastName, email: data.email
};
const htmlToSendU = TemplateUnblock.articleUnBlockedTemplate(mailUnblock.lastName, link);
await sendEmail(mailUnblock, htmlToSendU, 'Congratulation');
}
}
export default notifyAuthor;
10 changes: 5 additions & 5 deletions src/helpers/emailTemplete.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,19 @@
* @class ResetPassword
* @description Authentication based class
* */
class Templete {
class Template {
/**
* Verify token middleware
* @param {String} firstname - Request
* @param {String} lastname - Response
* @param {String} token -EmailTemplete
* @param {String} token -EmailTemplate
* @returns {String} The response String
*/
static getPasswordResetTemplete(firstname, lastname, token) {
return `
<div style="background:#e5eeff;width:100%;padding:20px 0;">
<div style="max-width:760px;margin:0 auto;background:#ffffff">
<div style="background:#266cef;padding:10px;color:#ffffff;text-align:center;font-size:34px">
<div style="background:#303346;padding:10px;color:#ffffff;text-align:center;font-size:34px">
Authors Haven
</div>
<div style="padding:20px;text-align:left;">
Expand All @@ -23,7 +23,7 @@ class Templete {
You Recently requested a password reset for your Authors Haven Account, Click the the Button below
to reset it.</br>
<form action="${token}">
<button style="background:#266cef;padding:10px; outline: none; border:0; border-radius:10px; margin:5px;color:#ffffff;text-align:center;font-size:13px">Reset your Password</button>
<button style="background:#303346;padding:10px; outline: none; border:0; border-radius:10px; margin:5px;color:#ffffff;text-align:center;font-size:13px">Reset your Password</button>
</form>
</br>
If you did not request a password reset please ignore this email or reply to let us know.
Expand All @@ -45,4 +45,4 @@ class Templete {
}
}

export default Templete;
export default Template;
41 changes: 41 additions & 0 deletions src/helpers/emailTempleteUnBlock.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/**
* @class ResetPassword
* @description Authentication based class
* */
class TemplateUnblock {
/**
* Verify token middleware
* @param {String} lastname - Request
* @param {string} link - Article link
* @returns {String} The response String
*/
static articleUnBlockedTemplate(lastname, link) {
return `
<div style="background:#e5eeff;width:100%;padding:20px 0;">
<div style="max-width:760px;margin:0 auto;background:#ffffff">
<div style="background:#303346;padding:10px;color:#ffffff;text-align:center;font-size:34px">
Authors Haven
</div>
<div style="padding:20px;text-align:left;">
<p>
<h2>Dear ${lastname} </h2></br>
Congratulation!! Your article is unblocked on our site, you can visit your article here ${link} </br>
</br>
</p>
<a href="https://andela.com">Visit Andela's website</a>
</div>
<br>
<div style="padding:20px;text-align:left;">
<b>Authors Haven</b>
</div>
</div>
<div style="padding:35px 10px;text-align:center;">
Copyright, 2019<br>
Authors Haven
</div>
</div>
`;
}
}

export default TemplateUnblock;
42 changes: 42 additions & 0 deletions src/helpers/emailTempleteblock.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/**
* @class ResetPassword
* @description Authentication based class
* */
class Template {
/**
* Verify token middleware
* @param {String} lastname - Request
* @param {String} token -EmailTemplate
* @returns {String} The response String
*/
static articleBlockedTemplate(lastname) {
return `
<div style="background:#e5eeff;width:100%;padding:20px 0;">
<div style="max-width:760px;margin:0 auto;background:#ffffff">
<div style="background:#303346;padding:10px;color:#ffffff;text-align:center;font-size:34px">
Authors Haven
</div>
<div style="padding:20px;text-align:left;">
<p>
<h2>Dear ${lastname} </h2></br>
Your article is blocked on our site because it doesn't follow our terms and conditions </br>
</br>
If you feel it's just a mistake, you can contact administrator
</p>
<a href="https://andela.com">Visit Andela's website</a>
</div>
<br>
<div style="padding:20px;text-align:left;">
<b>Authors Haven</b>
</div>
</div>
<div style="padding:35px 10px;text-align:center;">
Copyright, 2019<br>
Authors Haven
</div>
</div>
`;
}
}

export default Template;
48 changes: 48 additions & 0 deletions src/helpers/emailverifiyTemplete.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/**
* @class Template
* @description Authentication based class
* */
class Template {
/**
* Verifiy Template
* @param {String} names -EmailTemplate
* @param {String} to -EmailTemplate
* @param {String} token -EmailTemplate
* @returns {String} The response String
*/
static sendVerification(names, to, token) {
return `
<div style="background:#e5eeff;width:100%;padding:20px 0;">
<div style="max-width:760px;margin:0 auto;background:#ffffff">
<div style="background:#303346;padding:10px;color:#ffffff;text-align:center;font-size:34px">
Authors Haven - Team Tesla
</div>
<div style="padding:0;">
</div>
<div style="padding:20px;text-align:left;">
<p>
Well ${names}, congratulations for choosing AuthorsHaven.
To verify that ${to} is your email, could you please click this link below to verify your AuthorsHaven's account?
<br/>
<a href="${process.env.BASE_URL}/api/auth/verify/?token=${token}">Click here to verify your account</a>
<br/>
Here there is the link below where you can visit Andela and get more information about what's Andela
</p>
<a href="https://andela.com">Visit Andela's website</a>
</div>
<br>
<div style="padding:20px;text-align:left;">
<b>Andela, Team @Tesla - Cohort 5</b>
</div>
</div>
<div style="padding:35px 10px;text-align:center;">
Copyright, 2019<br>
Andela, Team Tesla
</div>
</div>
`;
}
}

export default Template;
Loading

0 comments on commit 910a8d3

Please sign in to comment.