Revert "Client config not render blocking (#12300)" (#12302)

This reverts commit 4ce7fc6987, to take
more time to address PR comments
This commit is contained in:
Félix Malfait
2025-05-27 09:04:47 +02:00
committed by GitHub
parent e8532faaaa
commit 9cdd0fdac0
18 changed files with 148 additions and 776 deletions

View File

@ -36,4 +36,5 @@ export type CaptchaModuleAsyncOptions = {
) => CaptchaModuleOptions | Promise<CaptchaModuleOptions> | undefined;
} & Pick<ModuleMetadata, 'imports'> &
Pick<FactoryProvider, 'inject'>;
export type CaptchaValidateResult = { success: boolean; error?: string };

View File

@ -1,96 +0,0 @@
import { Test, TestingModule } from '@nestjs/testing';
import { ClientConfigService } from 'src/engine/core-modules/client-config/services/client-config.service';
import { ClientConfigController } from './client-config.controller';
describe('ClientConfigController', () => {
let controller: ClientConfigController;
let clientConfigService: ClientConfigService;
beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
controllers: [ClientConfigController],
providers: [
{
provide: ClientConfigService,
useValue: {
getClientConfig: jest.fn(),
},
},
],
}).compile();
controller = module.get<ClientConfigController>(ClientConfigController);
clientConfigService = module.get<ClientConfigService>(ClientConfigService);
});
it('should be defined', () => {
expect(controller).toBeDefined();
});
describe('getClientConfig', () => {
it('should return client config from service', async () => {
const mockClientConfig = {
billing: {
isBillingEnabled: true,
billingUrl: 'https://billing.example.com',
trialPeriods: [
{
duration: 7,
isCreditCardRequired: false,
},
],
},
authProviders: {
google: true,
magicLink: false,
password: true,
microsoft: false,
sso: [],
},
signInPrefilled: false,
isMultiWorkspaceEnabled: true,
isEmailVerificationRequired: false,
defaultSubdomain: 'app',
frontDomain: 'localhost',
debugMode: true,
support: {
supportDriver: 'none',
supportFrontChatId: undefined,
},
sentry: {
environment: 'development',
release: '1.0.0',
dsn: undefined,
},
captcha: {
provider: undefined,
siteKey: undefined,
},
chromeExtensionId: undefined,
api: {
mutationMaximumAffectedRecords: 100,
},
isAttachmentPreviewEnabled: true,
analyticsEnabled: false,
canManageFeatureFlags: true,
publicFeatureFlags: [],
isMicrosoftMessagingEnabled: false,
isMicrosoftCalendarEnabled: false,
isGoogleMessagingEnabled: false,
isGoogleCalendarEnabled: false,
isConfigVariablesInDbEnabled: false,
};
jest
.spyOn(clientConfigService, 'getClientConfig')
.mockResolvedValue(mockClientConfig);
const result = await controller.getClientConfig();
expect(clientConfigService.getClientConfig).toHaveBeenCalled();
expect(result).toEqual(mockClientConfig);
});
});
});

View File

@ -1,14 +0,0 @@
import { Controller, Get } from '@nestjs/common';
import { ClientConfig } from 'src/engine/core-modules/client-config/client-config.entity';
import { ClientConfigService } from 'src/engine/core-modules/client-config/services/client-config.service';
@Controller('/client-config')
export class ClientConfigController {
constructor(private readonly clientConfigService: ClientConfigService) {}
@Get()
async getClientConfig(): Promise<ClientConfig> {
return this.clientConfigService.getClientConfig();
}
}

View File

@ -2,14 +2,10 @@ import { Module } from '@nestjs/common';
import { DomainManagerModule } from 'src/engine/core-modules/domain-manager/domain-manager.module';
import { ClientConfigController } from './client-config.controller';
import { ClientConfigResolver } from './client-config.resolver';
import { ClientConfigService } from './services/client-config.service';
@Module({
imports: [DomainManagerModule],
controllers: [ClientConfigController],
providers: [ClientConfigResolver, ClientConfigService],
providers: [ClientConfigResolver],
})
export class ClientConfigModule {}

