diff --git a/app/api/externalIntegrations.v2/automaticTranslation/GenerateAutomaticTranslationConfig.ts b/app/api/externalIntegrations.v2/automaticTranslation/GenerateAutomaticTranslationConfig.ts index 1be95ad91d7..c3315bc23cd 100644 --- a/app/api/externalIntegrations.v2/automaticTranslation/GenerateAutomaticTranslationConfig.ts +++ b/app/api/externalIntegrations.v2/automaticTranslation/GenerateAutomaticTranslationConfig.ts @@ -1,7 +1,7 @@ import { TemplatesDataSource } from 'api/templates.v2/contracts/TemplatesDataSource'; import { AutomaticTranslationConfigDataSource } from './contracts/AutomaticTranslationConfigDataSource'; -import { AutomaticTranslationConfig } from './model/AutomaticTranslationConfig'; import { AutomaticTranslationTemplateConfig } from './model/AutomaticTranslationTemplateConfig'; +import { RawAutomaticTranslationConfig } from './model/RawAutomaticTranslationConfig'; interface SemanticConfig { active: boolean; @@ -33,19 +33,19 @@ export class GenerateAutomaticTranslationsCofig { const templates = semanticConfig.templates.map(configData => { const templateData = templatesData.find(t => t.name === configData.template); return new AutomaticTranslationTemplateConfig( + templateData?.id ?? '', (configData.properties || []).map( label => templateData?.properties.find(p => p.label === label)?.id ?? '' ), - templateData?.id ?? '', (configData.commonProperties || []).map( label => templateData?.commonProperties.find(p => p.label === label)?.id ?? '' ) ); }); - const config = new AutomaticTranslationConfig(semanticConfig.active, [], templates); - - return this.atuomaticTranslationConfigDS.update(config); + return this.atuomaticTranslationConfigDS.update( + new RawAutomaticTranslationConfig(semanticConfig.active, templates) + ); } } diff --git a/app/api/externalIntegrations.v2/automaticTranslation/contracts/AutomaticTranslationConfigDataSource.ts b/app/api/externalIntegrations.v2/automaticTranslation/contracts/AutomaticTranslationConfigDataSource.ts index b2f9462e506..c6a92eacd81 100644 --- a/app/api/externalIntegrations.v2/automaticTranslation/contracts/AutomaticTranslationConfigDataSource.ts +++ b/app/api/externalIntegrations.v2/automaticTranslation/contracts/AutomaticTranslationConfigDataSource.ts @@ -1,6 +1,6 @@ -import { AutomaticTranslationConfig } from 'shared/types/settingsType'; +import { RawAutomaticTranslationConfig } from '../model/RawAutomaticTranslationConfig'; export interface AutomaticTranslationConfigDataSource { - get(): Promise; - update(config: AutomaticTranslationConfig): Promise; + get(): Promise; + update(config: RawAutomaticTranslationConfig): Promise; } diff --git a/app/api/externalIntegrations.v2/automaticTranslation/database/MongoAutomaticTranslationConfigDataSource.ts b/app/api/externalIntegrations.v2/automaticTranslation/database/MongoAutomaticTranslationConfigDataSource.ts index 18e285334a5..1c24818355a 100644 --- a/app/api/externalIntegrations.v2/automaticTranslation/database/MongoAutomaticTranslationConfigDataSource.ts +++ b/app/api/externalIntegrations.v2/automaticTranslation/database/MongoAutomaticTranslationConfigDataSource.ts @@ -1,6 +1,8 @@ import { MongoDataSource } from 'api/common.v2/database/MongoDataSource'; import { Settings as SettingsType, AutomaticTranslationConfig } from 'shared/types/settingsType'; import { AutomaticTranslationConfigDataSource } from '../contracts/AutomaticTranslationConfigDataSource'; +import { RawAutomaticTranslationConfig } from '../model/RawAutomaticTranslationConfig'; +import { AutomaticTranslationTemplateConfig } from '../model/AutomaticTranslationTemplateConfig'; export class MongoAutomaticTranslationConfigDataSource extends MongoDataSource @@ -8,12 +10,20 @@ export class MongoAutomaticTranslationConfigDataSource implements AutomaticTranslationConfigDataSource { protected collectionName = 'settings'; - async get(): Promise { + async get() { const settings = await this.getCollection().findOne(); - return settings?.features?.automaticTranslation ?? { active: false }; + const config = settings?.features?.automaticTranslation ?? { active: false }; + + return new RawAutomaticTranslationConfig( + config.active, + (config.templates || []).map( + t => + new AutomaticTranslationTemplateConfig(t.template, t.properties || [], t.commonProperties) + ) + ); } - async update(config: AutomaticTranslationConfig): Promise { + async update(config: RawAutomaticTranslationConfig) { await this.getCollection().findOneAndUpdate( {}, { $set: { 'features.automaticTranslation': config } } diff --git a/app/api/externalIntegrations.v2/automaticTranslation/model/AutomaticTranslationTemplateConfig.ts b/app/api/externalIntegrations.v2/automaticTranslation/model/AutomaticTranslationTemplateConfig.ts index 78effea761c..2c24ff68d65 100644 --- a/app/api/externalIntegrations.v2/automaticTranslation/model/AutomaticTranslationTemplateConfig.ts +++ b/app/api/externalIntegrations.v2/automaticTranslation/model/AutomaticTranslationTemplateConfig.ts @@ -5,7 +5,7 @@ class AutomaticTranslationTemplateConfig { readonly properties: string[]; - constructor(properties: string[], template: string, commonProperties: string[] = []) { + constructor(template: string, properties: string[], commonProperties: string[] = []) { this.template = template; this.commonProperties = commonProperties; this.properties = properties; diff --git a/app/api/externalIntegrations.v2/automaticTranslation/model/RawAutomaticTranslationConfig.ts b/app/api/externalIntegrations.v2/automaticTranslation/model/RawAutomaticTranslationConfig.ts new file mode 100644 index 00000000000..68d6ac95f3b --- /dev/null +++ b/app/api/externalIntegrations.v2/automaticTranslation/model/RawAutomaticTranslationConfig.ts @@ -0,0 +1,48 @@ +import { LanguageISO6391 } from 'shared/types/commonTypes'; +import { AutomaticTranslationTemplateConfig } from './AutomaticTranslationTemplateConfig'; +import { Property } from 'api/templates.v2/model/Property'; +import { AutomaticTranslationConfig } from './AutomaticTranslationConfig'; + +class RawAutomaticTranslationConfig { + readonly active: boolean; + + readonly templates: AutomaticTranslationTemplateConfig[]; + + constructor(active: boolean, templates: AutomaticTranslationTemplateConfig[]) { + this.active = active; + this.templates = templates; + } + + getValidConfig( + languages: LanguageISO6391[], + validProperties: Property[] + ): AutomaticTranslationConfig { + const validPropertiesMap = validProperties.reduce( + (memo, property) => { + // eslint-disable-next-line no-param-reassign + memo[property.id] = property; + return memo; + }, + {} as { [k: string]: Property } + ); + + const validPropertiesIds = Object.keys(validPropertiesMap); + + const templates = (this.templates || []).map( + templateConfig => + new AutomaticTranslationTemplateConfig( + templateConfig.template, + (templateConfig.properties || []).filter( + propertyId => + validPropertiesIds.includes(propertyId) && + validPropertiesMap[propertyId].template === templateConfig.template + ), + templateConfig.commonProperties + ) + ); + + return new AutomaticTranslationConfig(this.active, languages, templates); + } +} + +export { RawAutomaticTranslationConfig }; diff --git a/app/api/externalIntegrations.v2/automaticTranslation/services/GetAutomaticTranslationConfig.ts b/app/api/externalIntegrations.v2/automaticTranslation/services/GetAutomaticTranslationConfig.ts index 405986b6ac9..798c16e8f2c 100644 --- a/app/api/externalIntegrations.v2/automaticTranslation/services/GetAutomaticTranslationConfig.ts +++ b/app/api/externalIntegrations.v2/automaticTranslation/services/GetAutomaticTranslationConfig.ts @@ -1,9 +1,6 @@ import { SettingsDataSource } from 'api/settings.v2/contracts/SettingsDataSource'; import { TemplatesDataSource } from 'api/templates.v2/contracts/TemplatesDataSource'; -import { Property } from 'api/templates.v2/model/Property'; import { AutomaticTranslationGateway } from '../contracts/AutomaticTranslationGateway'; -import { AutomaticTranslationConfig } from '../model/AutomaticTranslationConfig'; -import { AutomaticTranslationTemplateConfig } from '../model/AutomaticTranslationTemplateConfig'; import { AutomaticTranslationConfigDataSource } from '../contracts/AutomaticTranslationConfigDataSource'; export class GetAutomaticTranslationConfig { @@ -30,36 +27,13 @@ export class GetAutomaticTranslationConfig { async execute() { const config = await this.config.get(); - const validProperties = (await this.templates.getAllTextProperties().all()).reduce( - (memo, property) => { - // eslint-disable-next-line no-param-reassign - memo[property.id] = property; - return memo; - }, - {} as { [k: string]: Property } - ); - - const validPropertiesIds = Object.keys(validProperties); + const validProperties = await this.templates.getAllTextProperties().all(); const configuredLanguages = await this.settings.getLanguageKeys(); const supportedLanguages = await this.automaticTranslation.supportedLanguages(); - const templates = (config.templates || []).map( - templateConfig => - new AutomaticTranslationTemplateConfig( - (templateConfig.properties || []).filter( - propertyId => - validPropertiesIds.includes(propertyId) && - validProperties[propertyId].template === templateConfig.template - ), - templateConfig.template, - templateConfig.commonProperties - ) - ); - - return new AutomaticTranslationConfig( - config.active, + return config.getValidConfig( configuredLanguages.filter(languageKey => supportedLanguages.includes(languageKey)), - templates + validProperties ); } }