Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix team delete for unmanaged-billing team #3181

Merged
merged 1 commit into from
Dec 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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