Skip to content

Commit

Permalink
Merge pull request FlowFuse#3181 from FlowFuse/fix-unmanaged-team-delete
Browse files Browse the repository at this point in the history
Fix team delete for unmanaged-billing team
  • Loading branch information
hardillb authored Dec 7, 2023
2 parents c7b9f95 + 257a980 commit a9b1fee
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 13 deletions.
2 changes: 0 additions & 2 deletions forge/containers/stub/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -155,8 +155,6 @@ module.exports = {
resolve()
}, 250)
})
} else {
throw new Error(`${project.id} not found`)
}
},
/**
Expand Down
26 changes: 17 additions & 9 deletions forge/ee/lib/billing/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()
},
Expand Down Expand Up @@ -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()}`)
}
Expand Down
3 changes: 1 addition & 2 deletions forge/routes/api/team.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
Expand Down

0 comments on commit a9b1fee

Please sign in to comment.