Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save philipimperato/d91595917c59ff8376be1b6ea795706a to your computer and use it in GitHub Desktop.
Save philipimperato/d91595917c59ff8376be1b6ea795706a to your computer and use it in GitHub Desktop.
the route with the most H12 timeouts
routeConfig.post('/Dashboard/Index', repos.DashboardRepository.getRangeData(repos), (req, res) => {
let queries = [],
sess = req.userSession,
filter = req.rangeFilter;
queries.push(repos.PromotionRepository.getAgencyPromotions(sess.agencyId, 'all'));
queries.push(repos.DashboardRepository.getIndex(repos, filter, sess));
queries.push(repos.PolicyRepository.getPolicies(filter, true));
Promise.all(queries)
.then(([promotions, [totalRanks, myNumbers, totalProduction], policies]) => {
let sources = helpers.PolicyHelper.parseSourcesFromPolicies(policies);
totalProductionByMonth = helpers.DashboardHelper.validateTotalProduction(totalProduction);
myNumbers = helpers.DashboardHelper.buildMyNumbers(myNumbers);
res.json({
view: {
agencies: sess.agencyId !== 0,
promotions: sess.agencyId !== 0,
isReport: filter.reportId !== 0,
latestReportMonth: filter.lastestPayrollName ? filter.lastestPayrollName : '',
noPreviousPayrolls: !filter.latestPayroll
},
promotions: helpers.PromotionHelper.dashboard(promotions),
totalProduction: totalProductionByMonth,
totalRanks: totalRanks.filter(tr => tr.amount > 0),
sources: sources,
myNumbers: myNumbers
});
});
});
routeConfig.all('/*', (req, res, next) => {
req.userSession = req.session.user;
if (req.userSession || util.publicUrls(req.originalUrl))
next();
else
req.xhr ? res.sendStatus(401) : res.redirect('/User/Login');
});
getRangeData(repos) {
return (req, res, next) => {
let filterQueries = [],
sess = req.userSession,
agentId = sess.agentId,
values = req.body.values,
filter = values ? values.filter : { reportId: 0, statuses: ['issued'] },
isUpcoming = filter.reportId === 'n',
agencyId = common.getAgencyIdForFiltering(filter, sess);
/* special case when only parts of page want pending data */
if (values.pendingConditional)
filter.statuses.push('pending');
/* Filter agencyIds prior to queries to avoid agency filter per query */
filterQueries.push(repos.UserRepository.byAgencyId(agencyId, true));
filterQueries.push(repos.ReportRepository.getLastPayroll(agentId));
Promise.all(filterQueries)
.then(([agency, latestPayroll]) => {
/* add range to filters */
let userArrays = agency.map(a => a.Users),
users = [].concat(...userArrays);
filter.userIds = users.map(u => u.userId);
filter.latestPayrollId = latestPayroll ? latestPayroll.reportId : 0;
filter.latestPayroll = common.parseAndBuildDates(latestPayroll, sess, isUpcoming);
filter.reportId = isUpcoming ? 0 : filter.reportId;
filter.lastestPayrollName = latestPayroll ? latestPayroll.name : null;
filter.payrollRun = filter.reportId > 0 && !isUpcoming;
filter.currentUserId = filter.userId || sess.userId;
filter.isUpcoming = isUpcoming;
/* add to request as it will repeat for many */
req.rangeFilter = filter;
next()
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment