From 3d901852abdefd849b0af0f31876ec9b6d6ccbc7 Mon Sep 17 00:00:00 2001 From: Ivan Socolsky Date: Wed, 29 Apr 2015 21:03:47 -0300 Subject: [PATCH] more robust send loop --- lib/emailservice.js | 14 ++++++++++++-- lib/model/email.js | 8 +++++++- test/integration/server.js | 17 ++++++++++------- 3 files changed, 29 insertions(+), 10 deletions(-) diff --git a/lib/emailservice.js b/lib/emailservice.js index 8dc6ab4..8aef86e 100644 --- a/lib/emailservice.js +++ b/lib/emailservice.js @@ -31,10 +31,17 @@ var EMAIL_TYPES = { function EmailService(opts) { + $.checkArgument(opts); + + opts.email = opts.email || {}; + this.storage = opts.storage; this.lock = opts.lock; this.mailer = opts.mailer || nodemailer.createTransport(opts.email); + this.from = opts.email.from; + $.checkState(this.mailer); + $.checkState(this.from); }; // TODO: cache for X minutes @@ -95,7 +102,7 @@ EmailService.prototype._getDataForTemplate = function(notification, cb) { data.walletM = wallet.m; data.walletN = wallet.n; var copayer = _.find(wallet.copayers, { - copayerId: notification.creatorId + id: notification.creatorId }); if (copayer) { data.creatorId = copayer.id; @@ -162,6 +169,7 @@ EmailService.prototype.sendEmail = function(notification, cb) { var email = Model.Email.create({ walletId: notification.walletId, copayerId: recipient.copayerId, + from: self.from, to: recipient.emailAddress, subject: content.subject, body: content.body, @@ -174,7 +182,9 @@ EmailService.prototype.sendEmail = function(notification, cb) { function(emails, next) { async.each(emails, function(email, next) { self._send(email, next); - }, next); + }, function(err) { + return next(); + }); }, ], cb); }; diff --git a/lib/model/email.js b/lib/model/email.js index 002facf..8671038 100644 --- a/lib/model/email.js +++ b/lib/model/email.js @@ -1,5 +1,8 @@ 'use strict'; +var _ = require('lodash'); +var Uuid = require('uuid'); + function Email() { this.version = '1.0.0'; }; @@ -9,7 +12,9 @@ Email.create = function(opts) { var x = new Email(); - x.createdOn = Math.floor(Date.now() / 1000); + var now = Date.now(); + x.createdOn = Math.floor(now / 1000); + x.id = _.padLeft(now, 14, '0') + Uuid.v4(); x.walletId = opts.walletId; x.copayerId = opts.copayerId; x.from = opts.from; @@ -26,6 +31,7 @@ Email.fromObj = function(obj) { var x = new Email(); x.createdOn = obj.createdOn; + x.id = obj.id; x.walletId = obj.walletId; x.copayerId = obj.copayerId; x.from = obj.from; diff --git a/test/integration/server.js b/test/integration/server.js index c50e12e..f027f6d 100644 --- a/test/integration/server.js +++ b/test/integration/server.js @@ -239,13 +239,13 @@ describe('Wallet service', function() { }); done(); }); - // storage = new Storage(); - // storage.connect({ - // mongoDb: { - // uri: 'mongodb://localhost:27017/bws_test' - // } - // }, done); -}); + // storage = new Storage(); + // storage.connect({ + // mongoDb: { + // uri: 'mongodb://localhost:27017/bws_test' + // } + // }, done); + }); beforeEach(function(done) { resetDb(function() { blockchainExplorer = sinon.stub(); @@ -254,6 +254,9 @@ describe('Wallet service', function() { storage: storage, blockchainExplorer: blockchainExplorer, mailer: mailer, + email: { + from: 'bws@dummy.net', + } }, done); }); });