2021-04-08 20:01:49 +03:00
|
|
|
const ghostBookshelf = require('./base');
|
2021-06-24 12:42:27 +03:00
|
|
|
const _ = require('lodash');
|
2021-04-08 20:01:49 +03:00
|
|
|
|
|
|
|
const Product = ghostBookshelf.Model.extend({
|
|
|
|
tableName: 'products',
|
|
|
|
|
|
|
|
async onSaving(model, _attr, options) {
|
|
|
|
ghostBookshelf.Model.prototype.onSaving.apply(this, arguments);
|
|
|
|
|
|
|
|
if (model.get('name')) {
|
|
|
|
model.set('name', model.get('name').trim());
|
|
|
|
}
|
|
|
|
|
|
|
|
if (model.hasChanged('slug') || !model.get('slug')) {
|
|
|
|
const slug = model.get('slug') || model.get('name');
|
|
|
|
|
2021-06-24 12:42:27 +03:00
|
|
|
if (slug) {
|
|
|
|
const cleanSlug = await ghostBookshelf.Model.generateSlug(Product, slug, {
|
|
|
|
transacting: options.transacting
|
|
|
|
});
|
|
|
|
|
|
|
|
model.set({slug: cleanSlug});
|
2021-04-08 20:01:49 +03:00
|
|
|
}
|
2021-06-24 12:42:27 +03:00
|
|
|
}
|
2021-04-08 20:01:49 +03:00
|
|
|
|
2021-06-24 12:42:27 +03:00
|
|
|
let benefitsToSave = [];
|
2021-04-08 20:01:49 +03:00
|
|
|
|
2021-06-24 12:42:27 +03:00
|
|
|
if (_.isUndefined(this.get('benefits'))) {
|
|
|
|
this.unset('benefits');
|
|
|
|
return;
|
2021-04-08 20:01:49 +03:00
|
|
|
}
|
2021-06-24 12:42:27 +03:00
|
|
|
|
|
|
|
// CASE: detect lowercase/uppercase label slugs
|
|
|
|
if (!_.isUndefined(this.get('benefits')) && !_.isNull(this.get('benefits'))) {
|
|
|
|
benefitsToSave = [];
|
|
|
|
|
|
|
|
// and deduplicate upper/lowercase tags
|
|
|
|
_.each(this.get('benefits'), function each(item) {
|
|
|
|
item.name = item.name && item.name.trim();
|
|
|
|
for (let i = 0; i < benefitsToSave.length; i = i + 1) {
|
|
|
|
if (benefitsToSave[i].name && item.name && benefitsToSave[i].name.toLocaleLowerCase() === item.name.toLocaleLowerCase()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
benefitsToSave.push(item);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
const existingBenefits = await ghostBookshelf.model('Benefit').findAll(Object.assign({
|
|
|
|
columns: ['id', 'name']
|
|
|
|
}, _.pick(options, 'transacting')));
|
|
|
|
|
|
|
|
benefitsToSave.forEach((benefitToSave) => {
|
|
|
|
const existingBenefitModel = existingBenefits.find((existingBenefit) => {
|
|
|
|
return benefitToSave.name.toLowerCase() === existingBenefit.get('name').toLowerCase();
|
|
|
|
});
|
|
|
|
if (existingBenefitModel) {
|
|
|
|
benefitToSave.name = existingBenefitModel.get('name');
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
model.set('benefits', benefitsToSave);
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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;
|
2021-04-08 20:01:49 +03:00
|
|
|
},
|
|
|
|
|
2021-06-01 13:00:28 +03:00
|
|
|
monthlyPrice() {
|
|
|
|
return this.belongsTo('StripePrice', 'monthly_price_id', 'id');
|
|
|
|
},
|
|
|
|
|
|
|
|
yearlyPrice() {
|
|
|
|
return this.belongsTo('StripePrice', 'yearly_price_id', 'id');
|
|
|
|
},
|
|
|
|
|
2021-04-14 21:20:39 +03:00
|
|
|
stripeProducts() {
|
|
|
|
return this.hasMany('StripeProduct', 'product_id', 'id');
|
|
|
|
},
|
|
|
|
|
|
|
|
stripePrices() {
|
|
|
|
return this.belongsToMany(
|
|
|
|
'StripePrice',
|
|
|
|
'stripe_products',
|
|
|
|
'product_id',
|
|
|
|
'stripe_product_id',
|
|
|
|
'id',
|
|
|
|
'stripe_product_id'
|
|
|
|
);
|
|
|
|
},
|
|
|
|
|
|
|
|
members() {
|
2021-04-08 20:01:49 +03:00
|
|
|
return this.belongsToMany('Member', 'members_products', 'product_id', 'member_id');
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
const Products = ghostBookshelf.Collection.extend({
|
|
|
|
model: Product
|
|
|
|
});
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
Product: ghostBookshelf.model('Product', Product),
|
|
|
|
Products: ghostBookshelf.collection('Products', Products)
|
|
|
|
};
|