diff --git a/services/web/app/src/Features/Project/ProjectRootDocManager.js b/services/web/app/src/Features/Project/ProjectRootDocManager.js index 89f79f2e5a..142cf6f0b3 100644 --- a/services/web/app/src/Features/Project/ProjectRootDocManager.js +++ b/services/web/app/src/Features/Project/ProjectRootDocManager.js @@ -204,50 +204,52 @@ module.exports = ProjectRootDocManager = { if (callback == null) { callback = function(error) {} } - return ProjectGetter.getProject(project_id, { rootDoc_id: 1 }, function( - error, - project - ) { - if (error != null) { - return callback(error) - } - if (project == null) { - return callback(new Error('project not found')) - } - - if (project.rootDoc_id != null) { - return ProjectEntityHandler.getAllDocPathsFromProjectById( - project_id, - function(error, docPaths) { - if (error != null) { - return callback(error) - } - let rootDocValid = false - for (let doc_id in docPaths) { - const _path = docPaths[doc_id] - if (doc_id === project.rootDoc_id) { - rootDocValid = true - } - } - if (rootDocValid) { - return callback() - } else { - return ProjectEntityUpdateHandler.unsetRootDoc(project_id, () => - ProjectRootDocManager.setRootDocAutomatically( - project_id, - callback - ) - ) - } - } - ) - } else { - return ProjectRootDocManager.setRootDocAutomatically( - project_id, + return ProjectGetter.getProject( + project_id, + { rootDoc_id: 1, rootFolder: 1 }, + function(error, project) { + if (error != null) { + return callback(error) + } + if (project == null) { + return callback(new Error('project not found')) + } + ProjectRootDocManager.ensureRootDocumentIsValidForProject( + project, callback ) } - }) + ) + }, + + ensureRootDocumentIsValidForProject(project, callback) { + const project_id = project._id + if (project.rootDoc_id != null) { + return ProjectEntityHandler.getAllDocPathsFromProject(project, function( + error, + docPaths + ) { + if (error != null) { + return callback(error) + } + let rootDocValid = false + for (let doc_id in docPaths) { + const _path = docPaths[doc_id] + if (doc_id === project.rootDoc_id) { + rootDocValid = true + } + } + if (rootDocValid) { + return callback() + } else { + return ProjectEntityUpdateHandler.unsetRootDoc(project_id, () => + ProjectRootDocManager.setRootDocAutomatically(project_id, callback) + ) + } + }) + } else { + return ProjectRootDocManager.setRootDocAutomatically(project_id, callback) + } }, _sortFileList(listToSort, rootDirectory, callback) { diff --git a/services/web/test/unit/src/Project/ProjectRootDocManagerTests.js b/services/web/test/unit/src/Project/ProjectRootDocManagerTests.js index 9f6f28b310..f2197d0eb4 100644 --- a/services/web/test/unit/src/Project/ProjectRootDocManagerTests.js +++ b/services/web/test/unit/src/Project/ProjectRootDocManagerTests.js @@ -575,13 +575,13 @@ describe('ProjectRootDocManager', function() { describe('ensureRootDocumentIsValid', function() { beforeEach(function() { - this.project = {} + this.project = { _id: this.project_id } this.ProjectGetter.getProject = sinon .stub() .callsArgWith(2, null, this.project) this.ProjectEntityUpdateHandler.setRootDoc = sinon.stub().yields() this.ProjectEntityUpdateHandler.unsetRootDoc = sinon.stub().yields() - this.ProjectEntityHandler.getAllDocPathsFromProjectById = sinon + this.ProjectEntityHandler.getAllDocPathsFromProject = sinon .stub() .callsArgWith(1, null, this.docPaths) return (this.ProjectRootDocManager.setRootDocAutomatically = sinon @@ -599,9 +599,9 @@ describe('ProjectRootDocManager', function() { ) }) - it('should find the project fetching only the rootDoc_id field', function() { + it('should find the project fetching only the rootDoc_id and rootFolder fields', function() { return this.ProjectGetter.getProject - .calledWith(this.project_id, { rootDoc_id: 1 }) + .calledWith(this.project_id, { rootDoc_id: 1, rootFolder: 1 }) .should.equal(true) }) @@ -625,9 +625,9 @@ describe('ProjectRootDocManager', function() { ) }) - it('should find the project fetching only the rootDoc_id field', function() { + it('should find the project fetching only the rootDoc_id and rootFolder fields', function() { return this.ProjectGetter.getProject - .calledWith(this.project_id, { rootDoc_id: 1 }) + .calledWith(this.project_id, { rootDoc_id: 1, rootFolder: 1 }) .should.equal(true) })