add new back
This commit is contained in:
@@ -3,6 +3,17 @@ const router = express.Router();
|
||||
const { verifyToken } = require('../middleware/auth');
|
||||
const Product = require('../models/Product');
|
||||
|
||||
// Функция для логирования с проверкой DEV переменной
|
||||
const log = (message, data = '') => {
|
||||
if (process.env.DEV === 'true') {
|
||||
if (data) {
|
||||
console.log(message, data);
|
||||
} else {
|
||||
console.log(message);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
// Helper to transform _id to id
|
||||
const transformProduct = (doc) => {
|
||||
if (!doc) return null;
|
||||
@@ -19,13 +30,13 @@ router.get('/', verifyToken, async (req, res) => {
|
||||
try {
|
||||
const companyId = req.user.companyId;
|
||||
|
||||
console.log('[Products] GET Fetching products for companyId:', companyId);
|
||||
log('[Products] GET Fetching products for companyId:', companyId);
|
||||
|
||||
const products = await Product.find({ companyId })
|
||||
.sort({ createdAt: -1 })
|
||||
.exec();
|
||||
|
||||
console.log('[Products] Found', products.length, 'products');
|
||||
log('[Products] Found', products.length, 'products');
|
||||
res.json(products.map(transformProduct));
|
||||
} catch (error) {
|
||||
console.error('[Products] Get error:', error.message);
|
||||
@@ -35,20 +46,20 @@ router.get('/', verifyToken, async (req, res) => {
|
||||
|
||||
// POST /products - Создать продукт/услугу
|
||||
router.post('/', verifyToken, async (req, res) => {
|
||||
try {
|
||||
// try {
|
||||
const { name, category, description, type, productUrl, price, unit, minOrder } = req.body;
|
||||
const companyId = req.user.companyId;
|
||||
|
||||
console.log('[Products] POST Creating product:', { name, category, type });
|
||||
log('[Products] POST Creating product:', { name, category, type });
|
||||
|
||||
// Валидация
|
||||
if (!name || !category || !description || !type) {
|
||||
return res.status(400).json({ error: 'name, category, description, and type are required' });
|
||||
}
|
||||
// // Валидация
|
||||
// if (!name || !category || !description || !type) {
|
||||
// return res.status(400).json({ error: 'name, category, description, and type are required' });
|
||||
// }
|
||||
|
||||
if (description.length < 20) {
|
||||
return res.status(400).json({ error: 'Description must be at least 20 characters' });
|
||||
}
|
||||
// if (description.length < 20) {
|
||||
// return res.status(400).json({ error: 'Description must be at least 20 characters' });
|
||||
// }
|
||||
|
||||
const newProduct = new Product({
|
||||
name: name.trim(),
|
||||
@@ -63,13 +74,13 @@ router.post('/', verifyToken, async (req, res) => {
|
||||
});
|
||||
|
||||
const savedProduct = await newProduct.save();
|
||||
console.log('[Products] Product created with ID:', savedProduct._id);
|
||||
log('[Products] Product created with ID:', savedProduct._id);
|
||||
|
||||
res.status(201).json(transformProduct(savedProduct));
|
||||
} catch (error) {
|
||||
console.error('[Products] Create error:', error.message);
|
||||
res.status(500).json({ error: 'Internal server error', message: error.message });
|
||||
}
|
||||
// } catch (error) {
|
||||
// console.error('[Products] Create error:', error.message);
|
||||
// res.status(500).json({ error: 'Internal server error', message: error.message });
|
||||
// }
|
||||
});
|
||||
|
||||
// PUT /products/:id - Обновить продукт/услугу
|
||||
@@ -96,7 +107,7 @@ router.put('/:id', verifyToken, async (req, res) => {
|
||||
{ new: true, runValidators: true }
|
||||
);
|
||||
|
||||
console.log('[Products] Product updated:', id);
|
||||
log('[Products] Product updated:', id);
|
||||
res.json(transformProduct(updatedProduct));
|
||||
} catch (error) {
|
||||
console.error('[Products] Update error:', error.message);
|
||||
@@ -127,7 +138,7 @@ router.patch('/:id', verifyToken, async (req, res) => {
|
||||
{ new: true, runValidators: true }
|
||||
);
|
||||
|
||||
console.log('[Products] Product patched:', id);
|
||||
log('[Products] Product patched:', id);
|
||||
res.json(transformProduct(updatedProduct));
|
||||
} catch (error) {
|
||||
console.error('[Products] Patch error:', error.message);
|
||||
@@ -153,7 +164,7 @@ router.delete('/:id', verifyToken, async (req, res) => {
|
||||
|
||||
await Product.findByIdAndDelete(id);
|
||||
|
||||
console.log('[Products] Product deleted:', id);
|
||||
log('[Products] Product deleted:', id);
|
||||
res.json({ message: 'Product deleted successfully' });
|
||||
} catch (error) {
|
||||
console.error('[Products] Delete error:', error.message);
|
||||
|
||||
Reference in New Issue
Block a user