Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/production' into staging
Browse files Browse the repository at this point in the history
  • Loading branch information
daneryl committed Oct 3, 2024
2 parents 398f9df + ecd9a1f commit 8f9c2ad
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 12 deletions.
3 changes: 2 additions & 1 deletion app/api/entities.v2/contracts/EntitiesDataSource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ export interface EntitiesDataSource {
): Promise<void>;
updateMetadataValues(
id: Entity['_id'],
values: Record<string, { value: MetadataValue }[]>
values: Record<string, { value: MetadataValue }[]>,
title?: string
): Promise<void>;
entitiesExist(sharedIds: string[]): Promise<boolean>;
getByIds(sharedIds: string[], language?: string): ResultSet<Entity>;
Expand Down
5 changes: 4 additions & 1 deletion app/api/entities.v2/database/MongoEntitiesDataSource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,8 @@ export class MongoEntitiesDataSource
// eslint-disable-next-line class-methods-use-this
async updateMetadataValues(
id: Entity['_id'],
values: Record<string, { value: MetadataValue }[]>
values: Record<string, { value: MetadataValue }[]>,
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
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,24 +30,34 @@ 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]);

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}` },
],
});
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) {
Expand All @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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: [
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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"
Expand Down

0 comments on commit 8f9c2ad

Please sign in to comment.