send both doc update and file updates to doc-updater

This commit is contained in:
Hayden Faulds 2017-11-06 15:11:33 +00:00
parent 06116dc956
commit 3ce03a40f9
4 changed files with 49 additions and 36 deletions

View file

@ -204,29 +204,19 @@ module.exports = DocumentUpdaterHandler =
logger.error {project_id, doc_id, thread_id}, "doc updater returned a non-success status code: #{res.statusCode}"
callback new Error("doc updater returned a non-success status code: #{res.statusCode}")
updateProjectStructure : (project_id, user_id, oldDocs, newDocs, callback = (error) ->)->
updateProjectStructure : (project_id, userId, oldDocs, newDocs, oldFiles, newFiles, callback = (error) ->)->
return callback() if !settings.apis.project_history?.enabled
docRenameUpdates = []
for oldDoc in oldDocs
newDoc = _.find newDocs, (newDoc) ->
newDoc.doc._id.toString() == oldDoc.doc._id.toString()
if newDoc.path != oldDoc.path
docRenameUpdates.push
id: oldDoc.doc._id
pathname: oldDoc.path
newPathname: newDoc.path
docUpdates = DocumentUpdaterHandler._getRenameUpdates('doc', oldDocs, newDocs)
fileUpdates = DocumentUpdaterHandler._getRenameUpdates('file', oldFiles, newFiles)
timer = new metrics.Timer("set-document")
url = "#{settings.apis.documentupdater.url}/project/#{project_id}"
body =
url: url
json:
updates: docRenameUpdates
user_id: user_id
json: { docUpdates, fileUpdates, userId }
return callback() if docRenameUpdates.length < 1
return callback() if (docUpdates.length + fileUpdates.length) < 1
request.post body, (error, res, body)->
timer.done()
@ -240,6 +230,22 @@ module.exports = DocumentUpdaterHandler =
logger.error {project_id, url}, "doc updater returned a non-success status code: #{res.statusCode}"
callback new Error("doc updater returned a non-success status code: #{res.statusCode}")
_getRenameUpdates: (entityType, oldEntities, newEntities) ->
updates = []
for oldEntity in oldEntities
id = oldEntity[entityType]._id
newEntity = _.find newEntities, (newEntity) ->
newEntity[entityType]._id.toString() == id.toString()
if newEntity.path != oldEntity.path
updates.push
id: id
pathname: oldEntity.path
newPathname: newEntity.path
updates
PENDINGUPDATESKEY = "PendingUpdates"
DOCLINESKEY = "doclines"
DOCIDSWITHPENDINGUPDATES = "DocsWithPendingUpdates"

View file

@ -368,7 +368,7 @@ module.exports = ProjectEntityHandler =
return callback(err) if err?
self._checkValidMove project, entityType, entityPath, destFolderId, (error) ->
return callback(error) if error?
ProjectEntityHandler.getAllEntitiesFromProject project, (error, oldDocs) =>
ProjectEntityHandler.getAllEntitiesFromProject project, (error, oldDocs, oldFiles) =>
return callback(error) if error?
self._removeElementFromMongoArray Project, project_id, entityPath.mongo, (err, newProject)->
return callback(err) if err?
@ -381,10 +381,11 @@ module.exports = ProjectEntityHandler =
endPath: result.path.fileSystem,
rev: entity.rev
tpdsUpdateSender.moveEntity opts
ProjectEntityHandler.getAllEntitiesFromProject newProject, (error, newDocs) =>
ProjectEntityHandler.getAllEntitiesFromProject newProject, (error, newDocs, newFiles
) =>
return callback(error) if error?
documentUpdaterHandler = require('../../Features/DocumentUpdater/DocumentUpdaterHandler')
documentUpdaterHandler.updateProjectStructure project_id, userId, oldDocs, newDocs, callback
documentUpdaterHandler.updateProjectStructure project_id, userId, oldDocs, newDocs, oldFiles, newFiles, callback
_checkValidMove: (project, entityType, entityPath, destFolderId, callback = (error) ->) ->
return callback() if !entityType.match(/folder/)
@ -427,7 +428,7 @@ module.exports = ProjectEntityHandler =
entityType = entityType.toLowerCase()
ProjectGetter.getProject project_id, {rootFolder:true, name:true}, (error, project)=>
return callback(error) if error?
ProjectEntityHandler.getAllEntitiesFromProject project, (error, oldDocs) =>
ProjectEntityHandler.getAllEntitiesFromProject project, (error, oldDocs, oldFiles) =>
return callback(error) if error?
projectLocator.findElement {project:project, element_id:entity_id, type:entityType}, (error, entity, path)=>
return callback(error) if error?
@ -439,10 +440,10 @@ module.exports = ProjectEntityHandler =
tpdsUpdateSender.moveEntity({project_id:project_id, startPath:path.fileSystem, endPath:endPath, project_name:project.name, rev:entity.rev})
Project.findOneAndUpdate conditions, update, { "new": true}, (error, newProject) ->
return callback(error) if error?
ProjectEntityHandler.getAllEntitiesFromProject newProject, (error, newDocs) =>
ProjectEntityHandler.getAllEntitiesFromProject newProject, (error, newDocs, newFiles) =>
return callback(error) if error?
documentUpdaterHandler = require('../../Features/DocumentUpdater/DocumentUpdaterHandler')
documentUpdaterHandler.updateProjectStructure project_id, userId, oldDocs, newDocs, callback
documentUpdaterHandler.updateProjectStructure project_id, userId, oldDocs, newDocs, oldFiles, newFiles, callback
_cleanUpEntity: (project, entity, entityType, callback = (error) ->) ->
if(entityType.indexOf("file") != -1)

