BILLING - Send dis-sync error to sentry (#17242)

Investigate Stripe activity when workspace not found, creating
[alert](https://twenty-v7.sentry.io/issues/alerts/rules/twenty-server/16611050/details/)
in Sentry
This commit is contained in:
Etienne
2026-01-19 16:31:37 +01:00
committed by GitHub
parent 59c7295033
commit f381516d30
2 changed files with 19 additions and 7 deletions
@@ -6,6 +6,7 @@ import { InjectRepository } from '@nestjs/typeorm';
import { isDefined } from 'twenty-shared/utils';
import { WorkspaceActivationStatus } from 'twenty-shared/workspace';
import { In, Repository } from 'typeorm';
import { msg } from '@lingui/core/macro';
import type Stripe from 'stripe';
@@ -18,12 +19,14 @@ import {
BillingExceptionCode,
} from 'src/engine/core-modules/billing/billing.exception';
import { BillingCustomerEntity } from 'src/engine/core-modules/billing/entities/billing-customer.entity';
import { BillingSubscriptionEntity } from 'src/engine/core-modules/billing/entities/billing-subscription.entity';
import { BillingSubscriptionItemEntity } from 'src/engine/core-modules/billing/entities/billing-subscription-item.entity';
import { BillingSubscriptionEntity } from 'src/engine/core-modules/billing/entities/billing-subscription.entity';
import { SubscriptionStatus } from 'src/engine/core-modules/billing/enums/billing-subscription-status.enum';
import { BillingWebhookEvent } from 'src/engine/core-modules/billing/enums/billing-webhook-events.enum';
import { BillingSubscriptionService } from 'src/engine/core-modules/billing/services/billing-subscription.service';
import { StripeBillingAlertService } from 'src/engine/core-modules/billing/stripe/services/stripe-billing-alert.service';
import { StripeCustomerService } from 'src/engine/core-modules/billing/stripe/services/stripe-customer.service';
import { StripeSubscriptionScheduleService } from 'src/engine/core-modules/billing/stripe/services/stripe-subscription-schedule.service';
import { InjectMessageQueue } from 'src/engine/core-modules/message-queue/decorators/message-queue.decorator';
import { MessageQueue } from 'src/engine/core-modules/message-queue/message-queue.constants';
import { MessageQueueService } from 'src/engine/core-modules/message-queue/services/message-queue.service';
@@ -33,8 +36,6 @@ import {
CleanWorkspaceDeletionWarningUserVarsJob,
type CleanWorkspaceDeletionWarningUserVarsJobData,
} from 'src/engine/workspace-manager/workspace-cleaner/jobs/clean-workspace-deletion-warning-user-vars.job';
import { StripeSubscriptionScheduleService } from 'src/engine/core-modules/billing/stripe/services/stripe-subscription-schedule.service';
import { StripeBillingAlertService } from 'src/engine/core-modules/billing/stripe/services/stripe-billing-alert.service';
@Injectable()
// eslint-disable-next-line twenty/inject-workspace-repository
@@ -75,14 +76,26 @@ export class BillingWebhookSubscriptionService {
});
if (!workspace) {
return { noWorkspace: true };
throw new BillingException(
`Workspace not found for subscription event ${event.id} / workspaceId: ${workspaceId}`,
BillingExceptionCode.BILLING_SUBSCRIPTION_EVENT_WORKSPACE_NOT_FOUND,
{
userFriendlyMessage: msg`Workspace ${workspaceId} is not found.`,
},
);
}
if (
isDefined(workspace.deletedAt) &&
type !== BillingWebhookEvent.CUSTOMER_SUBSCRIPTION_DELETED
) {
return { noWorkspace: true };
throw new BillingException(
`Workspace not found for subscription event ${event.id} / workspaceId: ${workspaceId}`,
BillingExceptionCode.BILLING_SUBSCRIPTION_EVENT_WORKSPACE_NOT_FOUND,
{
userFriendlyMessage: msg`Workspace ${workspaceId} is not found.`,
},
);
}
await this.billingCustomerRepository.upsert(
@@ -44,8 +44,6 @@ export class BillingRestApiExceptionFilter implements ExceptionFilter {
switch (exception.code) {
case BillingExceptionCode.BILLING_CUSTOMER_NOT_FOUND:
case BillingExceptionCode.BILLING_ACTIVE_SUBSCRIPTION_NOT_FOUND:
case BillingExceptionCode.BILLING_SUBSCRIPTION_EVENT_WORKSPACE_NOT_FOUND:
case BillingExceptionCode.BILLING_CUSTOMER_EVENT_WORKSPACE_NOT_FOUND:
case BillingExceptionCode.BILLING_PRODUCT_NOT_FOUND:
case BillingExceptionCode.BILLING_PLAN_NOT_FOUND:
case BillingExceptionCode.BILLING_METER_NOT_FOUND:
@@ -72,6 +70,7 @@ export class BillingRestApiExceptionFilter implements ExceptionFilter {
response,
402,
);
case BillingExceptionCode.BILLING_CUSTOMER_EVENT_WORKSPACE_NOT_FOUND:
default:
return this.httpExceptionHandlerService.handleError(
exception,