Change findAllUsersProjects, produce and object rather than lists

This commit is contained in:
Shane Kilkelly 2017-10-20 11:49:20 +01:00
parent d710d284fe
commit dc39e447b2
6 changed files with 69 additions and 20 deletions

View file

@ -151,7 +151,7 @@ module.exports = ProjectController =
notifications = require("underscore").map results.notifications, (notification)->
notification.html = req.i18n.translate(notification.templateKey, notification.messageOpts)
return notification
projects = ProjectController._buildProjectList results.projects[0], results.projects[1], results.projects[2], results.projects[3], results.projects[4]
projects = ProjectController._buildProjectList results.projects
user = results.user
ProjectController._injectProjectOwners projects, (error, projects) ->
return next(error) if error?
@ -309,22 +309,23 @@ module.exports = ProjectController =
maxDocLength: Settings.max_doc_length
timer.done()
_buildProjectList: (ownedProjects, readAndWriteProjects, readOnlyProjects, tokenReadAndWriteProjects, tokenReadOnlyProjects)->
_buildProjectList: (allProjects)->
{owned, readAndWrite, readOnly, tokenReadAndWrite, tokenReadOnly} = allProjects
projects = []
for project in ownedProjects
for project in owned
projects.push ProjectController._buildProjectViewModel(project, "owner", Sources.OWNER)
# Invite-access
for project in readAndWriteProjects
for project in readAndWrite
projects.push ProjectController._buildProjectViewModel(project, "readWrite", Sources.INVITE)
for project in readOnlyProjects
for project in readOnly
projects.push ProjectController._buildProjectViewModel(project, "readOnly", Sources.INVITE)
# Token-access
# Only add these projects if they're not already present, this gives us cascading access
# from 'owner' => 'token-read-only'
for project in tokenReadAndWriteProjects
for project in tokenReadAndWrite
if projects.filter((p) -> p.id.toString() == project._id.toString()).length == 0
projects.push ProjectController._buildProjectViewModel(project, "readAndWrite", Sources.TOKEN)
for project in tokenReadOnlyProjects
for project in tokenReadOnly
if projects.filter((p) -> p.id.toString() == project._id.toString()).length == 0
projects.push ProjectController._buildProjectViewModel(project, "readOnly", Sources.TOKEN)

View file

@ -50,14 +50,24 @@ module.exports = ProjectGetter =
return callback(err)
callback(null, project?[0])
findAllUsersProjects: (user_id, fields, callback = (error, ownedProjects, readAndWriteProjects, readOnlyProjects, tokenReadAndWrite, tokenReadOnly) ->) ->
findAllUsersProjects: (
user_id,
fields,
callback = (error, projects={owned: [], readAndWrite: [], readOnly: [], tokenReadAndWrite: [], tokenReadOnly: []}) ->
) ->
CollaboratorsHandler = require "../Collaborators/CollaboratorsHandler"
Project.find {owner_ref: user_id}, fields, (error, ownedProjects) ->
return callback(error) if error?
CollaboratorsHandler.getProjectsUserIsMemberOf user_id, fields, (error, projects) ->
return callback(error) if error?
callback null, ownedProjects, projects.readAndWrite, projects.readOnly, projects.tokenReadAndWrite, projects.tokenReadOnly
result = {
owned: ownedProjects || [],
readAndWrite: projects.readAndWrite || [],
readOnly: projects.readOnly || [],
tokenReadAndWrite: projects.tokenReadAndWrite || [],
tokenReadOnly: projects.tokenReadOnly || []
}
callback null, result
[
'getProject',

View file

@ -150,9 +150,10 @@ module.exports = ProjectLocator =
async.waterfall jobs, callback
findUsersProjectByName: (user_id, projectName, callback)->
ProjectGetter.findAllUsersProjects user_id, 'name archived', (err, projects, collabertions=[])->
ProjectGetter.findAllUsersProjects user_id, 'name archived', (err, allProjects)->
return callback(error) if error?
projects = projects.concat(collabertions)
{owned, readAndWrite} = allProjects
projects = owned.concat(readAndWrite)
projectName = projectName.toLowerCase()
project = _.find projects, (project)->
project.name.toLowerCase() == projectName and project.archived != true

View file

@ -239,6 +239,13 @@ describe "ProjectController", ->
@tokenReadOnly = [
{_id:7, lastUpdated:4, owner_ref: "user-5"}
]
@allProjects = {
owned: @projects,
readAndWrite: @collabertions,
readOnly: @readOnly,
tokenReadAndWrite: @tokenReadAndWrite,
tokenReadOnly: @tokenReadOnly
}
@users =
'user-1':
@ -252,7 +259,7 @@ describe "ProjectController", ->
@LimitationsManager.userHasSubscriptionOrIsGroupMember.callsArgWith(1, null, false)
@TagsHandler.getAllTags.callsArgWith(1, null, @tags, {})
@NotificationsHandler.getUserNotifications = sinon.stub().callsArgWith(1, null, @notifications, {})
@ProjectGetter.findAllUsersProjects.callsArgWith(2, null, @projects, @collabertions, @readOnly, @tokenReadAndWrite, @tokenReadOnly)
@ProjectGetter.findAllUsersProjects.callsArgWith(2, null, @allProjects)
it "should render the project/list page", (done)->
@res.render = (pageName, opts)=>
@ -307,6 +314,13 @@ describe "ProjectController", ->
{_id:6, lastUpdated:5, owner_ref: "user-4"} # Also in tokenReadAndWrite
{_id:7, lastUpdated:4, owner_ref: "user-5"}
]
@allProjects = {
owned: @projects,
readAndWrite: @collabertions,
readOnly: @readOnly,
tokenReadAndWrite: @tokenReadAndWrite,
tokenReadOnly: @tokenReadOnly
}
@users =
'user-1':
@ -320,7 +334,7 @@ describe "ProjectController", ->
@LimitationsManager.userHasSubscriptionOrIsGroupMember.callsArgWith(1, null, false)
@TagsHandler.getAllTags.callsArgWith(1, null, @tags, {})
@NotificationsHandler.getUserNotifications = sinon.stub().callsArgWith(1, null, @notifications, {})
@ProjectGetter.findAllUsersProjects.callsArgWith(2, null, @projects, @collabertions, @readOnly, @tokenReadAndWrite, @tokenReadOnly)
@ProjectGetter.findAllUsersProjects.callsArgWith(2, null, @allProjects)
it "should render the project/list page", (done)->
@res.render = (pageName, opts)=>

