From 095c62417242ad4dd081211887d5bf9541a19dc5 Mon Sep 17 00:00:00 2001 From: Fabien O'Carroll Date: Mon, 1 Mar 2021 11:23:58 +0000 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20Fixed=20cancelling=20subscriptio?= =?UTF-8?q?ns=20when=20destroying?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit refs https://github.com/TryGhost/Ghost/issues/12711 We must wait for the stripeSubscriptions relation to be loaded before attempting to loop through them. As well as this we should use `upsert` so that we can edit a subscription record by `subscription_id`, rather than the (internal) `id` --- ghost/members-api/lib/repositories/member/index.js | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/ghost/members-api/lib/repositories/member/index.js b/ghost/members-api/lib/repositories/member/index.js index 38598c672b..2862851b49 100644 --- a/ghost/members-api/lib/repositories/member/index.js +++ b/ghost/members-api/lib/repositories/member/index.js @@ -169,16 +169,20 @@ module.exports = class MemberRepository { } if (this._stripeAPIService && options.cancelStripeSubscriptions) { - await member.related('stripeSubscriptions'); + await member.related('stripeSubscriptions').fetch(); const subscriptions = member.related('stripeSubscriptions'); for (const subscription of subscriptions.models) { if (subscription.get('status') !== 'canceled') { const updatedSubscription = await this._stripeAPIService.cancelSubscription( subscription.get('subscription_id') ); - await this._StripeCustomerSubscription.update({ + + await this._StripeCustomerSubscription.upsert({ status: updatedSubscription.status - }, options); + }, { + subscription_id: updatedSubscription.id + }); + await this._MemberPaidSubscriptionEvent.add({ member_id: member.id, source: 'stripe',