mark emails as sent/fail
This commit is contained in:
parent
b83d220fa2
commit
897b39f8cd
|
@ -38,6 +38,7 @@ function EmailService(opts) {
|
||||||
this.storage = opts.storage;
|
this.storage = opts.storage;
|
||||||
this.lock = opts.lock;
|
this.lock = opts.lock;
|
||||||
this.mailer = opts.mailer || nodemailer.createTransport(opts.email);
|
this.mailer = opts.mailer || nodemailer.createTransport(opts.email);
|
||||||
|
this.subjectPrefix = opts.email.subjectPrefix || '[Wallet service]';
|
||||||
this.from = opts.email.from;
|
this.from = opts.email.from;
|
||||||
|
|
||||||
$.checkState(this.mailer);
|
$.checkState(this.mailer);
|
||||||
|
@ -95,6 +96,7 @@ EmailService.prototype._getDataForTemplate = function(notification, cb) {
|
||||||
var self = this;
|
var self = this;
|
||||||
|
|
||||||
var data = _.cloneDeep(notification.data);
|
var data = _.cloneDeep(notification.data);
|
||||||
|
data.subjectPrefix = _.trim(self.subjectPrefix) + ' ';
|
||||||
self.storage.fetchWallet(notification.walletId, function(err, wallet) {
|
self.storage.fetchWallet(notification.walletId, function(err, wallet) {
|
||||||
if (err) return cb(err);
|
if (err) return cb(err);
|
||||||
data.walletId = wallet.id;
|
data.walletId = wallet.id;
|
||||||
|
@ -175,7 +177,14 @@ EmailService.prototype.sendEmail = function(notification, cb) {
|
||||||
},
|
},
|
||||||
function(emails, next) {
|
function(emails, next) {
|
||||||
async.each(emails, function(email, next) {
|
async.each(emails, function(email, next) {
|
||||||
self._send(email, next);
|
self._send(email, function(err) {
|
||||||
|
if (err) {
|
||||||
|
email.setFail();
|
||||||
|
} else {
|
||||||
|
email.setSent();
|
||||||
|
}
|
||||||
|
self.storage.storeEmail(email, next);
|
||||||
|
});
|
||||||
}, function(err) {
|
}, function(err) {
|
||||||
return next();
|
return next();
|
||||||
});
|
});
|
||||||
|
|
|
@ -23,7 +23,7 @@ Email.create = function(opts) {
|
||||||
x.body = opts.body;
|
x.body = opts.body;
|
||||||
x.status = 'pending';
|
x.status = 'pending';
|
||||||
x.attempts = 0;
|
x.attempts = 0;
|
||||||
x.sentOn = null;
|
x.lastAttemptOn = null;
|
||||||
return x;
|
return x;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -40,9 +40,23 @@ Email.fromObj = function(obj) {
|
||||||
x.body = obj.body;
|
x.body = obj.body;
|
||||||
x.status = obj.status;
|
x.status = obj.status;
|
||||||
x.attempts = obj.attempts;
|
x.attempts = obj.attempts;
|
||||||
x.sentOn = obj.sentOn;
|
x.lastAttemptOn = obj.lastAttemptOn;
|
||||||
return x;
|
return x;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
Email.prototype._logAttempt = function(result) {
|
||||||
|
this.attempts++;
|
||||||
|
this.lastAttemptOn = Math.floor(Date.now() / 1000);
|
||||||
|
this.status = result;
|
||||||
|
};
|
||||||
|
|
||||||
|
Email.prototype.setSent = function() {
|
||||||
|
this._logAttempt('sent');
|
||||||
|
};
|
||||||
|
|
||||||
|
Email.prototype.setFail = function() {
|
||||||
|
this._logAttempt('fail');
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
module.exports = Email;
|
module.exports = Email;
|
||||||
|
|
|
@ -82,6 +82,7 @@ WalletService.initialize = function(opts, cb) {
|
||||||
};
|
};
|
||||||
|
|
||||||
function initEmailService(cb) {
|
function initEmailService(cb) {
|
||||||
|
if (!opts.mailer && !opts.email) return cb();
|
||||||
emailService = new EmailService({
|
emailService = new EmailService({
|
||||||
lock: lock,
|
lock: lock,
|
||||||
storage: storage,
|
storage: storage,
|
||||||
|
|
|
@ -402,7 +402,7 @@ Storage.prototype.fetchUnsentEmails = function(cb) {
|
||||||
status: 'pending',
|
status: 'pending',
|
||||||
}).toArray(function(err, result) {
|
}).toArray(function(err, result) {
|
||||||
if (err) return cb(err);
|
if (err) return cb(err);
|
||||||
if (!result) return cb();
|
if (!result || _.isEmpty(result)) return cb(null, []);
|
||||||
return cb(null, Model.Email.fromObj(result));
|
return cb(null, Model.Email.fromObj(result));
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
|
@ -3114,11 +3114,20 @@ describe('Wallet service', function() {
|
||||||
should.not.exist(err);
|
should.not.exist(err);
|
||||||
var calls = sendMailStub.getCalls();
|
var calls = sendMailStub.getCalls();
|
||||||
calls.length.should.equal(2);
|
calls.length.should.equal(2);
|
||||||
var recipients = _.pluck(_.map(calls, function(c) {
|
var emails = _.map(calls, function(c) {
|
||||||
return c.args[0];
|
return c.args[0];
|
||||||
}), 'to');
|
});
|
||||||
_.difference(['copayer1@domain.com', 'copayer2@domain.com'], recipients).should.be.empty;
|
_.difference(['copayer1@domain.com', 'copayer2@domain.com'], _.pluck(emails, 'to')).should.be.empty;
|
||||||
done();
|
var one = emails[0];
|
||||||
|
one.from.should.equal('bws@dummy.net');
|
||||||
|
one.subject.should.contain('New spend proposal');
|
||||||
|
one.text.should.contain(wallet.name);
|
||||||
|
one.text.should.contain(wallet.copayers[0].name);
|
||||||
|
server.storage.fetchUnsentEmails(function(err, unsent) {
|
||||||
|
should.not.exist(err);
|
||||||
|
unsent.should.be.empty;
|
||||||
|
done();
|
||||||
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
Loading…
Reference in New Issue