Initial setup, everything broken
This commit is contained in:
137
inventory-server/src/routes/products.js
Normal file
137
inventory-server/src/routes/products.js
Normal file
@@ -0,0 +1,137 @@
|
||||
const express = require('express');
|
||||
const router = express.Router();
|
||||
const { v4: uuidv4 } = require('uuid');
|
||||
const { importProductsFromCSV } = require('../utils/csvImporter');
|
||||
const multer = require('multer');
|
||||
|
||||
// Configure multer for file uploads
|
||||
const upload = multer({ dest: 'uploads/' });
|
||||
|
||||
// Get all products with their current inventory levels
|
||||
router.get('/', async (req, res) => {
|
||||
const pool = req.app.locals.pool;
|
||||
try {
|
||||
const [rows] = await pool.query(`
|
||||
SELECT p.*, il.quantity, il.reorder_point, il.reorder_quantity
|
||||
FROM products p
|
||||
LEFT JOIN inventory_levels il ON p.id = il.product_id
|
||||
ORDER BY p.created_at DESC
|
||||
`);
|
||||
res.json(rows);
|
||||
} catch (error) {
|
||||
console.error('Error fetching products:', error);
|
||||
res.status(500).json({ error: 'Failed to fetch products' });
|
||||
}
|
||||
});
|
||||
|
||||
// Get a single product with its inventory details
|
||||
router.get('/:id', async (req, res) => {
|
||||
const pool = req.app.locals.pool;
|
||||
try {
|
||||
const [rows] = await pool.query(`
|
||||
SELECT p.*, il.quantity, il.reorder_point, il.reorder_quantity
|
||||
FROM products p
|
||||
LEFT JOIN inventory_levels il ON p.id = il.product_id
|
||||
WHERE p.id = ?
|
||||
`, [req.params.id]);
|
||||
|
||||
if (rows.length === 0) {
|
||||
return res.status(404).json({ error: 'Product not found' });
|
||||
}
|
||||
res.json(rows[0]);
|
||||
} catch (error) {
|
||||
console.error('Error fetching product:', error);
|
||||
res.status(500).json({ error: 'Failed to fetch product' });
|
||||
}
|
||||
});
|
||||
|
||||
// Create a new product
|
||||
router.post('/', async (req, res) => {
|
||||
const pool = req.app.locals.pool;
|
||||
const { sku, name, description, category } = req.body;
|
||||
const id = uuidv4();
|
||||
|
||||
try {
|
||||
const connection = await pool.getConnection();
|
||||
try {
|
||||
await connection.beginTransaction();
|
||||
|
||||
await connection.query(
|
||||
'INSERT INTO products (id, sku, name, description, category) VALUES (?, ?, ?, ?, ?)',
|
||||
[id, sku, name, description, category]
|
||||
);
|
||||
|
||||
await connection.query(
|
||||
'INSERT INTO inventory_levels (id, product_id, quantity) VALUES (?, ?, 0)',
|
||||
[uuidv4(), id]
|
||||
);
|
||||
|
||||
await connection.commit();
|
||||
res.status(201).json({ id, sku, name, description, category });
|
||||
} catch (error) {
|
||||
await connection.rollback();
|
||||
throw error;
|
||||
} finally {
|
||||
connection.release();
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Error creating product:', error);
|
||||
res.status(500).json({ error: 'Failed to create product' });
|
||||
}
|
||||
});
|
||||
|
||||
// Import products from CSV
|
||||
router.post('/import', upload.single('file'), async (req, res) => {
|
||||
if (!req.file) {
|
||||
return res.status(400).json({ error: 'No file uploaded' });
|
||||
}
|
||||
|
||||
try {
|
||||
const result = await importProductsFromCSV(req.file.path, req.app.locals.pool);
|
||||
// Clean up the uploaded file
|
||||
require('fs').unlinkSync(req.file.path);
|
||||
res.json(result);
|
||||
} catch (error) {
|
||||
console.error('Error importing products:', error);
|
||||
res.status(500).json({ error: 'Failed to import products' });
|
||||
}
|
||||
});
|
||||
|
||||
// Update product inventory
|
||||
router.post('/:id/inventory', async (req, res) => {
|
||||
const pool = req.app.locals.pool;
|
||||
const { quantity, type, notes } = req.body;
|
||||
|
||||
try {
|
||||
const connection = await pool.getConnection();
|
||||
try {
|
||||
await connection.beginTransaction();
|
||||
|
||||
// Create inventory transaction
|
||||
await connection.query(
|
||||
'INSERT INTO inventory_transactions (id, product_id, transaction_type, quantity, notes) VALUES (?, ?, ?, ?, ?)',
|
||||
[uuidv4(), req.params.id, type, quantity, notes]
|
||||
);
|
||||
|
||||
// Update inventory level
|
||||
const quantityChange = type === 'sale' ? -quantity : quantity;
|
||||
await connection.query(
|
||||
'UPDATE inventory_levels SET quantity = quantity + ? WHERE product_id = ?',
|
||||
[quantityChange, req.params.id]
|
||||
);
|
||||
|
||||
await connection.commit();
|
||||
res.json({ success: true });
|
||||
} catch (error) {
|
||||
await connection.rollback();
|
||||
throw error;
|
||||
} finally {
|
||||
connection.release();
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Error updating inventory:', error);
|
||||
res.status(500).json({ error: 'Failed to update inventory' });
|
||||
}
|
||||
});
|
||||
|
||||
module.exports = router;
|
||||
Reference in New Issue
Block a user