diff --git a/services/docstore/app/coffee/DocManager.coffee b/services/docstore/app/coffee/DocManager.coffee index db60024a99..0d730ad7de 100644 --- a/services/docstore/app/coffee/DocManager.coffee +++ b/services/docstore/app/coffee/DocManager.coffee @@ -31,6 +31,14 @@ module.exports = DocManager = return callback(error) if error? callback null, true + deleteDoc: (project_id, doc_id, callback = (error) ->) -> + DocManager.getDoc project_id, doc_id, (error, doc) -> + return callback(error) if error? + return callback new Errors.NotFoundError("No such project/doc: #{project_id}/#{doc_id}") if !doc? + MongoManager.insertDoc project_id, doc_id, { lines: doc.lines, deleted: true }, (error) -> + return callback(error) if error? + callback() + findDocInProject: (project, doc_id, callback = (error, doc, mongoPath) ->) -> result = @_findDocInFolder project.rootFolder[0], doc_id, "rootFolder.0" if result? diff --git a/services/docstore/app/coffee/MongoManager.coffee b/services/docstore/app/coffee/MongoManager.coffee index 422e678255..a988c9bd01 100644 --- a/services/docstore/app/coffee/MongoManager.coffee +++ b/services/docstore/app/coffee/MongoManager.coffee @@ -12,4 +12,9 @@ module.exports = MongoManager = update.$set["#{docPath}.lines"] = lines update.$inc["#{docPath}.rev"] = 1 - db.projects.update _id: ObjectId(project_id), update, callback \ No newline at end of file + db.projects.update _id: ObjectId(project_id), update, callback + + insertDoc: (project_id, doc_id, attributes, callback = (error) ->) -> + attributes._id = ObjectId(doc_id) + attributes.project_id = ObjectId(project_id) + db.projects.insert attributes, callback \ No newline at end of file diff --git a/services/docstore/test/unit/coffee/DocManagerTests.coffee b/services/docstore/test/unit/coffee/DocManagerTests.coffee index e884be28af..0ab746e2d2 100644 --- a/services/docstore/test/unit/coffee/DocManagerTests.coffee +++ b/services/docstore/test/unit/coffee/DocManagerTests.coffee @@ -70,6 +70,44 @@ describe "DocManager", -> .calledWith(new Errors.NotFoundError("No such doc: #{@doc_id}")) .should.equal true + describe "deleteDoc", -> + describe "when the doc exists", -> + beforeEach -> + @lines = ["mock", "doc", "lines"] + @DocManager.getDoc = sinon.stub().callsArgWith(2, null, lines: @lines) + @MongoManager.insertDoc = sinon.stub().callsArg(3) + @DocManager.deleteDoc @project_id, @doc_id, @callback + + it "should get the doc", -> + @DocManager.getDoc + .calledWith(@project_id, @doc_id) + .should.equal true + + it "insert the doc as a deleted doc", -> + @MongoManager.insertDoc + .calledWith(@project_id, @doc_id, { + lines: @lines + deleted: true + }) + .should.equal true + + it "should return the callback", -> + @callback.called.should.equal true + + describe "when the doc does not exist", -> + beforeEach -> + @DocManager.getDoc = sinon.stub().callsArgWith(2, null, null) + @MongoManager.insertDoc = sinon.stub().callsArg(3) + @DocManager.deleteDoc @project_id, @doc_id, @callback + + it "should not try to insert a deleted doc", -> + @MongoManager.insertDoc.called.should.equal false + + it "should return a NotFoundError", -> + @callback + .calledWith(new Errors.NotFoundError("No such doc: #{@doc_id}")) + .should.equal true + describe "updateDoc", -> beforeEach -> @oldDocLines = ["old", "doc", "lines"] diff --git a/services/docstore/test/unit/coffee/MongoManagerTests.coffee b/services/docstore/test/unit/coffee/MongoManagerTests.coffee index 61c0b45de6..63386d757c 100644 --- a/services/docstore/test/unit/coffee/MongoManagerTests.coffee +++ b/services/docstore/test/unit/coffee/MongoManagerTests.coffee @@ -50,3 +50,22 @@ describe "MongoManager", -> it "should call the callback with the project", -> @callback.called.should.equal true + + describe "insertDoc", -> + beforeEach -> + @doc_id = ObjectId().toString() + @lines = ["mock-lines"] + @db.projects.insert = sinon.stub().callsArg(1) + @MongoManager.insertDoc @project_id, @doc_id, lines: @lines, @callback + + it "should insert the attributes with the given doc and project id", -> + @db.projects.insert + .calledWith({ + _id: ObjectId(@doc_id) + project_id: ObjectId(@project_id) + lines: @lines + }) + .should.equal true + + it "should call the callback", -> + @callback.called.should.equal true \ No newline at end of file