2018-12-10 12:20:54 +03:00
|
|
|
const ghostBookshelf = require('./base');
|
2019-11-05 13:07:30 +03:00
|
|
|
const uuid = require('uuid');
|
2020-02-14 12:33:10 +03:00
|
|
|
const _ = require('lodash');
|
2020-05-27 20:47:53 +03:00
|
|
|
const config = require('../../shared/config');
|
2020-02-19 13:08:12 +03:00
|
|
|
const crypto = require('crypto');
|
2018-12-10 12:20:54 +03:00
|
|
|
|
|
|
|
const Member = ghostBookshelf.Model.extend({
|
2019-10-11 10:15:39 +03:00
|
|
|
tableName: 'members',
|
|
|
|
|
2019-11-05 13:07:30 +03:00
|
|
|
defaults() {
|
|
|
|
return {
|
2021-01-27 17:06:21 +03:00
|
|
|
status: 'free',
|
2019-11-05 13:07:30 +03:00
|
|
|
subscribed: true,
|
2020-12-09 15:21:56 +03:00
|
|
|
uuid: uuid.v4(),
|
|
|
|
email_count: 0,
|
|
|
|
email_opened_count: 0
|
2019-11-05 13:07:30 +03:00
|
|
|
};
|
|
|
|
},
|
|
|
|
|
2021-08-04 17:30:53 +03:00
|
|
|
filterExpansions() {
|
|
|
|
return [{
|
|
|
|
key: 'label',
|
|
|
|
replacement: 'labels.slug'
|
|
|
|
}, {
|
|
|
|
key: 'labels',
|
|
|
|
replacement: 'labels.slug'
|
|
|
|
}, {
|
|
|
|
key: 'product',
|
|
|
|
replacement: 'products.slug'
|
|
|
|
}, {
|
|
|
|
key: 'products',
|
|
|
|
replacement: 'products.slug'
|
2022-04-12 18:31:49 +03:00
|
|
|
}, {
|
|
|
|
key: 'newsletters',
|
|
|
|
replacement: 'newsletters.slug'
|
2021-08-04 17:30:53 +03:00
|
|
|
}];
|
|
|
|
},
|
|
|
|
|
|
|
|
filterRelations() {
|
|
|
|
return {
|
|
|
|
labels: {
|
|
|
|
tableName: 'labels',
|
|
|
|
type: 'manyToMany',
|
|
|
|
joinTable: 'members_labels',
|
|
|
|
joinFrom: 'member_id',
|
|
|
|
joinTo: 'label_id'
|
|
|
|
},
|
|
|
|
products: {
|
|
|
|
tableName: 'products',
|
|
|
|
type: 'manyToMany',
|
|
|
|
joinTable: 'members_products',
|
|
|
|
joinFrom: 'member_id',
|
|
|
|
joinTo: 'product_id'
|
2021-08-12 12:10:51 +03:00
|
|
|
},
|
2022-04-04 15:45:30 +03:00
|
|
|
newsletters: {
|
|
|
|
tableName: 'newsletters',
|
|
|
|
type: 'manyToMany',
|
|
|
|
joinTable: 'members_newsletters',
|
|
|
|
joinFrom: 'member_id',
|
|
|
|
joinTo: 'newsletter_id'
|
|
|
|
},
|
2021-08-12 12:10:51 +03:00
|
|
|
subscriptions: {
|
|
|
|
tableName: 'members_stripe_customers_subscriptions',
|
|
|
|
tableNameAs: 'subscriptions',
|
|
|
|
type: 'manyToMany',
|
|
|
|
joinTable: 'members_stripe_customers',
|
|
|
|
joinFrom: 'member_id',
|
|
|
|
joinTo: 'customer_id',
|
|
|
|
joinToForeign: 'customer_id'
|
2021-08-04 17:30:53 +03:00
|
|
|
}
|
|
|
|
};
|
|
|
|
},
|
|
|
|
|
2022-04-04 15:45:30 +03:00
|
|
|
relationships: ['products', 'labels', 'stripeCustomers', 'email_recipients', 'newsletters'],
|
2020-02-14 12:33:10 +03:00
|
|
|
|
2021-01-05 17:06:20 +03:00
|
|
|
// do not delete email_recipients records when a member is destroyed. Recipient
|
|
|
|
// records are used for analytics and historical records
|
|
|
|
relationshipConfig: {
|
|
|
|
email_recipients: {
|
|
|
|
destroyRelated: false
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2020-02-14 12:33:10 +03:00
|
|
|
relationshipBelongsTo: {
|
2021-04-08 20:01:49 +03:00
|
|
|
products: 'products',
|
2022-04-04 15:45:30 +03:00
|
|
|
newsletters: 'newsletters',
|
2020-07-23 19:21:10 +03:00
|
|
|
labels: 'labels',
|
2020-12-10 13:04:05 +03:00
|
|
|
stripeCustomers: 'members_stripe_customers',
|
|
|
|
email_recipients: 'email_recipients'
|
2020-02-14 12:33:10 +03:00
|
|
|
},
|
|
|
|
|
2021-08-23 15:10:12 +03:00
|
|
|
productEvents() {
|
|
|
|
return this.hasMany('MemberProductEvent', 'member_id', 'id')
|
|
|
|
.query('orderBy', 'created_at', 'DESC');
|
|
|
|
},
|
|
|
|
|
2021-04-08 20:01:49 +03:00
|
|
|
products() {
|
|
|
|
return this.belongsToMany('Product', 'members_products', 'member_id', 'product_id')
|
|
|
|
.withPivot('sort_order')
|
|
|
|
.query('orderBy', 'sort_order', 'ASC')
|
|
|
|
.query((qb) => {
|
|
|
|
// avoids bookshelf adding a `DISTINCT` to the query
|
|
|
|
// we know the result set will already be unique and DISTINCT hurts query performance
|
|
|
|
qb.columns('products.*');
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2022-04-04 15:45:30 +03:00
|
|
|
newsletters() {
|
|
|
|
return this.belongsToMany('Newsletter', 'members_newsletters', 'member_id', 'newsletter_id')
|
|
|
|
.query((qb) => {
|
|
|
|
// avoids bookshelf adding a `DISTINCT` to the query
|
|
|
|
// we know the result set will already be unique and DISTINCT hurts query performance
|
|
|
|
qb.columns('newsletters.*');
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2021-10-20 12:02:23 +03:00
|
|
|
offerRedemptions() {
|
|
|
|
return this.hasMany('OfferRedemption', 'member_id', 'id')
|
|
|
|
.query('orderBy', 'created_at', 'DESC');
|
|
|
|
},
|
|
|
|
|
2020-02-14 12:33:10 +03:00
|
|
|
labels: function labels() {
|
|
|
|
return this.belongsToMany('Label', 'members_labels', 'member_id', 'label_id')
|
|
|
|
.withPivot('sort_order')
|
2020-07-29 14:50:22 +03:00
|
|
|
.query('orderBy', 'sort_order', 'ASC')
|
|
|
|
.query((qb) => {
|
|
|
|
// avoids bookshelf adding a `DISTINCT` to the query
|
|
|
|
// we know the result set will already be unique and DISTINCT hurts query performance
|
|
|
|
qb.columns('labels.*');
|
|
|
|
});
|
2020-02-14 12:33:10 +03:00
|
|
|
},
|
|
|
|
|
2020-07-23 19:21:10 +03:00
|
|
|
stripeCustomers() {
|
|
|
|
return this.hasMany('MemberStripeCustomer', 'member_id', 'id');
|
|
|
|
},
|
|
|
|
|
2020-08-12 16:17:44 +03:00
|
|
|
stripeSubscriptions() {
|
|
|
|
return this.belongsToMany(
|
|
|
|
'StripeCustomerSubscription',
|
|
|
|
'members_stripe_customers',
|
|
|
|
'member_id',
|
|
|
|
'customer_id',
|
|
|
|
'id',
|
|
|
|
'customer_id'
|
2020-11-25 13:39:07 +03:00
|
|
|
);
|
2020-08-12 16:17:44 +03:00
|
|
|
},
|
|
|
|
|
2020-12-10 13:04:05 +03:00
|
|
|
email_recipients() {
|
|
|
|
return this.hasMany('EmailRecipient', 'member_id', 'id');
|
|
|
|
},
|
|
|
|
|
2020-08-12 16:17:44 +03:00
|
|
|
serialize(options) {
|
|
|
|
const defaultSerializedObject = ghostBookshelf.Model.prototype.serialize.call(this, options);
|
|
|
|
|
|
|
|
if (defaultSerializedObject.stripeSubscriptions) {
|
2021-01-28 20:25:38 +03:00
|
|
|
defaultSerializedObject.subscriptions = defaultSerializedObject.stripeSubscriptions;
|
2020-08-12 16:17:44 +03:00
|
|
|
delete defaultSerializedObject.stripeSubscriptions;
|
|
|
|
}
|
|
|
|
|
|
|
|
return defaultSerializedObject;
|
|
|
|
},
|
|
|
|
|
2019-10-11 10:15:39 +03:00
|
|
|
emitChange: function emitChange(event, options) {
|
|
|
|
const eventToTrigger = 'member' + '.' + event;
|
|
|
|
ghostBookshelf.Model.prototype.emitChange.bind(this)(this, eventToTrigger, options);
|
|
|
|
},
|
|
|
|
|
2021-08-25 13:44:34 +03:00
|
|
|
onCreated: function onCreated(model, options) {
|
2019-10-11 10:15:39 +03:00
|
|
|
ghostBookshelf.Model.prototype.onCreated.apply(this, arguments);
|
|
|
|
|
|
|
|
model.emitChange('added', options);
|
|
|
|
},
|
|
|
|
|
2021-08-25 13:44:34 +03:00
|
|
|
onUpdated: function onUpdated(model, options) {
|
2019-10-31 17:34:50 +03:00
|
|
|
ghostBookshelf.Model.prototype.onUpdated.apply(this, arguments);
|
|
|
|
|
|
|
|
model.emitChange('edited', options);
|
|
|
|
},
|
|
|
|
|
2019-10-11 10:15:39 +03:00
|
|
|
onDestroyed: function onDestroyed(model, options) {
|
|
|
|
ghostBookshelf.Model.prototype.onDestroyed.apply(this, arguments);
|
|
|
|
|
|
|
|
model.emitChange('deleted', options);
|
2020-02-14 12:33:10 +03:00
|
|
|
},
|
2020-02-19 13:08:12 +03:00
|
|
|
|
2020-02-14 12:33:10 +03:00
|
|
|
onDestroying: function onDestroyed(model) {
|
|
|
|
ghostBookshelf.Model.prototype.onDestroying.apply(this, arguments);
|
|
|
|
|
|
|
|
this.handleAttachedModels(model);
|
|
|
|
},
|
2020-02-19 13:08:12 +03:00
|
|
|
|
2020-02-14 12:33:10 +03:00
|
|
|
onSaving: function onSaving(model, attr, options) {
|
2020-02-14 13:36:25 +03:00
|
|
|
let labelsToSave = [];
|
2020-02-14 12:33:10 +03:00
|
|
|
|
2021-02-05 15:01:21 +03:00
|
|
|
if (_.isUndefined(this.get('labels'))) {
|
|
|
|
this.unset('labels');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-02-14 12:33:10 +03:00
|
|
|
// CASE: detect lowercase/uppercase label slugs
|
|
|
|
if (!_.isUndefined(this.get('labels')) && !_.isNull(this.get('labels'))) {
|
|
|
|
labelsToSave = [];
|
|
|
|
|
|
|
|
// and deduplicate upper/lowercase tags
|
|
|
|
_.each(this.get('labels'), function each(item) {
|
|
|
|
item.name = item.name && item.name.trim();
|
|
|
|
for (let i = 0; i < labelsToSave.length; i = i + 1) {
|
|
|
|
if (labelsToSave[i].name && item.name && labelsToSave[i].name.toLocaleLowerCase() === item.name.toLocaleLowerCase()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
labelsToSave.push(item);
|
|
|
|
});
|
|
|
|
|
|
|
|
this.set('labels', labelsToSave);
|
|
|
|
}
|
|
|
|
|
2021-01-28 19:31:02 +03:00
|
|
|
this.handleAttachedModels(model);
|
|
|
|
|
2020-02-14 12:33:10 +03:00
|
|
|
// CASE: Detect existing labels with same case-insensitive name and replace
|
2021-01-28 19:31:02 +03:00
|
|
|
return ghostBookshelf.model('Label')
|
|
|
|
.findAll(Object.assign({
|
|
|
|
columns: ['id', 'name']
|
|
|
|
}, _.pick(options, 'transacting')))
|
|
|
|
.then((labels) => {
|
|
|
|
labelsToSave.forEach((label) => {
|
|
|
|
let existingLabel = labels.find((lab) => {
|
|
|
|
return label.name.toLowerCase() === lab.get('name').toLowerCase();
|
2020-02-14 12:33:10 +03:00
|
|
|
});
|
2021-01-28 19:31:02 +03:00
|
|
|
label.name = (existingLabel && existingLabel.get('name')) || label.name;
|
2021-07-16 11:31:47 +03:00
|
|
|
label.id = (existingLabel && existingLabel.id) || label.id;
|
2020-02-14 12:33:10 +03:00
|
|
|
});
|
|
|
|
|
2021-01-28 19:31:02 +03:00
|
|
|
model.set('labels', labelsToSave);
|
|
|
|
});
|
2020-02-14 12:33:10 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
handleAttachedModels: function handleAttachedModels(model) {
|
|
|
|
/**
|
|
|
|
* @NOTE:
|
|
|
|
* Bookshelf only exposes the object that is being detached on `detaching`.
|
|
|
|
* For the reason above, `detached` handler is using the scope of `detaching`
|
|
|
|
* to access the models that are not present in `detached`.
|
|
|
|
*/
|
2020-10-20 02:02:56 +03:00
|
|
|
model.related('labels').once('detaching', function onDetaching(collection, label) {
|
2020-02-14 12:33:10 +03:00
|
|
|
model.related('labels').once('detached', function onDetached(detachedCollection, response, options) {
|
|
|
|
label.emitChange('detached', options);
|
|
|
|
model.emitChange('label.detached', options);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2020-10-20 02:02:56 +03:00
|
|
|
model.related('labels').once('attaching', function onDetaching(collection, labels) {
|
2020-02-14 12:33:10 +03:00
|
|
|
model.related('labels').once('attached', function onDetached(detachedCollection, response, options) {
|
|
|
|
labels.forEach((label) => {
|
|
|
|
label.emitChange('attached', options);
|
|
|
|
model.emitChange('label.attached', options);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The base model keeps only the columns, which are defined in the schema.
|
|
|
|
* We have to add the relations on top, otherwise bookshelf-relations
|
|
|
|
* has no access to the nested relations, which should be updated.
|
|
|
|
*/
|
|
|
|
permittedAttributes: function permittedAttributes() {
|
|
|
|
let filteredKeys = ghostBookshelf.Model.prototype.permittedAttributes.apply(this, arguments);
|
|
|
|
|
|
|
|
this.relationships.forEach((key) => {
|
|
|
|
filteredKeys.push(key);
|
|
|
|
});
|
|
|
|
|
|
|
|
return filteredKeys;
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* We have to ensure consistency. If you listen on model events (e.g. `member.added`), you can expect that you always
|
|
|
|
* receive all fields including relations. Otherwise you can't rely on a consistent flow. And we want to avoid
|
|
|
|
* that event listeners have to re-fetch a resource. This function is used in the context of inserting
|
|
|
|
* and updating resources. We won't return the relations by default for now.
|
|
|
|
*/
|
|
|
|
defaultRelations: function defaultRelations(methodName, options) {
|
|
|
|
if (['edit', 'add', 'destroy'].indexOf(methodName) !== -1) {
|
|
|
|
options.withRelated = _.union(['labels'], options.withRelated || []);
|
|
|
|
}
|
|
|
|
|
|
|
|
return options;
|
2020-02-19 13:08:12 +03:00
|
|
|
},
|
|
|
|
|
2020-05-28 12:14:02 +03:00
|
|
|
searchQuery: function searchQuery(queryBuilder, query) {
|
2020-07-06 11:09:08 +03:00
|
|
|
queryBuilder.where('members.name', 'like', `%${query}%`);
|
|
|
|
queryBuilder.orWhere('members.email', 'like', `%${query}%`);
|
2020-05-28 12:14:02 +03:00
|
|
|
},
|
|
|
|
|
2020-12-08 15:43:48 +03:00
|
|
|
orderRawQuery(field, direction) {
|
|
|
|
if (field === 'email_open_rate') {
|
|
|
|
return {
|
|
|
|
orderByRaw: `members.email_open_rate IS NOT NULL DESC, members.email_open_rate ${direction}`
|
|
|
|
};
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2020-02-19 13:08:12 +03:00
|
|
|
toJSON(unfilteredOptions) {
|
|
|
|
const options = Member.filterOptions(unfilteredOptions, 'toJSON');
|
|
|
|
const attrs = ghostBookshelf.Model.prototype.toJSON.call(this, options);
|
|
|
|
|
|
|
|
// Inject a computed avatar url. Uses gravatar's default ?d= query param
|
|
|
|
// to serve a blank image if there is no gravatar for the member's email.
|
|
|
|
// Will not use gravatar if privacy.useGravatar is false in config
|
|
|
|
attrs.avatar_image = null;
|
|
|
|
if (attrs.email && !config.isPrivacyDisabled('useGravatar')) {
|
|
|
|
const emailHash = crypto.createHash('md5').update(attrs.email.toLowerCase().trim()).digest('hex');
|
|
|
|
attrs.avatar_image = `https://gravatar.com/avatar/${emailHash}?s=250&d=blank`;
|
|
|
|
}
|
|
|
|
|
|
|
|
return attrs;
|
2019-10-11 10:15:39 +03:00
|
|
|
}
|
2020-05-28 12:14:02 +03:00
|
|
|
}, {
|
|
|
|
/**
|
|
|
|
* Returns an array of keys permitted in a method's `options` hash, depending on the current method.
|
|
|
|
* @param {String} methodName The name of the method to check valid options for.
|
|
|
|
* @return {Array} Keys allowed in the `options` hash of the model's method.
|
|
|
|
*/
|
|
|
|
permittedOptions: function permittedOptions(methodName) {
|
|
|
|
let options = ghostBookshelf.Model.permittedOptions.call(this, methodName);
|
|
|
|
|
|
|
|
if (['findPage', 'findAll'].includes(methodName)) {
|
2021-01-28 19:31:02 +03:00
|
|
|
options = options.concat(['search']);
|
2020-05-28 12:14:02 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return options;
|
2020-07-23 19:21:10 +03:00
|
|
|
},
|
|
|
|
|
2020-07-24 16:29:21 +03:00
|
|
|
add(data, unfilteredOptions = {}) {
|
2020-07-23 19:21:10 +03:00
|
|
|
if (!unfilteredOptions.transacting) {
|
|
|
|
return ghostBookshelf.transaction((transacting) => {
|
|
|
|
return this.add(data, Object.assign({transacting}, unfilteredOptions));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return ghostBookshelf.Model.add.call(this, data, unfilteredOptions);
|
|
|
|
},
|
|
|
|
|
2020-07-24 16:29:21 +03:00
|
|
|
edit(data, unfilteredOptions = {}) {
|
2020-07-23 19:21:10 +03:00
|
|
|
if (!unfilteredOptions.transacting) {
|
|
|
|
return ghostBookshelf.transaction((transacting) => {
|
|
|
|
return this.edit(data, Object.assign({transacting}, unfilteredOptions));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return ghostBookshelf.Model.edit.call(this, data, unfilteredOptions);
|
|
|
|
},
|
|
|
|
|
2020-07-24 16:29:21 +03:00
|
|
|
destroy(unfilteredOptions = {}) {
|
2020-07-23 19:21:10 +03:00
|
|
|
if (!unfilteredOptions.transacting) {
|
|
|
|
return ghostBookshelf.transaction((transacting) => {
|
|
|
|
return this.destroy(Object.assign({transacting}, unfilteredOptions));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return ghostBookshelf.Model.destroy.call(this, unfilteredOptions);
|
2021-08-13 15:17:21 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
getLabelRelations(data, unfilteredOptions = {}) {
|
|
|
|
const query = ghostBookshelf.knex('members_labels')
|
|
|
|
.select('id')
|
|
|
|
.where('label_id', data.labelId)
|
|
|
|
.whereIn('member_id', data.memberIds);
|
|
|
|
|
|
|
|
if (unfilteredOptions.transacting) {
|
|
|
|
query.transacting(unfilteredOptions.transacting);
|
|
|
|
}
|
|
|
|
|
2022-04-25 23:07:46 +03:00
|
|
|
return query;
|
|
|
|
},
|
|
|
|
|
2022-04-28 10:50:05 +03:00
|
|
|
getNewsletterRelations(data, unfilteredOptions = {}) {
|
|
|
|
const query = ghostBookshelf.knex('members_newsletters')
|
|
|
|
.select('id')
|
|
|
|
.whereIn('member_id', data.memberIds);
|
|
|
|
|
|
|
|
if (unfilteredOptions.transacting) {
|
|
|
|
query.transacting(unfilteredOptions.transacting);
|
|
|
|
}
|
|
|
|
|
|
|
|
return query;
|
|
|
|
},
|
|
|
|
|
2022-04-25 23:07:46 +03:00
|
|
|
fetchAllSubscribed(unfilteredOptions = {}) {
|
|
|
|
// we use raw queries instead of model relationships because model hydration is expensive
|
|
|
|
const query = ghostBookshelf.knex('members_newsletters')
|
|
|
|
.join('newsletters', 'members_newsletters.newsletter_id', '=', 'newsletters.id')
|
|
|
|
.where('newsletters.status', 'active')
|
|
|
|
.distinct('member_id as id');
|
|
|
|
|
|
|
|
if (unfilteredOptions.transacting) {
|
|
|
|
query.transacting(unfilteredOptions.transacting);
|
|
|
|
}
|
|
|
|
|
2021-08-13 15:17:21 +03:00
|
|
|
return query;
|
2020-05-28 12:14:02 +03:00
|
|
|
}
|
2018-12-10 12:20:54 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
const Members = ghostBookshelf.Collection.extend({
|
|
|
|
model: Member
|
|
|
|
});
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
Member: ghostBookshelf.model('Member', Member),
|
|
|
|
Members: ghostBookshelf.collection('Members', Members)
|
|
|
|
};
|