Get data loading in frontend

This commit is contained in:
2025-01-09 17:08:46 -05:00
parent 608f376790
commit 9034897ef1
9 changed files with 253 additions and 201 deletions

View File

@@ -1,36 +1,35 @@
const express = require('express');
const router = express.Router();
// Get dashboard statistics
// Get dashboard stats
router.get('/stats', async (req, res) => {
const pool = req.app.locals.pool;
try {
const [totalProducts] = await pool.query(
'SELECT COUNT(*) as count FROM products WHERE visible = true'
);
const [lowStockProducts] = await pool.query(
'SELECT COUNT(*) as count FROM products WHERE stock_quantity <= 10 AND visible = true'
);
const [orderStats] = await pool.query(`
const [stats] = await pool.query(`
SELECT
COUNT(DISTINCT order_number) as total_orders,
AVG(price * quantity - COALESCE(discount, 0)) as avg_order_value
FROM orders
WHERE canceled = false
AND date >= DATE_SUB(CURDATE(), INTERVAL 30 DAY)
COUNT(*) as totalProducts,
COUNT(CASE WHEN stock_quantity <= 5 THEN 1 END) as lowStockProducts,
COALESCE(
(SELECT COUNT(DISTINCT order_number) FROM orders WHERE DATE(date) >= DATE_SUB(CURDATE(), INTERVAL 30 DAY) AND canceled = false),
0
) as totalOrders,
COALESCE(
(SELECT AVG(subtotal) FROM (
SELECT order_number, SUM(price * quantity) as subtotal
FROM orders
WHERE DATE(date) >= DATE_SUB(CURDATE(), INTERVAL 30 DAY)
AND canceled = false
GROUP BY order_number
) t),
0
) as averageOrderValue
FROM products
WHERE visible = true
`);
res.json({
totalProducts: totalProducts[0].count,
lowStockProducts: lowStockProducts[0].count,
totalOrders: orderStats[0].total_orders || 0,
averageOrderValue: orderStats[0].avg_order_value || 0
});
res.json(stats[0]);
} catch (error) {
console.error('Error fetching dashboard stats:', error);
res.status(500).json({ error: 'Failed to fetch dashboard statistics' });
res.status(500).json({ error: 'Failed to fetch dashboard stats' });
}
});
@@ -41,15 +40,17 @@ router.get('/sales-overview', async (req, res) => {
const [rows] = await pool.query(`
SELECT
DATE(date) as date,
SUM(price * quantity - COALESCE(discount, 0)) as total
SUM(price * quantity) as total
FROM orders
WHERE canceled = false
AND date >= DATE_SUB(CURDATE(), INTERVAL 30 DAY)
WHERE DATE(date) >= DATE_SUB(CURDATE(), INTERVAL 30 DAY)
AND canceled = false
GROUP BY DATE(date)
ORDER BY date ASC
`);
res.json(rows);
res.json(rows.map(row => ({
...row,
total: parseFloat(row.total || 0)
})));
} catch (error) {
console.error('Error fetching sales overview:', error);
res.status(500).json({ error: 'Failed to fetch sales overview' });
@@ -62,31 +63,35 @@ router.get('/recent-orders', async (req, res) => {
try {
const [rows] = await pool.query(`
SELECT
order_number as id,
customer,
date,
SUM(price * quantity - COALESCE(discount, 0)) as amount
FROM orders
WHERE canceled = false
GROUP BY order_number, customer, date
ORDER BY date DESC
o1.order_number as order_id,
o1.customer as customer_name,
SUM(o2.price * o2.quantity) as total_amount,
o1.date as order_date
FROM orders o1
JOIN orders o2 ON o1.order_number = o2.order_number
WHERE o1.canceled = false
GROUP BY o1.order_number, o1.customer, o1.date
ORDER BY o1.date DESC
LIMIT 5
`);
res.json(rows);
res.json(rows.map(row => ({
...row,
total_amount: parseFloat(row.total_amount || 0),
order_date: row.order_date
})));
} catch (error) {
console.error('Error fetching recent orders:', error);
res.status(500).json({ error: 'Failed to fetch recent orders' });
}
});
// Get category statistics
// Get category stats
router.get('/category-stats', async (req, res) => {
const pool = req.app.locals.pool;
try {
const [rows] = await pool.query(`
SELECT
COALESCE(NULLIF(categories, ''), 'Uncategorized') as category,
categories,
COUNT(*) as count
FROM products
WHERE visible = true
@@ -94,11 +99,10 @@ router.get('/category-stats', async (req, res) => {
ORDER BY count DESC
LIMIT 10
`);
res.json(rows);
} catch (error) {
console.error('Error fetching category stats:', error);
res.status(500).json({ error: 'Failed to fetch category statistics' });
res.status(500).json({ error: 'Failed to fetch category stats' });
}
});
@@ -109,13 +113,12 @@ router.get('/stock-levels', async (req, res) => {
const [rows] = await pool.query(`
SELECT
SUM(CASE WHEN stock_quantity = 0 THEN 1 ELSE 0 END) as outOfStock,
SUM(CASE WHEN stock_quantity > 0 AND stock_quantity <= 10 THEN 1 ELSE 0 END) as lowStock,
SUM(CASE WHEN stock_quantity > 10 AND stock_quantity <= 50 THEN 1 ELSE 0 END) as inStock,
SUM(CASE WHEN stock_quantity > 50 THEN 1 ELSE 0 END) as overStock
SUM(CASE WHEN stock_quantity > 0 AND stock_quantity <= 5 THEN 1 ELSE 0 END) as lowStock,
SUM(CASE WHEN stock_quantity > 5 AND stock_quantity <= 20 THEN 1 ELSE 0 END) as inStock,
SUM(CASE WHEN stock_quantity > 20 THEN 1 ELSE 0 END) as overStock
FROM products
WHERE visible = true
`);
res.json(rows[0]);
} catch (error) {
console.error('Error fetching stock levels:', error);