Skip to content

Instantly share code, notes, and snippets.

@adamrneary
Created August 21, 2015 15:45
Show Gist options
  • Save adamrneary/e7efc9cac377bd250e15 to your computer and use it in GitHub Desktop.
Save adamrneary/e7efc9cac377bd250e15 to your computer and use it in GitHub Desktop.
/**
* @param {Object?} params
* @return {Promise}
*/
exports.fetchAll = function(params) {
if (params && params.teamId) {
return Thrift.enhanced.Job.indexForTeam(params).then(jobs => {
processForeignKeys(jobs);
return jobs;
});
} else {
return Thrift.enhanced.Job.index().then(jobs => {
processForeignKeys(jobs);
return jobs;
});
}
};
function processForeignKeys(jobs) {
_.chain(jobs)
.pluck('jobTemplateId')
.uniq()
.value()
.forEach(jobTemplateId => {
JobTemplate.actions.fetch({id: jobTemplateId});
});
const teams = _.chain(jobs)
.pluck('team')
.uniq(record => record.id)
.value();
Team.actions.receiveAll(teams);
const jobUsers = _.chain(jobs)
.pluck('jobUsers')
.flatten()
.pluck('user')
.uniq(record => record.id)
.value();
User.actions.receiveAll(jobUsers);
const jobRoles = _.chain(jobs)
.pluck('jobUsers')
.flatten()
.pluck('jobRole')
.uniq(record => record.id)
.value();
JobRole.actions.receiveAll(jobRoles);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment