more changes

This commit is contained in:
Dobie Wollert
2015-12-21 00:18:22 -08:00
parent 977ea41cef
commit 2f00d0e03e

View File

@ -125,17 +125,18 @@ function validateWeek(req) {
function summaryHandler(params) { function summaryHandler(params) {
const spans = findAllSpansForWeek(params.week); const spans = findAllSpansForWeek(params.week);
spans.then(console.log);
return buildReport(spans); return buildReport(spans, params.week);
} }
function userSummaryHandler(params) { function userSummaryHandler(params) {
const spans = findUserSpansForWeek(params.id, params.week); const spans = findUserSpansForWeek(params.id, params.week);
return buildReport(spans); return buildReport(spans, params.week);
} }
function buildReport(spans) { function buildReport(spans, week) {
const workordersById = spans const workordersById = spans
.then(extractIds('workorder')) .then(extractIds('workorder'))
.then(findWorkordersById) .then(findWorkordersById)
@ -149,7 +150,7 @@ function buildReport(spans) {
.then(indexById); .then(indexById);
const timesheetsByUser = userIds const timesheetsByUser = userIds
.then(findTimesheetsByUser) .then(findTimesheetsByUser(week))
.then(indexByUser); .then(indexByUser);
return Promise.join(spans, workordersById, usersById, timesheetsByUser, generateSummary); return Promise.join(spans, workordersById, usersById, timesheetsByUser, generateSummary);
@ -158,6 +159,8 @@ function buildReport(spans) {
function generateSummary(spans, workordersById, usersById, timesheetsByUser) { function generateSummary(spans, workordersById, usersById, timesheetsByUser) {
var results = {}; var results = {};
console.log(spans);
function fetchOrCreateUserRecord(userId) { function fetchOrCreateUserRecord(userId) {
var record = results[userId]; var record = results[userId];
if (!record) { if (!record) {
@ -300,17 +303,21 @@ function findUsersById(ids) {
return User.find(query).exec(); return User.find(query).exec();
} }
function findTimesheetsByUser(ids) { function findTimesheetsByUser(week) {
const query = { return ids => {
user: { const query = {
$in: ids user: {
} $in: ids
}; },
week: week
};
return TimeSheet.find(query).exec(); return TimeSheet.find(query).exec();
}
} }
function findAllSpansForWeek(week) { function findAllSpansForWeek(week) {
console.log(week.format());
var startOfWeek = week.clone().startOf('week'); var startOfWeek = week.clone().startOf('week');
var endOfWeek = week.clone().endOf('week'); var endOfWeek = week.clone().endOf('week');
@ -390,18 +397,11 @@ module.exports = function () {
}, },
summary: function (req, res) { summary: function (req, res) {
req.check('week').notEmpty().isWeek(); Promise
.props({
var errors = req.validationErrors(); week: validateWeek(req)
if (errors) { })
return res.json(400, errors); .then(summaryHandler)
}
var params = {
week: moment(req.sanitize('week'))
};
summaryHandler(params)
.then(responseHandler(res)) .then(responseHandler(res))
.catch(errorHandler(res)); .catch(errorHandler(res));
}, },