Change filter sort to client side for vendors/cats

This commit is contained in:
2025-01-17 00:59:11 -05:00
parent 2235121761
commit 88d1189da0
4 changed files with 41 additions and 180 deletions

View File

@@ -1,66 +1,10 @@
const express = require('express');
const router = express.Router();
// Get categories with pagination, filtering, and sorting
// Get all categories
router.get('/', async (req, res) => {
const pool = req.app.locals.pool;
try {
const page = parseInt(req.query.page) || 1;
const limit = parseInt(req.query.limit) || 50;
const offset = (page - 1) * limit;
const search = req.query.search || '';
const parent = req.query.parent || 'all';
const performance = req.query.performance || 'all';
const sortColumn = req.query.sortColumn || 'name';
const sortDirection = req.query.sortDirection || 'asc';
// Build the WHERE clause based on filters
const whereConditions = [];
const params = [];
if (search) {
whereConditions.push('(LOWER(c.name) LIKE LOWER(?) OR LOWER(c.description) LIKE LOWER(?))');
params.push(`%${search}%`, `%${search}%`);
}
if (parent !== 'all') {
if (parent === 'none') {
whereConditions.push('c.parent_category IS NULL');
} else {
whereConditions.push('c.parent_category = ?');
params.push(parent);
}
}
if (performance !== 'all') {
switch (performance) {
case 'high_growth':
whereConditions.push('cm.growth_rate >= 20');
break;
case 'growing':
whereConditions.push('cm.growth_rate >= 5 AND cm.growth_rate < 20');
break;
case 'stable':
whereConditions.push('cm.growth_rate >= -5 AND cm.growth_rate < 5');
break;
case 'declining':
whereConditions.push('cm.growth_rate < -5');
break;
}
}
const whereClause = whereConditions.length > 0
? 'WHERE ' + whereConditions.join(' AND ')
: '';
// Get total count for pagination
const [countResult] = await pool.query(`
SELECT COUNT(DISTINCT c.id) as total
FROM categories c
LEFT JOIN category_metrics cm ON c.id = cm.category_id
${whereClause}
`, params);
// Get parent categories for filter dropdown
const [parentCategories] = await pool.query(`
SELECT DISTINCT parent_category
@@ -69,7 +13,7 @@ router.get('/', async (req, res) => {
ORDER BY parent_category
`);
// Get categories with metrics
// Get all categories with metrics
const [categories] = await pool.query(`
SELECT
c.id as category_id,
@@ -84,10 +28,8 @@ router.get('/', async (req, res) => {
cm.status
FROM categories c
LEFT JOIN category_metrics cm ON c.id = cm.category_id
${whereClause}
ORDER BY ${sortColumn} ${sortDirection}
LIMIT ? OFFSET ?
`, [...params, limit, offset]);
ORDER BY c.name ASC
`);
// Get overall stats
const [stats] = await pool.query(`
@@ -116,11 +58,6 @@ router.get('/', async (req, res) => {
totalValue: parseFloat(stats[0].totalValue || 0),
avgMargin: parseFloat(stats[0].avgMargin || 0),
avgGrowth: parseFloat(stats[0].avgGrowth || 0)
},
pagination: {
total: countResult[0].total,
pages: Math.ceil(countResult[0].total / limit),
current: page,
}
});
} catch (error) {

View File

@@ -5,63 +5,7 @@ const router = express.Router();
router.get('/', async (req, res) => {
const pool = req.app.locals.pool;
try {
const page = parseInt(req.query.page) || 1;
const limit = parseInt(req.query.limit) || 50;
const offset = (page - 1) * limit;
const search = req.query.search || '';
const status = req.query.status || 'all';
const performance = req.query.performance || 'all';
const sortColumn = req.query.sortColumn || 'name';
const sortDirection = req.query.sortDirection || 'asc';
// Build the WHERE clause based on filters
const whereConditions = ['p.vendor IS NOT NULL AND p.vendor != \'\''];
const params = [];
if (search) {
whereConditions.push('LOWER(p.vendor) LIKE LOWER(?)');
params.push(`%${search}%`);
}
if (status !== 'all') {
whereConditions.push(`
CASE
WHEN COALESCE(vm.total_orders, 0) > 0 AND COALESCE(vm.order_fill_rate, 0) >= 75 THEN 'active'
WHEN COALESCE(vm.total_orders, 0) > 0 THEN 'inactive'
ELSE 'pending'
END = ?
`);
params.push(status);
}
if (performance !== 'all') {
switch (performance) {
case 'excellent':
whereConditions.push('COALESCE(vm.order_fill_rate, 0) >= 95');
break;
case 'good':
whereConditions.push('COALESCE(vm.order_fill_rate, 0) >= 85 AND COALESCE(vm.order_fill_rate, 0) < 95');
break;
case 'fair':
whereConditions.push('COALESCE(vm.order_fill_rate, 0) >= 75 AND COALESCE(vm.order_fill_rate, 0) < 85');
break;
case 'poor':
whereConditions.push('COALESCE(vm.order_fill_rate, 0) < 75');
break;
}
}
const whereClause = 'WHERE ' + whereConditions.join(' AND ');
// Get total count for pagination
const [countResult] = await pool.query(`
SELECT COUNT(DISTINCT p.vendor) as total
FROM products p
LEFT JOIN vendor_metrics vm ON p.vendor = vm.vendor
${whereClause}
`, params);
// Get vendors with metrics
// Get all vendors with metrics
const [vendors] = await pool.query(`
SELECT DISTINCT
p.vendor as name,
@@ -77,13 +21,10 @@ router.get('/', async (req, res) => {
END as status
FROM products p
LEFT JOIN vendor_metrics vm ON p.vendor = vm.vendor
${whereClause}
AND p.vendor IS NOT NULL AND p.vendor != ''
ORDER BY ${sortColumn} ${sortDirection}
LIMIT ? OFFSET ?
`, [...params, limit, offset]);
WHERE p.vendor IS NOT NULL AND p.vendor != ''
`);
// Get cost metrics for these vendors
// Get cost metrics for all vendors
const vendorNames = vendors.map(v => v.name);
const [costMetrics] = await pool.query(`
SELECT
@@ -156,12 +97,6 @@ router.get('/', async (req, res) => {
avgOnTimeDelivery: parseFloat(stats[0].avgOnTimeDelivery || 0),
avgUnitCost: parseFloat(overallCostMetrics[0].avg_unit_cost || 0),
totalSpend: parseFloat(overallCostMetrics[0].total_spend || 0)
},
pagination: {
total: parseInt(countResult[0].total || 0),
currentPage: page,
pages: Math.ceil(parseInt(countResult[0].total || 0) / limit),
limit
}
});
} catch (error) {