Reused getDefaultProduct where possible

refs 82ed10473b
refs https://github.com/TryGhost/Team/issues/1869

- getDefaultProduct has unified logic across different places (see refed commit). It is recommended to use instead of writing custom queries prone to mistakes.
- Also added more readable name to the possible error message thrown by setComplimentarySubscription
This commit is contained in:
Naz 2022-10-20 17:23:59 +08:00
parent 5d5b77d32f
commit 0e094b371b
No known key found for this signature in database
2 changed files with 56 additions and 11 deletions

View File

@ -841,13 +841,10 @@ module.exports = class MemberRepository {
ghostProduct = await this._productRepository.get({stripe_product_id: subscriptionPriceData.product}, {...options, forUpdate: true});
// Use first Ghost product as default product in case of missing link
if (!ghostProduct) {
let {data: pageData} = await this._productRepository.list({
limit: 1,
filter: 'type:paid',
...options,
forUpdate: true
ghostProduct = await this._productRepository.getDefaultProduct({
forUpdate: true,
...options
});
ghostProduct = (pageData && pageData[0]) || null;
}
// Link Stripe Product & Price to Ghost Product
@ -1386,17 +1383,17 @@ module.exports = class MemberRepository {
return this.isActiveSubscriptionStatus(subscription.get('status'));
});
const productPage = await this._productRepository.list({
limit: 1,
const ghostProductModel = await this._productRepository.getDefaultProduct({
withRelated: ['stripePrices'],
filter: 'type:paid',
...options
});
const defaultProduct = productPage && productPage.data && productPage.data[0] && productPage.data[0].toJSON();
const defaultProduct = ghostProductModel?.toJSON();
if (!defaultProduct) {
throw new errors.NotFoundError({message: tpl(messages.productNotFound)});
throw new errors.NotFoundError({
message: tpl(messages.productNotFound, {id: '"default"'})
});
}
const zeroValuePrices = defaultProduct.stripePrices.filter((price) => {

View File

@ -53,6 +53,54 @@ describe('MemberRepository', function () {
});
});
describe('setComplimentarySubscription', function () {
let Member;
let productRepository;
beforeEach(function () {
Member = {
findOne: sinon.stub().resolves({
id: 'member_id_123',
related: () => {
return {
fetch: () => {
return {
models: []
};
}
};
}
})
};
});
it('throws an error when there is no default product', async function () {
productRepository = {
getDefaultProduct: sinon.stub().resolves(null)
};
const repo = new MemberRepository({
Member,
stripeAPIService: {
configured: true
},
productRepository
});
try {
await repo.setComplimentarySubscription({
id: 'member_id_123'
}, {
transacting: true
});
assert.fail('setComplimentarySubscription should have thrown');
} catch (err) {
assert.equal(err.message, 'Could not find Product "default"');
}
});
});
describe('linkSubscription', function (){
let Member;
let notifySpy;