bitcore-wallet-service/lib/model/email.js

74 lines
1.5 KiB
JavaScript
Raw Normal View History

2015-04-28 05:47:25 -07:00
'use strict';
2015-04-29 17:03:47 -07:00
var _ = require('lodash');
var Uuid = require('uuid');
function Email() {};
2015-04-28 05:47:25 -07:00
Email.create = function(opts) {
opts = opts || {};
var x = new Email();
x.version = 2;
2015-04-29 17:03:47 -07:00
var now = Date.now();
x.createdOn = Math.floor(now / 1000);
x.id = _.padLeft(now, 14, '0') + Uuid.v4();
2015-04-28 05:47:25 -07:00
x.walletId = opts.walletId;
x.copayerId = opts.copayerId;
2015-04-29 08:10:01 -07:00
x.from = opts.from;
2015-04-28 05:47:25 -07:00
x.to = opts.to;
x.subject = opts.subject;
2015-06-26 13:39:47 -07:00
x.bodyPlain = opts.bodyPlain;
x.bodyHtml = opts.bodyHtml;
2015-04-28 05:47:25 -07:00
x.status = 'pending';
x.attempts = 0;
2015-04-30 12:05:49 -07:00
x.lastAttemptOn = null;
2015-06-09 14:22:06 -07:00
x.notificationId = opts.notificationId;
2015-06-26 07:15:47 -07:00
x.language = opts.language || 'en';
2015-04-28 05:47:25 -07:00
return x;
};
Email.fromObj = function(obj) {
var x = new Email();
x.version = obj.version;
2015-04-28 05:47:25 -07:00
x.createdOn = obj.createdOn;
2015-04-29 17:03:47 -07:00
x.id = obj.id;
2015-04-28 05:47:25 -07:00
x.walletId = obj.walletId;
x.copayerId = obj.copayerId;
2015-04-29 08:10:01 -07:00
x.from = obj.from;
2015-04-28 05:47:25 -07:00
x.to = obj.to;
x.subject = obj.subject;
if (parseInt(x.version) == 1) {
2015-06-26 13:39:47 -07:00
x.bodyPlain = obj.body;
x.version = 2;
2015-06-26 13:39:47 -07:00
} else {
x.bodyPlain = obj.bodyPlain;
}
x.bodyHtml = obj.bodyHtml;
2015-04-28 05:47:25 -07:00
x.status = obj.status;
x.attempts = obj.attempts;
2015-04-30 12:05:49 -07:00
x.lastAttemptOn = obj.lastAttemptOn;
2015-06-09 14:22:06 -07:00
x.notificationId = obj.notificationId;
2015-06-26 07:15:47 -07:00
x.language = obj.language;
2015-04-28 05:47:25 -07:00
return x;
};
2015-04-30 12:05:49 -07:00
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');
};
2015-04-28 05:47:25 -07:00
module.exports = Email;