diff --git a/services/web/app/coffee/Features/Project/ProjectDuplicator.coffee b/services/web/app/coffee/Features/Project/ProjectDuplicator.coffee index 69f9eb309a..555a786007 100644 --- a/services/web/app/coffee/Features/Project/ProjectDuplicator.coffee +++ b/services/web/app/coffee/Features/Project/ProjectDuplicator.coffee @@ -3,7 +3,6 @@ projectEntityHandler = require('./ProjectEntityHandler') projectLocator = require('./ProjectLocator') projectOptionsHandler = require('./ProjectOptionsHandler') DocumentUpdaterHandler = require("../DocumentUpdater/DocumentUpdaterHandler") -DocstoreManager = require "../Docstore/DocstoreManager" Project = require("../../models/Project").Project _ = require('underscore') async = require('async') @@ -17,48 +16,38 @@ module.exports = projectCreationHandler.createBlankProject owner._id, newProjectName, (err, newProject)-> return callback(err) if err? projectLocator.findRootDoc {project:originalProject}, (err, originalRootDoc)-> - return callback(err) if err? - DocstoreManager.getAllDocs originalProjectId, (err, docContentsArray) -> - return callback(err) if err? + projectOptionsHandler.setCompiler newProject._id, originalProject.compiler - docContents = {} - for docContent in docContentsArray - docContents[docContent._id] = docContent + setRootDoc = _.once (doc_id)-> + projectEntityHandler.setRootDoc newProject, doc_id - projectOptionsHandler.setCompiler newProject._id, originalProject.compiler + copyDocs = (originalFolder, newParentFolder, callback)-> + jobs = originalFolder.docs.map (doc)-> + return (callback)-> + projectEntityHandler.addDoc newProject, newParentFolder._id, doc.name, doc.lines, (err, newDoc)-> + if originalRootDoc? and newDoc.name == originalRootDoc.name + setRootDoc newDoc._id + callback() + async.series jobs, callback - setRootDoc = _.once (doc_id)-> - projectEntityHandler.setRootDoc newProject, doc_id + copyFiles = (originalFolder, newParentFolder, callback)-> + jobs = originalFolder.fileRefs.map (file)-> + return (callback)-> + projectEntityHandler.copyFileFromExistingProject newProject, newParentFolder._id, originalProject._id, file, callback + async.parallelLimit jobs, 5, callback - copyDocs = (originalFolder, newParentFolder, callback)-> - 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)-> - if originalRootDoc? and newDoc.name == originalRootDoc.name - setRootDoc newDoc._id - callback() - async.series jobs, callback + copyFolder = (folder, desFolder, callback)-> + jobs = folder.folders.map (childFolder)-> + return (callback)-> + projectEntityHandler.addFolder newProject, desFolder._id, childFolder.name, (err, newFolder)-> + copyFolder childFolder, newFolder, callback + jobs.push (cb)-> + copyDocs folder, desFolder, cb + jobs.push (cb)-> + copyFiles folder, desFolder, cb - copyFiles = (originalFolder, newParentFolder, callback)-> - jobs = originalFolder.fileRefs.map (file)-> - return (callback)-> - projectEntityHandler.copyFileFromExistingProject newProject, newParentFolder._id, originalProject._id, file, callback - async.parallelLimit jobs, 5, callback + async.series jobs, callback - copyFolder = (folder, desFolder, callback)-> - jobs = folder.folders.map (childFolder)-> - return (callback)-> - projectEntityHandler.addFolder newProject, desFolder._id, childFolder.name, (err, newFolder)-> - copyFolder childFolder, newFolder, callback - jobs.push (cb)-> - copyDocs folder, desFolder, cb - jobs.push (cb)-> - copyFiles folder, desFolder, cb - - async.series jobs, callback - - copyFolder originalProject.rootFolder[0], newProject.rootFolder[0], -> - callback(err, newProject) + copyFolder originalProject.rootFolder[0], newProject.rootFolder[0], -> + callback(err, newProject) diff --git a/services/web/test/UnitTests/coffee/Project/ProjectDuplicatorTests.coffee b/services/web/test/UnitTests/coffee/Project/ProjectDuplicatorTests.coffee index c8577cb4dd..8df3f168dc 100644 --- a/services/web/test/UnitTests/coffee/Project/ProjectDuplicatorTests.coffee +++ b/services/web/test/UnitTests/coffee/Project/ProjectDuplicatorTests.coffee @@ -3,25 +3,25 @@ chai = require('chai').should() modulePath = "../../../../app/js/Features/Project/ProjectDuplicator.js" SandboxedModule = require('sandboxed-module') -describe 'ProjectDuplicator', -> +describe 'duplicating a project', -> beforeEach -> @level2folder = name: "level2folderName" _id:"level2folderId" - docs:[@doc2 = {_id: "doc2_id", name:"level2folderDocName"}] + docs:[{name:"level2folderDocName",docLines:"level2Doc"}] folders:[] fileRefs:[{name:"file2", _id:"file2"}] @level1folder = name:"level1folder" _id:"level1folderId" - docs:[@doc1 = {_id: "doc1_id", name:"level1folderDocName"}] + docs:[{name:"level1folderDocName",docLines:"level1Doc"}] folders:[@level2folder] fileRefs:[{name:"file1", _id:"file1"}] @rootFolder = name:"rootFolder" _id:"rootFolderId" - docs:[@doc0 = {_id: "doc0_id", name:"rootDocHere"}] + docs:[{name:"rootDocHere", docLines:["level0Doc"]}] folders:[@level1folder] fileRefs:[{name:"file0", _id:"file0"}] @project = @@ -30,19 +30,6 @@ describe 'ProjectDuplicator', -> rootFolder:[@rootFolder] compiler: "this_is_a_Compiler" - @docContents = [{ - _id: @doc0._id - lines: @doc0_lines = ["zero"] - }, { - _id: @doc1._id - lines: @doc1_lines = ["one"] - }, { - _id: @doc2._id - lines: @doc2_lines = ["two"] - }] - @DocstoreManager = - getAllDocs: sinon.stub().callsArgWith(1, null, @docContents) - @owner = {_id:"this_is_the_owner"} @stubbedNewProject = _id:"new_project_id" @@ -82,7 +69,6 @@ describe 'ProjectDuplicator', -> './ProjectEntityHandler': @entityHandler './ProjectLocator': @locator './ProjectOptionsHandler': @projectOptionsHandler - "../Docstore/DocstoreManager": @DocstoreManager 'logger-sharelatex':{log:->} it "should look up the original project", (done) -> @@ -115,7 +101,7 @@ describe 'ProjectDuplicator', -> @duplicator.duplicate @owner, @project_id, "", (err, newProject)=> @entityHandler.setRootDoc.calledWith(@stubbedNewProject, @rootFolder.docs[0]._id).should.equal true done() - + it 'should not copy the collaberators or read only refs', (done)-> @duplicator.duplicate @owner, @project_id, "", (err, newProject)=> newProject.collaberator_refs.length.should.equal 0 @@ -131,10 +117,9 @@ describe 'ProjectDuplicator', -> 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.addDoc.calledWith(@stubbedNewProject, @stubbedNewProject.rootFolder[0]._id, @rootFolder.docs[0].name, @rootFolder.docs[0].lines).should.equal true + @entityHandler.addDoc.calledWith(@stubbedNewProject, @newFolder._id, @level1folder.docs[0].name, @level1folder.docs[0].lines).should.equal true + @entityHandler.addDoc.calledWith(@stubbedNewProject, @newFolder._id, @level2folder.docs[0].name, @level2folder.docs[0].lines).should.equal true done() it 'should copy all the files', (done)->