Skip to content

The open alternative to Salesforce, designed for AI.

Cancel billing before removing members on workspace soft delete (#28152)

Etienne129ecfee6f18
diff --git a/packages/twenty-server/src/engine/core-modules/workspace/services/workspace.service.ts b/packages/twenty-server/src/engine/core-modules/workspace/services/workspace.service.ts
index 936a5ab18f1..d7609356aae 100644
--- a/packages/twenty-server/src/engine/core-modules/workspace/services/workspace.service.ts
+++ b/packages/twenty-server/src/engine/core-modules/workspace/services/workspace.service.ts
@@ -712,6 +712,10 @@ export class WorkspaceService {
return this.hardDeleteWorkspaceWithApplicationUninstallLock(workspace);
}
+ if (this.billingService.isBillingEnabled()) {
+ await this.billingSubscriptionService.cancelSubscription(workspace.id);
+ }
+
const userWorkspaces = await this.userWorkspaceRepository.find({
where: {
workspaceId: id,
@@ -726,10 +730,6 @@ export class WorkspaceService {
this.logger.log(`workspace ${id} cache flushed`);
- if (this.billingService.isBillingEnabled()) {
- await this.billingSubscriptionService.cancelSubscription(workspace.id);
- }
-
await this.workspaceRepository.softDelete({ id, deletedAt: IsNull() });
await this.coreEntityCacheService.invalidate('workspaceEntity', id);
diff --git a/packages/twenty-server/src/engine/workspace-manager/workspace-cleaner/services/cleaner.workspace-service.ts b/packages/twenty-server/src/engine/workspace-manager/workspace-cleaner/services/cleaner.workspace-service.ts
index 7724b14ec0d..1099cc49af2 100644
--- a/packages/twenty-server/src/engine/workspace-manager/workspace-cleaner/services/cleaner.workspace-service.ts
+++ b/packages/twenty-server/src/engine/workspace-manager/workspace-cleaner/services/cleaner.workspace-service.ts
@@ -15,7 +15,6 @@ import { In, Repository } from 'typeorm';
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 { BillingSubscriptionService } from 'src/engine/core-modules/billing/services/billing-subscription.service';
import {
KeyValuePairEntity,
KeyValuePairType,
@@ -27,7 +26,6 @@ import { InjectMessageQueue } from 'src/engine/core-modules/message-queue/decora
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';
import { TwentyConfigService } from 'src/engine/core-modules/twenty-config/twenty-config.service';
-import { UserWorkspaceEntity } from 'src/engine/core-modules/user-workspace/user-workspace.entity';
import { UserService } from 'src/engine/core-modules/user/services/user.service';
import { UserVarsService } from 'src/engine/core-modules/user/user-vars/services/user-vars.service';
import { WorkspaceService } from 'src/engine/core-modules/workspace/services/workspace.service';
@@ -76,9 +74,6 @@ export class CleanerWorkspaceService {
private readonly keyValuePairRepository: Repository<KeyValuePairEntity>,
@InjectWorkspaceScopedRepository(BillingSubscriptionEntity)
private readonly billingSubscriptionRepository: WorkspaceScopedRepository<BillingSubscriptionEntity>,
- private readonly billingSubscriptionService: BillingSubscriptionService,
- @InjectRepository(UserWorkspaceEntity)
- private readonly userWorkspaceRepository: Repository<UserWorkspaceEntity>,
private readonly i18nService: I18nService,
private readonly workspaceDomainsService: WorkspaceDomainsService,
@InjectMessageQueue(MessageQueue.workspaceDestroyQueue)
@@ -307,26 +302,6 @@ export class CleanerWorkspaceService {
);
if (!dryRun) {
- const userWorkspaces = await this.userWorkspaceRepository.find({
- where: {
- workspaceId: workspace.id,
- },
- withDeleted: true,
- });
-
- for (const userWorkspace of userWorkspaces) {
- await this.workspaceService.handleRemoveWorkspaceMember(
- workspace.id,
- userWorkspace.userId,
- );
- }
-
- if (this.twentyConfigService.get('IS_BILLING_ENABLED')) {
- await this.billingSubscriptionService.cancelSubscription(
- workspace.id,
- );
- }
-
await this.workspaceService.deleteWorkspace(workspace.id, true);
}
} else {
diff --git a/packages/twenty-server/src/engine/workspace-manager/workspace-cleaner/workspace-cleaner.module.ts b/packages/twenty-server/src/engine/workspace-manager/workspace-cleaner/workspace-cleaner.module.ts
index 04cfed1df2a..33b701ec789 100644
--- a/packages/twenty-server/src/engine/workspace-manager/workspace-cleaner/workspace-cleaner.module.ts
+++ b/packages/twenty-server/src/engine/workspace-manager/workspace-cleaner/workspace-cleaner.module.ts
@@ -1,12 +1,10 @@
import { Module } from '@nestjs/common';
import { TypeOrmModule } from '@nestjs/typeorm';
-import { BillingModule } from 'src/engine/core-modules/billing/billing.module';
import { BillingSubscriptionEntity } from 'src/engine/core-modules/billing/entities/billing-subscription.entity';
import { WorkspaceDomainsModule } from 'src/engine/core-modules/domain/workspace-domains/workspace-domains.module';
import { KeyValuePairEntity } from 'src/engine/core-modules/key-value-pair/key-value-pair.entity';
import { MetricsModule } from 'src/engine/core-modules/metrics/metrics.module';
-import { UserWorkspaceEntity } from 'src/engine/core-modules/user-workspace/user-workspace.entity';
import { UserVarsModule } from 'src/engine/core-modules/user/user-vars/user-vars.module';
import { UserModule } from 'src/engine/core-modules/user/user.module';
import { WorkspaceEntity } from 'src/engine/core-modules/workspace/workspace.entity';
@@ -24,14 +22,12 @@ import { CleanerWorkspaceService } from 'src/engine/workspace-manager/workspace-
imports: [
TypeOrmModule.forFeature([
WorkspaceEntity,
- UserWorkspaceEntity,
BillingSubscriptionEntity,
KeyValuePairEntity,
]),
WorkspaceModule,
UserVarsModule,
UserModule,
- BillingModule,
MetricsModule,
WorkspaceDomainsModule,
],