diff --git a/lib/model/email.js b/lib/model/email.js new file mode 100644 index 0000000..f3a90ab --- /dev/null +++ b/lib/model/email.js @@ -0,0 +1,40 @@ +'use strict'; + +function Email() { + this.version = '1.0.0'; +}; + +Email.create = function(opts) { + opts = opts || {}; + + var x = new Email(); + + x.createdOn = Math.floor(Date.now() / 1000); + x.walletId = opts.walletId; + x.copayerId = opts.copayerId; + x.to = opts.to; + x.subject = opts.subject; + x.body = opts.body; + x.status = 'pending'; + x.attempts = 0; + x.sentOn = null; + return x; +}; + +Email.fromObj = function(obj) { + var x = new Email(); + + x.createdOn = obj.createdOn; + x.walletId = obj.walletId; + x.copayerId = obj.copayerId; + x.to = obj.to; + x.subject = obj.subject; + x.body = obj.body; + x.status = obj.status; + x.attempts = obj.attempts; + x.sentOn = obj.sentOn; + return x; +}; + + +module.exports = Email; diff --git a/lib/storage.js b/lib/storage.js index 065e897..8b29a3c 100644 --- a/lib/storage.js +++ b/lib/storage.js @@ -19,6 +19,7 @@ var collections = { NOTIFICATIONS: 'notifications', COPAYERS_LOOKUP: 'copayers_lookup', PREFERENCES: 'preferences', + EMAIL_QUEUE: 'email_queue', }; var Storage = function(opts) { @@ -375,6 +376,26 @@ Storage.prototype.storePreferences = function(preferences, cb) { }, cb); }; +Storage.prototype.storeEmail = function(email, cb) { + this.db.collection(collections.EMAIL_QUEUE).update({ + id: email.id, + }, txp, { + w: 1, + upsert: true, + }, cb); +}; + +Storage.prototype.fetchUnsentEmails = function(cb) { + this.db.collection(collections.EMAIL_QUEUE).find({ + status: 'pending', + }, function(err, result) { + if (err) return cb(err); + if (!result) return cb(); + return cb(null, Model.Email.fromObj(result)); + }); +}; + + Storage.prototype._dump = function(cb, fn) { fn = fn || console.log; cb = cb || function() {};