Skip to content

Commit

Permalink
implement Entity change and get property value for markdown type (#7361)
Browse files Browse the repository at this point in the history
  • Loading branch information
daneryl authored Oct 16, 2024
1 parent 706e98f commit b3c31c9
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 10 deletions.
10 changes: 5 additions & 5 deletions app/api/entities.v2/model/Entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,8 @@ export class Entity {
);
}

changePropertyValue(property: Property, value: string) {
if (property.type === 'text') {
setPropertyValue(property: Property, value: string) {
if (property.type === 'text' || property.type === 'markdown') {
const isTitleProperty = property instanceof CommonProperty && property.name === 'title';
const { metadata } = this;
if (!(property instanceof CommonProperty)) {
Expand All @@ -80,17 +80,17 @@ export class Entity {
);
}

throw new Error('types other than string are not implemented yet');
throw new Error('types other than string/markdown are not implemented yet');
}

getPropertyValue(property: Property): string {
if (property.type === 'text') {
if (property.type === 'text' || property.type === 'markdown') {
const isTitleProperty = property instanceof CommonProperty && property.name === 'title';
if (isTitleProperty) return this.title;
return this.metadata[property.name][0].value as string;
}

throw new Error('types other than string are not implemented yet');
throw new Error('types other than string/markdown are not implemented yet');
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ export class RequestEntityTranslation {
await entities.forEach(async fetchedEntity => {
if (languagesTo.includes(fetchedEntity.language as LanguageISO6391)) {
await this.entitiesDS.updateEntity(
fetchedEntity.changePropertyValue(property, pendingText)
fetchedEntity.setPropertyValue(property, pendingText)
);
this.logger.info(
`[AT] - Pending translation saved on DB - ${property.name}: ${pendingText}`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ export class SaveEntityTranslations {
}
if (translation?.success && property) {
const textTranslated = `${SaveEntityTranslations.AITranslatedText} ${translation.text}`;
await this.entitiesDS.updateEntity(entity.changePropertyValue(property, textTranslated));
await this.entitiesDS.updateEntity(entity.setPropertyValue(property, textTranslated));

this.logger.info(
`[AT] - Property saved on DB - ${JSON.stringify({ entityId: entity._id, language: entity.language, [property.name]: translation.text })}`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,16 @@ import { ATTaskMessage, RequestEntityTranslation } from '../RequestEntityTransla
const factory = getFixturesFactory();
const fixtures: DBFixture = {
templates: [
factory.template('template1', [factory.property('text1'), factory.property('empty_text')]),
factory.template('template1', [
factory.property('text1'),
factory.property('text2', 'markdown'),
factory.property('empty_text'),
]),
],
entities: [
...factory.entityInMultipleLanguages(['en', 'es', 'pt'], 'entity1', 'template1', {
text1: [{ value: 'original text1' }],
text2: [{ value: 'markdown text' }],
empty_text: [{ value: '' }],
}),
],
Expand All @@ -39,7 +44,11 @@ const fixtures: DBFixture = {
templates: [
{
template: factory.idString('template1'),
properties: [factory.idString('text1'), factory.idString('empty_text')],
properties: [
factory.idString('text1'),
factory.idString('text2'),
factory.idString('empty_text'),
],
commonProperties: [factory.commonPropertiesTitleId('template1')],
},
],
Expand Down Expand Up @@ -112,7 +121,7 @@ describe('RequestEntityTranslation', () => {
});

it('should send a task to the automatic translation service queue', () => {
expect(taskManager.startTask).toHaveBeenCalledTimes(2);
expect(taskManager.startTask).toHaveBeenCalledTimes(3);

expect(taskManager.startTask).toHaveBeenCalledWith({
key: ['tenant', 'entity1', factory.commonPropertiesTitleId('template1').toString()],
Expand All @@ -127,6 +136,13 @@ describe('RequestEntityTranslation', () => {
language_from: 'en',
languages_to: ['es', 'pt'],
});

expect(taskManager.startTask).toHaveBeenCalledWith({
key: ['tenant', 'entity1', factory.property('text2')._id?.toString()],
text: 'markdown text',
language_from: 'en',
languages_to: ['es', 'pt'],
});
});
});

Expand Down

0 comments on commit b3c31c9

Please sign in to comment.