From a9992bc4861e629d9ef2c52367a9d07bc1af5246 Mon Sep 17 00:00:00 2001 From: daneryl Date: Tue, 28 Mar 2017 13:30:34 +0200 Subject: [PATCH 1/2] delete entities files now ignores file do not exist error --- app/api/entities/entities.js | 9 ++++++++- app/api/entities/specs/entities.spec.js | 12 ++++++++++++ app/api/entities/specs/fixtures.js | 2 +- app/api/utils/files.js | 2 +- 4 files changed, 22 insertions(+), 3 deletions(-) diff --git a/app/api/entities/entities.js b/app/api/entities/entities.js index a4149f2cc6..281ff0693c 100644 --- a/app/api/entities/entities.js +++ b/app/api/entities/entities.js @@ -172,7 +172,14 @@ export default { return `./uploaded_documents/${doc.file.filename}`; }); filesToDelete = filesToDelete.filter((doc, index) => filesToDelete.indexOf(doc) === index); - return deleteFiles(filesToDelete); + return deleteFiles(filesToDelete) + .catch((error) => { + if (error.errno === -2) { + return Promise.resolve(); + } + + return Promise.reject(error); + }); }, deleteMultiple(sharedIds) { diff --git a/app/api/entities/specs/entities.spec.js b/app/api/entities/specs/entities.spec.js index 184d605c83..ae044c90f1 100644 --- a/app/api/entities/specs/entities.spec.js +++ b/app/api/entities/specs/entities.spec.js @@ -398,6 +398,18 @@ describe('entities', () => { fs.writeFileSync('./uploaded_documents/8202c463d6158af8065022d9b5014cc1.pdf'); }); + describe('when the original file does not exist', () => { + it('should delete the entiti and not throw an error', (done) => { + entities.delete('shared1') + .then(() => entities.get({sharedId: 'shared1'})) + .then((response) => { + expect(response.length).toBe(0); + done(); + }) + .catch(catchErrors(done)); + }); + }); + it('should delete the document in the database', (done) => { entities.delete('shared') .then(() => entities.get({sharedId: 'shared'})) diff --git a/app/api/entities/specs/fixtures.js b/app/api/entities/specs/fixtures.js index 859d4c2879..fff60a203f 100644 --- a/app/api/entities/specs/fixtures.js +++ b/app/api/entities/specs/fixtures.js @@ -17,7 +17,7 @@ export default { _id: db.id(), sharedId: 'shared', type: 'entity', language: 'pt', title: 'Penguin almost done', creationDate: 1, published: true, metadata: {text: 'test'} }, //select/multiselect/date sync - {_id: syncPropertiesEntityId, template: templateId, sharedId: 'shared1', type: 'entity', language: 'en', title: 'EN', published: true, metadata: {property1: 'text'}}, + {_id: syncPropertiesEntityId, template: templateId, sharedId: 'shared1', type: 'entity', language: 'en', title: 'EN', published: true, metadata: {property1: 'text'}, file: {filename: 'nonexistent.pdf'}}, {_id: db.id(), template: templateId, sharedId: 'shared1', type: 'entity', language: 'es', title: 'ES', creationDate: 1, published: true, metadata: {property1: 'text'}}, {_id: db.id(), template: templateId, sharedId: 'shared1', type: 'entity', language: 'pt', title: 'PT', creationDate: 1, published: true, metadata: {property1: 'text'}}, //docs to change metadata property names diff --git a/app/api/utils/files.js b/app/api/utils/files.js index 0859a5a590..5a9380ac06 100644 --- a/app/api/utils/files.js +++ b/app/api/utils/files.js @@ -12,7 +12,7 @@ function deleteFile(file) { } function deleteFiles(files) { - return Promise.all(files.map((file) => deleteFile(file))) + return Promise.all(files.map((file) => deleteFile(file))); } export {deleteFiles, deleteFile}; From 375ad89befcff3ac476f3700ae6d62ef5f591a29 Mon Sep 17 00:00:00 2001 From: daneryl Date: Tue, 28 Mar 2017 17:04:44 +0200 Subject: [PATCH 2/2] refactor error code to a constant --- app/api/entities/entities.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/app/api/entities/entities.js b/app/api/entities/entities.js index 281ff0693c..0c4d92b383 100644 --- a/app/api/entities/entities.js +++ b/app/api/entities/entities.js @@ -174,7 +174,8 @@ export default { filesToDelete = filesToDelete.filter((doc, index) => filesToDelete.indexOf(doc) === index); return deleteFiles(filesToDelete) .catch((error) => { - if (error.errno === -2) { + const fileNotExist = -2; + if (error.errno === fileNotExist) { return Promise.resolve(); }