diff --git a/app.js b/app.js index c27cfee..4050064 100755 --- a/app.js +++ b/app.js @@ -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' @@ -97,7 +98,8 @@ app.engine( engine({ defaultLayout: 'layout', extname: '.hbs', - handlebars: allowInsecurePrototypeAccess(handlebars) + handlebars: allowInsecurePrototypeAccess(handlebars), + helpers: handlebarsHelpers }) ) app.enable('trust proxy') diff --git a/functions/handlebarsHelpers.js b/functions/handlebarsHelpers.js new file mode 100644 index 0000000..b4ea0b9 --- /dev/null +++ b/functions/handlebarsHelpers.js @@ -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 diff --git a/routes/orders.js b/routes/orders.js index deb02c0..107b204 100644 --- a/routes/orders.js +++ b/routes/orders.js @@ -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', @@ -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, @@ -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 } diff --git a/views/shop/orders.hbs b/views/shop/orders.hbs index 3283d9e..073f2c4 100644 --- a/views/shop/orders.hbs +++ b/views/shop/orders.hbs @@ -33,7 +33,9 @@ {{/unless}} Dodavatel Způsob + {{#if admin.buyerId}} Fakturováno + {{/if}} @@ -71,6 +73,7 @@ E-Shop {{/if}} + {{#if ../admin.buyerId}} {{#if this.invoice }} Ano @@ -78,6 +81,7 @@ Ne {{/if}} + {{/if}} {{/each}} @@ -86,6 +90,8 @@ +{{#lt orders.results 2500}} {{> datatables }} +{{/lt}}