The open alternative to Salesforce, designed for AI.
TypeScript crmcrm-systemcustomergood-first-issuegraphqlhacktoberfestjavascriptmarketingmonoreponestjsopen-sourcepostgresqlreactreactjssalestypescriptweb
feat(billing): exempt email domains from the trial workspace limit (#29147)
Etienne1136780aca27
diff --git a/packages/twenty-server/src/engine/core-modules/billing/billing.resolver.ts b/packages/twenty-server/src/engine/core-modules/billing/billing.resolver.tsindex 59f35d2b13c..1479ed9dece 100644--- a/packages/twenty-server/src/engine/core-modules/billing/billing.resolver.ts+++ b/packages/twenty-server/src/engine/core-modules/billing/billing.resolver.ts@@ -143,6 +143,7 @@ export class BillingResolver {return await this.billingPortalWorkspaceService.withTrialLimitLock({userId: user.id,+ userEmail: user.email,workspaceId: workspace.id,createSubscription: async (isTrialLimitReached) => {const isPaymentMethodRequired =@@ -221,6 +222,7 @@ export class BillingResolver {return await this.billingPortalWorkspaceService.withTrialLimitLock({userId: user.id,+ userEmail: user.email,workspaceId: workspace.id,createSubscription: (isTrialLimitReached) =>this.billingPortalWorkspaceService.createSubscriptionPaymentIntent({diff --git a/packages/twenty-server/src/engine/core-modules/billing/services/billing-portal.workspace-service.ts b/packages/twenty-server/src/engine/core-modules/billing/services/billing-portal.workspace-service.tsindex 7e5220e2496..c7b405c1581 100644--- a/packages/twenty-server/src/engine/core-modules/billing/services/billing-portal.workspace-service.ts+++ b/packages/twenty-server/src/engine/core-modules/billing/services/billing-portal.workspace-service.ts@@ -8,6 +8,7 @@ import {isDefined,isNonEmptyArray,} from 'twenty-shared/utils';+import { isNonEmptyString } from '@sniptt/guards';import { Not, Repository } from 'typeorm';import type Stripe from 'stripe';@@ -32,10 +33,12 @@ import { findSellableBaseProductPriceOrThrow } from 'src/engine/core-modules/bilimport { isSellableCatalogPrice } from 'src/engine/core-modules/billing/utils/is-sellable-catalog-price.util';import { CacheLockService } from 'src/engine/core-modules/cache-lock/cache-lock.service';import { WorkspaceDomainsService } from 'src/engine/core-modules/domain/workspace-domains/services/workspace-domains.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 { type WorkspaceEntity } from 'src/engine/core-modules/workspace/workspace.entity';import { InjectWorkspaceScopedRepository } from 'src/engine/twenty-orm/workspace-scoped-repository/inject-workspace-scoped-repository.decorator';import { WorkspaceScopedRepository } from 'src/engine/twenty-orm/workspace-scoped-repository/workspace-scoped-repository';+import { getDomainFromEmail } from 'src/utils/get-domain-from-email';@Injectable()export class BillingPortalWorkspaceService {protected readonly logger = new Logger(BillingPortalWorkspaceService.name);@@ -52,15 +55,22 @@ export class BillingPortalWorkspaceService {@InjectRepository(UserWorkspaceEntity)private readonly userWorkspaceRepository: Repository<UserWorkspaceEntity>,private readonly cacheLockService: CacheLockService,+ private readonly twentyConfigService: TwentyConfigService,) {}async hasReachedTrialLimit({userId,+ userEmail,workspaceId,}: {userId: string;+ userEmail: string;workspaceId: string;}): Promise<boolean> {+ if (this.isTrialLimitExemptEmail(userEmail)) {+ return false;+ }+const otherTrialWorkspaceCount = await this.userWorkspaceRepository.createQueryBuilder('userWorkspace').innerJoin(@@ -80,23 +90,36 @@ export class BillingPortalWorkspaceService {async withTrialLimitLock<TResult>({userId,+ userEmail,workspaceId,createSubscription,}: {userId: string;+ userEmail: string;workspaceId: string;createSubscription: (isTrialLimitReached: boolean) => Promise<TResult>;}): Promise<TResult> {return await this.cacheLockService.withLock(async () =>createSubscription(- await this.hasReachedTrialLimit({ userId, workspaceId }),+ await this.hasReachedTrialLimit({ userId, userEmail, workspaceId }),),buildBillingTrialLimitLockKey(userId),BILLING_SUBSCRIPTION_STATE_LOCK_OPTIONS,);}+ private isTrialLimitExemptEmail(email: string): boolean {+ const domain = getDomainFromEmail(email)?.toLowerCase();++ return (+ isNonEmptyString(domain) &&+ this.twentyConfigService+ .get('BILLING_TRIAL_LIMIT_EXEMPT_EMAIL_DOMAINS')+ .some((exemptDomain) => exemptDomain.toLowerCase() === domain)+ );+ }+async computeCheckoutSessionURL({user,workspace,diff --git a/packages/twenty-server/src/engine/core-modules/twenty-config/config-variables.ts b/packages/twenty-server/src/engine/core-modules/twenty-config/config-variables.tsindex b3894be0485..1966efb76b2 100644--- a/packages/twenty-server/src/engine/core-modules/twenty-config/config-variables.ts+++ b/packages/twenty-server/src/engine/core-modules/twenty-config/config-variables.ts@@ -1027,6 +1027,15 @@ export class ConfigVariables {@ValidateIf((env) => env.IS_BILLING_ENABLED === true)BILLING_FREE_TRIAL_WITHOUT_CREDIT_CARD_DURATION_IN_DAYS = 7;+ @ConfigVariablesMetadata({+ group: ConfigVariablesGroup.BILLING_CONFIG,+ description:+ 'Email domains whose users keep the free trial on every workspace, however many trialing workspaces they are already in (twenty.com, acme.com). Exact domain match, subdomains not included',+ type: ConfigVariableType.ARRAY,+ })+ @IsOptional()+ BILLING_TRIAL_LIMIT_EXEMPT_EMAIL_DOMAINS: string[] = [];+@ConfigVariablesMetadata({group: ConfigVariablesGroup.BILLING_CONFIG,description: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 bc407f60153..29ee9536ea9 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@@ -361,6 +361,7 @@ export class WorkspaceResolver {return !(await this.billingPortalWorkspaceService.hasReachedTrialLimit({userId: user.id,+ userEmail: user.email,workspaceId: workspace.id,}));}