Skip to content

Commit

Permalink
AutomaticTranslationConfigDataSource fix
Browse files Browse the repository at this point in the history
now returns and accepts domain object instead of a db raw type
  • Loading branch information
daneryl committed Aug 12, 2024
1 parent b165917 commit caa92ef
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 41 deletions.
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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)
);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { AutomaticTranslationConfig } from 'shared/types/settingsType';
import { RawAutomaticTranslationConfig } from '../model/RawAutomaticTranslationConfig';

export interface AutomaticTranslationConfigDataSource {
get(): Promise<AutomaticTranslationConfig>;
update(config: AutomaticTranslationConfig): Promise<AutomaticTranslationConfig>;
get(): Promise<RawAutomaticTranslationConfig>;
update(config: RawAutomaticTranslationConfig): Promise<RawAutomaticTranslationConfig>;
}
Original file line number Diff line number Diff line change
@@ -1,19 +1,29 @@
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<SettingsType>
// eslint-disable-next-line prettier/prettier
implements AutomaticTranslationConfigDataSource {
protected collectionName = 'settings';

async get(): Promise<AutomaticTranslationConfig> {
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<AutomaticTranslationConfig> {
async update(config: RawAutomaticTranslationConfig) {
await this.getCollection().findOneAndUpdate(
{},
{ $set: { 'features.automaticTranslation': config } }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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 };
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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
);
}
}

0 comments on commit caa92ef

Please sign in to comment.