View File

@ -1,6 +1,7 @@
import { Test, TestingModule } from '@nestjs/testing';
import { ClientConfigService } from 'src/engine/core-modules/client-config/services/client-config.service';
import { DomainManagerService } from 'src/engine/core-modules/domain-manager/services/domain-manager.service';
import { TwentyConfigService } from 'src/engine/core-modules/twenty-config/twenty-config.service';
import { ClientConfigResolver } from './client-config.resolver';
@ -12,10 +13,12 @@ describe('ClientConfigResolver', () => {
providers: [
ClientConfigResolver,
{
provide: ClientConfigService,
useValue: {
getClientConfig: jest.fn(),
},
provide: TwentyConfigService,
useValue: {},
},
{
provide: DomainManagerService,
useValue: {},
},
],
}).compile();

View File

@ -1,15 +1,107 @@
import { Query, Resolver } from '@nestjs/graphql';
import { ClientConfigService } from 'src/engine/core-modules/client-config/services/client-config.service';
import { NodeEnvironment } from 'src/engine/core-modules/twenty-config/interfaces/node-environment.interface';
import { DomainManagerService } from 'src/engine/core-modules/domain-manager/services/domain-manager.service';
import { PUBLIC_FEATURE_FLAGS } from 'src/engine/core-modules/feature-flag/constants/public-feature-flag.const';
import { TwentyConfigService } from 'src/engine/core-modules/twenty-config/twenty-config.service';
import { ClientConfig } from './client-config.entity';
@Resolver()
export class ClientConfigResolver {
constructor(private clientConfigService: ClientConfigService) {}
constructor(
private twentyConfigService: TwentyConfigService,
private domainManagerService: DomainManagerService,
) {}
@Query(() => ClientConfig)
async clientConfig(): Promise<ClientConfig> {
return this.clientConfigService.getClientConfig();
const clientConfig: ClientConfig = {
billing: {
isBillingEnabled: this.twentyConfigService.get('IS_BILLING_ENABLED'),
billingUrl: this.twentyConfigService.get('BILLING_PLAN_REQUIRED_LINK'),
trialPeriods: [
{
duration: this.twentyConfigService.get(
'BILLING_FREE_TRIAL_WITH_CREDIT_CARD_DURATION_IN_DAYS',
),
isCreditCardRequired: true,
},
{
duration: this.twentyConfigService.get(
'BILLING_FREE_TRIAL_WITHOUT_CREDIT_CARD_DURATION_IN_DAYS',
),
isCreditCardRequired: false,
},
],
},
authProviders: {
google: this.twentyConfigService.get('AUTH_GOOGLE_ENABLED'),
magicLink: false,
password: this.twentyConfigService.get('AUTH_PASSWORD_ENABLED'),
microsoft: this.twentyConfigService.get('AUTH_MICROSOFT_ENABLED'),
sso: [],
},
signInPrefilled: this.twentyConfigService.get('SIGN_IN_PREFILLED'),
isMultiWorkspaceEnabled: this.twentyConfigService.get(
'IS_MULTIWORKSPACE_ENABLED',
),
isEmailVerificationRequired: this.twentyConfigService.get(
'IS_EMAIL_VERIFICATION_REQUIRED',
),
defaultSubdomain: this.twentyConfigService.get('DEFAULT_SUBDOMAIN'),
frontDomain: this.domainManagerService.getFrontUrl().hostname,
debugMode:
this.twentyConfigService.get('NODE_ENV') ===
NodeEnvironment.development,
support: {
supportDriver: this.twentyConfigService.get('SUPPORT_DRIVER'),
supportFrontChatId: this.twentyConfigService.get(
'SUPPORT_FRONT_CHAT_ID',
),
},
sentry: {
environment: this.twentyConfigService.get('SENTRY_ENVIRONMENT'),
release: this.twentyConfigService.get('APP_VERSION'),
dsn: this.twentyConfigService.get('SENTRY_FRONT_DSN'),
},
captcha: {
provider: this.twentyConfigService.get('CAPTCHA_DRIVER'),
siteKey: this.twentyConfigService.get('CAPTCHA_SITE_KEY'),
},
chromeExtensionId: this.twentyConfigService.get('CHROME_EXTENSION_ID'),
api: {
mutationMaximumAffectedRecords: this.twentyConfigService.get(
'MUTATION_MAXIMUM_AFFECTED_RECORDS',
),
},
isAttachmentPreviewEnabled: this.twentyConfigService.get(
'IS_ATTACHMENT_PREVIEW_ENABLED',
),
analyticsEnabled: this.twentyConfigService.get('ANALYTICS_ENABLED'),
canManageFeatureFlags:
this.twentyConfigService.get('NODE_ENV') ===
NodeEnvironment.development ||
this.twentyConfigService.get('IS_BILLING_ENABLED'),
publicFeatureFlags: PUBLIC_FEATURE_FLAGS,
isMicrosoftMessagingEnabled: this.twentyConfigService.get(
'MESSAGING_PROVIDER_MICROSOFT_ENABLED',
),
isMicrosoftCalendarEnabled: this.twentyConfigService.get(
'CALENDAR_PROVIDER_MICROSOFT_ENABLED',
),
isGoogleMessagingEnabled: this.twentyConfigService.get(
'MESSAGING_PROVIDER_GMAIL_ENABLED',
),
isGoogleCalendarEnabled: this.twentyConfigService.get(
'CALENDAR_PROVIDER_GOOGLE_ENABLED',
),
isConfigVariablesInDbEnabled: this.twentyConfigService.get(
'IS_CONFIG_VARIABLES_IN_DB_ENABLED',
),
};
return Promise.resolve(clientConfig);
}
}

