Skip to content

Instantly share code, notes, and snippets.

@pmeaney
Created June 26, 2018 22:54
Show Gist options
  • Save pmeaney/10068adcd91aa954326a1513ce3a0f94 to your computer and use it in GitHub Desktop.
Save pmeaney/10068adcd91aa954326a1513ce3a0f94 to your computer and use it in GitHub Desktop.
// Original, before trying something at line 23.
router.get('/timesheets', (req, res) => {
return Promise.try(() => {
return Api_fns.getAllTimesheets();
}).then((timesheets) => {
return Promise.map(timesheets, (timesheet) => {
return Promise.all([
Api_fns.getEmployee_by_id(timesheet['emp_accepted_by']), /* emp completing the task */
Api_fns.getActivity_by_id(timesheet['activity_id']),
// these we do in next round:
// Api_fns.getActivityType_by_activity_code(timesheet['activity_code']), activity type info: such as 'painting', by code (i.e. activity_code is basically activity type's uniqueID)
// Api_fns.getLocation_by_project_id(timesheet['project_id']), /* work-activity location info by for employee (activity's project id) */
// Api_fns.getProjectMgr_by_project_id(timesheet['project_id'])
]).spread(( employee_acceptedBy, timesheet_activityData) => {
return { employee_acceptedBy, timesheet_activityData }
});
}).then((data) => {
// do stuff with data
return {data}
}).then((data)=>{
// console.log('data in scope?', data)
res.status(200).json(data);
})
})
// After...
router.get('/timesheets', (req, res) => {
return Promise.try(() => {
return Api_fns.getAllTimesheets();
}).then((timesheets) => {
return Promise.map(timesheets, (timesheet) => {
return Promise.all([
Api_fns.getEmployee_by_id(timesheet['emp_accepted_by']), /* emp completing the task */
Api_fns.getActivity_by_id(timesheet['activity_id']),
]).spread(( employee_acceptedBy, timesheet_activityData) => {
return { employee_acceptedBy, timesheet_activityData }
});
}).then((data) => {
return Promise.map(data, (datapoint) => {
console.log('datapoint.timesheet_activityData[0].activity_code',datapoint.timesheet_activityData[0].activity_code)
return Promise.all([
Api_fns.getActivityType_by_activity_code(datapoint.timesheet_activityData[0].activity_code)
])
})
}).then((data)=>{
data.map((curr, i)=>{
// console.log(curr)
curr.map((curr2, i2)=>{
// console.log(curr2)
curr2.map((curr3,i3)=>{
console.log(curr3.activity_type)
})
})
})
// console.log('data in scope?', data)
// res.status(200).json(data);
})
})
});
// Terminal output:
datapoint.timesheet_activityData[0].activity_code 1
datapoint.timesheet_activityData[0].activity_code 3
indoor painting
indoor carpentry
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment