2019-11-06 10:44:52 +03:00
|
|
|
const uuid = require('uuid');
|
2019-11-06 08:52:58 +03:00
|
|
|
const ghostBookshelf = require('./base');
|
|
|
|
|
|
|
|
const Email = ghostBookshelf.Model.extend({
|
|
|
|
tableName: 'emails',
|
|
|
|
|
2019-11-06 10:44:52 +03:00
|
|
|
defaults: function defaults() {
|
|
|
|
return {
|
|
|
|
uuid: uuid.v4(),
|
2019-11-08 13:11:54 +03:00
|
|
|
status: 'pending',
|
2021-05-07 13:56:41 +03:00
|
|
|
recipient_filter: 'status:-free',
|
2020-11-25 20:48:24 +03:00
|
|
|
track_opens: false,
|
|
|
|
delivered_count: 0,
|
|
|
|
opened_count: 0,
|
|
|
|
failed_count: 0
|
2019-11-06 10:44:52 +03:00
|
|
|
};
|
|
|
|
},
|
|
|
|
|
2021-05-07 13:56:41 +03:00
|
|
|
parse() {
|
|
|
|
const attrs = ghostBookshelf.Model.prototype.parse.apply(this, arguments);
|
|
|
|
|
|
|
|
// update legacy recipient_filter values to proper NQL
|
|
|
|
if (attrs.recipient_filter === 'free') {
|
|
|
|
attrs.recipient_filter = 'status:free';
|
|
|
|
}
|
|
|
|
if (attrs.recipient_filter === 'paid') {
|
|
|
|
attrs.recipient_filter = 'status:-free';
|
|
|
|
}
|
|
|
|
|
|
|
|
return attrs;
|
|
|
|
},
|
|
|
|
|
|
|
|
formatOnWrite(attrs) {
|
|
|
|
// update legacy recipient_filter values to proper NQL
|
|
|
|
if (attrs.recipient_filter === 'free') {
|
|
|
|
attrs.recipient_filter = 'status:free';
|
|
|
|
}
|
|
|
|
if (attrs.recipient_filter === 'paid') {
|
|
|
|
attrs.recipient_filter = 'status:-free';
|
|
|
|
}
|
|
|
|
|
|
|
|
return attrs;
|
|
|
|
},
|
|
|
|
|
2020-09-14 17:40:00 +03:00
|
|
|
post() {
|
|
|
|
return this.belongsTo('Post', 'post_id');
|
|
|
|
},
|
2021-05-07 13:56:41 +03:00
|
|
|
|
2020-09-14 17:40:00 +03:00
|
|
|
emailBatches() {
|
|
|
|
return this.hasMany('EmailBatch', 'email_id');
|
|
|
|
},
|
2021-05-07 13:56:41 +03:00
|
|
|
|
2020-09-14 17:40:00 +03:00
|
|
|
recipients() {
|
|
|
|
return this.hasMany('EmailRecipient', 'email_id');
|
|
|
|
},
|
|
|
|
|
2019-11-06 08:52:58 +03:00
|
|
|
emitChange: function emitChange(event, options) {
|
|
|
|
const eventToTrigger = 'email' + '.' + event;
|
|
|
|
ghostBookshelf.Model.prototype.emitChange.bind(this)(this, eventToTrigger, options);
|
|
|
|
},
|
|
|
|
|
|
|
|
onCreated: function onCreated(model, attrs, options) {
|
|
|
|
ghostBookshelf.Model.prototype.onCreated.apply(this, arguments);
|
|
|
|
|
|
|
|
model.emitChange('added', options);
|
|
|
|
},
|
|
|
|
|
|
|
|
onUpdated: function onUpdated(model, attrs, options) {
|
|
|
|
ghostBookshelf.Model.prototype.onUpdated.apply(this, arguments);
|
|
|
|
|
|
|
|
model.emitChange('edited', options);
|
|
|
|
},
|
|
|
|
|
|
|
|
onDestroyed: function onDestroyed(model, options) {
|
|
|
|
ghostBookshelf.Model.prototype.onDestroyed.apply(this, arguments);
|
|
|
|
|
|
|
|
model.emitChange('deleted', options);
|
|
|
|
}
|
2019-11-06 12:30:11 +03:00
|
|
|
}, {
|
|
|
|
post() {
|
|
|
|
return this.belongsTo('Post');
|
|
|
|
}
|
2019-11-06 08:52:58 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
const Emails = ghostBookshelf.Collection.extend({
|
|
|
|
model: Email
|
|
|
|
});
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
Email: ghostBookshelf.model('Email', Email),
|
|
|
|
Emails: ghostBookshelf.collection('Emails', Emails)
|
|
|
|
};
|