Skip to content

Commit

Permalink
Merge branch 'develop' into feature/DEVSU-2457-update-kbmatches-multi…
Browse files Browse the repository at this point in the history
…ple-variants-per-statement
  • Loading branch information
bnguyen-bcgsc authored Oct 21, 2024
2 parents b4be7ff + a664414 commit b254bee
Show file tree
Hide file tree
Showing 8 changed files with 125 additions and 17 deletions.
17 changes: 15 additions & 2 deletions app/api/graphkb.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ const graphkbAutocomplete = async (targetType, graphkbToken, keyword = null) =>
if (targetType === 'context') {
query.target = 'Vocabulary';
query.queryType = 'ancestors';
query.filters = {name: 'therapeutic efficacy'};
query.filters = {name: 'therapeutic indicator'};
} else {
if (targetType === 'evidenceLevel') {
query.target = 'EvidenceLevel';
Expand All @@ -49,7 +49,7 @@ const graphkbAutocomplete = async (targetType, graphkbToken, keyword = null) =>
}
}

return request({
const res = await request({
url: `${uri}/query`,
method: 'POST',
body: JSON.stringify(query),
Expand All @@ -59,6 +59,19 @@ const graphkbAutocomplete = async (targetType, graphkbToken, keyword = null) =>
'Content-Type': 'application/json',
},
});

// DEVSU-2344; Filtering results for context route
// Needs to be done after the query since cannot be combined with ancestors as a subquery
if (targetType === 'context') {
res.result = res.result.filter((term) => {
return [
'resistance', 'sensitivity', 'toxicity',
].includes(term.displayName);
});
res.metadata.records = res.result.length;
}

return res;
};

/* Get IPR evidence level descriptions from GraphKB
Expand Down
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 @@ -372,6 +372,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

0 comments on commit b254bee

Please sign in to comment.