diff --git a/app/api/entities.v2/contracts/EntitiesDataSource.ts b/app/api/entities.v2/contracts/EntitiesDataSource.ts index 5f637a26e7..f31c363cd7 100644 --- a/app/api/entities.v2/contracts/EntitiesDataSource.ts +++ b/app/api/entities.v2/contracts/EntitiesDataSource.ts @@ -12,7 +12,8 @@ export interface EntitiesDataSource { ): Promise; updateMetadataValues( id: Entity['_id'], - values: Record + values: Record, + title?: string ): Promise; entitiesExist(sharedIds: string[]): Promise; getByIds(sharedIds: string[], language?: string): ResultSet; diff --git a/app/api/entities.v2/database/MongoEntitiesDataSource.ts b/app/api/entities.v2/database/MongoEntitiesDataSource.ts index 3a2de5c661..61635e415e 100644 --- a/app/api/entities.v2/database/MongoEntitiesDataSource.ts +++ b/app/api/entities.v2/database/MongoEntitiesDataSource.ts @@ -127,7 +127,8 @@ export class MongoEntitiesDataSource // eslint-disable-next-line class-methods-use-this async updateMetadataValues( id: Entity['_id'], - values: Record + values: Record, + title?: string ) { // This is using V1 so that it gets denormalized to speed up development // this is a hack and should be changed as soon as we finish AT @@ -136,6 +137,8 @@ export class MongoEntitiesDataSource throw new Error(`entity does not exists: ${id}`); } + entityToModify.title = title || entityToModify.title; + Object.entries(values).forEach(([propertyName, metadataValues]) => { entityToModify.metadata = entityToModify.metadata || {}; // @ts-ignore diff --git a/app/api/externalIntegrations.v2/automaticTranslation/SaveEntityTranslations.ts b/app/api/externalIntegrations.v2/automaticTranslation/SaveEntityTranslations.ts index 06671d0f44..91169687b8 100644 --- a/app/api/externalIntegrations.v2/automaticTranslation/SaveEntityTranslations.ts +++ b/app/api/externalIntegrations.v2/automaticTranslation/SaveEntityTranslations.ts @@ -30,7 +30,7 @@ export class SaveEntityTranslations { const [, entitySharedId, propertyId] = translationResult.key; - const property = await this.getProperty(entitySharedId, propertyId); + const { property, scope } = await this.getProperty(entitySharedId, propertyId); const entities = this.entitiesDS.getByIds([entitySharedId]); @@ -38,16 +38,26 @@ export class SaveEntityTranslations { 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}` }, - ], - }); + if (translation && property) { + if (scope === 'metadata') { + await this.entitiesDS.updateMetadataValues(oneEntity._id, { + [property.name]: [ + { value: `${SaveEntityTranslations.AITranslatedText} ${translation.text}` }, + ], + }); + } + if (scope === 'title') { + await this.entitiesDS.updateMetadataValues( + oneEntity._id, + {}, + `${SaveEntityTranslations.AITranslatedText} ${translation.text}` + ); + } } }); } + // eslint-disable-next-line max-statements private async getProperty(entitySharedId: string, propertyId: string) { const entity = await this.entitiesDS.getByIds([entitySharedId]).first(); if (!entity) { @@ -60,10 +70,15 @@ export class SaveEntityTranslations { } const property = template.properties.find(p => p.id === propertyId); + const commonProperty = template.commonProperties.find(p => p.id === propertyId); - if (!property) { + if (!property && !commonProperty) { throw new Error('Property does not exists'); } - return property; + + return { + property: commonProperty || property, + scope: commonProperty ? 'title' : 'metadata', + } as const; } } diff --git a/app/api/externalIntegrations.v2/automaticTranslation/specs/SaveEntityTranslations.spec.ts b/app/api/externalIntegrations.v2/automaticTranslation/specs/SaveEntityTranslations.spec.ts index fd7f7c8a29..6c555ce3b9 100644 --- a/app/api/externalIntegrations.v2/automaticTranslation/specs/SaveEntityTranslations.spec.ts +++ b/app/api/externalIntegrations.v2/automaticTranslation/specs/SaveEntityTranslations.spec.ts @@ -100,6 +100,37 @@ describe('GenerateAutomaticTranslationConfig', () => { }); }); + it('should save translated common properties (title)', async () => { + const translationResult: TranslationResult = { + key: ['tenant', 'entity', factory.commonPropertiesTitleId('template1')], + text: 'entity', + language_from: 'en', + languages_to: ['es', 'pt'], + translations: [ + { text: 'titulo original', language: 'es', success: true, error_message: '' }, + { text: 'titulo original (pt)', language: 'pt', success: true, error_message: '' }, + ], + }; + + await saveEntityTranslations.execute(translationResult); + + const entities = + (await testingDB.mongodb?.collection('entities').find({ sharedId: 'entity' }).toArray()) || + []; + + expect(entities.find(e => e.language === 'es')).toMatchObject({ + title: `${SaveEntityTranslations.AITranslatedText} titulo original`, + }); + + expect(entities.find(e => e.language === 'pt')).toMatchObject({ + title: `${SaveEntityTranslations.AITranslatedText} titulo original (pt)`, + }); + + expect(entities.find(e => e.language === 'en')).toMatchObject({ + title: 'entity', + }); + }); + it('should denormalize text property on related entities', async () => { const fixtures: DBFixture = { settings: [ diff --git a/package.json b/package.json index 6d0de2e078..93808cfa78 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "uwazi", - "version": "1.187.0-rc2", + "version": "1.187.0-rc3", "description": "Uwazi is a free, open-source solution for organising, analysing and publishing your documents.", "keywords": [ "react"