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 2 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
5 changes: 5 additions & 0 deletions app/models/notification/notification.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,11 @@ module.exports = (sequelize, Sq) => {
key: 'id',
},
},
status: {
name: 'status',
field: 'status',
type: Sq.STRING,
},
},
{
...DEFAULT_OPTIONS,
Expand Down
40 changes: 29 additions & 11 deletions app/routes/report/report.js
Original file line number Diff line number Diff line change
Expand Up @@ -314,20 +314,38 @@ router.route('/')
projectIdArray.push(project.project_id);
});

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}`,
{
const notif = await db.models.notification.findOrCreate({
where: {
userId: req.user.id,
eventType: NOTIFICATION_EVENT.REPORT_CREATED,
templateId: report.templateId,
projectId: projectIdArray,
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}`);
}

return res.status(HTTP_STATUS.CREATED).json({message: 'Report upload was successful', ident: report.ident});
} catch (error) {
Expand Down
15 changes: 15 additions & 0 deletions app/routes/report/reportUser.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,18 @@ 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,
status: 'Pending',
},
});

// Try sending email
try {
await email.notifyUsers(
Expand All @@ -140,8 +152,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';

module.exports = {
up: async (queryInterface, Sq) => {
return queryInterface.sequelize.transaction(async (transaction) => {
await queryInterface.addColumn(
TABLE,
'status',
{
type: Sq.STRING,
},
{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