Skip to content
This repository has been archived by the owner on Jan 7, 2020. It is now read-only.

EPIC-1259 Deleting a document does not update its associated collections #296

Merged
merged 1 commit into from
Nov 1, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 19 additions & 4 deletions modules/documents/client/directives/documents.manager.directive.js
Original file line number Diff line number Diff line change
Expand Up @@ -394,11 +394,13 @@ angular.module('documents')
};

self.deleteDocument = function(documentID) {
var collections = null;
return Document.lookup(documentID)
.then( function (doc) {
.then(function(doc) {
collections = doc.collections;
return Document.getProjectDocumentVersions(doc._id);
})
.then( function (docs) {
.then(function(docs) {
// Are there any prior versions? If so, make them the latest and then delete
// otherwise delete
if (docs.length > 0) {
Expand All @@ -407,9 +409,22 @@ angular.module('documents')
return null;
}
})
.then( function () {
// Delete it from the system.
.then(function() {
// EPIC-1259: If there are there any collections associated with this document,
// then delete the collection document reference in the collections as well.
var promises = _.union(_.map(collections, function(c) {
return CollectionModel.removeOtherDocument(c._id, documentID);
}), _.map(collections, function(c) {
return CollectionModel.removeMainDocument(c._id, documentID);
}));
return Promise.all(promises);
})
.then(function () {
// Delete the document from the system.
return Document.deleteDocument(documentID);
})
.catch (function(err) {
AlertService.error('The document could not be deleted.');
});
};

Expand Down