5188 bug some canceled subscriptions are billed (#5254)

When user is deleting its account on a specific workspace, we remove it
as if it was a workspaceMember, and if no workspaceMember remains, we
delete the workspace and the associated stripe subscription
This commit is contained in:
martmull
2024-05-13 10:23:32 +02:00
committed by GitHub
parent 92acfe57a1
commit 1ac8abb118
11 changed files with 157 additions and 101 deletions

View File

@ -196,7 +196,10 @@ export class BillingService {
? frontBaseUrl + successUrlPath
: frontBaseUrl;
let quantity = 1;
const quantity =
(await this.userWorkspaceService.getWorkspaceMemberCount(
user.defaultWorkspaceId,
)) || 1;
const stripeCustomerId = (
await this.billingSubscriptionRepository.findOneBy({
@ -204,16 +207,6 @@ export class BillingService {
})
)?.stripeCustomerId;
try {
quantity = await this.userWorkspaceService.getWorkspaceMemberCount(
user.defaultWorkspaceId,
);
} catch (e) {
this.logger.error(
`Failed to get workspace member count for workspace ${user.defaultWorkspaceId}`,
);
}
const session = await this.stripeService.createCheckoutSession(
user,
priceId,
@ -260,6 +253,26 @@ export class BillingService {
| Stripe.CustomerSubscriptionCreatedEvent.Data
| Stripe.CustomerSubscriptionDeletedEvent.Data,
) {
const workspace = this.workspaceRepository.find({
where: { id: workspaceId },
});
if (!workspace) {
return;
}
await this.workspaceRepository.update(workspaceId, {
subscriptionStatus: data.object.status,
});
const billingSubscription = await this.getCurrentBillingSubscription({
workspaceId,
});
if (!billingSubscription) {
return;
}
await this.billingSubscriptionRepository.upsert(
{
workspaceId: workspaceId,
@ -274,18 +287,6 @@ export class BillingService {
},
);
await this.workspaceRepository.update(workspaceId, {
subscriptionStatus: data.object.status,
});
const billingSubscription = await this.getCurrentBillingSubscription({
workspaceId,
});
if (!billingSubscription) {
return;
}
await this.billingSubscriptionItemRepository.upsert(
data.object.items.data.map((item) => {
return {

View File

@ -21,7 +21,7 @@ export class UpdateSubscriptionJob
const workspaceMembersCount =
await this.userWorkspaceService.getWorkspaceMemberCount(data.workspaceId);
if (workspaceMembersCount <= 0) {
if (!workspaceMembersCount || workspaceMembersCount <= 0) {
return;
}