Skip to content

Commit

Permalink
Merge branch 'development'
Browse files Browse the repository at this point in the history
  • Loading branch information
daneryl committed Nov 22, 2018
2 parents f0a8f37 + 1806169 commit 4d510b7
Show file tree
Hide file tree
Showing 5 changed files with 172 additions and 143 deletions.
5 changes: 3 additions & 2 deletions app/api/entities/entities.js
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ export default {
}

return this.get(query, select, { skip: offset, limit })
.then(entities => Promise.all(entities.map(entity => relationships.get({ entity: entity.sharedId, language: entity.language })
.then(entities => Promise.all(entities.map(entity => relationships.get({ entity: entity.sharedId })
.then((relations) => {
entity.relationships = relations || [];
return entity;
Expand Down Expand Up @@ -379,7 +379,8 @@ export default {
if (!entity) {
throw createError('entity does not exists', 404);
}
if (!entity.fullText[pageNumber]) {

if (typeof entity.fullText[pageNumber] === 'undefined') {
throw createError('page does not exist', 404);
}

Expand Down
65 changes: 48 additions & 17 deletions app/api/entities/specs/entities.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -418,15 +418,38 @@ describe('entities', () => {

describe('indexEntities', () => {
it('should index entities based on query params passed', (done) => {
entities.indexEntities({ sharedId: 'shared' }, { title: 1 })
entities.indexEntities({ sharedId: 'shared' })
.then(() => {
const documentsToIndex = search.bulkIndex.calls.argsFor(0)[0];
expect(documentsToIndex[0].title).toBeDefined();
expect(documentsToIndex[0].metadata).not.toBeDefined();
expect(documentsToIndex[0].fullText).not.toBeDefined();
expect(documentsToIndex[0].relationships.length).toBe(4);

expect(documentsToIndex[1].title).toBeDefined();
expect(documentsToIndex[1].fullText).not.toBeDefined();
expect(documentsToIndex[1].relationships.length).toBe(4);

expect(documentsToIndex[2].title).toBeDefined();
expect(documentsToIndex[2].fullText).not.toBeDefined();
expect(documentsToIndex[2].relationships.length).toBe(4);
done();
})
.catch(catchErrors(done));
});

it('should index entities withh fullText', (done) => {
entities.indexEntities({ sharedId: 'shared' }, '+fullText')
.then(() => {
const documentsToIndex = search.bulkIndex.calls.argsFor(0)[0];
expect(documentsToIndex[0].title).toBeDefined();
expect(documentsToIndex[0].fullText).toBeDefined();
expect(documentsToIndex[0].relationships.length).toBe(4);

expect(documentsToIndex[1].title).toBeDefined();
expect(documentsToIndex[1].metadata).not.toBeDefined();
expect(documentsToIndex[1].relationships.length).toBe(4);

expect(documentsToIndex[2].title).toBeDefined();
expect(documentsToIndex[2].metadata).not.toBeDefined();
expect(documentsToIndex[2].relationships.length).toBe(4);
done();
})
.catch(catchErrors(done));
Expand Down Expand Up @@ -692,6 +715,14 @@ describe('entities', () => {
});
});

describe('when page is blank', () => {
it('should not throw a 404', async () => {
const pageNumber = 3;
const page = await entities.getRawPage('shared', 'en', pageNumber);

expect(page).toBe('');
});
});
describe('when page do not exists', () => {
it('should throw 404 error', async () => {
const pageNumber = 200;
Expand All @@ -715,21 +746,21 @@ describe('entities', () => {
});

it('should delete the document from the search', done => entities.delete('shared')
.then(() => {
const argumnets = search.delete.calls.allArgs();
expect(search.delete).toHaveBeenCalled();
expect(argumnets[0][0]._id.toString()).toBe(batmanFinishesId.toString());
done();
})
.catch(catchErrors(done)));
.then(() => {
const argumnets = search.delete.calls.allArgs();
expect(search.delete).toHaveBeenCalled();
expect(argumnets[0][0]._id.toString()).toBe(batmanFinishesId.toString());
done();
})
.catch(catchErrors(done)));

it('should delete the document relationships', done => entities.delete('shared')
.then(() => relationships.get({ entity: 'shared' }))
.then((refs) => {
expect(refs.length).toBe(0);
done();
})
.catch(catchErrors(done)));
.then(() => relationships.get({ entity: 'shared' }))
.then((refs) => {
expect(refs.length).toBe(0);
done();
})
.catch(catchErrors(done)));

it('should delete the original file', (done) => {
fs.writeFileSync(path.join(uploadDocumentsPath, '8202c463d6158af8065022d9b5014ccb.pdf'));
Expand Down
21 changes: 11 additions & 10 deletions app/api/entities/specs/fixtures.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ export default {
fullText: {
1: 'page[[1]] 1[[1]]',
2: 'page[[2]] 2[[2]]',
3: '',
},
metadata: {
property1: 'value1'
Expand Down Expand Up @@ -107,16 +108,16 @@ export default {
] }
],
connections: [
{ _id: referenceId, entity: 'shared', template: null, hub: hub1, language: 'en' },
{ entity: 'shared2', template: 'relation1', hub: hub1, language: 'en' },
{ entity: 'shared', template: null, hub: hub2, language: 'en' },
{ entity: 'source2', template: 'relation2', hub: hub2, language: 'en' },
{ entity: 'another', template: 'relation3', hub: hub3, language: 'en' },
{ entity: 'document', template: 'relation3', hub: hub3, language: 'en' },
{ entity: 'shared', template: 'relation2', hub: hub4, language: 'en' },
{ entity: 'shared1', template: 'relation2', hub: hub4, language: 'en' },
{ entity: 'shared1', template: 'relation2', hub: hub5, language: 'en' },
{ entity: 'shared', template: 'relation2', hub: hub5, language: 'en' }
{ _id: referenceId, entity: 'shared', template: null, hub: hub1 },
{ entity: 'shared2', template: 'relation1', hub: hub1 },
{ entity: 'shared', template: null, hub: hub2 },
{ entity: 'source2', template: 'relation2', hub: hub2 },
{ entity: 'another', template: 'relation3', hub: hub3 },
{ entity: 'document', template: 'relation3', hub: hub3 },
{ entity: 'shared', template: 'relation2', hub: hub4 },
{ entity: 'shared1', template: 'relation2', hub: hub4 },
{ entity: 'shared1', template: 'relation2', hub: hub5 },
{ entity: 'shared', template: 'relation2', hub: hub5 }
]
};

Expand Down
64 changes: 29 additions & 35 deletions app/react/Viewer/actions/documentActions.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,20 +43,18 @@ export function saveDocument(doc) {
}
});

return function (dispatch) {
return documents.api.save(updateDoc)
.then((updatedDoc) => {
dispatch(notify('Document updated', 'success'));
dispatch({ type: types.VIEWER_UPDATE_DOCUMENT, doc });
dispatch(formActions.reset('documentViewer.sidepanel.metadata'));
dispatch(actions.set('viewer/doc', updatedDoc));
dispatch(relationshipActions.reloadRelationships(updatedDoc.sharedId));
});
};
return dispatch => documents.api.save(updateDoc)
.then((updatedDoc) => {
dispatch(notify('Document updated', 'success'));
dispatch({ type: types.VIEWER_UPDATE_DOCUMENT, doc });
dispatch(formActions.reset('documentViewer.sidepanel.metadata'));
dispatch(actions.set('viewer/doc', updatedDoc));
dispatch(relationshipActions.reloadRelationships(updatedDoc.sharedId));
});
}

export function saveToc(toc) {
return function (dispatch, getState) {
return (dispatch, getState) => {
const { _id, _rev, sharedId, file } = getState().documentViewer.doc.toJS();
dispatch(formActions.reset('documentViewer.sidepanel.metadata'));
dispatch(actions.set('documentViewer/tocBeingEdited', false));
Expand All @@ -65,15 +63,13 @@ export function saveToc(toc) {
}

export function deleteDocument(doc) {
return function (dispatch) {
return documents.api.delete(doc)
.then(() => {
dispatch(notify('Document deleted', 'success'));
dispatch(resetDocumentViewer());
dispatch(removeDocument(doc));
dispatch(unselectAllDocuments());
});
};
return dispatch => documents.api.delete(doc)
.then(() => {
dispatch(notify('Document deleted', 'success'));
dispatch(resetDocumentViewer());
dispatch(removeDocument(doc));
dispatch(unselectAllDocuments());
});
}

export function getDocument(id) {
Expand All @@ -88,28 +84,26 @@ export function getDocument(id) {
}
return PDFUtils.extractPDFInfo(`${APIURL}documents/download?_id=${doc._id}`)
.then((pdfInfo) => {
doc.pdfInfo = pdfInfo;
return api.post('documents/pdfInfo', doc)
const { _id, sharedId } = doc;
return api.post('documents/pdfInfo', { _id, sharedId, pdfInfo })
.then(res => res.json);
});
});
}

export function loadTargetDocument(id) {
return function (dispatch) {
return Promise.all([
return dispatch => Promise.all([
getDocument(id),
referencesAPI.get(id)
])
.then(([targetDoc, references]) => {
dispatch(actions.set('viewer/targetDoc', targetDoc));
dispatch(actions.set('viewer/targetDocReferences', references));
});
};
])
.then(([targetDoc, references]) => {
dispatch(actions.set('viewer/targetDoc', targetDoc));
dispatch(actions.set('viewer/targetDocReferences', references));
});
}

export function cancelTargetDocument() {
return function (dispatch) {
return (dispatch) => {
dispatch({ type: connectionsTypes.CANCEL_RANGED_CONNECTION });
dispatch(actions.unset('viewer/targetDoc'));
dispatch(actions.unset('viewer/targetDocReferences'));
Expand All @@ -119,7 +113,7 @@ export function cancelTargetDocument() {
}

export function editToc(toc) {
return function (dispatch) {
return (dispatch) => {
dispatch(actions.set('documentViewer/tocBeingEdited', true));
dispatch(formActions.load('documentViewer.tocForm', toc));
dispatch(uiActions.openPanel('viewMetadataPanel'));
Expand All @@ -128,7 +122,7 @@ export function editToc(toc) {
}

export function removeFromToc(tocElement) {
return function (dispatch, getState) {
return (dispatch, getState) => {
const state = getState();
let toc = state.documentViewer.tocForm;

Expand All @@ -139,7 +133,7 @@ export function removeFromToc(tocElement) {
}

export function indentTocElement(tocElement, indentation) {
return function (dispatch, getState) {
return (dispatch, getState) => {
const state = getState();
const toc = state.documentViewer.tocForm.map((_entry) => {
const entry = Object.assign({}, _entry);
Expand All @@ -154,7 +148,7 @@ export function indentTocElement(tocElement, indentation) {
}

export function addToToc(textSelectedObject) {
return function (dispatch, getState) {
return (dispatch, getState) => {
const state = getState();
let toc = state.documentViewer.tocForm.concat();
if (!toc.length) {
Expand Down
Loading

0 comments on commit 4d510b7

Please sign in to comment.