mirror of
https://github.com/overleaf/overleaf.git
synced 2024-11-21 20:47:08 -05:00
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:
parent
5861231548
commit
7242119532
2 changed files with 25 additions and 31 deletions
|
@ -24,31 +24,26 @@ 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?
|
||||||
isNewDoc = error?.name == new Errors.NotFoundError().name
|
logger.err project_id: project_id, doc_id: doc_id, err:err, "error getting document for update"
|
||||||
|
return callback(err)
|
||||||
if !isNewDoc and error?
|
|
||||||
logger.err project_id: project_id, doc_id: doc_id, err:error, "error getting document for update"
|
if _.isEqual(doc?.lines, lines)
|
||||||
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
|
||||||
doc_id: doc_id,
|
doc_id: doc_id,
|
||||||
oldDocLines: doc?.lines
|
oldDocLines: doc?.lines
|
||||||
newDocLines: lines
|
newDocLines: lines
|
||||||
rev: oldRev
|
rev: oldRev
|
||||||
}, "updating doc lines"
|
}, "updating doc lines"
|
||||||
MongoManager.upsertIntoDocCollection project_id, doc_id, lines, oldRev, (error)->
|
MongoManager.upsertIntoDocCollection project_id, doc_id, lines, oldRev, (error)->
|
||||||
return callback(callback) if error?
|
return callback(callback) if error?
|
||||||
callback null, true, oldRev + 1 # rev will have been incremented in mongo by MongoManager.updateDoc
|
callback null, true, oldRev + 1 # rev will have been incremented in mongo by MongoManager.updateDoc
|
||||||
|
|
||||||
deleteDoc: (project_id, doc_id, callback = (error) ->) ->
|
deleteDoc: (project_id, doc_id, callback = (error) ->) ->
|
||||||
DocManager.getDoc project_id, doc_id, (error, doc) ->
|
DocManager.getDoc project_id, doc_id, (error, doc) ->
|
||||||
|
|
|
@ -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", ->
|
||||||
|
|
Loading…
Reference in a new issue