From 257a9803d65dbdfab14d79ee6ec09b7ffade23f4 Mon Sep 17 00:00:00 2001 From: Nick O'Leary Date: Thu, 7 Dec 2023 11:11:52 +0000 Subject: [PATCH] Fix team delete for unmanaged-billing team --- forge/containers/stub/index.js | 2 -- forge/ee/lib/billing/index.js | 26 +++++++++++++++++--------- forge/routes/api/team.js | 3 +-- 3 files changed, 18 insertions(+), 13 deletions(-) diff --git a/forge/containers/stub/index.js b/forge/containers/stub/index.js index 7ed6efdebd..b38531c29f 100644 --- a/forge/containers/stub/index.js +++ b/forge/containers/stub/index.js @@ -155,8 +155,6 @@ module.exports = { resolve() }, 250) }) - } else { - throw new Error(`${project.id} not found`) } }, /** diff --git a/forge/ee/lib/billing/index.js b/forge/ee/lib/billing/index.js index b849d43f99..3f6286b670 100644 --- a/forge/ee/lib/billing/index.js +++ b/forge/ee/lib/billing/index.js @@ -378,12 +378,14 @@ module.exports.init = async function (app) { }, closeSubscription: async (subscription) => { - app.log.info(`Closing subscription for team ${subscription.Team.hashid}`) + if (subscription.subscription) { + app.log.info(`Canceling subscription ${subscription.subscription} for team ${subscription.Team.hashid}`) - await stripe.subscriptions.del(subscription.subscription, { - invoice_now: true, - prorate: true - }) + await stripe.subscriptions.del(subscription.subscription, { + invoice_now: true, + prorate: true + }) + } subscription.status = app.db.models.Subscription.STATUS.CANCELED await subscription.save() }, @@ -538,10 +540,16 @@ module.exports.init = async function (app) { app.log.info(`Canceling existing subscription ${existingSubscription} for team ${team.hashid}`) // There is an existing subscription to cancel try { - // Note: stripe.subscriptions.del is deprecated in the latest - // version of the module - however at our current version there - // is no .cancel to use. - await stripe.subscriptions.del(existingSubscription) + // We do not use `app.billing.closeSubscription` because + // that expects a Subscription object. However, we've already + // updated the local Subscription object to remove the information + // needed by closeSubscription. This is to ensure when the + // stripe callback arrives we don't trigger a suspension of + // the team resources. + await stripe.subscriptions.del(subscription.subscription, { + invoice_now: true, + prorate: true + }) } catch (err) { app.log.warn(`Error canceling existing subscription ${existingSubscription} for team ${team.hashid}: ${err.toString()}`) } diff --git a/forge/routes/api/team.js b/forge/routes/api/team.js index 0617106385..6c4abb5d9e 100644 --- a/forge/routes/api/team.js +++ b/forge/routes/api/team.js @@ -486,8 +486,7 @@ module.exports = async function (app) { try { if (app.license.active() && app.billing) { const subscription = await request.team.getSubscription() - if (subscription && !subscription.isTrial()) { - // const subId = subscription.subscription + if (subscription && !subscription.isTrial() && !subscription.isUnmanaged()) { await app.billing.closeSubscription(subscription) } }