Billing - fix duplicate key value violates unique constraint "IndexOnActiveSubscriptionPerWorkspace" (#16676)

[Sentry event
example](https://twenty-v7.sentry.io/issues/6606854024/events/4a78b9d1a9d5468e897ec0f90a643112/)


fixes https://github.com/twentyhq/twenty/issues/16573
This commit is contained in:
Etienne
2025-12-18 15:59:07 +01:00
committed by GitHub
parent 5984be992d
commit 65f0a5bb18
@@ -3,7 +3,7 @@
import { Injectable, Logger } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { findOrThrow, isDefined } from 'twenty-shared/utils';
import { findOrThrow, isDefined, isNonEmptyArray } from 'twenty-shared/utils';
import { Not, Repository } from 'typeorm';
import type Stripe from 'stripe';
@@ -93,6 +93,18 @@ export class BillingPortalWorkspaceService {
successUrlPath,
});
if (
isNonEmptyArray(customer?.billingSubscriptions) &&
customer.billingSubscriptions.some(
(subscription) => subscription.status !== SubscriptionStatus.Canceled,
)
) {
throw new BillingException(
'Customer already has a non-canceled billing subscription',
BillingExceptionCode.BILLING_SUBSCRIPTION_INVALID,
);
}
const subscription =
await this.stripeCheckoutService.createDirectSubscription({
user,