Skip to content

Commit

Permalink
Added option to deactivate the service
Browse files Browse the repository at this point in the history
  • Loading branch information
RafaPolit committed Sep 27, 2024
1 parent 3102087 commit 8aa39c5
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,38 @@ import { EntityCreatedEvent } from 'api/entities/events/EntityCreatedEvent';
import { EventsBus } from 'api/eventsbus';
import { permissionsContext } from 'api/permissions/permissionsContext';
import { AutomaticTranslationFactory } from '../../AutomaticTranslationFactory';
import { ATConfigService } from '../../services/GetAutomaticTranslationConfig';

export class ATEntityCreationListener {
private eventBus: EventsBus;

private ATFactory: typeof AutomaticTranslationFactory;

private aTConfigService: ATConfigService;

constructor(
eventBus: EventsBus,
aTConfigService: ATConfigService,
ATFactory: typeof AutomaticTranslationFactory = AutomaticTranslationFactory
) {
this.eventBus = eventBus;
this.aTConfigService = aTConfigService;
this.ATFactory = ATFactory;
}

start() {
this.eventBus.on(EntityCreatedEvent, async event => {
permissionsContext.setCommandContext();
const entityFrom = event.entities.find(e => e.language === event.targetLanguageKey) || {};
const { active } = await this.aTConfigService.get();

if (active) {
permissionsContext.setCommandContext();
const entityFrom = event.entities.find(e => e.language === event.targetLanguageKey) || {};

entityFrom._id = entityFrom._id?.toString();
entityFrom.template = entityFrom.template?.toString();
entityFrom._id = entityFrom._id?.toString();
entityFrom.template = entityFrom.template?.toString();

await this.ATFactory.defaultRequestEntityTranslation().execute(entityFrom);
await this.ATFactory.defaultRequestEntityTranslation().execute(entityFrom);
}
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ import { RequestEntityTranslation } from 'api/externalIntegrations.v2/automaticT
import { testingEnvironment } from 'api/utils/testingEnvironment';
import { AutomaticTranslationFactory } from 'api/externalIntegrations.v2/automaticTranslation/AutomaticTranslationFactory';
import { getFixturesFactory } from 'api/utils/fixturesFactory';
import { ATEntityCreationListener } from '../ATEntityCreationListener';
import { tenants } from 'api/tenants';
import { ATConfigService } from 'api/externalIntegrations.v2/automaticTranslation/services/GetAutomaticTranslationConfig';

Check failure on line 8 in app/api/externalIntegrations.v2/automaticTranslation/adapters/driving/specs/ATEntityCreatedListener.spec.ts

View workflow job for this annotation

GitHub Actions / eslint

'ATConfigService' is defined but never used
import testingDB from 'api/utils/testing_db';

Check failure on line 9 in app/api/externalIntegrations.v2/automaticTranslation/adapters/driving/specs/ATEntityCreatedListener.spec.ts

View workflow job for this annotation

GitHub Actions / eslint

'testingDB' is defined but never used
import { ATEntityCreationListener } from '../ATEntityCreationListener';

const factory = getFixturesFactory();

Expand All @@ -27,13 +29,17 @@ describe('ATEntityCreationListener', () => {

beforeEach(async () => {
await testingEnvironment.setUp({
settings: [{ features: { automaticTranslation: { active: true } } }],
settings: [{ features: { automaticTranslation: { active: false } } }],
});
await testingEnvironment.setTenant('tenant');

executeSpy = jest.fn();

listener = new ATEntityCreationListener(eventBus, prepareATFactory(executeSpy));
listener = new ATEntityCreationListener(
eventBus,
AutomaticTranslationFactory.defaultATConfigService(),
prepareATFactory(executeSpy)
);
listener.start();
});

Expand All @@ -42,7 +48,23 @@ describe('ATEntityCreationListener', () => {
});

describe('Request entity translation', () => {
it('should not request translations if feature flag is off', async () => {
const entityCreationEvent = new EntityCreatedEvent({
entities: [{ sharedId: 'entity 1' }],
targetLanguageKey: 'en',
});

await tenants.run(async () => {
await eventBus.emit(entityCreationEvent);
}, 'tenant');

expect(executeSpy).not.toHaveBeenCalled();
});

it('should call RequestEntityTranslation on receiving entity creation event', async () => {
await testingEnvironment.setFixtures({
settings: [{ features: { automaticTranslation: { active: true } } }],
});
const entityEn = factory.entity('entity1', 'template1', {}, { language: 'en' });
const entityCreationEvent = new EntityCreatedEvent({
entities: [factory.entity('entity1', 'template1', {}, { language: 'es' }), entityEn],
Expand Down

0 comments on commit 8aa39c5

Please sign in to comment.