Created
December 15, 2016 22:21
-
-
Save emersonbroga/29bba155f0f54eae2ef77de79231a62c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
getAccountSports: function getAccountSports(accountId, callback) { | |
var allSports = ['lacrosse', 'soccer']; | |
var result = []; | |
Account.findOne(accountId).populate('events').then(function(account) { | |
// if admin return all sports. | |
if (account.type === 'admin') { | |
return callback(null, allSports); | |
} | |
// from events | |
if (account.events.length) { | |
account.events.map(function(event) { | |
if (result.indexOf(event.sport) === -1) { | |
result.push(event.sport); | |
} | |
}); | |
return callback(null, result); | |
} | |
// from recruits. | |
MyRecruitItem.find({owner: accountId}).exec(function(err, recruits) { | |
var playerIds = []; | |
if (err || !recruits) { | |
return callback(null, allSports); | |
} | |
recruits.map(function(recruit){ | |
playerIds.push(recruit.player); | |
}); | |
TeamPlayer.find({player: playerIds}).exec(function(err, teamPlayers) { | |
if (err || !teamPlayers) { | |
return callback(null, allSports); | |
} | |
teamPlayers.map(function(teamPlayer) { | |
if (result.indexOf(teamPlayer.sport) === -1) { | |
result.push(teamPlayer.sport); | |
} | |
}); | |
return callback(null, result); | |
}); // TeamPlayer.find | |
}); | |
}).catch(function(err) { | |
return callback(null, allSports); | |
}); | |
}, |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment