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

Add adminIsNotified to sites and aggregate emails #283

Open
wants to merge 1 commit into
base: devel
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
15 changes: 15 additions & 0 deletions migrations/042-add-site-is-notified-field.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
let db = require('../src/db').sequelize;

module.exports = {
up: function() {

try {
return db.query(`
ALTER TABLE sites ADD adminIsNotified DATETIME NULL DEFAULT NULL AFTER areaId;
`);
} catch(e) {
return true;
}

}
}
44 changes: 31 additions & 13 deletions src/cron/send_site_issues_notifications.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,10 @@ module.exports = {
// for each site
for (let i=0; i < shouldHaveEndedButAreNot.length; i++) {
let site = shouldHaveEndedButAreNot[i];
if (!notificationsToBeSent[ site.id ]) notificationsToBeSent[ site.id ] = { site, messages: [] };
notificationsToBeSent[ site.id ].messages.push(`Site ${ site.title } (${ site.domain }) has an endDate in the past but projectHasEnded is not set.`);
if (!site.adminIsNotified || site.adminIsNotified.getTime() < Date.now() - 23 * 60 * 60 * 1000) {
if (!notificationsToBeSent[ site.id ]) notificationsToBeSent[ site.id ] = { site, messages: [] };
notificationsToBeSent[ site.id ].messages.push(`Site ${ site.title } (${ site.domain }) has an endDate in the past but projectHasEnded is not set.`);
}
}

// sites that have ended but are not anonymized
Expand All @@ -42,22 +44,38 @@ module.exports = {
// for each site
for (let i=0; i < endedButNotAnonymized.length; i++) {
let site = endedButNotAnonymized[i];
if (!notificationsToBeSent[ site.id ]) notificationsToBeSent[ site.id ] = { site, messages: [] };
notificationsToBeSent[ site.id ].messages.push(`Project ${ site.title } (${ site.domain }) has ended but is not yet anonymized.`);
if (!site.adminIsNotified || site.adminIsNotified.getTime() < Date.now() - 23 * 60 * 60 * 1000) {
if (!notificationsToBeSent[ site.id ]) notificationsToBeSent[ site.id ] = { site, messages: [] };
notificationsToBeSent[ site.id ].messages.push(`Project ${ site.title } (${ site.domain }) has ended but is not yet anonymized.`);
}
}

// send notifications
// aggregate notifications to the same address
let aggregatedNotificationsToBeSent = [];
Object.keys(notificationsToBeSent).forEach(id => {
let target = notificationsToBeSent[ id ];
let data = {
from: target.site.config.notifications.fromAddress,
to: target.site.config.notifications.siteadminAddress,
subject: 'Sites with issues',
template: target.messages.join('\r\n'),
};
Notifications.sendMessage({ site: target.site, data });
let toAddress = target.site.config.notifications.siteadminAddress || target.site.config.notifications.projectmanagerAddress;
if ( aggregatedNotificationsToBeSent[ toAddress ] ) {
aggregatedNotificationsToBeSent[ toAddress ].data.template += '<br/>\r\n' + target.messages.join('<br/>\r\n');
} else {
aggregatedNotificationsToBeSent[ toAddress ] = {
site: target.site,
data: {
from: target.site.config.notifications.fromAddress,
to: toAddress,
subject: 'Sites with issues',
template: target.messages.join('<br/>\r\n'),
}
};
}
target.site.update({ adminIsNotified: new Date() })
});


// send notifications
Object.values(aggregatedNotificationsToBeSent).forEach(target => {
Notifications.sendMessage({ site: target.site, data: target.data });
})

return next();

} catch (err) {
Expand Down
8 changes: 7 additions & 1 deletion src/models/Site.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,13 @@ module.exports = function (db, sequelize, DataTypes) {
areaId: {
type: DataTypes.INTEGER,
allowNull: true,
}
},

adminIsNotified: {
type: DataTypes.DATE,
allowNull: true,
default: null,
},

}, {

Expand Down