mirror of
https://github.com/overleaf/overleaf.git
synced 2025-04-05 07:10:55 +00:00
added WithProject versions of functions
rather than project_or_id
This commit is contained in:
parent
57f00d76bd
commit
5be05a35d8
6 changed files with 223 additions and 215 deletions
|
@ -35,8 +35,7 @@ module.exports =
|
|||
jobs = originalFolder.docs.map (doc)->
|
||||
return (callback)->
|
||||
content = docContents[doc._id.toString()]
|
||||
return callback(new Error("doc_id not found: #{doc._id}")) if !content?
|
||||
projectEntityHandler.addDoc newProject, newParentFolder._id, doc.name, content.lines, (err, newDoc)->
|
||||
projectEntityHandler.addDocWithProject newProject, newParentFolder._id, doc.name, content.lines, (err, newDoc)->
|
||||
if err?
|
||||
logger.err err:err, originalProjectId:originalProjectId, newProjectName:newProjectName, "error adding doc"
|
||||
return callback(err)
|
||||
|
@ -48,13 +47,13 @@ module.exports =
|
|||
copyFiles = (originalFolder, newParentFolder, callback)->
|
||||
jobs = originalFolder.fileRefs.map (file)->
|
||||
return (callback)->
|
||||
projectEntityHandler.copyFileFromExistingProject newProject, newParentFolder._id, originalProject._id, file, callback
|
||||
projectEntityHandler.copyFileFromExistingProjectWithProject newProject, newParentFolder._id, originalProject._id, file, callback
|
||||
async.parallelLimit jobs, 5, callback
|
||||
|
||||
copyFolder = (folder, desFolder, callback)->
|
||||
jobs = folder.folders.map (childFolder)->
|
||||
return (callback)->
|
||||
projectEntityHandler.addFolder newProject, desFolder._id, childFolder.name, (err, newFolder)->
|
||||
projectEntityHandler.addFolderWithProject newProject, desFolder._id, childFolder.name, (err, newFolder)->
|
||||
copyFolder childFolder, newFolder, callback
|
||||
jobs.push (cb)->
|
||||
copyDocs folder, desFolder, cb
|
||||
|
|
|
@ -109,29 +109,36 @@ module.exports = ProjectEntityHandler =
|
|||
options = {}
|
||||
DocstoreManager.getDoc project_id, doc_id, options, callback
|
||||
|
||||
addDoc: (project_or_id, folder_id, docName, docLines, callback = (error, doc, folder_id) ->)=>
|
||||
ProjectGetter.getProjectWithoutDocLines project_or_id, (err, project) ->
|
||||
return callback(err) if err?
|
||||
logger.log project_id: project._id, folder_id: folder_id, doc_name: docName, "adding doc to project"
|
||||
confirmFolder project, folder_id, (folder_id)=>
|
||||
doc = new Doc name: docName
|
||||
# Put doc in docstore first, so that if it errors, we don't have a doc_id in the project
|
||||
# which hasn't been created in docstore.
|
||||
DocstoreManager.updateDoc project._id.toString(), doc._id.toString(), docLines, (err, modified, rev) ->
|
||||
return callback(err) if err?
|
||||
|
||||
ProjectEntityHandler._putElement project, folder_id, doc, "doc", (err, result)=>
|
||||
return callback(err) if err?
|
||||
tpdsUpdateSender.addDoc {
|
||||
project_id: project?._id,
|
||||
doc_id: doc?._id
|
||||
path: result?.path?.fileSystem,
|
||||
project_name: project.name,
|
||||
rev: 0
|
||||
}, (err) ->
|
||||
if err?
|
||||
logger.err err:err, "error adding doc to tpdsworker, contining anyway"
|
||||
callback(null, doc, folder_id)
|
||||
addDoc: (project_id, folder_id, docName, docLines, callback = (error, doc, folder_id) ->)=>
|
||||
ProjectGetter.getProjectWithOnlyFolders project_id, (err, project) ->
|
||||
if err?
|
||||
logger.err project_id:project_id, err:err, "error getting project for add doc"
|
||||
return callback(err)
|
||||
ProjectEntityHandler.addDocWithProject project, folder_id, docName, docLines, callback
|
||||
|
||||
addDocWithProject: (project, folder_id, docName, docLines, callback = (error, doc, folder_id) ->)=>
|
||||
project_id = project._id
|
||||
logger.log project_id: project_id, folder_id: folder_id, doc_name: docName, "adding doc to project with project"
|
||||
confirmFolder project, folder_id, (folder_id)=>
|
||||
doc = new Doc name: docName
|
||||
# Put doc in docstore first, so that if it errors, we don't have a doc_id in the project
|
||||
# which hasn't been created in docstore.
|
||||
DocstoreManager.updateDoc project_id.toString(), doc._id.toString(), docLines, (err, modified, rev) ->
|
||||
return callback(err) if err?
|
||||
|
||||
ProjectEntityHandler._putElement project, folder_id, doc, "doc", (err, result)=>
|
||||
return callback(err) if err?
|
||||
tpdsUpdateSender.addDoc {
|
||||
project_id: project_id,
|
||||
doc_id: doc?._id
|
||||
path: result?.path?.fileSystem,
|
||||
project_name: project.name,
|
||||
rev: 0
|
||||
}, (err) ->
|
||||
if err?
|
||||
logger.err err:err, "error adding doc to tpdsworker, contining anyway"
|
||||
callback(null, doc, folder_id)
|
||||
|
||||
restoreDoc: (project_id, doc_id, name, callback = (error, doc, folder_id) ->) ->
|
||||
# getDoc will return the deleted doc's lines, but we don't actually remove
|
||||
|
@ -140,22 +147,29 @@ module.exports = ProjectEntityHandler =
|
|||
return callback(error) if error?
|
||||
ProjectEntityHandler.addDoc project_id, null, name, lines, callback
|
||||
|
||||
addFile: (project_or_id, folder_id, fileName, path, callback = (error, fileRef, folder_id) ->)->
|
||||
ProjectGetter.getProjectWithOnlyFolders project_or_id, (err, project) ->
|
||||
logger.log project_id: project._id, folder_id: folder_id, file_name: fileName, path:path, "adding file"
|
||||
return callback(err) if err?
|
||||
confirmFolder project, folder_id, (folder_id)->
|
||||
fileRef = new File name : fileName
|
||||
FileStoreHandler.uploadFileFromDisk project._id, fileRef._id, path, (err)->
|
||||
if err?
|
||||
logger.err err:err, project_id: project._id, folder_id: folder_id, file_name: fileName, fileRef:fileRef, "error uploading image to s3"
|
||||
return callback(err)
|
||||
ProjectEntityHandler._putElement project._id, folder_id, fileRef, "file", (err, result)=>
|
||||
tpdsUpdateSender.addFile {project_id:project._id, file_id:fileRef._id, path:result.path.fileSystem, project_name:project.name, rev:fileRef.rev}, ->
|
||||
callback(err, fileRef, folder_id)
|
||||
addFile: (project_id, folder_id, fileName, path, callback = (error, fileRef, folder_id) ->)->
|
||||
ProjectGetter.getProjectWithOnlyFolders project_id, (err, project) ->
|
||||
if err?
|
||||
logger.err project_id:project_id, err:err, "error getting project for add file"
|
||||
return callback(err)
|
||||
ProjectEntityHandler.addFileWithProject project, folder_id, fileName, path, callback
|
||||
|
||||
replaceFile: (project_or_id, file_id, fsPath, callback)->
|
||||
Project.getProject project_or_id, "", (err, project) ->
|
||||
addFileWithProject: (project, folder_id, fileName, path, callback = (error, fileRef, folder_id) ->)->
|
||||
project_id = project._id
|
||||
logger.log project_id: project._id, folder_id: folder_id, file_name: fileName, path:path, "adding file"
|
||||
return callback(err) if err?
|
||||
confirmFolder project, folder_id, (folder_id)->
|
||||
fileRef = new File name : fileName
|
||||
FileStoreHandler.uploadFileFromDisk project._id, fileRef._id, path, (err)->
|
||||
if err?
|
||||
logger.err err:err, project_id: project._id, folder_id: folder_id, file_name: fileName, fileRef:fileRef, "error uploading image to s3"
|
||||
return callback(err)
|
||||
ProjectEntityHandler._putElement project, folder_id, fileRef, "file", (err, result)=>
|
||||
tpdsUpdateSender.addFile {project_id:project._id, file_id:fileRef._id, path:result.path.fileSystem, project_name:project.name, rev:fileRef.rev}, ->
|
||||
callback(err, fileRef, folder_id)
|
||||
|
||||
replaceFile: (project_id, file_id, fsPath, callback)->
|
||||
ProjectGetter.getProject project_id, {name:true}, (err, project) ->
|
||||
return callback(err) if err?
|
||||
findOpts =
|
||||
project_id:project._id
|
||||
|
@ -183,25 +197,34 @@ module.exports = ProjectEntityHandler =
|
|||
Project.update conditons, update, {}, (err, second)->
|
||||
callback()
|
||||
|
||||
copyFileFromExistingProject: (project_or_id, folder_id, originalProject_id, origonalFileRef, callback = (error, fileRef, folder_id) ->)->
|
||||
ProjectGetter.getProject project_or_id, {name:true}, (err, project) ->
|
||||
logger.log project_id:project._id, folder_id:folder_id, originalProject_id:originalProject_id, origonalFileRef:origonalFileRef, "copying file in s3"
|
||||
return callback(err) if err?
|
||||
confirmFolder project, folder_id, (folder_id)=>
|
||||
if !origonalFileRef?
|
||||
logger.err project_id:project._id, folder_id:folder_id, originalProject_id:originalProject_id, origonalFileRef:origonalFileRef, "file trying to copy is null"
|
||||
return callback()
|
||||
fileRef = new File name : origonalFileRef.name
|
||||
FileStoreHandler.copyFile originalProject_id, origonalFileRef._id, project._id, fileRef._id, (err)->
|
||||
copyFileFromExistingProject: (project_id, folder_id, originalProject_id, origonalFileRef, callback = (error, fileRef, folder_id) ->)->
|
||||
logger.log project_id:project_id, folder_id:folder_id, originalProject_id:originalProject_id, origonalFileRef:origonalFileRef, "copying file in s3"
|
||||
ProjectGetter.getProject project_id, {name:true}, (err, project) ->
|
||||
if err?
|
||||
logger.err project_id:project_id, err:err, "error getting project for copy file from existing project"
|
||||
return callback(err)
|
||||
ProjectEntityHandler.copyFileFromExistingProjectWithProject project, folder_id, originalProject_id, origonalFileRef, callback
|
||||
|
||||
|
||||
copyFileFromExistingProjectWithProject: (project, folder_id, originalProject_id, origonalFileRef, callback = (error, fileRef, folder_id) ->)->
|
||||
project_id = project._id
|
||||
logger.log project_id:project_id, folder_id:folder_id, originalProject_id:originalProject_id, origonalFileRef:origonalFileRef, "copying file in s3 with project"
|
||||
return callback(err) if err?
|
||||
confirmFolder project, folder_id, (folder_id)=>
|
||||
if !origonalFileRef?
|
||||
logger.err project_id:project._id, folder_id:folder_id, originalProject_id:originalProject_id, origonalFileRef:origonalFileRef, "file trying to copy is null"
|
||||
return callback()
|
||||
fileRef = new File name : origonalFileRef.name
|
||||
FileStoreHandler.copyFile originalProject_id, origonalFileRef._id, project._id, fileRef._id, (err)->
|
||||
if err?
|
||||
logger.err err:err, project_id:project._id, folder_id:folder_id, originalProject_id:originalProject_id, origonalFileRef:origonalFileRef, "error coping file in s3"
|
||||
return callback(err)
|
||||
ProjectEntityHandler._putElement project, folder_id, fileRef, "file", (err, result)=>
|
||||
if err?
|
||||
logger.err err:err, project_id:project._id, folder_id:folder_id, originalProject_id:originalProject_id, origonalFileRef:origonalFileRef, "error coping file in s3"
|
||||
logger.err err:err, project_id:project._id, folder_id:folder_id, "error putting element as part of copy"
|
||||
return callback(err)
|
||||
ProjectEntityHandler._putElement project._id, folder_id, fileRef, "file", (err, result)=>
|
||||
if err?
|
||||
logger.err err:err, project_id:project._id, folder_id:folder_id, "error putting element as part of copy"
|
||||
return callback(err)
|
||||
tpdsUpdateSender.addFile {project_id:project._id, file_id:fileRef._id, path:result?.path?.fileSystem, rev:fileRef.rev, project_name:project.name}, (error) ->
|
||||
callback(error, fileRef, folder_id)
|
||||
tpdsUpdateSender.addFile {project_id:project._id, file_id:fileRef._id, path:result?.path?.fileSystem, rev:fileRef.rev, project_name:project.name}, (error) ->
|
||||
callback(error, fileRef, folder_id)
|
||||
|
||||
mkdirp: (project_id, path, callback = (err, newlyCreatedFolders, lastFolderInPath)->)->
|
||||
self = @
|
||||
|
@ -240,16 +263,23 @@ module.exports = ProjectEntityHandler =
|
|||
folders = _.select folders, (folder)->
|
||||
!folder.filterOut
|
||||
callback(null, folders, lastFolder)
|
||||
|
||||
addFolder: (project_or_id, parentFolder_id, folderName, callback) ->
|
||||
folder = new Folder name: folderName
|
||||
ProjectGetter.getProjectWithOnlyFolders project_or_id, (err, project)=>
|
||||
return callback(err) if err?
|
||||
confirmFolder project, parentFolder_id, (parentFolder_id)=>
|
||||
logger.log project: project._id, parentFolder_id:parentFolder_id, folderName:folderName, "new folder added"
|
||||
ProjectEntityHandler._putElement project._id, parentFolder_id, folder, "folder", (err, result)=>
|
||||
if callback?
|
||||
callback(err, folder, parentFolder_id)
|
||||
|
||||
addFolder: (project_id, parentFolder_id, folderName, callback) ->
|
||||
ProjectGetter.getProjectWithOnlyFolders project_id, (err, project)=>
|
||||
if err?
|
||||
logger.err project_id:project_id, err:err, "error getting project for add folder"
|
||||
return callback(err)
|
||||
ProjectEntityHandler.addFolderWithProject project, parentFolder_id, folderName, callback
|
||||
|
||||
addFolderWithProject: (project, parentFolder_id, folderName, callback = (err, folder, parentFolder_id)->) ->
|
||||
confirmFolder project, parentFolder_id, (parentFolder_id)=>
|
||||
folder = new Folder name: folderName
|
||||
logger.log project: project._id, parentFolder_id:parentFolder_id, folderName:folderName, "adding new folder"
|
||||
ProjectEntityHandler._putElement project, parentFolder_id, folder, "folder", (err, result)=>
|
||||
if err?
|
||||
logger.err err:err, project_id:project._id, "error adding folder to project"
|
||||
return callback(err)
|
||||
callback(err, folder, parentFolder_id)
|
||||
|
||||
updateDocLines : (project_id, doc_id, lines, callback = (error) ->)->
|
||||
ProjectGetter.getProjectWithoutDocLines project_id, (err, project)->
|
||||
|
@ -307,7 +337,7 @@ module.exports = ProjectEntityHandler =
|
|||
return callback(error) if error?
|
||||
self._removeElementFromMongoArray Project, project_id, path.mongo, (err)->
|
||||
return callback(err) if err?
|
||||
ProjectEntityHandler._putElement project_id, destinationFolder_id, entity, entityType, (err, result)->
|
||||
ProjectEntityHandler._putElement project, destinationFolder_id, entity, entityType, (err, result)->
|
||||
return callback(err) if err?
|
||||
opts =
|
||||
project_id:project_id
|
||||
|
@ -431,6 +461,7 @@ module.exports = ProjectEntityHandler =
|
|||
}
|
||||
}, {}, callback
|
||||
|
||||
|
||||
_countElements : (project, callback)->
|
||||
|
||||
countFolder = (folder, cb = (err, count)->)->
|
||||
|
@ -453,7 +484,7 @@ module.exports = ProjectEntityHandler =
|
|||
|
||||
countFolder project.rootFolder[0], callback
|
||||
|
||||
_putElement: (project_or_id, folder_id, element, type, callback = (err, path)->)->
|
||||
_putElement: (project, folder_id, element, type, callback = (err, path)->)->
|
||||
|
||||
sanitizeTypeOfElement = (elementType)->
|
||||
lastChar = elementType.slice -1
|
||||
|
@ -465,37 +496,35 @@ module.exports = ProjectEntityHandler =
|
|||
|
||||
if !element?
|
||||
e = new Error("no element passed to be inserted")
|
||||
logger.err project_id:project_or_id, folder_id:folder_id, element:element, type:type, "failed trying to insert element as it was null"
|
||||
logger.err project_id:project._id, folder_id:folder_id, element:element, type:type, "failed trying to insert element as it was null"
|
||||
return callback(e)
|
||||
type = sanitizeTypeOfElement type
|
||||
ProjectGetter.getProject project_or_id, {rootFolder:true}, (err, project)=>
|
||||
if err?
|
||||
return callback(err)
|
||||
if !folder_id?
|
||||
folder_id = project.rootFolder[0]._id
|
||||
ProjectEntityHandler._countElements project, (err, count)->
|
||||
if count > settings.maxFilesPerProject
|
||||
logger.warn project_id:project._id, "project too big, stopping insertions"
|
||||
return callback("project_has_to_many_files")
|
||||
projectLocator.findElement {project:project, element_id:folder_id, type:"folders"}, (err, folder, path)=>
|
||||
|
||||
if !folder_id?
|
||||
folder_id = project.rootFolder[0]._id
|
||||
ProjectEntityHandler._countElements project, (err, count)->
|
||||
if count > settings.maxFilesPerProject
|
||||
logger.warn project_id:project._id, "project too big, stopping insertions"
|
||||
return callback("project_has_to_many_files")
|
||||
projectLocator.findElement {project:project, element_id:folder_id, type:"folders"}, (err, folder, path)=>
|
||||
if err?
|
||||
logger.err err:err, project_id:project._id, folder_id:folder_id, type:type, element:element, "error finding folder for _putElement"
|
||||
return callback(err)
|
||||
newPath =
|
||||
fileSystem: "#{path.fileSystem}/#{element.name}"
|
||||
mongo: path.mongo
|
||||
logger.log project_id: project._id, element_id: element._id, fileType: type, folder_id: folder_id, "adding element to project"
|
||||
id = element._id+''
|
||||
element._id = require('mongoose').Types.ObjectId(id)
|
||||
conditions = _id:project._id
|
||||
mongopath = "#{path.mongo}.#{type}"
|
||||
update = "$push":{}
|
||||
update["$push"][mongopath] = element
|
||||
Project.update conditions, update, {}, (err)->
|
||||
if err?
|
||||
logger.err err:err, project_id:project._id, folder_id:folder_id, type:type, element:element, "error finding folder for _putElement"
|
||||
logger.err err: err, project_id: project._id, 'error saving in putElement project'
|
||||
return callback(err)
|
||||
newPath =
|
||||
fileSystem: "#{path.fileSystem}/#{element.name}"
|
||||
mongo: path.mongo
|
||||
logger.log project_id: project._id, element_id: element._id, fileType: type, folder_id: folder_id, "adding element to project"
|
||||
id = element._id+''
|
||||
element._id = require('mongoose').Types.ObjectId(id)
|
||||
conditions = _id:project._id
|
||||
mongopath = "#{path.mongo}.#{type}"
|
||||
update = "$push":{}
|
||||
update["$push"][mongopath] = element
|
||||
Project.update conditions, update, {}, (err)->
|
||||
if err?
|
||||
logger.err err: err, project_id: project._id, 'error saving in putElement project'
|
||||
return callback(err)
|
||||
callback(err, {path:newPath})
|
||||
callback(err, {path:newPath})
|
||||
|
||||
|
||||
confirmFolder = (project, folder_id, callback)->
|
||||
|
|
|
@ -11,62 +11,45 @@ module.exports = ProjectGetter =
|
|||
EXCLUDE_DEPTH: 8
|
||||
|
||||
|
||||
_returnProjectIfPassed: (project_or_id, callback, continueCallback)->
|
||||
if project_or_id._id?
|
||||
callback null, project_or_id
|
||||
else
|
||||
try
|
||||
ObjectId(project_or_id.toString())
|
||||
catch e
|
||||
return continueCallback(new Errors.NotFoundError(e.message))
|
||||
continueCallback()
|
||||
getProjectWithoutDocLines: (project_id, callback=(error, project) ->) ->
|
||||
excludes = {}
|
||||
for i in [1..ProjectGetter.EXCLUDE_DEPTH]
|
||||
excludes["rootFolder#{Array(i).join(".folder")}.docs.lines"] = 0
|
||||
db.projects.find _id: ObjectId(project_id.toString()), excludes, (error, projects = []) ->
|
||||
callback error, projects[0]
|
||||
|
||||
getProjectWithoutDocLines: (project_or_id, callback=(error, project) ->) ->
|
||||
ProjectGetter._returnProjectIfPassed project_or_id, callback, (err)->
|
||||
return callback(err) if err?
|
||||
project_id = project_or_id
|
||||
excludes = {}
|
||||
for i in [1..ProjectGetter.EXCLUDE_DEPTH]
|
||||
excludes["rootFolder#{Array(i).join(".folder")}.docs.lines"] = 0
|
||||
db.projects.find _id: ObjectId(project_id.toString()), excludes, (error, projects = []) ->
|
||||
callback error, projects[0]
|
||||
getProjectWithOnlyFolders: (project_id, callback=(error, project) ->) ->
|
||||
excludes = {}
|
||||
for i in [1..ProjectGetter.EXCLUDE_DEPTH]
|
||||
excludes["rootFolder#{Array(i).join(".folder")}.docs"] = 0
|
||||
excludes["rootFolder#{Array(i).join(".folder")}.fileRefs"] = 0
|
||||
db.projects.find _id: ObjectId(project_id.toString()), excludes, (error, projects = []) ->
|
||||
callback error, projects[0]
|
||||
|
||||
getProjectWithOnlyFolders: (project_or_id, callback=(error, project) ->) ->
|
||||
ProjectGetter._returnProjectIfPassed project_or_id, callback, (err)->
|
||||
return callback(err) if err?
|
||||
project_id = project_or_id
|
||||
excludes = {}
|
||||
for i in [1..ProjectGetter.EXCLUDE_DEPTH]
|
||||
excludes["rootFolder#{Array(i).join(".folder")}.docs"] = 0
|
||||
excludes["rootFolder#{Array(i).join(".folder")}.fileRefs"] = 0
|
||||
db.projects.find _id: ObjectId(project_id.toString()), excludes, (error, projects = []) ->
|
||||
callback error, projects[0]
|
||||
|
||||
getProject: (project_or_id, projection, callback = (error, project) ->) ->
|
||||
if !project_or_id?
|
||||
return callback("no id or project provided")
|
||||
getProject: (query, projection, callback = (error, project) ->) ->
|
||||
if !query?
|
||||
return callback("no query provided")
|
||||
|
||||
if typeof(projection) == "function"
|
||||
callback = projection
|
||||
|
||||
ProjectGetter._returnProjectIfPassed project_or_id, callback, (err)->
|
||||
if typeof query == "string"
|
||||
query = _id: ObjectId(query)
|
||||
else if query instanceof ObjectId
|
||||
query = _id: query
|
||||
else if query?.toString().length == 24 # sometimes mongoose ids are hard to identify, this will catch them
|
||||
query = _id: ObjectId(query.toString())
|
||||
else
|
||||
err = new Error("malformed get request")
|
||||
logger.log query:query, err:err, type:typeof(query), "malformed get request"
|
||||
return callback(err)
|
||||
|
||||
if typeof project_or_id == "string"
|
||||
query = _id: ObjectId(project_or_id)
|
||||
else if project_or_id instanceof ObjectId
|
||||
query = _id: project_or_id
|
||||
else if project_or_id?.toString().length == 24 # sometimes mongoose ids are hard to identify, this will catch them
|
||||
query = _id: ObjectId(project_or_id.toString())
|
||||
else
|
||||
err = new Error("malformed get request")
|
||||
logger.log project_or_id:project_or_id, err:err, type:typeof(project_or_id), "malformed get request"
|
||||
db.projects.find query, projection, (err, project)->
|
||||
if err?
|
||||
logger.err err:err, query:query, projection:projection, "error getting project"
|
||||
return callback(err)
|
||||
|
||||
db.projects.find query, projection, (err, project)->
|
||||
if err?
|
||||
logger.err err:err, query:query, projection:projection, "error getting project"
|
||||
return callback(err)
|
||||
callback(null, project?[0])
|
||||
callback(null, project?[0])
|
||||
|
||||
populateProjectWithUsers: (project, callback=(error, project) ->) ->
|
||||
# eventually this should be in a UserGetter.getUser module
|
||||
|
|
|
@ -64,10 +64,10 @@ describe 'ProjectDuplicator', ->
|
|||
@projectOptionsHandler =
|
||||
setCompiler : sinon.stub()
|
||||
@entityHandler =
|
||||
addDoc: sinon.stub().callsArgWith(4, null, {name:"somDoc"})
|
||||
copyFileFromExistingProject: sinon.stub().callsArgWith(4)
|
||||
addDocWithProject: sinon.stub().callsArgWith(4, null, {name:"somDoc"})
|
||||
copyFileFromExistingProjectWithProject: sinon.stub().callsArgWith(4)
|
||||
setRootDoc: sinon.stub()
|
||||
addFolder: sinon.stub().callsArgWith(3, null, @newFolder)
|
||||
addFolderWithProject: sinon.stub().callsArgWith(3, null, @newFolder)
|
||||
|
||||
@DocumentUpdaterHandler =
|
||||
flushProjectToMongo: sinon.stub().callsArg(1)
|
||||
|
@ -109,13 +109,13 @@ describe 'ProjectDuplicator', ->
|
|||
done()
|
||||
|
||||
it 'should use the same compiler', (done)->
|
||||
@entityHandler.addDoc.callsArgWith(4, null, @rootFolder.docs[0])
|
||||
@entityHandler.addDocWithProject.callsArgWith(4, null, @rootFolder.docs[0])
|
||||
@duplicator.duplicate @owner, @project_id, "", (err, newProject)=>
|
||||
@projectOptionsHandler.setCompiler.calledWith(@stubbedNewProject._id, @project.compiler).should.equal true
|
||||
done()
|
||||
|
||||
it 'should use the same root doc', (done)->
|
||||
@entityHandler.addDoc.callsArgWith(4, null, @rootFolder.docs[0])
|
||||
@entityHandler.addDocWithProject.callsArgWith(4, null, @rootFolder.docs[0])
|
||||
@duplicator.duplicate @owner, @project_id, "", (err, newProject)=>
|
||||
@entityHandler.setRootDoc.calledWith(@stubbedNewProject, @rootFolder.docs[0]._id).should.equal true
|
||||
done()
|
||||
|
@ -128,22 +128,22 @@ describe 'ProjectDuplicator', ->
|
|||
|
||||
it 'should copy all the folders', (done)->
|
||||
@duplicator.duplicate @owner, @project_id, "", (err, newProject)=>
|
||||
@entityHandler.addFolder.calledWith(@stubbedNewProject, @stubbedNewProject.rootFolder[0]._id, @level1folder.name).should.equal true
|
||||
@entityHandler.addFolder.calledWith(@stubbedNewProject, @newFolder._id, @level2folder.name).should.equal true
|
||||
@entityHandler.addFolder.callCount.should.equal 2
|
||||
@entityHandler.addFolderWithProject.calledWith(@stubbedNewProject, @stubbedNewProject.rootFolder[0]._id, @level1folder.name).should.equal true
|
||||
@entityHandler.addFolderWithProject.calledWith(@stubbedNewProject, @newFolder._id, @level2folder.name).should.equal true
|
||||
@entityHandler.addFolderWithProject.callCount.should.equal 2
|
||||
done()
|
||||
|
||||
it 'should copy all the docs', (done)->
|
||||
@duplicator.duplicate @owner, @project_id, "", (err, newProject)=>
|
||||
@DocstoreManager.getAllDocs.calledWith(@project_id).should.equal true
|
||||
@entityHandler.addDoc.calledWith(@stubbedNewProject, @stubbedNewProject.rootFolder[0]._id, @doc0.name, @doc0_lines).should.equal true
|
||||
@entityHandler.addDoc.calledWith(@stubbedNewProject, @newFolder._id, @doc1.name, @doc1_lines).should.equal true
|
||||
@entityHandler.addDoc.calledWith(@stubbedNewProject, @newFolder._id, @doc2.name, @doc2_lines).should.equal true
|
||||
@entityHandler.addDocWithProject.calledWith(@stubbedNewProject, @stubbedNewProject.rootFolder[0]._id, @doc0.name, @doc0_lines).should.equal true
|
||||
@entityHandler.addDocWithProject.calledWith(@stubbedNewProject, @newFolder._id, @doc1.name, @doc1_lines).should.equal true
|
||||
@entityHandler.addDocWithProject.calledWith(@stubbedNewProject, @newFolder._id, @doc2.name, @doc2_lines).should.equal true
|
||||
done()
|
||||
|
||||
it 'should copy all the files', (done)->
|
||||
@duplicator.duplicate @owner, @project_id, "", (err, newProject)=>
|
||||
@entityHandler.copyFileFromExistingProject.calledWith(@stubbedNewProject, @stubbedNewProject.rootFolder[0]._id, @project._id, @rootFolder.fileRefs[0]).should.equal true
|
||||
@entityHandler.copyFileFromExistingProject.calledWith(@stubbedNewProject, @newFolder._id, @project._id, @level1folder.fileRefs[0]).should.equal true
|
||||
@entityHandler.copyFileFromExistingProject.calledWith(@stubbedNewProject, @newFolder._id, @project._id, @level2folder.fileRefs[0]).should.equal true
|
||||
@entityHandler.copyFileFromExistingProjectWithProject.calledWith(@stubbedNewProject, @stubbedNewProject.rootFolder[0]._id, @project._id, @rootFolder.fileRefs[0]).should.equal true
|
||||
@entityHandler.copyFileFromExistingProjectWithProject.calledWith(@stubbedNewProject, @newFolder._id, @project._id, @level1folder.fileRefs[0]).should.equal true
|
||||
@entityHandler.copyFileFromExistingProjectWithProject.calledWith(@stubbedNewProject, @newFolder._id, @project._id, @level2folder.fileRefs[0]).should.equal true
|
||||
done()
|
||||
|
|
|
@ -248,32 +248,14 @@ describe 'ProjectEntityHandler', ->
|
|||
@ProjectEntityHandler.moveEntity project_id, @docId, folder_id, "docs", done
|
||||
|
||||
it 'should find the project then element', ->
|
||||
@projectLocator.findElement
|
||||
.calledWith({
|
||||
element_id: @docId,
|
||||
type: "docs",
|
||||
project: @project
|
||||
})
|
||||
.should.equal true
|
||||
@projectLocator.findElement.calledWith({element_id: @docId, type: "docs", project: @project }).should.equal true
|
||||
|
||||
it 'should remove the element from its current position', ->
|
||||
@ProjectEntityHandler._removeElementFromMongoArray
|
||||
.calledWith(
|
||||
@ProjectModel,
|
||||
project_id,
|
||||
@path.mongo
|
||||
)
|
||||
.should.equal true
|
||||
.calledWith(@ProjectModel, project_id, @path.mongo ).should.equal true
|
||||
|
||||
it "should put the element back in the new folder", ->
|
||||
@ProjectEntityHandler._putElement
|
||||
.calledWith(
|
||||
project_id,
|
||||
folder_id,
|
||||
@doc,
|
||||
"docs"
|
||||
)
|
||||
.should.equal true
|
||||
@ProjectEntityHandler._putElement.calledWith(@project, folder_id, @doc, "docs").should.equal true
|
||||
|
||||
it 'should tell the third party data store', ->
|
||||
@tpdsUpdateSender.moveEntity
|
||||
|
@ -336,7 +318,7 @@ describe 'ProjectEntityHandler', ->
|
|||
it "should put the element back in the new folder", ->
|
||||
@ProjectEntityHandler._putElement
|
||||
.calledWith(
|
||||
project_id,
|
||||
@project,
|
||||
@move_to_folder_id,
|
||||
@folder,
|
||||
"folder"
|
||||
|
@ -487,8 +469,8 @@ describe 'ProjectEntityHandler', ->
|
|||
@ProjectEntityHandler.addFile project_id, folder_id, fileName, @filePath, (err, fileRef, parentFolder)->
|
||||
|
||||
it 'should put file into folder by calling put element', (done)->
|
||||
@ProjectEntityHandler._putElement = (passedProject_id, passedFolder_id, passedFileRef, passedType, callback)->
|
||||
passedProject_id.should.equal project_id
|
||||
@ProjectEntityHandler._putElement = (passedProject, passedFolder_id, passedFileRef, passedType, callback)->
|
||||
passedProject._id.should.equal project_id
|
||||
passedFolder_id.should.equal folder_id
|
||||
passedFileRef.name.should.equal fileName
|
||||
passedType.should.equal 'file'
|
||||
|
@ -530,6 +512,7 @@ describe 'ProjectEntityHandler', ->
|
|||
@filePaths = {fileSystem:"/folder1/file.png", mongo:"folder.1.files.somewhere"}
|
||||
@projectLocator.findElement = sinon.stub().callsArgWith(1, null, @fileRef, @filePaths)
|
||||
@ProjectModel.update = (_, __, ___, cb)-> cb()
|
||||
@ProjectGetter.getProject = sinon.stub().callsArgWith(2, null, @project)
|
||||
|
||||
it 'should find the file', (done)->
|
||||
|
||||
|
@ -574,12 +557,14 @@ describe 'ProjectEntityHandler', ->
|
|||
@ProjectEntityHandler.replaceFile project_id, @file_id, @fsPath, =>
|
||||
|
||||
|
||||
describe 'adding a folder', ->
|
||||
describe 'addFolder', ->
|
||||
folderName = "folder1234"
|
||||
beforeEach ->
|
||||
@ProjectGetter.getProjectWithOnlyFolders = sinon.stub().callsArgWith(1, null, @project)
|
||||
|
||||
it 'should call put element', (done)->
|
||||
@ProjectEntityHandler._putElement = (passedProject_id, passedFolder_id, passedFolder, passedType, callback)->
|
||||
passedProject_id.should.equal project_id
|
||||
@ProjectEntityHandler._putElement = (passedProject, passedFolder_id, passedFolder, passedType, callback)->
|
||||
passedProject._id.should.equal project_id
|
||||
passedFolder_id.should.equal folder_id
|
||||
passedFolder.name.should.equal folderName
|
||||
passedType.should.equal 'folder'
|
||||
|
@ -884,8 +869,8 @@ describe 'ProjectEntityHandler', ->
|
|||
done()
|
||||
|
||||
it 'should put file into folder by calling put element', (done)->
|
||||
@ProjectEntityHandler._putElement = (passedProject_id, passedFolder_id, passedFileRef, passedType, callback)->
|
||||
passedProject_id.should.equal project_id
|
||||
@ProjectEntityHandler._putElement = (passedProject, passedFolder_id, passedFileRef, passedType, callback)->
|
||||
passedProject._id.should.equal project_id
|
||||
passedFolder_id.should.equal folder_id
|
||||
passedFileRef.name.should.equal fileName
|
||||
passedType.should.equal 'file'
|
||||
|
@ -1039,24 +1024,24 @@ describe 'ProjectEntityHandler', ->
|
|||
|
||||
|
||||
it "should use the correct mongo path", (done)->
|
||||
@ProjectEntityHandler._putElement @project_id, @folder._id, @doc, "docs", (err)=>
|
||||
@ProjectEntityHandler._putElement @project, @folder._id, @doc, "docs", (err)=>
|
||||
@ProjectModel.update.args[0][0]._id.should.equal @project._id
|
||||
assert.deepEqual @ProjectModel.update.args[0][1].$push[@path.mongo+".docs"], @doc
|
||||
done()
|
||||
|
||||
it "should add an s onto the type if not included", (done)->
|
||||
@ProjectEntityHandler._putElement @project_id, @folder._id, @doc, "doc", (err)=>
|
||||
@ProjectEntityHandler._putElement @project, @folder._id, @doc, "doc", (err)=>
|
||||
assert.deepEqual @ProjectModel.update.args[0][1].$push[@path.mongo+".docs"], @doc
|
||||
done()
|
||||
|
||||
|
||||
it "should not call update if elemenet is null", (done)->
|
||||
@ProjectEntityHandler._putElement @project_id, @folder._id, null, "doc", (err)=>
|
||||
@ProjectEntityHandler._putElement @project, @folder._id, null, "doc", (err)=>
|
||||
@ProjectModel.update.called.should.equal false
|
||||
done()
|
||||
|
||||
it "should default to root folder insert", (done)->
|
||||
@ProjectEntityHandler._putElement @project_id, null, @doc, "doc", (err)=>
|
||||
@ProjectEntityHandler._putElement @project, null, @doc, "doc", (err)=>
|
||||
@projectLocator.findElement.args[0][0].element_id.should.equal @project.rootFolder[0]._id
|
||||
done()
|
||||
|
||||
|
|
|
@ -50,18 +50,8 @@ describe "ProjectGetter", ->
|
|||
@callback.calledWith(null, @project).should.equal true
|
||||
|
||||
|
||||
describe "passing a project", ->
|
||||
beforeEach ->
|
||||
@ProjectGetter.getProjectWithoutDocLines @project, @callback
|
||||
|
||||
it "should not call the db", ->
|
||||
@db.projects.find.called.should.equal false
|
||||
|
||||
it "should call the callback with the project", ->
|
||||
@callback.calledWith(null, @project).should.equal true
|
||||
|
||||
|
||||
describe "getProjectWithOnlyFolders", ->
|
||||
|
||||
beforeEach ()->
|
||||
@project =
|
||||
_id: @project_id = "56d46b0a1d3422b87c5ebcb1"
|
||||
|
@ -96,20 +86,48 @@ describe "ProjectGetter", ->
|
|||
|
||||
it "should call the callback with the project", ->
|
||||
@callback.calledWith(null, @project).should.equal true
|
||||
|
||||
describe "passing a project", ->
|
||||
beforeEach ->
|
||||
@ProjectGetter.getProjectWithoutDocLines @project, @callback
|
||||
|
||||
it "should not call the db", ->
|
||||
@db.projects.find.called.should.equal false
|
||||
|
||||
|
||||
describe "getProject", ->
|
||||
beforeEach ()->
|
||||
@project =
|
||||
_id: @project_id = "56d46b0a1d3422b87c5ebcb1"
|
||||
@db.projects.find = sinon.stub().callsArgWith(2, null, [@project])
|
||||
|
||||
describe "passing an id", ->
|
||||
beforeEach ->
|
||||
@ProjectGetter.getProjectWithOnlyFolders @project_id, @callback
|
||||
|
||||
it "should call find with the project id", ->
|
||||
@db.projects.find.calledWith(_id: ObjectId(@project_id)).should.equal true
|
||||
|
||||
it "should exclude the docs and files linesaaaa", ->
|
||||
excludes =
|
||||
"rootFolder.docs": 0
|
||||
"rootFolder.fileRefs": 0
|
||||
"rootFolder.folder.docs": 0
|
||||
"rootFolder.folder.fileRefs": 0
|
||||
"rootFolder.folder.folder.docs": 0
|
||||
"rootFolder.folder.folder.fileRefs": 0
|
||||
"rootFolder.folder.folder.folder.docs": 0
|
||||
"rootFolder.folder.folder.folder.fileRefs": 0
|
||||
"rootFolder.folder.folder.folder.folder.docs": 0
|
||||
"rootFolder.folder.folder.folder.folder.fileRefs": 0
|
||||
"rootFolder.folder.folder.folder.folder.folder.docs": 0
|
||||
"rootFolder.folder.folder.folder.folder.folder.fileRefs": 0
|
||||
"rootFolder.folder.folder.folder.folder.folder.folder.docs": 0
|
||||
"rootFolder.folder.folder.folder.folder.folder.folder.fileRefs": 0
|
||||
"rootFolder.folder.folder.folder.folder.folder.folder.folder.docs": 0
|
||||
"rootFolder.folder.folder.folder.folder.folder.folder.folder.fileRefs": 0
|
||||
@db.projects.find.calledWith(sinon.match.any, excludes).should.equal true
|
||||
|
||||
it "should call the callback with the project", ->
|
||||
@callback.calledWith(null, @project).should.equal true
|
||||
|
||||
|
||||
|
||||
describe "getProjectaaaaa", ->
|
||||
describe "getProject", ->
|
||||
beforeEach ()->
|
||||
@project =
|
||||
_id: @project_id = "56d46b0a1d3422b87c5ebcb1"
|
||||
|
@ -128,12 +146,6 @@ describe "ProjectGetter", ->
|
|||
assert.deepEqual @project, project
|
||||
done()
|
||||
|
||||
it "should not call db when project is passed", (done)->
|
||||
@ProjectGetter.getProject @project, (err, project)=>
|
||||
@db.projects.find.called.should.equal false
|
||||
assert.deepEqual @project, project
|
||||
done()
|
||||
|
||||
it "should call the db when a mongoose objectid is used", (done)->
|
||||
mongooseID = require('mongoose').Types.ObjectId(@project_id)
|
||||
@ProjectGetter.getProject mongooseID, (err, project)=>
|
||||
|
|
Loading…
Reference in a new issue