Skip to content

Commit

Permalink
AT entity creation event listener fix
Browse files Browse the repository at this point in the history
not using permissions commandContext anymore, the listener gets executed
in the same context as the entity creation, we are only separating this
into a event listener / use case to decouple v1 and v2 codes.
  • Loading branch information
daneryl committed Oct 19, 2024
1 parent de13916 commit d9d0563
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,8 @@ export class RequestEntityTranslation {

async execute(entityInputModel: EntityInputModel | unknown) {
this.inputValidator.ensure(entityInputModel);
const atConfig = await this.ATConfigDS.get();
const atTemplateConfig = atConfig.templates.find(
t => t.template === entityInputModel.template?.toString()
);

const languageFrom = entityInputModel.language;
const languagesTo = atConfig.languages.filter(
language => language !== entityInputModel.language
);
const entity = Entity.fromInputModel(entityInputModel);
const { atTemplateConfig, languagesTo, atConfig, languageFrom } = await this.getConfig(entity);

if (
!atTemplateConfig ||
Expand All @@ -64,7 +57,6 @@ export class RequestEntityTranslation {
return;
}

const entity = Entity.fromInputModel(entityInputModel);
let updatedEntities = (await this.entitiesDS.getByIds([entity.sharedId]).all()).filter(
e => e.language !== languageFrom
);
Expand Down Expand Up @@ -105,4 +97,15 @@ export class RequestEntityTranslation {
})
);
}

private async getConfig(entity: Entity) {
const atConfig = await this.ATConfigDS.get();
const atTemplateConfig = atConfig.templates.find(
t => t.template === entity.template?.toString()
);

const languageFrom = entity.language;
const languagesTo = atConfig.languages.filter(language => language !== entity.language);
return { atTemplateConfig, languagesTo, atConfig, languageFrom };
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { DefaultTransactionManager } from 'api/common.v2/database/data_source_defaults';
import { EntityCreatedEvent } from 'api/entities/events/EntityCreatedEvent';
import { EventsBus } from 'api/eventsbus';
import { permissionsContext } from 'api/permissions/permissionsContext';
import { AutomaticTranslationFactory } from '../../AutomaticTranslationFactory';
import { DefaultTransactionManager } from 'api/common.v2/database/data_source_defaults';

export class ATEntityCreationListener {
private eventBus: EventsBus;
Expand All @@ -24,7 +23,6 @@ export class ATEntityCreationListener {
).get();

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

entityFrom._id = entityFrom._id?.toString();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,6 @@ describe('ATEntityCreationListener', () => {
it('should execute RequestEntityTranslation on receiving entity creation event', async () => {
expect(executeSpy).toHaveBeenCalledWith(entityEn);
});

it('should execute RequestEntityTranslation with commandUser as its context user', async () => {
expect(userInContext).toBe(permissionsContext.commandUser);
});
});
});
});

0 comments on commit d9d0563

Please sign in to comment.