View file

@ -401,13 +401,15 @@ describe 'DocumentUpdaterHandler', ->
{ path: '/old_a', doc: _id: new ObjectId(@docIdA.toString()) }
{ path: '/new_b', doc: _id: new ObjectId(@docIdB.toString()) }
]
@oldFiles = []
@newFiles = []
describe "with project history disabled", ->
beforeEach ->
@settings.apis.project_history.enabled = false
@request.post = sinon.stub()
@handler.updateProjectStructure @project_id, @user_id, @oldDocs, @newDocs, @callback
@handler.updateProjectStructure @project_id, @user_id, @oldDocs, @newDocs, @oldFiles, @newFiles, @callback
it 'does not make a web request', ->
@request.post.called.should.equal false
@ -419,10 +421,10 @@ describe 'DocumentUpdaterHandler', ->
beforeEach ->
@settings.apis.project_history.enabled = true
@request.post = sinon.stub().callsArgWith(1, null, {statusCode: 204}, "")
@handler.updateProjectStructure @project_id, @user_id, @oldDocs, @newDocs, @callback
@handler.updateProjectStructure @project_id, @user_id, @oldDocs, @newDocs, @oldFiles, @newFiles, @callback
it 'should send the structure update to the document updater', ->
updates = [
docUpdates = [
id: @docIdB,
pathname: "/old_b"
newPathname: "/new_b"
@ -430,7 +432,7 @@ describe 'DocumentUpdaterHandler', ->
url = "#{@settings.apis.documentupdater.url}/project/#{@project_id}"
@request.post
.calledWith(url: url, json: {updates, @user_id})
.calledWith(url: url, json: {docUpdates, fileUpdates: [], userId: @user_id})
.should.equal true
it "should call the callback with no error", ->

View file

@ -240,12 +240,14 @@ describe 'ProjectEntityHandler', ->
@ProjectEntityHandler._putElement = sinon.stub().callsArgWith(4, null, path: @pathAfterMove)
@ProjectGetter.getProject.callsArgWith(2, null, @project)
@tpdsUpdateSender.moveEntity = sinon.stub()
@documentUpdaterHandler.updateProjectStructure = sinon.stub().callsArg(4)
@documentUpdaterHandler.updateProjectStructure = sinon.stub().callsArg(6)
@ProjectEntityHandler.getAllEntitiesFromProject = sinon.stub()
@ProjectEntityHandler.getAllEntitiesFromProject
.onFirstCall().callsArgWith(1, null, @oldDocs = [])
.onFirstCall()
.callsArgWith(1, null, @oldDocs = ['old-doc'], @oldFiles = ['old-file'])
@ProjectEntityHandler.getAllEntitiesFromProject
.onSecondCall().callsArgWith(1, null, @newDocs = [])
.onSecondCall()
.callsArgWith(1, null, @newDocs = ['new-doc'], @newFiles = ['new-file'])
describe "moving a doc", ->
beforeEach (done) ->
@ -270,7 +272,7 @@ describe 'ProjectEntityHandler', ->
it "should should send the update to the doc updater", ->
@documentUpdaterHandler.updateProjectStructure
.calledWith(project_id, userId, @oldDocs, @newDocs)
.calledWith(project_id, userId, @oldDocs, @newDocs, @oldFiles, @newFiles)
.should.equal true
it 'should remove the element from its current position', ->
@ -322,7 +324,7 @@ describe 'ProjectEntityHandler', ->
it "should should send the update to the doc updater", ->
@documentUpdaterHandler.updateProjectStructure
.calledWith(project_id, userId, @oldDocs, @newDocs)
.calledWith(project_id, userId, @oldDocs, @newDocs, @oldFiles, @newFiles)
.should.equal true
it 'should remove the element from its current position', ->
@ -1001,20 +1003,22 @@ describe 'ProjectEntityHandler', ->
@ProjectGetter.getProject.callsArgWith(2, null, @project)
@ProjectEntityHandler.getAllEntitiesFromProject = sinon.stub()
@ProjectEntityHandler.getAllEntitiesFromProject
.onFirstCall().callsArgWith(1, null, @oldDocs = [])
.onFirstCall()
.callsArgWith(1, null, @oldDocs = ['old-doc'], @oldFiles = ['old-file'])
@ProjectEntityHandler.getAllEntitiesFromProject
.onSecondCall().callsArgWith(1, null, @newDocs = [])
.onSecondCall()
.callsArgWith(1, null, @newDocs = ['new-doc'], @newFiles = ['new-file'])
@projectLocator.findElement = sinon.stub().callsArgWith(1, null, @entity = { _id: @entity_id, name:"old.tex", rev:4 }, @path)
@tpdsUpdateSender.moveEntity = sinon.stub()
@ProjectModel.findOneAndUpdate = sinon.stub().callsArgWith(3, null, @project)
@documentUpdaterHandler.updateProjectStructure = sinon.stub().callsArg(4)
@documentUpdaterHandler.updateProjectStructure = sinon.stub().callsArg(6)
it "should should send the old and new project structure to the doc updater", (done) ->
@ProjectEntityHandler.renameEntity project_id, @entity_id, @entityType, @newName, userId, =>
@documentUpdaterHandler.updateProjectStructure.calledWith(
project_id, userId, @oldDocs, @newDocs,
).should.equal true
@documentUpdaterHandler.updateProjectStructure
.calledWith(project_id, userId, @oldDocs, @newDocs, @oldFiles, @newFiles)
.should.equal true
done()
it "should update the name in mongo", (done)->