Fixed products being removed when canceling subscriptions

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

Previously when a change happened to a subscription the member's
products would be reset purely based on the subscriptions. Since we now
support setting products outside of subscriptions, we must make sure
than a change to a subscription will only affect the product for which
the subscription was for
This commit is contained in:
Fabien O'Carroll 2021-06-10 18:33:22 +01:00 committed by Fabien 'egg' O'Carroll
parent 5a74c614c5
commit 9079a9b9e0

View File

@ -409,36 +409,35 @@ module.exports = class MemberRepository {
} }
let status = 'free'; let status = 'free';
let memberProducts = []; let memberProducts = (await member.related('products').fetch(options)).toJSON();
if (this.isActiveSubscriptionStatus(subscription.status)) { if (this.isActiveSubscriptionStatus(subscription.status)) {
status = 'paid'; status = 'paid';
try { if (ghostProduct) {
if (ghostProduct) { memberProducts.push(ghostProduct.toJSON());
memberProducts.push(ghostProduct.toJSON());
}
const existingProducts = await member.related('products').fetch(options);
for (const productModel of existingProducts.models) {
memberProducts.push(productModel.toJSON());
}
} catch (e) {
this._logging.error(`Failed to attach products to member - ${data.id}`);
} }
} else { } else {
const subscriptions = await member.related('stripeSubscriptions').fetch(options); const subscriptions = await member.related('stripeSubscriptions').fetch(options);
let activeSubscriptionForGhostProduct = false;
for (const subscription of subscriptions.models) { for (const subscription of subscriptions.models) {
if (this.isActiveSubscriptionStatus(subscription.get('status'))) { if (this.isActiveSubscriptionStatus(subscription.get('status'))) {
status = 'paid';
try { try {
const subscriptionProduct = await this._productRepository.get({stripe_price_id: subscription.get('stripe_price_id')}); const subscriptionProduct = await this._productRepository.get({stripe_price_id: subscription.get('stripe_price_id')});
if (subscriptionProduct) { if (subscriptionProduct && ghostProduct && subscriptionProduct.id === ghostProduct.id) {
memberProducts.push(subscriptionProduct.toJSON()); activeSubscriptionForGhostProduct = true;
} }
} catch (e) { } catch (e) {
this._logging.error(`Failed to attach products to member - ${data.id}`); this._logging.error(`Failed to attach products to member - ${data.id}`);
this._logging.error(e); this._logging.error(e);
} }
status = 'paid';
} }
} }
if (!activeSubscriptionForGhostProduct) {
memberProducts = memberProducts.filter((product) => {
return product.id !== ghostProduct.id;
});
}
} }
let updatedMember; let updatedMember;
try { try {