View File

@ -1,252 +0,0 @@
import { Test, TestingModule } from '@nestjs/testing';
import { NodeEnvironment } from 'src/engine/core-modules/twenty-config/interfaces/node-environment.interface';
import { SupportDriver } from 'src/engine/core-modules/twenty-config/interfaces/support.interface';
import { CaptchaDriverType } from 'src/engine/core-modules/captcha/interfaces';
import { ClientConfigService } from 'src/engine/core-modules/client-config/services/client-config.service';
import { DomainManagerService } from 'src/engine/core-modules/domain-manager/services/domain-manager.service';
import { PUBLIC_FEATURE_FLAGS } from 'src/engine/core-modules/feature-flag/constants/public-feature-flag.const';
import { TwentyConfigService } from 'src/engine/core-modules/twenty-config/twenty-config.service';
describe('ClientConfigService', () => {
let service: ClientConfigService;
let twentyConfigService: TwentyConfigService;
let domainManagerService: DomainManagerService;
beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
providers: [
ClientConfigService,
{
provide: TwentyConfigService,
useValue: {
get: jest.fn(),
},
},
{
provide: DomainManagerService,
useValue: {
getFrontUrl: jest.fn(),
},
},
],
}).compile();
service = module.get<ClientConfigService>(ClientConfigService);
twentyConfigService = module.get<TwentyConfigService>(TwentyConfigService);
domainManagerService =
module.get<DomainManagerService>(DomainManagerService);
});
it('should be defined', () => {
expect(service).toBeDefined();
});
describe('getClientConfig', () => {
beforeEach(() => {
// Setup default mock values
jest
.spyOn(twentyConfigService, 'get')
.mockImplementation((key: string) => {
const mockValues: Record<string, any> = {
IS_BILLING_ENABLED: true,
BILLING_PLAN_REQUIRED_LINK: 'https://billing.example.com',
BILLING_FREE_TRIAL_WITH_CREDIT_CARD_DURATION_IN_DAYS: 30,
BILLING_FREE_TRIAL_WITHOUT_CREDIT_CARD_DURATION_IN_DAYS: 7,
AUTH_GOOGLE_ENABLED: true,
AUTH_PASSWORD_ENABLED: true,
AUTH_MICROSOFT_ENABLED: false,
SIGN_IN_PREFILLED: false,
IS_MULTIWORKSPACE_ENABLED: true,
IS_EMAIL_VERIFICATION_REQUIRED: true,
DEFAULT_SUBDOMAIN: 'app',
NODE_ENV: NodeEnvironment.development,
SUPPORT_DRIVER: SupportDriver.Front,
SUPPORT_FRONT_CHAT_ID: 'chat-123',
SENTRY_ENVIRONMENT: 'development',
APP_VERSION: '1.0.0',
SENTRY_FRONT_DSN: 'https://sentry.example.com',
CAPTCHA_DRIVER: CaptchaDriverType.GoogleRecaptcha,
CAPTCHA_SITE_KEY: 'site-key-123',
CHROME_EXTENSION_ID: 'extension-123',
MUTATION_MAXIMUM_AFFECTED_RECORDS: 1000,
IS_ATTACHMENT_PREVIEW_ENABLED: true,
ANALYTICS_ENABLED: true,
MESSAGING_PROVIDER_MICROSOFT_ENABLED: false,
CALENDAR_PROVIDER_MICROSOFT_ENABLED: false,
MESSAGING_PROVIDER_GMAIL_ENABLED: true,
CALENDAR_PROVIDER_GOOGLE_ENABLED: true,
IS_CONFIG_VARIABLES_IN_DB_ENABLED: false,
};
return mockValues[key];
});
jest.spyOn(domainManagerService, 'getFrontUrl').mockReturnValue({
hostname: 'app.twenty.com',
} as URL);
});
it('should return complete client config with all properties', async () => {
const result = await service.getClientConfig();
expect(result).toEqual({
billing: {
isBillingEnabled: true,
billingUrl: 'https://billing.example.com',
trialPeriods: [
{
duration: 30,
isCreditCardRequired: true,
},
{
duration: 7,
isCreditCardRequired: false,
},
],
},
authProviders: {
google: true,
magicLink: false,
password: true,
microsoft: false,
sso: [],
},
signInPrefilled: false,
isMultiWorkspaceEnabled: true,
isEmailVerificationRequired: true,
defaultSubdomain: 'app',
frontDomain: 'app.twenty.com',
debugMode: true,
support: {
supportDriver: 'Front',
supportFrontChatId: 'chat-123',
},
sentry: {
environment: 'development',
release: '1.0.0',
dsn: 'https://sentry.example.com',
},
captcha: {
provider: 'GoogleRecaptcha',
siteKey: 'site-key-123',
},
chromeExtensionId: 'extension-123',
api: {
mutationMaximumAffectedRecords: 1000,
},
isAttachmentPreviewEnabled: true,
analyticsEnabled: true,
canManageFeatureFlags: true,
publicFeatureFlags: PUBLIC_FEATURE_FLAGS,
isMicrosoftMessagingEnabled: false,
isMicrosoftCalendarEnabled: false,
isGoogleMessagingEnabled: true,
isGoogleCalendarEnabled: true,
isConfigVariablesInDbEnabled: false,
});
});
it('should handle production environment correctly', async () => {
jest
.spyOn(twentyConfigService, 'get')
.mockImplementation((key: string) => {
if (key === 'NODE_ENV') return NodeEnvironment.production;
if (key === 'IS_BILLING_ENABLED') return false;
return undefined;
});
const result = await service.getClientConfig();
expect(result.debugMode).toBe(false);
expect(result.canManageFeatureFlags).toBe(false);
});
it('should handle missing captcha driver', async () => {
jest
.spyOn(twentyConfigService, 'get')
.mockImplementation((key: string) => {
if (key === 'CAPTCHA_DRIVER') return undefined;
if (key === 'CAPTCHA_SITE_KEY') return 'site-key';
return undefined;
});
const result = await service.getClientConfig();
expect(result.captcha.provider).toBeUndefined();
expect(result.captcha.siteKey).toBe('site-key');
});
it('should handle missing support driver', async () => {
jest
.spyOn(twentyConfigService, 'get')
.mockImplementation((key: string) => {
if (key === 'SUPPORT_DRIVER') return undefined;
return undefined;
});
const result = await service.getClientConfig();
expect(result.support.supportDriver).toBe(SupportDriver.None);
});
it('should handle billing enabled with feature flags', async () => {
jest
.spyOn(twentyConfigService, 'get')
.mockImplementation((key: string) => {
if (key === 'NODE_ENV') return NodeEnvironment.production;
if (key === 'IS_BILLING_ENABLED') return true;
return undefined;
});
const result = await service.getClientConfig();
expect(result.canManageFeatureFlags).toBe(true);
});
});
describe('transformEnum', () => {
it('should transform enum by direct key match', () => {
const result = (service as any).transformEnum(
'GoogleRecaptcha',
CaptchaDriverType,
);
expect(result).toBe(CaptchaDriverType.GoogleRecaptcha);
});
it('should transform enum by value match', () => {
const result = (service as any).transformEnum(
'google-recaptcha',
CaptchaDriverType,
);
expect(result).toBe('GoogleRecaptcha');
});
it('should transform SupportDriver enum correctly', () => {
const result = (service as any).transformEnum('front', SupportDriver);
expect(result).toBe('Front');
});
it('should throw error for unknown enum value', () => {
expect(() => {
(service as any).transformEnum('unknown-value', CaptchaDriverType);
}).toThrow(
'Unknown enum value: unknown-value. Available keys: GoogleRecaptcha, Turnstile. Available values: google-recaptcha, turnstile',
);
});
it('should handle direct key match for SupportDriver', () => {
const result = (service as any).transformEnum('Front', SupportDriver);
expect(result).toBe(SupportDriver.Front);
});
});
});

