Kaynağa Gözat

simplified DocManager.updateDoc so it calls mongomanager.findDoc

makes the logic for dealing new documents that are not in mongo yet
much simpler
Henry Oswald 11 yıl önce
ebeveyn
işleme
7242119532

+ 18 - 23
services/docstore/app/coffee/DocManager.coffee

@@ -24,31 +24,26 @@ module.exports = DocManager =
 				return callback(null, docs)
 
 	updateDoc: (project_id, doc_id, lines, callback = (error, modified, rev) ->) ->
-		DocManager.getDoc project_id, doc_id, (error, doc) ->
-
-			isNewDoc = error?.name == new Errors.NotFoundError().name
-
-			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)
+		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)
+			
+			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"
 				return callback null, false, doc?.rev
-
-			oldRev = doc?.rev || 0
-			logger.log {
-				project_id: project_id
-				doc_id: doc_id,
-				oldDocLines: doc?.lines
-				newDocLines: lines
-				rev: oldRev
-			}, "updating doc lines"
-			MongoManager.upsertIntoDocCollection project_id, doc_id, lines, oldRev, (error)->
-				return callback(callback) if error?
-				callback null, true,  oldRev + 1 # rev will have been incremented in mongo by MongoManager.updateDoc
+			else
+				oldRev = doc?.rev || 0
+				logger.log {
+					project_id: project_id
+					doc_id: doc_id,
+					oldDocLines: doc?.lines
+					newDocLines: lines
+					rev: oldRev
+				}, "updating doc lines"
+				MongoManager.upsertIntoDocCollection project_id, doc_id, lines, oldRev, (error)->
+					return callback(callback) if error?
+					callback null, true,  oldRev + 1 # rev will have been incremented in mongo by MongoManager.updateDoc
 
 	deleteDoc: (project_id, doc_id, callback = (error) ->) ->
 		DocManager.getDoc project_id, doc_id, (error, doc) ->

+ 7 - 8
services/docstore/test/unit/coffee/DocManagerTests.coffee

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