From a6bd916fbf91541cab6bb71f5496b4370aa1e53a Mon Sep 17 00:00:00 2001 From: Henry Oswald Date: Tue, 8 Apr 2014 16:56:34 +0100 Subject: [PATCH] refactored project list, and updated sig for findAllUsersProjects to include err --- .../Features/Dropbox/DropboxHandler.coffee | 2 +- .../Features/Project/ProjectController.coffee | 80 ++++++++++--------- .../Features/Project/ProjectLocator.coffee | 2 +- services/web/app/coffee/models/Project.coffee | 2 +- .../coffee/Dropbox/DropboxHandlerTests.coffee | 2 +- .../Project/ProjectControllerTests.coffee | 2 +- .../coffee/Project/ProjectLocatorTests.coffee | 4 +- 7 files changed, 51 insertions(+), 43 deletions(-) diff --git a/services/web/app/coffee/Features/Dropbox/DropboxHandler.coffee b/services/web/app/coffee/Features/Dropbox/DropboxHandler.coffee index eb6859f801..33b9caf7b7 100644 --- a/services/web/app/coffee/Features/Dropbox/DropboxHandler.coffee +++ b/services/web/app/coffee/Features/Dropbox/DropboxHandler.coffee @@ -58,7 +58,7 @@ module.exports = callback(err) flushUsersProjectToDropbox: (user_id, callback)-> - Project.findAllUsersProjects user_id, '_id', (projects = [], collabertions = [], readOnlyProjects = [])-> + Project.findAllUsersProjects user_id, '_id', (err, projects = [], collabertions = [], readOnlyProjects = [])-> projectList = [] projectList = projectList.concat(projects) projectList = projectList.concat(collabertions) diff --git a/services/web/app/coffee/Features/Project/ProjectController.coffee b/services/web/app/coffee/Features/Project/ProjectController.coffee index eb410d3b9a..196efbcff6 100644 --- a/services/web/app/coffee/Features/Project/ProjectController.coffee +++ b/services/web/app/coffee/Features/Project/ProjectController.coffee @@ -58,39 +58,47 @@ module.exports = projectListPage: (req, res, next)-> timer = new metrics.Timer("project-list") user_id = req.session.user._id - SubscriptionLocator.getUsersSubscription user_id, (err, subscription)-> - logger.log user_id: user_id, "project list timer - Subscription.getUsersSubscription" - return next(error) if error? - # TODO: Remove this one month after the ability to start free trials was removed - if subscription? and subscription.freeTrial? and subscription.freeTrial.expiresAt? - freeTrial = - expired: !!subscription.freeTrial.downgraded - expiresAt: SubscriptionFormatters.formatDate(subscription.freeTrial.expiresAt) - TagsHandler.getAllTags user_id, (err, tags, tagsGroupedByProject)-> - logger.log user_id: user_id, "project list timer - TagsHandler.getAllTags" - Project.findAllUsersProjects user_id, 'name lastUpdated publicAccesLevel', (projects, collabertions, readOnlyProjects)-> - logger.log user_id: user_id, "project list timer - Project.findAllUsersProjects" - for project in projects - project.accessLevel = "owner" - for project in collabertions - project.accessLevel = "readWrite" - for project in readOnlyProjects - project.accessLevel = "readOnly" - projects = projects.concat(collabertions).concat(readOnlyProjects) - projects = projects.map (project)-> - project.tags = tagsGroupedByProject[project._id] || [] - return project - tags = _.sortBy tags, (tag)-> - -tag.project_ids.length - logger.log projects:projects, collabertions:collabertions, readOnlyProjects:readOnlyProjects, user_id:user_id, "rendering project list" - sortedProjects = _.sortBy projects, (project)-> - return - project.lastUpdated - res.render 'project/list', - title:'Your Projects' - priority_title: true - projects: sortedProjects - freeTrial: freeTrial - tags:tags - projectTabActive: true - logger.log user_id: user_id, "project list timer - Finished" - timer.done() + async.parallel { + subscription: (cb)-> + SubscriptionLocator.getUsersSubscription user_id, cb + tags: (cb)-> + TagsHandler.getAllTags user_id, cb + projects: (cb)-> + Project.findAllUsersProjects user_id, 'name lastUpdated publicAccesLevel', cb + }, (err, results)-> + logger.log results:results, user_id:user_id, "rendering project list" + viewModel = _buildListViewModel results.projects[0], results.projects[1], results.projects[2], results.tags[0], results.tags[1], results.subscription[0] + res.render 'project/list', viewModel + timer.done() + + +_buildListViewModel = (projects, collabertions, readOnlyProjects, tags, tagsGroupedByProject, subscription)-> + # TODO: Remove this one month after the ability to start free trials was removed + if subscription? and subscription.freeTrial? and subscription.freeTrial.expiresAt? + freeTrial = + expired: !!subscription.freeTrial.downgraded + expiresAt: SubscriptionFormatters.formatDate(subscription.freeTrial.expiresAt) + + for project in projects + project.accessLevel = "owner" + for project in collabertions + project.accessLevel = "readWrite" + for project in readOnlyProjects + project.accessLevel = "readOnly" + projects = projects.concat(collabertions).concat(readOnlyProjects) + projects = projects.map (project)-> + project.tags = tagsGroupedByProject[project._id] || [] + return project + tags = _.sortBy tags, (tag)-> + -tag.project_ids.length + sortedProjects = _.sortBy projects, (project)-> + return - project.lastUpdated + + return { + title:'Your Projects' + priority_title: true + projects: sortedProjects + freeTrial: freeTrial + tags:tags + projectTabActive: true + } \ No newline at end of file diff --git a/services/web/app/coffee/Features/Project/ProjectLocator.coffee b/services/web/app/coffee/Features/Project/ProjectLocator.coffee index 3a415b144a..a0fd455e90 100644 --- a/services/web/app/coffee/Features/Project/ProjectLocator.coffee +++ b/services/web/app/coffee/Features/Project/ProjectLocator.coffee @@ -114,7 +114,7 @@ module.exports = async.waterfall jobs, callback findUsersProjectByName: (user_id, projectName, callback)-> - Project.findAllUsersProjects user_id, 'name', (projects, collabertions=[])-> + Project.findAllUsersProjects user_id, 'name', (err, projects, collabertions=[])-> projects = projects.concat(collabertions) projectName = projectName.toLowerCase() project = _.find projects, (project)-> diff --git a/services/web/app/coffee/models/Project.coffee b/services/web/app/coffee/models/Project.coffee index bf6a1c1c27..f35f714af8 100644 --- a/services/web/app/coffee/models/Project.coffee +++ b/services/web/app/coffee/models/Project.coffee @@ -56,7 +56,7 @@ ProjectSchema.statics.findAllUsersProjects = (user_id, requiredFields, callback) this.find {owner_ref:user_id}, requiredFields, (err, projects)=> this.find {collaberator_refs:user_id}, requiredFields, (err, collabertions)=> this.find {readOnly_refs:user_id}, requiredFields, (err, readOnlyProjects)=> - callback(projects, collabertions, readOnlyProjects) + callback(err, projects, collabertions, readOnlyProjects) sanitizeTypeOfElement = (elementType)-> lastChar = elementType.slice -1 diff --git a/services/web/test/UnitTests/coffee/Dropbox/DropboxHandlerTests.coffee b/services/web/test/UnitTests/coffee/Dropbox/DropboxHandlerTests.coffee index f5c562a894..7cad2ac9ff 100644 --- a/services/web/test/UnitTests/coffee/Dropbox/DropboxHandlerTests.coffee +++ b/services/web/test/UnitTests/coffee/Dropbox/DropboxHandlerTests.coffee @@ -73,7 +73,7 @@ describe 'third party data store', -> user_id = "123u9oijllkj" projectList = [{_id:"123lk"}, {_id:"12ji3ojio"}, {_id:"2jiojdoi"}] collabProjectList = [{_id:"213ds"}] - @projectModel.findAllUsersProjects.callsArgWith(2, projectList, collabProjectList) + @projectModel.findAllUsersProjects.callsArgWith(2, null, projectList, collabProjectList) @handler.flushUsersProjectToDropbox user_id, => @projectEntityHandler.flushProjectToThirdPartyDataStore.calledWith(projectList[0]._id).should.equal true @projectEntityHandler.flushProjectToThirdPartyDataStore.calledWith(projectList[1]._id).should.equal true diff --git a/services/web/test/UnitTests/coffee/Project/ProjectControllerTests.coffee b/services/web/test/UnitTests/coffee/Project/ProjectControllerTests.coffee index 808d45207a..b0d86553bf 100644 --- a/services/web/test/UnitTests/coffee/Project/ProjectControllerTests.coffee +++ b/services/web/test/UnitTests/coffee/Project/ProjectControllerTests.coffee @@ -102,7 +102,7 @@ describe "ProjectController", -> @readOnly = [{lastUpdated:3, _id:3}] @SubscriptionLocator.getUsersSubscription.callsArgWith(1, null, {}) @TagsHandler.getAllTags.callsArgWith(1, null, @tags, {}) - @ProjectModel.findAllUsersProjects.callsArgWith(2, @projects, @collabertions, @readOnly) + @ProjectModel.findAllUsersProjects.callsArgWith(2, null, @projects, @collabertions, @readOnly) it "should render the project/list page", (done)-> diff --git a/services/web/test/UnitTests/coffee/Project/ProjectLocatorTests.coffee b/services/web/test/UnitTests/coffee/Project/ProjectLocatorTests.coffee index 43a3acad01..7881f11bf8 100644 --- a/services/web/test/UnitTests/coffee/Project/ProjectLocatorTests.coffee +++ b/services/web/test/UnitTests/coffee/Project/ProjectLocatorTests.coffee @@ -227,7 +227,7 @@ describe 'project model', -> user_id = "123jojoidns" stubbedProject = {name:"findThis"} projects = [{name:"notThis"}, {name:"wellll"}, stubbedProject, {name:"Noooo"}] - Project.findAllUsersProjects = sinon.stub().callsArgWith(2, projects) + Project.findAllUsersProjects = sinon.stub().callsArgWith(2, null, projects) @locator.findUsersProjectByName user_id, stubbedProject.name.toLowerCase(), (err, project)-> project.should.equal stubbedProject done() @@ -236,7 +236,7 @@ describe 'project model', -> user_id = "123jojoidns" stubbedProject = {name:"findThis"} projects = [{name:"notThis"}, {name:"wellll"}, {name:"Noooo"}] - Project.findAllUsersProjects = sinon.stub().callsArgWith(2, projects, [stubbedProject]) + Project.findAllUsersProjects = sinon.stub().callsArgWith(2, null, projects, [stubbedProject]) @locator.findUsersProjectByName user_id, stubbedProject.name.toLowerCase(), (err, project)-> project.should.equal stubbedProject done()