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:
+13
-1
@@ -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,
|
||||
|
||||
Reference in New Issue
Block a user