Skip to content

The open alternative to Salesforce, designed for AI.

fix(billing): guard workspace suspend against concurrent soft-delete (#22955)

Paul Rastoinfe10975927e1
diff --git a/packages/twenty-server/src/engine/core-modules/workspace/services/__tests__/workspace.service.spec.ts b/packages/twenty-server/src/engine/core-modules/workspace/services/__tests__/workspace.service.spec.ts
index b30da46b968..5c8fcfbd0b5 100644
--- a/packages/twenty-server/src/engine/core-modules/workspace/services/__tests__/workspace.service.spec.ts
+++ b/packages/twenty-server/src/engine/core-modules/workspace/services/__tests__/workspace.service.spec.ts
@@ -1,7 +1,8 @@
import { Test, type TestingModule } from '@nestjs/testing';
import { getDataSourceToken, getRepositoryToken } from '@nestjs/typeorm';
-import { type Repository } from 'typeorm';
+import { WorkspaceActivationStatus } from 'twenty-shared/workspace';
+import { IsNull, Not, type Repository } from 'typeorm';
import { BillingSubscriptionService } from 'src/engine/core-modules/billing/services/billing-subscription.service';
import { BillingService } from 'src/engine/core-modules/billing/services/billing.service';
@@ -57,6 +58,7 @@ describe('WorkspaceService', () => {
findOne: jest.fn(),
softDelete: jest.fn(),
delete: jest.fn(),
+ update: jest.fn(),
manager: {
connection: { driver: { options: { type: 'postgres' } } },
},
@@ -374,4 +376,36 @@ describe('WorkspaceService', () => {
});
});
});
+
+ describe('suspendWorkspace', () => {
+ it('should only suspend workspaces that are not already suspended and not soft-deleted', async () => {
+ jest
+ .spyOn(workspaceRepository, 'update')
+ .mockResolvedValue({ affected: 1 } as never);
+
+ const hasBeenSuspended = await service.suspendWorkspace('workspace-id');
+
+ expect(workspaceRepository.update).toHaveBeenCalledWith(
+ {
+ id: 'workspace-id',
+ activationStatus: Not(WorkspaceActivationStatus.SUSPENDED),
+ deletedAt: IsNull(),
+ },
+ expect.objectContaining({
+ activationStatus: WorkspaceActivationStatus.SUSPENDED,
+ }),
+ );
+ expect(hasBeenSuspended).toBe(true);
+ });
+
+ it('should report no suspension when the guarded update affects no rows', async () => {
+ jest
+ .spyOn(workspaceRepository, 'update')
+ .mockResolvedValue({ affected: 0 } as never);
+
+ const hasBeenSuspended = await service.suspendWorkspace('workspace-id');
+
+ expect(hasBeenSuspended).toBe(false);
+ });
+ });
});
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 48707b50c61..bb6b722dfd1 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
@@ -493,6 +493,7 @@ export class WorkspaceService {
{
id,
activationStatus: Not(WorkspaceActivationStatus.SUSPENDED),
+ deletedAt: IsNull(),
},
{
activationStatus: WorkspaceActivationStatus.SUSPENDED,