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 2449 add updates to notifications table when notifications are created completed #385

Merged
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
7 changes: 4 additions & 3 deletions app/libs/email.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,15 +57,16 @@ const getEmailList = async (triggers) => {
const notifyUsers = async (subject, text, triggers) => {
const emailList = await getEmailList(triggers);

emailList.forEach(({toEmail, notifId, eventType}) => {
for (const emailItem of emailList) {
const {toEmail, notifId, eventType} = emailItem;
const mailOptions = {
from: `${email}${domain}`,
to: toEmail,
subject,
text,
};
addJobToEmailQueue({mailOptions, notifId, eventType});
});
await addJobToEmailQueue({mailOptions, notifId, eventType});
}
};

module.exports = {sendEmail, getEmailList, notifyUsers};
7 changes: 7 additions & 0 deletions app/models/notification/notificationTrack.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,13 @@ module.exports = (sequelize, Sq) => {
},
allowNull: false,
},
jobId: {
name: 'jobId',
field: 'job_id',
unique: false,
type: Sq.INTEGER,
allowNull: false,
},
recipient: {
name: 'recipient',
field: 'recipient',
Expand Down
53 changes: 43 additions & 10 deletions app/queue.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,20 @@ const EMAIL_REMOVE_CONFIG = {
const addJobToEmailQueue = async (data) => {
if (emailQueue) {
logger.info('adding email to queue: ', data);
return emailQueue.add('job', data, EMAIL_REMOVE_CONFIG);
const job = await emailQueue.add('job', data, EMAIL_REMOVE_CONFIG);
try {
await db.models.notificationTrack.create({
notificationId: data.notifId,
jobId: job.id,
recipient: data.mailOptions.to,
reason: data.eventType,
outcome: 'created',
});
} catch (error) {
logger.error(`Unable to create notification track ${error}`);
throw new Error(error);
}
return job;
}
return null;
};
Expand Down Expand Up @@ -84,10 +97,13 @@ const setUpEmailWorker = (emailJobProcessor) => {

const onEmailWorkderCompleted = async (job) => {
try {
await db.models.notificationTrack.create({
notificationId: job.data.notifId,
recipient: job.data.mailOptions.to,
reason: job.data.eventType,
const notification = await db.models.notificationTrack.findOne({
where: {
notificationId: job.data.notifId,
jobId: job.id,
},
});
await notification.update({
outcome: 'completed',
});
} catch (error) {
Expand All @@ -98,11 +114,14 @@ const onEmailWorkderCompleted = async (job) => {

const onEmailWorkderFailed = async (job) => {
try {
await db.models.notificationTrack.create({
notificationId: job.data.notifId,
recipient: job.data.mailOptions.to,
reason: job.data.eventType,
outcome: 'Failed',
const notification = await db.models.notificationTrack.findOne({
where: {
notificationId: job.data.notifId,
jobId: job.id,
},
});
await notification.update({
outcome: 'failed',
});
} catch (error) {
logger.error(`Unable to create notification track ${error}`);
Expand All @@ -124,6 +143,20 @@ const emailProcessor = async (job) => {
});

try {
try {
const notification = await db.models.notificationTrack.findOne({
where: {
notificationId: job.data.notifId,
jobId: job.id,
},
});
await notification.update({
outcome: 'processing',
});
} catch (error) {
logger.error(`Unable to add process notification track ${error}`);
throw new Error(error);
}
await transporter.sendMail(job.data.mailOptions);
} catch (err) {
logger.error(JSON.stringify(job.data.mailOptions));
Expand Down
13 changes: 11 additions & 2 deletions app/routes/report/report.js
Original file line number Diff line number Diff line change
Expand Up @@ -175,8 +175,8 @@ router.route('/')
...((keyVariant && matchingThreshold) ? {
'$genomicAlterationsIdentified.geneVariant$': {
[Op.in]: literal(
`(SELECT "geneVariant"
FROM (SELECT "geneVariant", word_similarity('${keyVariant}', "geneVariant") FROM reports_summary_genomic_alterations_identified) AS subquery
`(SELECT "geneVariant"
FROM (SELECT "geneVariant", word_similarity('${keyVariant}', "geneVariant") FROM reports_summary_genomic_alterations_identified) AS subquery
WHERE word_similarity >= ${matchingThreshold})`,
),
},
Expand Down Expand Up @@ -363,6 +363,15 @@ router.route('/')
projectIdArray.push(project.project_id);
});

await db.models.notification.findOrCreate({
where: {
userId: req.user.id,
eventType: NOTIFICATION_EVENT.REPORT_CREATED,
templateId: report.templateId,
projectId: report.projects[0].project_id,
},
});

await email.notifyUsers(
`Report Created: ${req.body.patientId} ${req.body.template}`,
`New report:
Expand Down
14 changes: 14 additions & 0 deletions app/routes/report/reportUser.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,17 @@ router.route('/')
addedBy_id: req.user.id,
});

const report = await db.models.report.findOne({where: {id: req.report.id}});
const reportProject = await db.models.reportProject.findOne({where: {report_id: req.report.id}});
const notif = await db.models.notification.findOrCreate({
where: {
userId: req.user.id,
eventType: NOTIFICATION_EVENT.USER_BOUND,
templateId: report.templateId,
projectId: reportProject.project_id,
},
});

// Try sending email
try {
await email.notifyUsers(
Expand All @@ -140,8 +151,11 @@ router.route('/')
templateId: req.report.templateId,
},
);

await notif[0].update({status: 'Success'}, {userId: req.user.id});
logger.info('Email sent successfully');
} catch (error) {
await notif[0].update({status: 'Unsuccess'}, {userId: req.user.id});
logger.error(`Email not sent successfully: ${error}`);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
const TABLE = 'notifications_tracks';

module.exports = {
up: async (queryInterface, Sq) => {
return queryInterface.sequelize.transaction(async (transaction) => {
await queryInterface.addColumn(
TABLE,
'job_id',
{
type: Sq.INTEGER,
},
{transaction},
);
});
},

down: async () => {
throw new Error('Not Implemented!');
},
};
11 changes: 11 additions & 0 deletions test/routes/report/reportUser.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ beforeAll(async () => {
describe('/reports/{REPORTID}/user', () => {
let user;
let report;
let project;
let reportProject;
let createUser;
let userReportBinding;

Expand All @@ -77,6 +79,13 @@ describe('/reports/{REPORTID}/user', () => {
createdBy_id: user.id,
});

project = await db.models.project.create({name: 'test'});

reportProject = await db.models.reportProject.create({
reportId: report.id,
project_id: project.id,
});

createUser = await db.models.user.create({
username: uuidv4(),
type: 'bcgsc',
Expand Down Expand Up @@ -316,6 +325,8 @@ describe('/reports/{REPORTID}/user', () => {
// delete newly created report and all of it's components
// indirectly by hard deleting newly created patient
await db.models.report.destroy({where: {ident: report.ident}});
await db.models.project.destroy({where: {ident: project.ident}});
await db.models.reportProject.destroy({where: {id: reportProject.id}});
});
});

Expand Down
Loading