Skip to content

Commit

Permalink
Merge pull request #901 from huridocs/899
Browse files Browse the repository at this point in the history
changing a template of an entity now deletes all related metadata connections
  • Loading branch information
konzz authored Mar 28, 2017
2 parents 6043416 + 2033793 commit 37ae890
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 6 deletions.
10 changes: 7 additions & 3 deletions app/api/entities/entities.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,11 @@ export default {
])
.then(([docLanguages, templateResult]) => {
if (docLanguages[0].template && doc.template && docLanguages[0].template.toString() !== doc.template.toString()) {
return this.deleteEntityFromMetadata(docLanguages[0]).then(() => [docLanguages, templateResult]);
return Promise.all([
this.deleteEntityFromMetadata(docLanguages[0]),
references.delete({sourceType: 'metadata', $or: [{targetDocument: doc.sharedId}, {sourceDocument: doc.sharedId}]})
])
.then(() => [docLanguages, templateResult]);
}
return [docLanguages, templateResult];
})
Expand Down Expand Up @@ -249,8 +253,8 @@ export default {
}

return Promise.all([
this.get(selectQuery, {_id: 1}),
this.get(multiSelectQuery, {_id: 1}),
selectQuery.$or.length ? this.get(selectQuery, {_id: 1}) : [],
multiSelectQuery.$or.length ? this.get(multiSelectQuery, {_id: 1}) : [],
model.db.updateMany(selectQuery, {$set: selectChanges}),
model.db.updateMany(multiSelectQuery, {$pull: multiSelectChanges})
])
Expand Down
43 changes: 43 additions & 0 deletions app/api/entities/specs/entities.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,23 @@ describe('entities', () => {
})
.catch(catchErrors(done));
});

it('should delete all metadata connections created automatically', (done) => {
let doc = {_id: batmanFinishesId, sharedId: 'shared', metadata: {}, published: false, template: templateChangingNames};
spyOn(entities, 'deleteEntityFromMetadata').and.returnValue(Promise.resolve());
entities.save(doc, {language: 'en'})
.then(() => {
return references.get();
})
.then((connections) => {
let batmanFinishesConnections = connections.filter(c => c.targetDocument === 'shared' || c.sourceDocument === 'shared');
expect(batmanFinishesConnections.length).toBe(2);
expect(batmanFinishesConnections[0].title).toBe('reference1');
expect(batmanFinishesConnections[1].title).toBe('reference2');
done();
})
.catch(catchErrors(done));
});
});

describe('when entity its being used as thesauri and template do not change', () => {
Expand Down Expand Up @@ -454,6 +471,32 @@ describe('entities', () => {
})
.catch(catchErrors(done));
});

describe('when there is no multiselects but there is selects', () => {
it('should only delete selects and not throw an error', (done) => {
spyOn(search, 'bulkIndex');
entities.delete('shared10')
.then(() => {
const documentsToIndex = search.bulkIndex.calls.argsFor(0)[0];
expect(documentsToIndex[0].metadata.select).toBe('');
done();
})
.catch(catchErrors(done));
});
});

describe('when there is no selects but there is multiselects', () => {
it('should only delete multiselects and not throw an error', (done) => {
spyOn(search, 'bulkIndex');
entities.delete('multiselect')
.then(() => {
const documentsToIndex = search.bulkIndex.calls.argsFor(0)[0];
expect(documentsToIndex[0].metadata.multiselect).toEqual(['value1']);
done();
})
.catch(catchErrors(done));
});
});
});
});

Expand Down
16 changes: 14 additions & 2 deletions app/api/entities/specs/fixtures.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ const templateChangingNames = db.id();
const referenceId = db.id();
const templateWithEntityAsThesauri = db.id();
const templateWithEntityAsThesauri2 = db.id();
const templateWithOnlySelect = db.id();
const templateWithOnlyMultiselect = db.id();

