bridge-node-server/node_server/utils/adminNotifier.js
Martin Donnelly 57bd6c8e6a init
2018-06-24 21:15:03 +01:00

143 lines
3.8 KiB
JavaScript

/**
* Support utilities for notifying the admin of various cases
*/
'use strict';
const Q = require('q');
const _ = require('lodash');
const mailer = require(global.pathPrefix + 'mailer.js');
const templates = require(global.pathPrefix + '../utils/templates.js');
var debug = require('debug')('utils:adminNotifier');
/**
* Exports from this module
*/
module.exports = {
notifyIdentityCheckIssue: notifyIdentityCheckIssue,
notifyCredits: notifyCredits
};
/**
* Address that all notifications should be sent to
*/
const NOTIFICATION_EMAIL = 'admin@comcarde.com';
/**
* Table of credit limits to send emails
*/
const SERVICES_TABLE = {
tracesmart: {
limit: 100,
lastReport: Number.MAX_VALUE,
reportStep: 10
},
txtlocal: {
limit: 100,
lastReport: Number.MAX_VALUE,
reportStep: 10
}
};
/**
* Notifies admin that there is an identity check issue to investigate
*
* @param {Object} client - the client object (updated with the latest identify results)
*
*@return {Promise} - promise for the result of sending the email
*/
function notifyIdentityCheckIssue(client) {
const caller = 'notifyIdentityCheckIssue';
//
// Get the email parameters
//
var params = {
ClientID: client.ClientID,
ProfileURL: client.KYC[0].ProfileURL
};
//
// Render the email
//
var htmlEmail = templates.render('adminNotifier/identity_check.pug', params);
var subject = 'Manual Identity Check Needed';
//
// Pass it to the mailer to send (wrapped in a Q.nfcall to turn it into
// a promise).
// When the promise completes, call any callback defined
//
return Q.nfcall(mailer.sendEmail, '', NOTIFICATION_EMAIL, subject, htmlEmail, caller);
}
/**
* Used to notify the admin if credits for a service are getting low.
* The meaning of "getting low" is defined in here, so callers just call this
* every time, and this system sends or does not send an email as neccessary.
*
* @param {String} name - the name of the service the credits are for
* @param {Number} value - the number of credits remaining
*
* @return {Promise} - promise for the result of sending the notification
*/
function notifyCredits(name, value) {
debug('notifyCredits: ', name, value);
let service = SERVICES_TABLE[name];
if (_.isUndefined(service)) {
/**
* Use a default service that will always report - should get fixed quickly!
*/
service = {
limit: Number.MAX_VALUE,
lastReport: Number.MAX_VALUE,
reportStep: 0
};
}
/**
* Check if we need to send a notification
*/
let nextReport = service.lastReport - service.reportStep;
if (
value > service.limit || // Above threshold
(service.lastReport > value && value > nextReport)// Between reporting steps
) {
/**
* Don't need to send a report
*/
return Q.resolve();
}
/**
* DO need to send a report
*/
const caller = 'notifyCredits';
//
// Get the email parameters
//
var params = {
Service: name,
CreditsRemaining: value,
CreditsLimit: service.limit
};
//
// Render the email
//
var htmlEmail = templates.render('adminNotifier/credits_low.pug', params);
var subject = '[' + name + '] Low Credits!';
//
// Pass it to the mailer to send (wrapped in a Q.nfcall to turn it into
// a promise).
// When the promise completes, call any callback defined
//
return Q.nfcall(mailer.sendEmail, '', NOTIFICATION_EMAIL, subject, htmlEmail, caller)
.then(() => {
/**
* Successfully sent the email, so updated the value we last reported at
*/
service.lastReport = value;
});
}