Skip to content

Commit

Permalink
put status in notification track table outcome
Browse files Browse the repository at this point in the history
  • Loading branch information
sshugsc committed Oct 16, 2024
1 parent 569491e commit c88bfd2
Show file tree
Hide file tree
Showing 7 changed files with 74 additions and 48 deletions.
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};
5 changes: 0 additions & 5 deletions app/models/notification/notification.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,6 @@ module.exports = (sequelize, Sq) => {
key: 'id',
},
},
status: {
name: 'status',
field: 'status',
type: Sq.STRING,
},
},
{
...DEFAULT_OPTIONS,
Expand Down
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
43 changes: 17 additions & 26 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 @@ -314,38 +314,29 @@ router.route('/')
projectIdArray.push(project.project_id);
});

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

try {
await email.notifyUsers(
`Report Created: ${req.body.patientId} ${req.body.template}`,
`New report:
Ident: ${report.ident}
Created by: ${req.user.firstName} ${req.user.lastName}
Project: ${req.body.project}
Template: ${req.body.template}
Patient: ${req.body.patientId}`,
{
eventType: NOTIFICATION_EVENT.REPORT_CREATED,
templateId: report.templateId,
projectId: projectIdArray,
},
);

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}`);
}
await email.notifyUsers(
`Report Created: ${req.body.patientId} ${req.body.template}`,
`New report:
Ident: ${report.ident}
Created by: ${req.user.firstName} ${req.user.lastName}
Project: ${req.body.project}
Template: ${req.body.template}
Patient: ${req.body.patientId}`,
{
eventType: NOTIFICATION_EVENT.REPORT_CREATED,
templateId: report.templateId,
projectId: projectIdArray,
},
);

return res.status(HTTP_STATUS.CREATED).json({message: 'Report upload was successful', ident: report.ident});
} catch (error) {
Expand Down
1 change: 0 additions & 1 deletion app/routes/report/reportUser.js
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,6 @@ router.route('/')
eventType: NOTIFICATION_EVENT.USER_BOUND,
templateId: report.templateId,
projectId: reportProject.project_id,
status: 'Pending',
},
});

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

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

0 comments on commit c88bfd2

Please sign in to comment.