Skip to content

Commit

Permalink
Do not load datatables when orders above 2500 count to optimize loadi…
Browse files Browse the repository at this point in the history
…ng speeds
  • Loading branch information
megastary committed Jan 7, 2025
1 parent f1eb205 commit 0fe38ac
Show file tree
Hide file tree
Showing 4 changed files with 125 additions and 89 deletions.
4 changes: 3 additions & 1 deletion app.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import './tasks/daily-unpaid-invoices-notice.js'
import './tasks/daily-paid-invoices-needs-approval.js'
import './tasks/daily-user-phones.js'
import './tasks/continuous-products-aims-integration.js'
import handlebarsHelpers from './functions/handlebarsHelpers.js'
import indexRouter from './routes/index.js'
import aboutRouter from './routes/about.js'
import docsRouter from './routes/docs.js'
Expand Down Expand Up @@ -97,7 +98,8 @@ app.engine(
engine({
defaultLayout: 'layout',
extname: '.hbs',
handlebars: allowInsecurePrototypeAccess(handlebars)
handlebars: allowInsecurePrototypeAccess(handlebars),
helpers: handlebarsHelpers
})
)
app.enable('trust proxy')
Expand Down
18 changes: 18 additions & 0 deletions functions/handlebarsHelpers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
const helpers = {
gt: function (context, limitcount, options) {
const array = Array.isArray(context) ? context : context[options.hash.var]
if (array.length > limitcount) {
return options.fn(this)
}
return options.inverse(this)
},
lt: function (context, limitcount, options) {
const array = Array.isArray(context) ? context : context[options.hash.var]
if (array.length < limitcount) {
return options.fn(this)
}
return options.inverse(this)
}
}

export default helpers
186 changes: 98 additions & 88 deletions routes/orders.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,11 @@ router.get('/', ensureAuthenticated, checkKiosk, function (req, res) {
var alert = req.session.alert
delete req.session.alert
}

Order.aggregate([
{
$match: filter
},
{
$sort: {
_id: -1
}
},
{
$lookup: {
from: 'deliveries',
Expand All @@ -57,39 +53,6 @@ router.get('/', ensureAuthenticated, checkKiosk, function (req, res) {
{
$unwind: '$deliveryInfo'
},
{
$lookup: {
from: 'users',
localField: 'deliveryInfo.supplierId',
foreignField: '_id',
as: 'supplierInfo'
}
},
{
$unwind: '$supplierInfo'
},
{
$lookup: {
from: 'users',
localField: 'buyerId',
foreignField: '_id',
as: 'buyerInfo'
}
},
{
$unwind: '$buyerInfo'
},
{
$lookup: {
from: 'products',
localField: 'deliveryInfo.productId',
foreignField: '_id',
as: 'productInfo'
}
},
{
$unwind: '$productInfo'
},
{
$group: {
_id: null,
Expand All @@ -99,67 +62,114 @@ router.get('/', ensureAuthenticated, checkKiosk, function (req, res) {
totalSpend: {
$sum: '$deliveryInfo.price'
},
results: {
$push: '$$ROOT'
}
}
},
{
$project: {
totalOrders: 1,
totalSpend: 1,
results: 1,
totalUnpaid: {
$let: {
vars: {
field: {
$filter: {
input: '$results',
as: 'calc',
cond: {
$eq: ['$$calc.invoice', false]
}
}
}
},
in: {
$sum: '$$field.deliveryInfo.price'
}
$sum: {
$cond: [{ $eq: ['$invoice', false] }, '$deliveryInfo.price', 0]
}
}
}
}
])
.then((docs) => {
if (docs[0]) {
logger.debug(
`server.routes.orders.get__Successfully loaded ${docs[0].results.length} orders.`,
{
.then((sums) => {
Order.aggregate([
{
$match: filter
},
{
$sort: {
_id: -1
}
},
{
$lookup: {
from: 'deliveries',
localField: 'deliveryId',
foreignField: '_id',
as: 'deliveryInfo'
}
},
{
$unwind: '$deliveryInfo'
},
{
$lookup: {
from: 'users',
localField: 'deliveryInfo.supplierId',
foreignField: '_id',
as: 'supplierInfo'
}
},
{
$unwind: '$supplierInfo'
},
{
$lookup: {
from: 'users',
localField: 'buyerId',
foreignField: '_id',
as: 'buyerInfo'
}
},
{
$unwind: '$buyerInfo'
},
{
$lookup: {
from: 'products',
localField: 'deliveryInfo.productId',
foreignField: '_id',
as: 'productInfo'
}
},
{
$unwind: '$productInfo'
}
])
.then((docs) => {
if (docs.length > 0) {
logger.debug(
`server.routes.orders.get__Successfully loaded ${docs.length} orders.`,
{
metadata: {
result: docs
}
}
)
docs.forEach(function (element) {
element.order_date_format = moment(element.order_date).format(
'LLLL'
)
element.order_date = moment(element.order_date).format()
})
}

res.render('shop/orders', {
title:
req.baseUrl === '/admin_orders'
? 'Všechny objednávky | Lednice IT'
: 'Objednávky | Lednice IT',
orders: {
totalOrders: sums[0]?.totalOrders || 0,
totalSpend: sums[0]?.totalSpend || 0,
totalUnpaid: sums[0]?.totalUnpaid || 0,
results: docs
},
admin: filter,
user: req.user,
alert,
csrfToken: req.csrfToken()
})
})
.catch((err) => {
logger.error('server.routes.orders.get__Failed to load orders.', {
metadata: {
result: docs
error: err.message
}
}
)
docs[0].results.forEach(function (element) {
element.order_date_format = moment(element.order_date).format('LLLL')
element.order_date = moment(element.order_date).format()
})
})
}

res.render('shop/orders', {
title:
req.baseUrl === '/admin_orders'
? 'Všechny objednávky | Lednice IT'
: 'Objednávky | Lednice IT',
orders: docs[0],
admin: filter,
user: req.user,
alert,
csrfToken: req.csrfToken()
})
})
.catch((err) => {
logger.error('server.routes.orders.get__Failed to load orders.', {
logger.error('server.routes.orders.get__Failed to load order sums.', {
metadata: {
error: err.message
}
Expand Down
6 changes: 6 additions & 0 deletions views/shop/orders.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@
{{/unless}}
<th scope="col">Dodavatel</th>
<th scope="col">Způsob</th>
{{#if admin.buyerId}}
<th scope="col">Fakturováno</th>
{{/if}}
</tr>
</thead>
<tbody>
Expand Down Expand Up @@ -71,13 +73,15 @@
E-Shop
{{/if}}
</td>
{{#if ../admin.buyerId}}
<td>
{{#if this.invoice }}
Ano
{{else}}
Ne
{{/if}}
</td>
{{/if}}
</tr>
{{/each}}
</tbody>
Expand All @@ -86,6 +90,8 @@
</div>
</div>

{{#lt orders.results 2500}}
{{> datatables }}

<script src="/javascripts/orders.js"></script>
{{/lt}}

0 comments on commit 0fe38ac

Please sign in to comment.