0
Fork 0
mirror of https://github.com/TryGhost/Ghost.git synced 2025-02-10 23:36:14 -05:00

Ordered Products by their monthly price by default (#13234)

refs https://github.com/TryGhost/Team/issues/714

In order to order products by their monthly price we need to apply a
join with the stripe_prices table when querying so we have access to the
amount column of stripe_prices.

As this ordering is core to how the tiers feature is intended to work,
we have added it as the default order. But this can be overriden by
manually passing the order option.

Also ensured that we do not create duplicate products in test fixtures
This commit is contained in:
Fabien 'egg' O'Carroll 2021-08-20 14:46:06 +02:00 committed by GitHub
parent 70bea3ddf8
commit 26c3e77640
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 16 additions and 1 deletions

View file

@ -10,6 +10,12 @@ const Product = ghostBookshelf.Model.extend({
benefits: 'benefits'
},
applyCustomQuery() {
this.query((qb) => {
qb.leftJoin('stripe_prices', 'products.monthly_price_id', 'stripe_prices.id');
});
},
async onSaving(model, _attr, options) {
ghostBookshelf.Model.prototype.onSaving.apply(this, arguments);
@ -121,6 +127,10 @@ const Product = ghostBookshelf.Model.extend({
members() {
return this.belongsToMany('Member', 'members_products', 'product_id', 'member_id');
}
}, {
orderDefaultRaw() {
return 'stripe_prices.amount asc';
}
});
const Products = ghostBookshelf.Collection.extend({

View file

@ -462,7 +462,12 @@ const fixtures = {
return models.Label.add(label, context.internal);
}).then(function () {
let productsToInsert = fixtureUtils.findModelFixtures('Product').entries;
return Promise.map(productsToInsert, product => models.Product.add(product, context.internal));
return Promise.map(productsToInsert, async (product) => {
const found = await models.Product.findOne(product, context.internal);
if (!found) {
await models.Product.add(product, context.internal);
}
});
}).then(function () {
return models.Product.findOne({}, context.internal);
}).then(function (product) {