simplified DocManager.updateDoc so it calls mongomanager.findDoc

makes the logic for dealing new documents that are not in mongo yet
much simpler
This commit is contained in:
Henry Oswald 2015-02-26 16:00:28 +00:00
parent 5861231548
commit 7242119532
2 changed files with 25 additions and 31 deletions

View file

@ -24,20 +24,15 @@ module.exports = DocManager =
return callback(null, docs) return callback(null, docs)
updateDoc: (project_id, doc_id, lines, callback = (error, modified, rev) ->) -> updateDoc: (project_id, doc_id, lines, callback = (error, modified, rev) ->) ->
DocManager.getDoc project_id, doc_id, (error, doc) -> MongoManager.findDoc doc_id, (err, doc)->
if err?
logger.err project_id: project_id, doc_id: doc_id, err:err, "error getting document for update"
return callback(err)
isNewDoc = error?.name == new Errors.NotFoundError().name if _.isEqual(doc?.lines, lines)
if !isNewDoc and error?
logger.err project_id: project_id, doc_id: doc_id, err:error, "error getting document for update"
return callback(error)
else if !isNewDoc and !doc?
logger.err project_id: project_id, doc_id: doc_id, "existing document to update could not be found"
return callback new Errors.NotFoundError("No such project/doc to update: #{project_id}/#{doc_id}")
else if _.isEqual(doc?.lines, lines)
logger.log project_id: project_id, doc_id: doc_id, rev: doc?.rev, "doc lines have not changed - not updating" logger.log project_id: project_id, doc_id: doc_id, rev: doc?.rev, "doc lines have not changed - not updating"
return callback null, false, doc?.rev return callback null, false, doc?.rev
else
oldRev = doc?.rev || 0 oldRev = doc?.rev || 0
logger.log { logger.log {
project_id: project_id project_id: project_id

View file

@ -142,17 +142,17 @@ describe "DocManager", ->
@newDocLines = ["new", "doc", "lines"] @newDocLines = ["new", "doc", "lines"]
@doc = { _id: @doc_id, lines: @oldDocLines, rev: @rev = 5 } @doc = { _id: @doc_id, lines: @oldDocLines, rev: @rev = 5 }
@MongoManager.updateDoc = sinon.stub().callsArg(3)
@MongoManager.upsertIntoDocCollection = sinon.stub().callsArg(4) @MongoManager.upsertIntoDocCollection = sinon.stub().callsArg(4)
@MongoManager.findDoc = sinon.stub()
describe "when the doc lines have changed", -> describe "when the doc lines have changed", ->
beforeEach -> beforeEach ->
@DocManager.getDoc = sinon.stub().callsArgWith(2, null, @doc) @MongoManager.findDoc = sinon.stub().callsArgWith(1, null, @doc)
@DocManager.updateDoc @project_id, @doc_id, @newDocLines, @callback @DocManager.updateDoc @project_id, @doc_id, @newDocLines, @callback
it "should get the existing doc", -> it "should get the existing doc", ->
@DocManager.getDoc @MongoManager.findDoc
.calledWith(@project_id, @doc_id) .calledWith(@doc_id)
.should.equal true .should.equal true
it "should upsert the document to the doc collection", -> it "should upsert the document to the doc collection", ->
@ -179,7 +179,7 @@ describe "DocManager", ->
beforeEach -> beforeEach ->
@error = new Error("doc could not be found") @error = new Error("doc could not be found")
@DocManager.getDoc = sinon.stub().callsArgWith(2, @error, null, null) @MongoManager.findDoc = sinon.stub().callsArgWith(1, @error, null, null)
@DocManager.updateDoc @project_id, @doc_id, @newDocLines, @callback @DocManager.updateDoc @project_id, @doc_id, @newDocLines, @callback
it "should not upsert the document to the doc collection", -> it "should not upsert the document to the doc collection", ->
@ -190,7 +190,7 @@ describe "DocManager", ->
describe "when the doc lines have not changed", -> describe "when the doc lines have not changed", ->
beforeEach -> beforeEach ->
@DocManager.getDoc = sinon.stub().callsArgWith(2, null, @doc) @MongoManager.findDoc = sinon.stub().callsArgWith(1, null, @doc)
@DocManager.updateDoc @project_id, @doc_id, @oldDocLines.slice(), @callback @DocManager.updateDoc @project_id, @doc_id, @oldDocLines.slice(), @callback
it "should not update the doc", -> it "should not update the doc", ->
@ -201,8 +201,7 @@ describe "DocManager", ->
describe "when the doc does not exist", -> describe "when the doc does not exist", ->
beforeEach -> beforeEach ->
NotFoundError = Errors.NotFoundError("doc could not be found") @MongoManager.findDoc = sinon.stub().callsArgWith(1, null, null, null)
@DocManager.getDoc = sinon.stub().callsArgWith(2, NotFoundError, null, null)
@DocManager.updateDoc @project_id, @doc_id, @newDocLines, @callback @DocManager.updateDoc @project_id, @doc_id, @newDocLines, @callback
it "should upsert the document to the doc collection", -> it "should upsert the document to the doc collection", ->