Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature/DEVSU-834-add-email-when-user-assigned #269

Closed
Closed
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
10 changes: 10 additions & 0 deletions app/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ if (ENV === 'production') {
}

const DEFAULT_TEST_USER = 'ipr-bamboo-admin';
const DEFAULT_EMAIL_ADDRESS = 'rpletz';
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let's make this 'dat' or 'ipr', so it targets the team.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually what happens if there's just no default email address?


const DEFAULTS = {
env: ENV,
Expand All @@ -46,6 +47,9 @@ const DEFAULTS = {
testing: {
username: DEFAULT_TEST_USER,
},
email: {
email: DEFAULT_EMAIL_ADDRESS,
},
log: {
level: DEFAULT_LOG_LEVEL,
},
Expand Down Expand Up @@ -129,6 +133,12 @@ const CONFIG = nconf
'testing.username': {
alias: 'testing:username',
},
'email.email': {
alias: 'email.email',
},
'email.password': {
alias: 'email.password',
},
'log.level': {
alias: 'log:level',
},
Expand Down
3 changes: 3 additions & 0 deletions app/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ module.exports = {
msi: 'msi',
tmb: 'tmburMutationBurden',
},
NOTIFICATION_EVENT: {
USER_BOUND: 'userBound',
},
KB_PIVOT_COLUMN: 'variantType',
GENE_LINKED_VARIANT_MODELS: [
'expressionVariants',
Expand Down
90 changes: 90 additions & 0 deletions app/libs/email.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
const nodemailer = require('nodemailer');
const CONFIG = require('../config');
const db = require('../models');

const {email, password} = CONFIG.get('email');

const sendEmail = (subject, text, toEmail) => {
const transporter = nodemailer.createTransport({
host: 'webmail.bcgsc.ca',
auth: {
user: email,
pass: password,
},
tls: {
rejectUnauthorized: false,
},
});

const mailOptions = {
from: `${email}@bcgsc.ca`,
to: toEmail,
subject,
text,
};

transporter.sendMail(mailOptions, (error, info) => {
if (error) {
console.log(error);
} else {
console.log(`Email sent: ${info.response}`);
}
});
};

const getEmailList = async (triggers) => {
const notifs = await db.models.notification.scope('extended').findAll({
where: triggers,
});

const emailList = [];
for (const notif of notifs) {
if (notif.user) {
if (!emailList.includes(notif.user.email)) {
emailList.push(notif.user.email);
}
} else if (notif.userGroup) {
for (const groupUser of notif.userGroup.users) {
if (!emailList.includes(groupUser.email)) {
emailList.push(groupUser.email);
}
}
}
}

return emailList;
};

const notifyUsers = async (subject, text, triggers) => {
const emailList = await getEmailList(triggers);

const transporter = nodemailer.createTransport({
host: 'webmail.bcgsc.ca',
auth: {
user: email,
pass: password,
},
tls: {
rejectUnauthorized: false,
},
});

emailList.forEach((toEmail) => {
const mailOptions = {
from: `${email}@bcgsc.ca`,
to: toEmail,
subject,
text,
};

transporter.sendMail(mailOptions, (error, info) => {
if (error) {
console.log(error);
} else {
console.log(`Email sent: ${info.response}`);
}
});
});
};

module.exports = {sendEmail, getEmailList, notifyUsers};
8 changes: 8 additions & 0 deletions app/models/notification/notification.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,14 @@ module.exports = (sequelize, Sq) => {
{model: sequelize.models.project.scope('minimal'), as: 'project'},
],
},
extended: {
include: [
{model: sequelize.models.user, as: 'user'},
{model: sequelize.models.userGroup, as: 'userGroup', include: {model: sequelize.models.user, as: 'users', through: {attributes: []}}},
{model: sequelize.models.template, as: 'template'},
{model: sequelize.models.project, as: 'project'},
],
},
},
},
);
Expand Down
12 changes: 9 additions & 3 deletions app/routes/notification/notification.js
Original file line number Diff line number Diff line change
Expand Up @@ -164,13 +164,19 @@ router.route('/')
}

try {
const newnotif = await db.models.notification.create({
projectId: req.body.project_id, userId: req.body.user_id ? req.body.user_id : null, userGroupId: req.body.user_group_id ? req.body.user_group_id : null, eventType: req.body.event_type, templateId: req.body.template_id,
const newnotif = await db.models.notification.findOrCreate({
where: {
projectId: req.body.project_id,
userId: req.body.user_id ? req.body.user_id : null,
userGroupId: req.body.user_group_id ? req.body.user_group_id : null,
eventType: req.body.event_type,
templateId: req.body.template_id,
},
});

// Load new notif with associations
const result = await db.models.notification.scope('public').findOne({
where: {id: newnotif.id},
where: {id: newnotif[0].id},
});

return res.status(HTTP_STATUS.CREATED).json(result);
Expand Down
18 changes: 17 additions & 1 deletion app/routes/report/reportUser.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ const router = express.Router({mergeParams: true});

const schemaGenerator = require('../../schemas/schemaGenerator');
const validateAgainstSchema = require('../../libs/validateAgainstSchema');
const {REPORT_CREATE_BASE_URI} = require('../../constants');
const email = require('../../libs/email');
const {REPORT_CREATE_BASE_URI, NOTIFICATION_EVENT} = require('../../constants');
const {REPORT_EXCLUDE} = require('../../schemas/exclude');

// Generate create schema
Expand Down Expand Up @@ -129,6 +130,21 @@ router.route('/')
addedBy_id: req.user.id,
});

// Try sending email
try {
await email.notifyUsers(
`${bindUser.firstName} ${bindUser.lastName} has been bound to a report`,
`User ${bindUser.firstName} ${bindUser.lastName} has been bound to report ${req.report.ident}`,
{
eventType: NOTIFICATION_EVENT.USER_BOUND,
templateId: req.report.templateId,
},
);
logger.info('Email sent successfully');
} catch (error) {
logger.error(`Email not sent successfully: ${error}`);
}

await req.report.reload();
return res.status(HTTP_STATUS.CREATED).json(req.report.view('public'));
} catch (error) {
Expand Down
53 changes: 53 additions & 0 deletions emaildebug/emailtest.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
const nodemailer = require('nodemailer');
const Queue = require('bull');

const emailQueue = new Queue('emails');

const email = 'rpletz';
const password = 'password';

const transporter = nodemailer.createTransport({
host: 'webmail.bcgsc.ca',
auth: {
user: email,
pass: password,
},
tls: {
rejectUnauthorized: false,
},
pool: true,
});

const mail = {
to: `${email}@bcgsc.ca`,
subject: 'subject',
body: 'body',
};

emailQueue.process(async (job) => {
const {to, subject, body} = job.data;
const mailOptions = {
from: `${email}@bcgsc.ca`,
to,
subject,
text: body,
};
await transporter.sendMail(mailOptions, (error, info) => {
if (error) {
console.log(error);
} else {
console.log(`Email sent: ${info.response}`);
}
});
});

emailQueue.add(mail);
emailQueue.add(mail);
emailQueue.add(mail);
emailQueue.add(mail);
emailQueue.add(mail);
emailQueue.add(mail);
emailQueue.add(mail);
emailQueue.add(mail);
emailQueue.add(mail);
emailQueue.add(mail);
Loading
Loading