export default {
entities: [
Expand All @@ -28,7 +30,9 @@ export default {
{_id: db.id(), template: templateWithEntityAsThesauri2, sharedId: 'multiselect', type: 'entity', language: 'es', metadata: {multiselect2: ['shared', 'value2']}},
{_id: db.id(), template: templateWithEntityAsThesauri, sharedId: 'select', type: 'entity', language: 'en', metadata: {select: 'shared'}},
{_id: db.id(), template: templateWithEntityAsThesauri2, sharedId: 'select', type: 'entity', language: 'es', metadata: {select2: 'shared'}},
{_id: db.id(), template: db.id(), sharedId: 'otherTemplateWithMultiselect', type: 'entity', language: 'es', metadata: {select2: 'value'}}
{_id: db.id(), template: db.id(), sharedId: 'otherTemplateWithMultiselect', type: 'entity', language: 'es', metadata: {select2: 'value'}},
{_id: db.id(), template: templateWithOnlySelect, sharedId: 'otherTemplateWithSelect', type: 'entity', language: 'es', metadata: {select: 'shared10'}},
{_id: db.id(), template: templateWithOnlyMultiselect, sharedId: 'otherTemplateWithMultiselect', type: 'entity', language: 'es', metadata: {multiselect: ['value1', 'multiselect']}}
],
settings: [
{_id: db.id(), languages: [{key: 'es'}, {key: 'pt'}, {key: 'en'}]}
Expand All @@ -42,6 +46,12 @@ export default {
{type: 'multidate', name: 'multidate'},
{type: 'multidaterange', name: 'multidaterange'}
]},
{_id: templateWithOnlyMultiselect, name: 'templateWithOnlyMultiSelectSelect', properties: [
{type: 'multiselect', name: 'multiselect', content: templateWithEntityAsThesauri.toString()}
]},
{_id: templateWithOnlySelect, name: 'templateWithOnlySelect', properties: [
{type: 'select', name: 'select', content: templateChangingNames.toString()}
]},
{_id: templateWithEntityAsThesauri, name: 'template_with_thesauri_as_template', properties: [
{type: 'select', name: 'select', content: templateId.toString()},
{type: 'multiselect', name: 'multiselect', content: templateId.toString()}
Expand All @@ -59,7 +69,9 @@ export default {
connections: [
{_id: referenceId, title: 'reference1', sourceDocument: 'shared', relationtype: 'relation1'},
{_id: db.id(), title: 'reference2', sourceDocument: 'source2', relationtype: 'relation2', targetDocument: 'shared'},
{_id: db.id(), title: 'reference3', sourceDocument: 'another', relationtype: 'relation3', targetDocument: 'document'}
{_id: db.id(), title: 'reference3', sourceDocument: 'another', relationtype: 'relation3', targetDocument: 'document'},
{_id: db.id(), title: 'reference4', sourceDocument: 'shared', relationtype: 'relation2', targetDocument: 'shared1', sourceType: 'metadata'},
{_id: db.id(), title: 'reference5', sourceDocument: 'shared1', relationtype: 'relation2', targetDocument: 'shared', sourceType: 'metadata'}
]
};

Expand Down
8 changes: 7 additions & 1 deletion app/api/utils/error_handling_middleware.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,13 @@ export default function (req, res, next) {
result = error.message;
}

res.status(error.code || 500);
let code = error.code || 500;

if (error.name === 'MongoError') {
code = 500;
}

res.status(code);
res.json({error: result});
};
next();
Expand Down
10 changes: 10 additions & 0 deletions app/api/utils/specs/error_handling_middleware.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,14 @@ describe('Error handling middleware', function () {
expect(res.json).toHaveBeenCalledWith({error: 'error'});
});
});

describe('when error is a MongoError', () => {
it('should respond the error message with the status 500', () => {
middleware(req, res, next);
const error = {name: 'MongoError', message: 'error', code: 'code'};
res.error(error);
expect(res.status).toHaveBeenCalledWith(500);
expect(res.json).toHaveBeenCalledWith({error: 'error'});
});
});
});

0 comments on commit 37ae890

Please sign in to comment.