Update schemas with new fields, create initial import from prod script

This commit is contained in:
2025-01-25 09:45:50 -05:00
parent 31e1568868
commit 353ae4540e
4 changed files with 879 additions and 15 deletions

View File

@@ -7,10 +7,11 @@ router.get('/', async (req, res) => {
try {
// Get parent categories for filter dropdown
const [parentCategories] = await pool.query(`
SELECT DISTINCT parent_category
FROM categories
WHERE parent_category IS NOT NULL
ORDER BY parent_category
SELECT DISTINCT c2.name as parent_name
FROM categories c1
JOIN categories c2 ON c1.parent_id = c2.id
WHERE c1.parent_id IS NOT NULL
ORDER BY c2.name
`);
// Get all categories with metrics
@@ -19,7 +20,7 @@ router.get('/', async (req, res) => {
c.id as category_id,
c.name,
c.description,
c.parent_category,
COALESCE(p.name, '') as parent_name,
cm.product_count,
cm.total_value,
cm.avg_margin,
@@ -27,6 +28,7 @@ router.get('/', async (req, res) => {
cm.growth_rate,
cm.status
FROM categories c
LEFT JOIN categories p ON c.parent_id = p.id
LEFT JOIN category_metrics cm ON c.id = cm.category_id
ORDER BY c.name ASC
`);
@@ -46,13 +48,14 @@ router.get('/', async (req, res) => {
res.json({
categories: categories.map(cat => ({
...cat,
parent_category: cat.parent_name, // Map parent_name to parent_category for frontend compatibility
product_count: parseInt(cat.product_count || 0),
total_value: parseFloat(cat.total_value || 0),
avg_margin: parseFloat(cat.avg_margin || 0),
turnover_rate: parseFloat(cat.turnover_rate || 0),
growth_rate: parseFloat(cat.growth_rate || 0)
})),
parentCategories: parentCategories.map(p => p.parent_category),
parentCategories: parentCategories.map(p => p.parent_name),
stats: {
...stats[0],
totalValue: parseFloat(stats[0].totalValue || 0),