The open alternative to Salesforce, designed for AI.
TypeScript crmcrm-systemcustomergood-first-issuegraphqlhacktoberfestjavascriptmarketingmonoreponestjsopen-sourcepostgresqlreactreactjssalestypescriptweb
Fix Active Workspaces check (#6084)
Charles Bochet88915291d9f1
diff --git a/packages/twenty-server/src/engine/core-modules/billing/billing.service.ts b/packages/twenty-server/src/engine/core-modules/billing/billing.service.tsindex d5cd471fe7a..34cae6a376e 100644--- a/packages/twenty-server/src/engine/core-modules/billing/billing.service.ts+++ b/packages/twenty-server/src/engine/core-modules/billing/billing.service.ts@@ -4,34 +4,68 @@ import { InjectRepository } from '@nestjs/typeorm';import { In, Repository } from 'typeorm';import { EnvironmentService } from 'src/engine/integrations/environment/environment.service';-import { SubscriptionStatus } from 'src/engine/core-modules/billing/entities/billing-subscription.entity';+import {+ BillingSubscription,+ SubscriptionStatus,+} from 'src/engine/core-modules/billing/entities/billing-subscription.entity';import { Workspace } from 'src/engine/core-modules/workspace/workspace.entity';+import {+ FeatureFlagEntity,+ FeatureFlagKeys,+} from 'src/engine/core-modules/feature-flag/feature-flag.entity';@Injectable()export class BillingService {protected readonly logger = new Logger(BillingService.name);constructor(private readonly environmentService: EnvironmentService,+ @InjectRepository(BillingSubscription, 'core')+ private readonly billingSubscriptionRepository: Repository<BillingSubscription>,+ @InjectRepository(FeatureFlagEntity, 'core')+ private readonly featureFlagRepository: Repository<FeatureFlagEntity>,@InjectRepository(Workspace, 'core')private readonly workspaceRepository: Repository<Workspace>,) {}async getActiveSubscriptionWorkspaceIds() {- return (- await this.workspaceRepository.find({- where: this.environmentService.get('IS_BILLING_ENABLED')- ? {- currentBillingSubscription: {- status: In([- SubscriptionStatus.Active,- SubscriptionStatus.Trialing,- SubscriptionStatus.PastDue,- ]),- },- }- : {},- select: ['id'],- })- ).map((workspace) => workspace.id);+ if (!this.environmentService.get('IS_BILLING_ENABLED')) {+ return (await this.workspaceRepository.find({ select: ['id'] })).map(+ (workspace) => workspace.id,+ );+ }++ const activeSubscriptions = await this.billingSubscriptionRepository.find({+ where: {+ status: In([+ SubscriptionStatus.Active,+ SubscriptionStatus.Trialing,+ SubscriptionStatus.PastDue,+ ]),+ },+ select: ['workspaceId'],+ });++ const freeAccessFeatureFlags = await this.featureFlagRepository.find({+ where: {+ key: FeatureFlagKeys.IsFreeAccessEnabled,+ value: true,+ },+ select: ['workspaceId'],+ });++ const activeWorkspaceIdsBasedOnSubscriptions = activeSubscriptions.map(+ (subscription) => subscription.workspaceId,+ );++ const activeWorkspaceIdsBasedOnFeatureFlags = freeAccessFeatureFlags.map(+ (featureFlag) => featureFlag.workspaceId,+ );++ return Array.from(+ new Set([+ ...activeWorkspaceIdsBasedOnSubscriptions,+ ...activeWorkspaceIdsBasedOnFeatureFlags,+ ]),+ );}}diff --git a/packages/twenty-server/src/engine/core-modules/workspace/workspace.entity.ts b/packages/twenty-server/src/engine/core-modules/workspace/workspace.entity.tsindex 3d8efa2b6cf..6b4d2b42534 100644--- a/packages/twenty-server/src/engine/core-modules/workspace/workspace.entity.ts+++ b/packages/twenty-server/src/engine/core-modules/workspace/workspace.entity.ts@@ -84,9 +84,6 @@ export class Workspace {@OneToMany(() => FeatureFlagEntity, (featureFlag) => featureFlag.workspace)featureFlags: Relation<FeatureFlagEntity[]>;- @Field({ nullable: true })- currentBillingSubscription: BillingSubscription;-@Field({ nullable: true })workspaceMembersCount: number;diff --git a/packages/twenty-server/src/engine/core-modules/workspace/workspace.resolver.ts b/packages/twenty-server/src/engine/core-modules/workspace/workspace.resolver.tsindex 9840cf1109c..5ead7800d66 100644--- a/packages/twenty-server/src/engine/core-modules/workspace/workspace.resolver.ts+++ b/packages/twenty-server/src/engine/core-modules/workspace/workspace.resolver.ts@@ -118,7 +118,7 @@ export class WorkspaceResolver {return this.workspaceCacheVersionService.getVersion(workspace.id);}- @ResolveField(() => BillingSubscription)+ @ResolveField(() => BillingSubscription, { nullable: true })async currentBillingSubscription(@Parent() workspace: Workspace,): Promise<BillingSubscription | null> {diff --git a/packages/twenty-server/src/engine/workspace-manager/workspace-cleaner/commands/delete-incomplete-workspaces.command.ts b/packages/twenty-server/src/engine/workspace-manager/workspace-cleaner/commands/delete-workspaces.command.tssimilarity index 54%rename from packages/twenty-server/src/engine/workspace-manager/workspace-cleaner/commands/delete-incomplete-workspaces.command.tsrename to packages/twenty-server/src/engine/workspace-manager/workspace-cleaner/commands/delete-workspaces.command.tsindex fb5faa313a1..2bbc17b7587 100644--- a/packages/twenty-server/src/engine/workspace-manager/workspace-cleaner/commands/delete-incomplete-workspaces.command.ts+++ b/packages/twenty-server/src/engine/workspace-manager/workspace-cleaner/commands/delete-workspaces.command.ts@@ -2,27 +2,29 @@ import { InjectRepository } from '@nestjs/typeorm';import { Logger } from '@nestjs/common';import { Command, CommandRunner, Option } from 'nest-commander';-import { FindOptionsWhere, In, Repository } from 'typeorm';+import { In, Repository } from 'typeorm';-import { WorkspaceService } from 'src/engine/core-modules/workspace/services/workspace.service';import { Workspace } from 'src/engine/core-modules/workspace/workspace.entity';-import { getDryRunLogHeader } from 'src/utils/get-dry-run-log-header';import { DataSourceService } from 'src/engine/metadata-modules/data-source/data-source.service';-import { SubscriptionStatus } from 'src/engine/core-modules/billing/entities/billing-subscription.entity';+import { getDryRunLogHeader } from 'src/utils/get-dry-run-log-header';+import { WorkspaceService } from 'src/engine/core-modules/workspace/services/workspace.service';+import { LoadServiceWithWorkspaceContext } from 'src/engine/twenty-orm/context/load-service-with-workspace.context';-type DeleteIncompleteWorkspacesCommandOptions = {+type DeleteWorkspacesCommandOptions = {dryRun?: boolean;- workspaceIds?: string[];+ workspaceIds: string[];};@Command({- name: 'workspace:delete-incomplete',- description: 'Delete incomplete workspaces',+ name: 'workspace:delete',+ description: 'Delete workspace',})-export class DeleteIncompleteWorkspacesCommand extends CommandRunner {- private readonly logger = new Logger(DeleteIncompleteWorkspacesCommand.name);+export class DeleteWorkspacesCommand extends CommandRunner {+ private readonly logger = new Logger(DeleteWorkspacesCommand.name);+constructor(private readonly workspaceService: WorkspaceService,+ private readonly loadServiceWithWorkspaceContext: LoadServiceWithWorkspaceContext,@InjectRepository(Workspace, 'core')private readonly workspaceRepository: Repository<Workspace>,private readonly dataSourceService: DataSourceService,@@ -42,7 +44,7 @@ export class DeleteIncompleteWorkspacesCommand extends CommandRunner {@Option({flags: '-w, --workspace-ids [workspace_ids]',description: 'comma separated workspace ids',- required: false,+ required: true,})parseWorkspaceIds(value: string): string[] {return value.split(',');@@ -50,41 +52,43 @@ export class DeleteIncompleteWorkspacesCommand extends CommandRunner {async run(_passedParam: string[],- options: DeleteIncompleteWorkspacesCommandOptions,+ options: DeleteWorkspacesCommandOptions,): Promise<void> {- const where: FindOptionsWhere<Workspace> = {- currentBillingSubscription: { status: SubscriptionStatus.Incomplete },- };+ const workspaces = await this.workspaceRepository.find({+ where: { id: In(options.workspaceIds) },+ });- if (options.workspaceIds) {- where.id = In(options.workspaceIds);- }-- const incompleteWorkspaces = await this.workspaceRepository.findBy(where);const dataSources =await this.dataSourceService.getManyDataSourceMetadata();+const workspaceIdsWithSchema = dataSources.map((dataSource) => dataSource.workspaceId,);- const incompleteWorkspacesToDelete = incompleteWorkspaces.filter(- (incompleteWorkspace) =>- workspaceIdsWithSchema.includes(incompleteWorkspace.id),++ const workspacesToDelete = workspaces.filter((Workspace) =>+ workspaceIdsWithSchema.includes(Workspace.id),);- if (incompleteWorkspacesToDelete.length) {+ if (workspacesToDelete.length) {this.logger.log(- `Running Deleting incomplete workspaces on ${incompleteWorkspacesToDelete.length} workspaces`,+ `Running Deleting workspaces on ${workspacesToDelete.length} workspaces`,);}- for (const incompleteWorkspace of incompleteWorkspacesToDelete) {+ for (const workspace of workspacesToDelete) {this.logger.log(`${getDryRunLogHeader(options.dryRun)}Deleting workspace ${- incompleteWorkspace.id- } name: '${incompleteWorkspace.displayName}'`,+ workspace.id+ } name: '${workspace.displayName}'`,);+ const workspaceServiceInstance =+ await this.loadServiceWithWorkspaceContext.load(+ this.workspaceService,+ workspace.id,+ );+if (!options.dryRun) {- await this.workspaceService.softDeleteWorkspace(incompleteWorkspace.id);+ await workspaceServiceInstance.softDeleteWorkspace(workspace.id);}}}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.tsindex 6bf7eb0136a..681755be16a 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@@ -2,12 +2,12 @@ import { Module } from '@nestjs/common';import { TypeOrmModule } from '@nestjs/typeorm';import { WorkspaceModule } from 'src/engine/core-modules/workspace/workspace.module';-import { DeleteIncompleteWorkspacesCommand } from 'src/engine/workspace-manager/workspace-cleaner/commands/delete-incomplete-workspaces.command';import { Workspace } from 'src/engine/core-modules/workspace/workspace.entity';import { CleanInactiveWorkspacesCommand } from 'src/engine/workspace-manager/workspace-cleaner/commands/clean-inactive-workspaces.command';import { StartCleanInactiveWorkspacesCronCommand } from 'src/engine/workspace-manager/workspace-cleaner/commands/start-clean-inactive-workspaces.cron.command';import { StopCleanInactiveWorkspacesCronCommand } from 'src/engine/workspace-manager/workspace-cleaner/commands/stop-clean-inactive-workspaces.cron.command';import { DataSourceModule } from 'src/engine/metadata-modules/data-source/data-source.module';+import { DeleteWorkspacesCommand } from 'src/engine/workspace-manager/workspace-cleaner/commands/delete-workspaces.command';@Module({imports: [@@ -16,7 +16,7 @@ import { DataSourceModule } from 'src/engine/metadata-modules/data-source/data-sDataSourceModule,],providers: [- DeleteIncompleteWorkspacesCommand,+ DeleteWorkspacesCommand,CleanInactiveWorkspacesCommand,StartCleanInactiveWorkspacesCronCommand,StopCleanInactiveWorkspacesCronCommand,