Skip to content

Commit

Permalink
Propagated language to entity save.
Browse files Browse the repository at this point in the history
  • Loading branch information
RafaPolit committed Sep 25, 2024
1 parent efef3d9 commit e390582
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 11 deletions.
11 changes: 8 additions & 3 deletions app/api/entities/entities.js
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ async function updateEntity(entity, _template, unrestricted = false) {
return result;
}

async function createEntity(doc, languages, sharedId, docTemplate) {
async function createEntity(doc, [currentLanguage, languages], sharedId, docTemplate) {
if (!docTemplate) docTemplate = await templates.getById(doc.template);
const thesauriByKey = await templates.getRelatedThesauri(docTemplate);

Expand Down Expand Up @@ -208,7 +208,7 @@ async function createEntity(doc, languages, sharedId, docTemplate) {
await applicationEventsBus.emit(
new EntityCreatedEvent({
entities: await model.get({ sharedId }),
targetLanguageKey: languages[0].key,
targetLanguageKey: currentLanguage,
})
);

Expand Down Expand Up @@ -419,7 +419,12 @@ export default {
docTemplate = defaultTemplate;
}
doc.metadata = doc.metadata || {};
await this.createEntity(this.sanitize(doc, docTemplate), languages, sharedId, docTemplate);
await this.createEntity(
this.sanitize(doc, docTemplate),
[language, languages],
sharedId,
docTemplate
);
}

const [entity] = includeDocuments
Expand Down
16 changes: 8 additions & 8 deletions app/api/entities/specs/entities.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -511,9 +511,14 @@ describe('entities', () => {
});

describe('events', () => {
it('should emit an event when an entity is created', async () => {
const emitSpy = jest.spyOn(applicationEventsBus, 'emit');
let emitSpy;

beforeEach(() => {
emitSpy = jest.spyOn(applicationEventsBus, 'emit');
emitSpy.mockClear();
});

it('should emit an event when an entity is created', async () => {
const newEntity = {
template: templateId,
title: 'New Super Hero',
Expand All @@ -539,15 +544,12 @@ describe('entities', () => {
expect(emitSpy).toHaveBeenCalledWith(
new EntityCreatedEvent({
entities: afterAllLanguages,
targetLanguageKey: 'es',
targetLanguageKey: 'en',
})
);

emitSpy.mockRestore();
});

it('should emit an event when an entity is updated', async () => {
const emitSpy = jest.spyOn(applicationEventsBus, 'emit');
const before = fixtures.entities.find(e => e._id === batmanFinishesId);
const beforeAllLanguages = await entities.getAllLanguages(before.sharedId);
const after = { ...before, title: 'new title' };
Expand All @@ -564,8 +566,6 @@ describe('entities', () => {
targetLanguageKey: 'en',
})
);

emitSpy.mockRestore();
});
});
});
Expand Down

0 comments on commit e390582

Please sign in to comment.