View File

@ -1,144 +0,0 @@
import { Injectable } from '@nestjs/common';
import { NodeEnvironment } from 'src/engine/core-modules/twenty-config/interfaces/node-environment.interface';
import { SupportDriver } from 'src/engine/core-modules/twenty-config/interfaces/support.interface';
import { CaptchaDriverType } from 'src/engine/core-modules/captcha/interfaces';
import { ClientConfig } from 'src/engine/core-modules/client-config/client-config.entity';
import { DomainManagerService } from 'src/engine/core-modules/domain-manager/services/domain-manager.service';
import { PUBLIC_FEATURE_FLAGS } from 'src/engine/core-modules/feature-flag/constants/public-feature-flag.const';
import { TwentyConfigService } from 'src/engine/core-modules/twenty-config/twenty-config.service';
@Injectable()
export class ClientConfigService {
constructor(
private twentyConfigService: TwentyConfigService,
private domainManagerService: DomainManagerService,
) {}
async getClientConfig(): Promise<ClientConfig> {
const captchaProvider = this.twentyConfigService.get('CAPTCHA_DRIVER');
const supportDriver = this.twentyConfigService.get('SUPPORT_DRIVER');
const clientConfig: ClientConfig = {
billing: {
isBillingEnabled: this.twentyConfigService.get('IS_BILLING_ENABLED'),
billingUrl: this.twentyConfigService.get('BILLING_PLAN_REQUIRED_LINK'),
trialPeriods: [
{
duration: this.twentyConfigService.get(
'BILLING_FREE_TRIAL_WITH_CREDIT_CARD_DURATION_IN_DAYS',
),
isCreditCardRequired: true,
},
{
duration: this.twentyConfigService.get(
'BILLING_FREE_TRIAL_WITHOUT_CREDIT_CARD_DURATION_IN_DAYS',
),
isCreditCardRequired: false,
},
],
},
authProviders: {
google: this.twentyConfigService.get('AUTH_GOOGLE_ENABLED'),
magicLink: false,
password: this.twentyConfigService.get('AUTH_PASSWORD_ENABLED'),
microsoft: this.twentyConfigService.get('AUTH_MICROSOFT_ENABLED'),
sso: [],
},
signInPrefilled: this.twentyConfigService.get('SIGN_IN_PREFILLED'),
isMultiWorkspaceEnabled: this.twentyConfigService.get(
'IS_MULTIWORKSPACE_ENABLED',
),
isEmailVerificationRequired: this.twentyConfigService.get(
'IS_EMAIL_VERIFICATION_REQUIRED',
),
defaultSubdomain: this.twentyConfigService.get('DEFAULT_SUBDOMAIN'),
frontDomain: this.domainManagerService.getFrontUrl().hostname,
debugMode:
this.twentyConfigService.get('NODE_ENV') ===
NodeEnvironment.development,
support: {
supportDriver: supportDriver
? this.transformEnum(supportDriver, SupportDriver)
: SupportDriver.None,
supportFrontChatId: this.twentyConfigService.get(
'SUPPORT_FRONT_CHAT_ID',
),
},
sentry: {
environment: this.twentyConfigService.get('SENTRY_ENVIRONMENT'),
release: this.twentyConfigService.get('APP_VERSION'),
dsn: this.twentyConfigService.get('SENTRY_FRONT_DSN'),
},
captcha: {
provider: captchaProvider
? this.transformEnum(captchaProvider, CaptchaDriverType)
: undefined,
siteKey: this.twentyConfigService.get('CAPTCHA_SITE_KEY'),
},
chromeExtensionId: this.twentyConfigService.get('CHROME_EXTENSION_ID'),
api: {
mutationMaximumAffectedRecords: this.twentyConfigService.get(
'MUTATION_MAXIMUM_AFFECTED_RECORDS',
),
},
isAttachmentPreviewEnabled: this.twentyConfigService.get(
'IS_ATTACHMENT_PREVIEW_ENABLED',
),
analyticsEnabled: this.twentyConfigService.get('ANALYTICS_ENABLED'),
canManageFeatureFlags:
this.twentyConfigService.get('NODE_ENV') ===
NodeEnvironment.development ||
this.twentyConfigService.get('IS_BILLING_ENABLED'),
publicFeatureFlags: PUBLIC_FEATURE_FLAGS,
isMicrosoftMessagingEnabled: this.twentyConfigService.get(
'MESSAGING_PROVIDER_MICROSOFT_ENABLED',
),
isMicrosoftCalendarEnabled: this.twentyConfigService.get(
'CALENDAR_PROVIDER_MICROSOFT_ENABLED',
),
isGoogleMessagingEnabled: this.twentyConfigService.get(
'MESSAGING_PROVIDER_GMAIL_ENABLED',
),
isGoogleCalendarEnabled: this.twentyConfigService.get(
'CALENDAR_PROVIDER_GOOGLE_ENABLED',
),
isConfigVariablesInDbEnabled: this.twentyConfigService.get(
'IS_CONFIG_VARIABLES_IN_DB_ENABLED',
),
};
return clientConfig;
}
// GraphQL enum values are in PascalCase, but the config values are in kebab-case
// This function transforms the config values, the same way GraphQL does
private transformEnum<T extends Record<string, string>>(
value: string,
enumObject: T,
): T[keyof T] {
const directMatch = Object.keys(enumObject).find(
(key) => key === value,
) as keyof T;
if (directMatch) {
return enumObject[directMatch];
}
const valueMatch = Object.entries(enumObject).find(
([, enumValue]) => enumValue === value,
);
if (valueMatch) {
return valueMatch[0] as T[keyof T];
}
const availableKeys = Object.keys(enumObject);
const availableValues = Object.values(enumObject);
throw new Error(
`Unknown enum value: ${value}. Available keys: ${availableKeys.join(', ')}. Available values: ${availableValues.join(', ')}`,
);
}
}