Change filter sort to client side for vendors/cats
This commit is contained in:
@@ -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) {
|
||||
|
||||
Reference in New Issue
Block a user