Skip to content

Commit b01a7fe

Browse files
committed
set up email notifications for user self and admins
1 parent 8a5097f commit b01a7fe

7 files changed

Lines changed: 50 additions & 30 deletions

File tree

app/config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
const nconf = require('nconf');
22
const {merge} = require('lodash');
3+
require('dotenv').config();
34

45
const ENV = process.env.NODE_ENV || 'local';
56

app/libs/email.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,15 @@ const getEmailList = async (triggers) => {
3535
const emailList = [];
3636
for (const notif of notifs) {
3737
if (notif.user) {
38-
if (!emailList.includes(notif.user.email)
38+
if (!emailList.some((el) => {return el.toEmail === notif.user.email;})
3939
&& notif.user.email.endsWith(domain)
4040
&& notif.user.allowNotifications) {
4141
emailList.push({toEmail: notif.user.email, notifId: notif.id, eventType: notif.eventType});
4242
}
43-
} else if (notif.userGroup) {
43+
}
44+
if (notif.userGroup) {
4445
for (const groupUser of notif.userGroup.users) {
45-
if (!emailList.includes(groupUser.email)
46+
if (!emailList.some((el) => {return el.toEmail === groupUser.email;})
4647
&& groupUser.email.endsWith(domain)
4748
&& groupUser.allowNotifications) {
4849
emailList.push({toEmail: groupUser.email, notifId: notif.id, eventType: notif.eventType});

app/queue.js

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,13 @@ const addJobToEmailQueue = async (data) => {
4444
logger.info('adding email to queue: ', data);
4545
const job = await emailQueue.add('job', data, EMAIL_REMOVE_CONFIG);
4646
try {
47-
await db.models.notificationTrack.create({
47+
await db.models.notificationTrack.findOrCreate({where: {
4848
notificationId: data.notifId,
4949
jobId: job.id,
5050
recipient: data.mailOptions.to,
5151
reason: data.eventType,
5252
outcome: 'created',
53-
});
53+
}});
5454
} catch (error) {
5555
logger.error(`Unable to create notification track ${error}`);
5656
throw new Error(error);
@@ -152,20 +152,6 @@ const emailProcessor = async (job) => {
152152
});
153153

154154
try {
155-
try {
156-
const notification = await db.models.notificationTrack.findOne({
157-
where: {
158-
notificationId: job.data.notifId,
159-
jobId: job.id,
160-
},
161-
});
162-
await notification.update({
163-
outcome: 'processing',
164-
});
165-
} catch (error) {
166-
logger.error(`Unable to add process notification track ${error}`);
167-
throw new Error(error);
168-
}
169155
await transporter.sendMail(job.data.mailOptions);
170156
} catch (err) {
171157
logger.error(JSON.stringify(job.data.mailOptions));

app/routes/report/report.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -332,13 +332,15 @@ router.route('/')
332332
report.projects.forEach((project) => {
333333
projectIdArray.push(project.project_id);
334334
});
335+
const userGroup = await db.models.userGroup.findOne({where: {name: 'admin'}});
335336

336-
await db.models.notification.findOrCreate({
337+
const notify = await db.models.notification.findOrCreate({
337338
where: {
338339
userId: req.user.id,
339340
eventType: NOTIFICATION_EVENT.REPORT_CREATED,
340341
templateId: report.templateId,
341342
projectId: report.projects[0].project_id,
343+
userGroupId: userGroup.id,
342344
},
343345
});
344346

@@ -351,9 +353,7 @@ router.route('/')
351353
Template: ${req.body.template}
352354
Patient: ${req.body.patientId}`,
353355
{
354-
eventType: NOTIFICATION_EVENT.REPORT_CREATED,
355-
templateId: report.templateId,
356-
projectId: projectIdArray,
356+
id: notify[0].id,
357357
},
358358
);
359359

app/routes/report/reportUser.js

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -132,30 +132,44 @@ router.route('/')
132132

133133
const report = await db.models.report.findOne({where: {id: req.report.id}});
134134
const reportProject = await db.models.reportProject.findOne({where: {report_id: req.report.id}});
135-
const notif = await db.models.notification.findOrCreate({
135+
const project = await db.models.project.findOne({where: {id: reportProject.project_id}});
136+
const template = await db.models.template.findOne({where: {id: report.templateId}});
137+
const userGroup = await db.models.userGroup.findOne({where: {name: 'admin'}});
138+
139+
const notifyReq = await db.models.notification.findOrCreate({
136140
where: {
137141
userId: req.user.id,
138142
eventType: NOTIFICATION_EVENT.USER_BOUND,
139143
templateId: report.templateId,
140144
projectId: reportProject.project_id,
145+
userGroupId: userGroup.id,
146+
},
147+
});
148+
149+
const notifyBinding = await db.models.notification.findOrCreate({
150+
where: {
151+
userId: bindUser.id,
152+
eventType: NOTIFICATION_EVENT.USER_BOUND,
153+
templateId: report.templateId,
154+
projectId: reportProject.project_id,
155+
userGroupId: userGroup.id,
141156
},
142157
});
143158

144159
// Try sending email
145160
try {
146161
await email.notifyUsers(
147162
`${bindUser.firstName} ${bindUser.lastName} has been bound to a report`,
148-
`User ${bindUser.firstName} ${bindUser.lastName} has been bound to report ${req.report.ident} as ${role}`,
163+
`User ${bindUser.firstName} ${bindUser.lastName} has been bound to report ${req.report.ident} as ${role}.
164+
Report Type: ${template.name}
165+
Patient Id: ${report.patientId}
166+
Project: ${project.name}`,
149167
{
150-
eventType: NOTIFICATION_EVENT.USER_BOUND,
151-
templateId: req.report.templateId,
168+
id: [notifyReq[0].id, notifyBinding[0].id],
152169
},
153170
);
154-
155-
await notif[0].update({status: 'Success'}, {userId: req.user.id});
156171
logger.info('Email sent successfully');
157172
} catch (error) {
158-
await notif[0].update({status: 'Unsuccess'}, {userId: req.user.id});
159173
logger.error(`Email not sent successfully: ${error}`);
160174
}
161175

package-lock.json

Lines changed: 17 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
"bullmq": "^5.1.11",
2222
"compression": "^1.7.4",
2323
"cors": "~2.8.5",
24+
"dotenv": "^16.5.0",
2425
"exceljs": "^4.3.0",
2526
"express": "^4.18.1",
2627
"express-fileupload": "^1.4.0",

0 commit comments

Comments
 (0)