return new Promise(function(resolve, reject){ var thisUserLists = [], newUserList = []; // Fill thisUserLists array with names of the user lists thisUserLists = user.userLists.map(function(list){ return list.name; }); // We convert userList array to a set; this will make sure there will be no duplicate entries in the user list array var thisUserListsSet = new Set(thisUserLists); // If our new list name is not already in the set if (!thisUserListsSet.contains(listName)) { // save new list newUserList = new UserList({ name: listName, users: [] }); newUserList.save(function(err){ if (err) reject(err); resolve(newUserList); }); } else { resolve(new ReferenceError("User list with the same name already exists.")); } });
parents: function(){ var result = new Set(); this.parentAccountabilities.forEach(function(x) { result.add(x.parent()); }); return result; },
parents: function(partytype){ var result = new Set(); this.parentAccountabilities.forEach(function(x) { if (x.type() == partytype) { result.add(x.parent()); } }); return result; },
userCommon.checkUserExistence(userToAdd).then(function(result) { if (result === "already exists.") { // We use a set so duplicate user entries inside a user list are impossible listToSaveSet = new Set(foundUserList.users); listToSaveSet.add(userToAdd); listToSave = listToSaveSet.map(function(usr){ return usr; }) UserList.update( // we use model object itself, not an instance - it wouldn't work { _id: foundUserList._id }, { $set: { users: listToSave } }, function(err){ if (err) reject(err); // TODO resolve(); } ); } // if result else { resolve(new ReferenceError("User to add doesn't exist")); } }).catch(function(reason){
this.parentAccountabilities.forEach(function(x) { result.add(x.parent()); });
this.parentAccountabilities.forEach(function(x) { if (x.type() == partytype) { result.add(x.parent()); } });