fix ProjectEntityHandler.replaceFile

This commit is contained in:
Hayden Faulds 2017-11-29 11:37:47 +00:00
parent 77eac423c7
commit 497c6facd5
2 changed files with 31 additions and 34 deletions

View file

@ -224,7 +224,7 @@ module.exports = ProjectEntityHandler =
replaceFile: (project_id, file_id, fsPath, userId, callback)->
self = ProjectEntityHandler
FileStoreHandler.uploadFileFromDisk project_id, file_id, fsPath, (err)->
FileStoreHandler.uploadFileFromDisk project_id, file_id, fsPath, (err, fileStoreUrl)->
return callback(err) if err?
ProjectGetter.getProject project_id, {rootFolder: true, name:true}, (err, project) ->
return callback(err) if err?
@ -233,25 +233,25 @@ module.exports = ProjectEntityHandler =
# then the path to the file element will be out of date. In practice
# this is not a problem so long as we do not do anything longer running
# between them (like waiting for the file to upload.)
self.getAllEntitiesFromProject project, (err, oldDocs, oldFiles) =>
projectLocator.findElement {project:project, element_id: file_id, type: 'file'}, (err, fileRef, path)=>
return callback(err) if err?
projectLocator.findElement {project:project, element_id: file_id, type: 'file'}, (err, fileRef, path)=>
tpdsUpdateSender.addFile {project_id:project._id, file_id:fileRef._id, path:path.fileSystem, rev:fileRef.rev+1, project_name:project.name}, (err) ->
return callback(err) if err?
tpdsUpdateSender.addFile {project_id:project._id, file_id:fileRef._id, path:path.fileSystem, rev:fileRef.rev+1, project_name:project.name}, (err) ->
conditions = _id:project._id
inc = {}
inc["#{path.mongo}.rev"] = 1
set = {}
set["#{path.mongo}.created"] = new Date()
update =
"$inc": inc
"$set": set
Project.findOneAndUpdate conditions, update, { "new": true}, (err) ->
return callback(err) if err?
conditions = _id:project._id
inc = {}
inc["#{path.mongo}.rev"] = 1
set = {}
set["#{path.mongo}.created"] = new Date()
update =
"$inc": inc
"$set": set
Project.findOneAndUpdate conditions, update, { "new": true}, (err, newProject) ->
return callback(err) if err?
self.getAllEntitiesFromProject newProject, (err, newDocs, newFiles) =>
return callback(err) if err?
DocumentUpdaterHandler.updateProjectStructure project_id, userId, oldDocs, newDocs, oldFiles, newFiles, callback
newFile =
file: fileRef
path: path.fileSystem
url: fileStoreUrl
DocumentUpdaterHandler.updateProjectStructure project_id, userId, [], [], [], [newFile], callback
copyFileFromExistingProjectWithProject: (project, folder_id, originalProject_id, origonalFileRef, userId, callback = (error, fileRef, folder_id) ->)->
project_id = project._id

View file

@ -582,24 +582,15 @@ describe 'ProjectEntityHandler', ->
@ProjectEntityHandler.addFile project_id, folder_id, fileName, {}, userId, () ->
describe 'replacing a file', ->
describe 'replaceFile', ->
beforeEach ->
@projectLocator
@file_id = "file_id_here"
@fsPath = "fs_path_here.png"
@fileRef = {rev:3, _id:@file_id}
@filePaths = {fileSystem:"/folder1/file.png", mongo:"folder.1.files.somewhere"}
@fileRef = {rev:3, _id: @file_id, name: @fileName = "fileName"}
@filePaths = {fileSystem: @fileSystemPath="/folder1/file.png", mongo:"folder.1.files.somewhere"}
@projectLocator.findElement = sinon.stub().callsArgWith(1, null, @fileRef, @filePaths)
@ProjectEntityHandler.getAllEntitiesFromProject = sinon.stub()
@ProjectEntityHandler.getAllEntitiesFromProject
.onFirstCall()
.callsArgWith(1, null, [], @oldFiles = ['old-file'])
@ProjectEntityHandler.getAllEntitiesFromProject
.onSecondCall()
.callsArgWith(1, null, [], @newFiles = ['new-file'])
@ProjectModel.findOneAndUpdate = sinon.stub().callsArgWith(3, null, @project)
@ProjectModel.findOneAndUpdate = sinon.stub().callsArgWith(3)
@ProjectGetter.getProject = sinon.stub().callsArgWith(2, null, @project)
it 'should find the file', (done)->
@ -644,12 +635,18 @@ describe 'ProjectEntityHandler', ->
@ProjectEntityHandler.replaceFile project_id, @file_id, @fsPath, userId, =>
it "should should send the old and new project structure to the doc updater", (done) ->
@ProjectEntityHandler.replaceFile project_id, @file_id, @fsPath, userId, =>
@documentUpdaterHandler.updateProjectStructure
.calledWith(project_id, userId, [], [], @oldFiles, @newFiles)
.should.equal true
@documentUpdaterHandler.updateProjectStructure = (passed_project_id, passed_user_id, oldDocs, newDocs, oldFiles, newFiles) =>
passed_project_id.should.equal project_id
passed_user_id.should.equal userId
newFiles.length.should.equal 1
newFile = newFiles[0]
newFile.file.name.should.equal @fileName
newFile.path.should.equal @fileSystemPath
newFile.url.should.equal @fileUrl
done()
@ProjectEntityHandler.replaceFile project_id, @file_id, @fsPath, userId, =>
describe 'addFolder', ->
folderName = "folder1234"
beforeEach ->