View file

@ -152,12 +152,21 @@ describe "ProjectGetter", ->
@CollaboratorsHandler.getProjectsUserIsMemberOf.withArgs(@user_id, @fields).yields(
null,
{
readAndWrite: ["mock-rw-projects"], readOnly: ["mock-ro-projects"]
readAndWrite: ["mock-rw-projects"],
readOnly: ["mock-ro-projects"],
tokenReadAndWrite: ['mock-token-rw-projects'],
tokenReadOnly: ['mock-token-ro-projects']
}
)
@ProjectGetter.findAllUsersProjects @user_id, @fields, @callback
it "should call the callback with all the projects", ->
@callback
.calledWith(null, ["mock-owned-projects"], ["mock-rw-projects"], ["mock-ro-projects"])
.calledWith(null, {
owned: ["mock-owned-projects"],
readAndWrite: ["mock-rw-projects"],
readOnly: ["mock-ro-projects"]
tokenReadAndWrite: ['mock-token-rw-projects'],
tokenReadOnly: ['mock-token-ro-projects']
})
.should.equal true

View file

@ -317,7 +317,9 @@ describe 'ProjectLocator', ->
it 'should return the project from an array case insenstive', (done)->
user_id = "123jojoidns"
stubbedProject = {name:"findThis"}
projects = [{name:"notThis"}, {name:"wellll"}, stubbedProject, {name:"Noooo"}]
projects = {
owned: [{name:"notThis"}, {name:"wellll"}, stubbedProject, {name:"Noooo"}]
}
@ProjectGetter.findAllUsersProjects = sinon.stub().callsArgWith(2, null, projects)
@locator.findUsersProjectByName user_id, stubbedProject.name.toLowerCase(), (err, project)->
project.should.equal stubbedProject
@ -326,7 +328,16 @@ describe 'ProjectLocator', ->
it 'should return the project which is not archived', (done)->
user_id = "123jojoidns"
stubbedProject = {name:"findThis", _id:12331321}
projects = [{name:"notThis"}, {name:"wellll"}, {name:"findThis",archived:true}, stubbedProject, {name:"findThis",archived:true}, {name:"Noooo"}]
projects = {
owned: [
{name:"notThis"},
{name:"wellll"},
{name:"findThis",archived:true},
stubbedProject,
{name:"findThis",archived:true},
{name:"Noooo"}
]
}
@ProjectGetter.findAllUsersProjects = sinon.stub().callsArgWith(2, null, projects)
@locator.findUsersProjectByName user_id, stubbedProject.name.toLowerCase(), (err, project)->
project._id.should.equal stubbedProject._id
@ -335,8 +346,11 @@ describe 'ProjectLocator', ->
it 'should search collab projects as well', (done)->
user_id = "123jojoidns"
stubbedProject = {name:"findThis"}
projects = [{name:"notThis"}, {name:"wellll"}, {name:"Noooo"}]
@ProjectGetter.findAllUsersProjects = sinon.stub().callsArgWith(2, null, projects, [stubbedProject])
projects = {
owned: [{name:"notThis"}, {name:"wellll"}, {name:"Noooo"}]
readAndWrite: [stubbedProject]
}
@ProjectGetter.findAllUsersProjects = sinon.stub().callsArgWith(2, null, projects)
@locator.findUsersProjectByName user_id, stubbedProject.name.toLowerCase(), (err, project)->
project.should.equal stubbedProject
done()