Skip to content

The open alternative to Salesforce, designed for AI.

fix(billing): map already-subscribed checkout retry to a user error (#24150)

Thomas Trompette6eea2e64fb84
diff --git a/packages/twenty-server/src/engine/core-modules/billing/billing.exception.ts b/packages/twenty-server/src/engine/core-modules/billing/billing.exception.ts
index 04796482334..45108bc3b8d 100644
--- a/packages/twenty-server/src/engine/core-modules/billing/billing.exception.ts
+++ b/packages/twenty-server/src/engine/core-modules/billing/billing.exception.ts
@@ -16,6 +16,7 @@ export enum BillingExceptionCode {
BILLING_SUBSCRIPTION_NOT_FOUND = 'BILLING_SUBSCRIPTION_NOT_FOUND',
BILLING_SUBSCRIPTION_ITEM_NOT_FOUND = 'BILLING_SUBSCRIPTION_ITEM_NOT_FOUND',
BILLING_SUBSCRIPTION_INVALID = 'BILLING_SUBSCRIPTION_INVALID',
+ BILLING_SUBSCRIPTION_ALREADY_EXISTS = 'BILLING_SUBSCRIPTION_ALREADY_EXISTS',
BILLING_SUBSCRIPTION_EVENT_WORKSPACE_NOT_FOUND = 'BILLING_SUBSCRIPTION_EVENT_WORKSPACE_NOT_FOUND',
BILLING_CUSTOMER_EVENT_WORKSPACE_NOT_FOUND = 'BILLING_CUSTOMER_EVENT_WORKSPACE_NOT_FOUND',
BILLING_ACTIVE_SUBSCRIPTION_NOT_FOUND = 'BILLING_ACTIVE_SUBSCRIPTION_NOT_FOUND',
@@ -59,6 +60,8 @@ const getBillingExceptionUserFriendlyMessage = (code: BillingExceptionCode) => {
return msg`Subscription item not found.`;
case BillingExceptionCode.BILLING_SUBSCRIPTION_INVALID:
return msg`Invalid subscription.`;
+ case BillingExceptionCode.BILLING_SUBSCRIPTION_ALREADY_EXISTS:
+ return msg`This workspace already has a subscription.`;
case BillingExceptionCode.BILLING_SUBSCRIPTION_EVENT_WORKSPACE_NOT_FOUND:
return msg`Workspace not found for subscription event.`;
case BillingExceptionCode.BILLING_CUSTOMER_EVENT_WORKSPACE_NOT_FOUND:
diff --git a/packages/twenty-server/src/engine/core-modules/billing/services/billing-portal.workspace-service.ts b/packages/twenty-server/src/engine/core-modules/billing/services/billing-portal.workspace-service.ts
index f60b8e37b57..8aef3045d36 100644
--- a/packages/twenty-server/src/engine/core-modules/billing/services/billing-portal.workspace-service.ts
+++ b/packages/twenty-server/src/engine/core-modules/billing/services/billing-portal.workspace-service.ts
@@ -111,7 +111,7 @@ export class BillingPortalWorkspaceService {
) {
throw new BillingException(
'Customer already has a non-canceled billing subscription',
- BillingExceptionCode.BILLING_SUBSCRIPTION_INVALID,
+ BillingExceptionCode.BILLING_SUBSCRIPTION_ALREADY_EXISTS,
);
}
diff --git a/packages/twenty-server/src/engine/core-modules/billing/utils/__tests__/billing-graphql-api-exception-handler.util.spec.ts b/packages/twenty-server/src/engine/core-modules/billing/utils/__tests__/billing-graphql-api-exception-handler.util.spec.ts
index 15b6869a2ac..7d0b4ca3901 100644
--- a/packages/twenty-server/src/engine/core-modules/billing/utils/__tests__/billing-graphql-api-exception-handler.util.spec.ts
+++ b/packages/twenty-server/src/engine/core-modules/billing/utils/__tests__/billing-graphql-api-exception-handler.util.spec.ts
@@ -49,6 +49,21 @@ describe('billingGraphqlApiExceptionHandler', () => {
);
});
+ it('maps an already existing subscription to BAD_USER_INPUT', () => {
+ const error = new BillingException(
+ 'Customer already has a non-canceled billing subscription',
+ BillingExceptionCode.BILLING_SUBSCRIPTION_ALREADY_EXISTS,
+ );
+
+ const graphqlError = catchGraphqlError(error);
+
+ expect(graphqlError.extensions.code).toBe(ErrorCode.BAD_USER_INPUT);
+ expect(graphqlError.extensions.subCode).toBe(
+ BillingExceptionCode.BILLING_SUBSCRIPTION_ALREADY_EXISTS,
+ );
+ expect(graphqlError.extensions.userFriendlyMessage).toBeDefined();
+ });
+
it('maps internal billing failures to INTERNAL_SERVER_ERROR', () => {
const error = new BillingException(
'Invalid price tiers',
diff --git a/packages/twenty-server/src/engine/core-modules/billing/utils/get-billing-exception-status-code.util.ts b/packages/twenty-server/src/engine/core-modules/billing/utils/get-billing-exception-status-code.util.ts
index eb448f1fc26..02c1a25eb87 100644
--- a/packages/twenty-server/src/engine/core-modules/billing/utils/get-billing-exception-status-code.util.ts
+++ b/packages/twenty-server/src/engine/core-modules/billing/utils/get-billing-exception-status-code.util.ts
@@ -28,6 +28,7 @@ export const getBillingExceptionStatusCode = (
case BillingExceptionCode.BILLING_CREDIT_AMOUNT_INVALID:
case BillingExceptionCode.BILLING_CREDIT_GRANT_VALIDITY_INVALID:
case BillingExceptionCode.BILLING_CREDIT_GRANT_TYPE_NOT_GRANTABLE:
+ case BillingExceptionCode.BILLING_SUBSCRIPTION_ALREADY_EXISTS:
return 400;
case BillingExceptionCode.BILLING_CREDITS_EXHAUSTED:
return 402;