diff --git a/backend/src/routes/products.js b/backend/src/routes/products.js index 8b189a7..9b5b61a 100644 --- a/backend/src/routes/products.js +++ b/backend/src/routes/products.js @@ -314,7 +314,8 @@ router.post('/user', validateRequest(userProductSchema), async (req, res, next) categoryId, productType, activeIngredients, - description + description, + spreaderSettings } = req.body; // If based on existing product, verify it exists @@ -346,6 +347,20 @@ router.post('/user', validateRequest(userProductSchema), async (req, res, next) const userProduct = result.rows[0]; + // Handle spreader settings for granular products + if (spreaderSettings && Array.isArray(spreaderSettings) && productType === 'granular') { + // Add spreader settings for this user product + for (const setting of spreaderSettings) { + await pool.query( + `INSERT INTO product_spreader_settings + (user_product_id, spreader_brand, spreader_model, setting_value, rate_description, notes) + VALUES ($1, $2, $3, $4, $5, $6)`, + [userProduct.id, setting.spreaderBrand, setting.spreaderModel, setting.settingValue, + setting.rateDescription, setting.notes] + ); + } + } + res.status(201).json({ success: true, message: 'Custom product created successfully', @@ -380,11 +395,13 @@ router.get('/user/:id', validateParams(idParamSchema), async (req, res, next) => const userProductId = req.params.id; const result = await pool.query( - `SELECT up.*, p.name as base_product_name, p.brand, p.product_type, - p.active_ingredients, pc.name as category_name + `SELECT up.*, p.name as base_product_name, p.brand as base_brand, p.product_type as base_product_type, + p.active_ingredients as base_active_ingredients, pc.name as base_category_name, + upc.name as custom_category_name FROM user_products up LEFT JOIN products p ON up.product_id = p.id LEFT JOIN product_categories pc ON p.category_id = pc.id + LEFT JOIN product_categories upc ON up.category_id = upc.id WHERE up.id = $1 AND up.user_id = $2`, [userProductId, req.user.id] ); @@ -395,6 +412,25 @@ router.get('/user/:id', validateParams(idParamSchema), async (req, res, next) => const userProduct = result.rows[0]; + // Get spreader settings for this user product + let spreaderSettings = []; + const settingsResult = await pool.query( + `SELECT * FROM product_spreader_settings + WHERE user_product_id = $1 + ORDER BY spreader_brand, spreader_model NULLS LAST, setting_value`, + [userProductId] + ); + + spreaderSettings = settingsResult.rows.map(row => ({ + id: row.id, + spreaderBrand: row.spreader_brand, + spreaderModel: row.spreader_model, + settingValue: row.setting_value, + rateDescription: row.rate_description, + notes: row.notes, + createdAt: row.created_at + })); + res.json({ success: true, data: { @@ -403,14 +439,18 @@ router.get('/user/:id', validateParams(idParamSchema), async (req, res, next) => baseProductId: userProduct.product_id, baseProductName: userProduct.base_product_name, customName: userProduct.custom_name, - brand: userProduct.brand, - categoryName: userProduct.category_name, - productType: userProduct.product_type, - activeIngredients: userProduct.active_ingredients, - customRateAmount: parseFloat(userProduct.custom_rate_amount), + brand: userProduct.custom_brand || userProduct.base_brand, + categoryName: userProduct.custom_category_name || userProduct.base_category_name, + categoryId: userProduct.category_id || (userProduct.product_id ? userProduct.category_id : null), + productType: userProduct.custom_product_type || userProduct.base_product_type, + activeIngredients: userProduct.custom_active_ingredients || userProduct.base_active_ingredients, + description: userProduct.custom_description, + customRateAmount: userProduct.custom_rate_amount ? parseFloat(userProduct.custom_rate_amount) : null, customRateUnit: userProduct.custom_rate_unit, notes: userProduct.notes, - createdAt: userProduct.created_at + spreaderSettings: spreaderSettings, + createdAt: userProduct.created_at, + updatedAt: userProduct.updated_at } } });