Use flag to only get deleted doc when appropriate

This commit is contained in:
James Allen 2014-06-05 14:10:44 +01:00
parent 7870b4fb55
commit 4818ae8131
4 changed files with 41 additions and 35 deletions

View file

@ -4,7 +4,11 @@ logger = require "logger-sharelatex"
_ = require "underscore"
module.exports = DocManager =
getDoc: (project_id, doc_id, callback = (error, doc, mongoPath) ->) ->
getDoc: (project_id, doc_id, options, callback = (error, doc, mongoPath) ->) ->
if typeof(options) == "function"
callback = options
options.include_deleted = false
MongoManager.findProject project_id, (error, project) ->
return callback(error) if error?
return callback new Errors.NotFoundError("No such project: #{project_id}") if !project?
@ -13,11 +17,13 @@ module.exports = DocManager =
if doc?
return callback null, doc, mongoPath
else
# Perhaps it's a deleted doc
if options.include_deleted
MongoManager.findDoc doc_id, (error, doc) ->
return callback(error) if error?
return callback new Errors.NotFoundError("No such doc: #{project_id}") if !doc?
return callback null, doc
else
return callback new Errors.NotFoundError("No such doc: #{project_id}")
getAllDocs: (project_id, callback = (error, docs) ->) ->
MongoManager.findProject project_id, (error, project) ->

View file

@ -6,7 +6,7 @@ module.exports = HttpController =
project_id = req.params.project_id
doc_id = req.params.doc_id
logger.log project_id: project_id, doc_id: doc_id, "getting doc"
DocManager.getDoc project_id, doc_id, (error, doc) ->
DocManager.getDoc project_id, doc_id, include_deleted: true, (error, doc) ->
return next(error) if error?
logger.log doc: doc, "got doc"
if !doc?

View file

@ -17,7 +17,7 @@ describe "DocManager", ->
@callback = sinon.stub()
describe "getDoc", ->
describe "when the project exists", ->
describe "when the project exists and the doc is in it", ->
beforeEach ->
@project = { name: "mock-project" }
@doc = { _id: @doc_id, lines: ["mock-lines"] }
@ -53,17 +53,15 @@ describe "DocManager", ->
.calledWith(new Errors.NotFoundError("No such project: #{@project_id}"))
.should.equal true
describe "when the doc does not exist in the project tree", ->
describe "when the doc is deleted", ->
beforeEach ->
@MongoManager.findProject = sinon.stub().callsArgWith(1, null, @project)
@DocManager.findDocInProject = sinon.stub().callsArgWith(2, null, null, null)
@MongoManager.findDoc = sinon.stub().callsArgWith(1, null, @doc)
@DocManager.getDoc @project_id, @doc_id, @callback
it "should try to find the doc in the project", ->
@DocManager.findDocInProject
.calledWith(@project, @doc_id)
.should.equal true
describe "when include_deleted = true", ->
beforeEach ->
@DocManager.getDoc @project_id, @doc_id, include_deleted: true, @callback
it "should try to find the doc in the docs collection", ->
@MongoManager.findDoc
@ -75,6 +73,18 @@ describe "DocManager", ->
.calledWith(null, @doc)
.should.equal true
describe "when include_deleted is not set", ->
beforeEach ->
@DocManager.getDoc @project_id, @doc_id, @callback
it "should not try to find the doc in the docs collection", ->
@MongoManager.findDoc.called.should.equal false
it "should return a NotFoundError", ->
@callback
.calledWith(new Errors.NotFoundError("No such doc: #{@doc_id}"))
.should.equal true
describe "when the doc does not exist anywhere", ->
beforeEach ->
@MongoManager.findProject = sinon.stub().callsArgWith(1, null, @project)
@ -82,16 +92,6 @@ describe "DocManager", ->
@MongoManager.findDoc = sinon.stub().callsArgWith(1, null, null)
@DocManager.getDoc @project_id, @doc_id, @callback
it "should try to find the doc in the project", ->
@DocManager.findDocInProject
.calledWith(@project, @doc_id)
.should.equal true
it "should try to find the doc in the docs collection", ->
@MongoManager.findDoc
.calledWith(@doc_id)
.should.equal true
it "should return a NotFoundError", ->
@callback
.calledWith(new Errors.NotFoundError("No such doc: #{@doc_id}"))

View file

@ -29,12 +29,12 @@ describe "HttpController", ->
@req.params =
project_id: @project_id
doc_id: @doc_id
@DocManager.getDoc = sinon.stub().callsArgWith(2, null, @doc)
@DocManager.getDoc = sinon.stub().callsArgWith(3, null, @doc)
@HttpController.getDoc @req, @res, @next
it "should get the document", ->
it "should get the document (including deleted)", ->
@DocManager.getDoc
.calledWith(@project_id, @doc_id)
.calledWith(@project_id, @doc_id, include_deleted: true)
.should.equal true
it "should return the doc as JSON", ->