Fix Active Workspaces check (#6084)

We have recently deprecated our subscriptionStatus on workspace to
replace it by a check on existing subscription (+ freeAccess
featureFlag) but the logic was not properly implemented
This commit is contained in:
Charles Bochet
2024-07-01 16:45:56 +02:00
committed by GitHub
parent bb627a91e2
commit 88915291d9
5 changed files with 87 additions and 52 deletions

View File

@ -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,
]),
);
}
}

View File

@ -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;

View File

@ -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> {