-
Notifications
You must be signed in to change notification settings - Fork 80
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'origin/production' into hotfix-commits-…
…fixes
- Loading branch information
Showing
9 changed files
with
328 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
69 changes: 69 additions & 0 deletions
69
app/api/externalIntegrations.v2/automaticTranslation/SaveEntityTranslations.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import { EntitiesDataSource } from 'api/entities.v2/contracts/EntitiesDataSource'; | ||
import { TemplatesDataSource } from 'api/templates.v2/contracts/TemplatesDataSource'; | ||
import { ATTranslationResultValidator } from './contracts/ATTranslationResultValidator'; | ||
import { InvalidInputDataFormat } from './errors/generateATErrors'; | ||
import { TranslationResult } from './types/TranslationResult'; | ||
|
||
export class SaveEntityTranslations { | ||
static AITranslatedText = '(AI translated)'; | ||
|
||
private entitiesDS: EntitiesDataSource; | ||
|
||
private templatesDS: TemplatesDataSource; | ||
|
||
private validator: ATTranslationResultValidator; | ||
|
||
constructor( | ||
templatesDS: TemplatesDataSource, | ||
entitiesDS: EntitiesDataSource, | ||
validator: ATTranslationResultValidator | ||
) { | ||
this.entitiesDS = entitiesDS; | ||
this.templatesDS = templatesDS; | ||
this.validator = validator; | ||
} | ||
|
||
async execute(translationResult: TranslationResult | unknown) { | ||
if (!this.validator.validate(translationResult)) { | ||
throw new InvalidInputDataFormat(this.validator.getErrors()[0]); | ||
} | ||
|
||
const [, entitySharedId, propertyId] = translationResult.key; | ||
|
||
const property = await this.getProperty(entitySharedId, propertyId); | ||
|
||
const entities = this.entitiesDS.getByIds([entitySharedId]); | ||
|
||
await entities.forEach(async oneEntity => { | ||
const translation = translationResult.translations.find( | ||
t => t.language === oneEntity.language | ||
); | ||
if (translation) { | ||
await this.entitiesDS.updateMetadataValues(oneEntity._id, { | ||
[property.name]: [ | ||
{ value: `${SaveEntityTranslations.AITranslatedText} ${translation.text}` }, | ||
], | ||
}); | ||
} | ||
}); | ||
} | ||
|
||
private async getProperty(entitySharedId: string, propertyId: string) { | ||
const entity = await this.entitiesDS.getByIds([entitySharedId]).first(); | ||
if (!entity) { | ||
throw new Error('Entity does not exists'); | ||
} | ||
|
||
const template = await this.templatesDS.getById(entity.template); | ||
if (!template) { | ||
throw new Error('Template does not exists'); | ||
} | ||
|
||
const property = template.properties.find(p => p.id === propertyId); | ||
|
||
if (!property) { | ||
throw new Error('Property does not exists'); | ||
} | ||
return property; | ||
} | ||
} |
6 changes: 6 additions & 0 deletions
6
...pi/externalIntegrations.v2/automaticTranslation/contracts/ATTranslationResultValidator.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import { TranslationResult } from '../types/TranslationResult'; | ||
|
||
export interface ATTranslationResultValidator { | ||
getErrors(): string[]; | ||
validate(data: unknown): data is TranslationResult; | ||
} |
40 changes: 40 additions & 0 deletions
40
...ernalIntegrations.v2/automaticTranslation/infrastructure/AJVTranslationResultValidator.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import { Ajv } from 'ajv'; | ||
import { JTDSchemaType } from 'ajv/dist/core'; | ||
import { TranslationResult } from '../types/TranslationResult'; | ||
import { ATTranslationResultValidator } from '../contracts/ATTranslationResultValidator'; | ||
|
||
const schema: JTDSchemaType<TranslationResult> = { | ||
additionalProperties: false, | ||
properties: { | ||
key: { elements: { type: 'string' } }, | ||
text: { type: 'string' }, | ||
language_from: { type: 'string' }, | ||
languages_to: { elements: { type: 'string' } }, | ||
translations: { | ||
elements: { | ||
properties: { | ||
text: { type: 'string' }, | ||
language: { type: 'string' }, | ||
success: { type: 'boolean' }, | ||
error_message: { type: 'string' }, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}; | ||
|
||
export class AJVTranslationResultValidator implements ATTranslationResultValidator { | ||
private errors: string[] = []; | ||
|
||
getErrors() { | ||
return this.errors; | ||
} | ||
|
||
validate(data: unknown) { | ||
const ajv = new Ajv({ strict: false }); | ||
const validate = ajv.compile<TranslationResult>(schema); | ||
const result = validate(data); | ||
this.errors = validate.errors ? validate.errors?.map(e => JSON.stringify(e.params)) : []; | ||
return result; | ||
} | ||
} |
Oops, something went wrong.