iterate over owned projects in a more robust way

This commit is contained in:
Brian Gough 2018-09-27 16:41:45 +01:00
parent 6b80d3563d
commit 8f8694ad94

View file

@ -78,12 +78,14 @@ module.exports = ProjectDetailsHandler =
# with a unique name. But that requires thinking through how we would handle incoming projects from # with a unique name. But that requires thinking through how we would handle incoming projects from
# dropbox for example. # dropbox for example.
ensureProjectNameIsUnique: (user_id, name, suffixes = [], callback = (error, name, changed)->) -> ensureProjectNameIsUnique: (user_id, name, suffixes = [], callback = (error, name, changed)->) ->
ProjectGetter.findAllUsersProjects user_id, {name: 1}, (error, allUsersProjects) -> ProjectGetter.findAllUsersProjects user_id, {name: 1}, (error, allUsersProjectNames) ->
return callback(error) if error? return callback(error) if error?
{owned, readAndWrite, readOnly, tokenReadAndWrite, tokenReadOnly} = allUsersProjects # allUsersProjectNames is returned as a hash {owned: [name1, name2, ...], readOnly: [....]}
# collect all of the names and flatten them into a single array
projectNameList = _.flatten(_.values(allUsersProjectNames))
# create a set of all project names # create a set of all project names
allProjectNames = new Set() allProjectNames = new Set()
for projectName in owned.concat(readAndWrite, readOnly, tokenReadAndWrite, tokenReadOnly) for projectName in projectNameList
allProjectNames.add(projectName) allProjectNames.add(projectName)
isUnique = (x) -> !allProjectNames.has(x) isUnique = (x) -> !allProjectNames.has(x)
# check if the supplied name is already unique # check if the supplied name is already unique