-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add cron job checking for late notifications
- Loading branch information
1 parent
be1581d
commit 5cb5166
Showing
4 changed files
with
112 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
express-api/src/notificationTemplates/FailedProjectNotifications.njk
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<html> | ||
<head> | ||
<title>PIMS Notifications Warning</title> | ||
</head> | ||
<body> | ||
<h2>Some PIMS notifications may not have been sent.</h2> | ||
<p>PIMS has detected that some projects have notifications in a pending state that have passed their intended send date.</p> | ||
<p>Please review the following projects for notification issues:</p> | ||
<ul> | ||
{% for project in Projects %} | ||
<li><p><b><a href={{project.Link}}>{{project.ProjectNumber}}</a></b> {{project.Name}}</p></li> | ||
{% endfor %} | ||
</ul> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
import { AppDataSource } from '@/appDataSource'; | ||
import notificationServices, { | ||
NotificationStatus, | ||
} from '@/services/notifications/notificationServices'; | ||
import { NotificationQueue } from '@/typeorm/Entities/NotificationQueue'; | ||
import { User } from '@/typeorm/Entities/User'; | ||
import logger from '@/utilities/winstonLogger'; | ||
import { IsNull, LessThan, Not } from 'typeorm'; | ||
import nunjucks from 'nunjucks'; | ||
import getConfig from '@/constants/config'; | ||
import chesServices, { EmailBody, IEmail } from '@/services/ches/chesServices'; | ||
import { PimsRequestUser } from '@/middleware/userAuthCheck'; | ||
import { Project } from '@/typeorm/Entities/Project'; | ||
import networking from '@/constants/networking'; | ||
|
||
// Retrieves overdue notifications belonging to projects | ||
const getOverdueNotifications = async (joinProjects: boolean = false) => | ||
await AppDataSource.getRepository(NotificationQueue).find({ | ||
where: { | ||
Status: NotificationStatus.Pending, | ||
SendOn: LessThan(new Date()), | ||
ProjectId: Not(IsNull()), | ||
}, | ||
relations: { | ||
Project: joinProjects, | ||
}, | ||
}); | ||
|
||
/** | ||
* Used to check for notifications in the Pending state that have past their send date. | ||
* If any notifications are found, an attempt to update their status is made. | ||
* If any are still Pending after update, an email is created and sent to the business contact email. | ||
*/ | ||
const failedEmailCheck = async () => { | ||
try { | ||
// Get all the notification records that are still pending and past their send date. | ||
const overdueNotifications = await getOverdueNotifications(); | ||
// Get system user | ||
const system = await AppDataSource.getRepository(User).findOneOrFail({ | ||
where: { Username: 'system' }, | ||
}); | ||
// Update these records | ||
await Promise.all( | ||
overdueNotifications.map((notif) => | ||
notificationServices.updateNotificationStatus(notif.Id, system), | ||
), | ||
); | ||
// Temporary interface to allow for URL to project | ||
interface ProjectWithLink extends Project { | ||
Link?: string; | ||
} | ||
// If any of those records are still Pending, send an email to business with project names. | ||
const stillOverdueNotifications = await getOverdueNotifications(true); | ||
if (stillOverdueNotifications.length > 0) { | ||
const uniqueProjects: Record<string, ProjectWithLink> = {}; | ||
stillOverdueNotifications.forEach( | ||
(notif) => | ||
(uniqueProjects[notif.ProjectId] = { | ||
...notif.Project, | ||
Link: `${networking.FRONTEND_URL}/projects/${notif.ProjectId}`, | ||
}), | ||
); | ||
const emailBody = nunjucks.render('FailedProjectNotifications.njk', { | ||
Projects: Object.values(uniqueProjects) ?? [], | ||
}); | ||
const config = getConfig(); | ||
const email: IEmail = { | ||
to: [config.contact.toEmail], | ||
cc: [], | ||
from: 'PIMS System <[email protected]>', // Made up for this purpose. | ||
bodyType: EmailBody.Html, | ||
subject: 'PIMS Notifications Warning', | ||
body: emailBody, | ||
}; | ||
const sendResult = await chesServices.sendEmailAsync(email, system as PimsRequestUser); | ||
// If the email fails to send, throw an error for logging purposes. | ||
if (sendResult == null) { | ||
throw new Error('Email was attempted but not sent. This feature could be disabled.'); | ||
} | ||
} | ||
} catch (e) { | ||
logger.error(`Error in failedEmailCheck: ${e}`); | ||
} | ||
}; | ||
|
||
export default failedEmailCheck; |