From e22cc10310a355bbb4c899333234965bdf6e95b6 Mon Sep 17 00:00:00 2001 From: Jake Kasper Date: Thu, 28 Aug 2025 08:04:12 -0500 Subject: [PATCH] asdfas --- backend/src/routes/productSpreaderSettings.js | 13 +++++-- .../Applications/ApplicationPlanModal.js | 34 +++++++++++++++++-- 2 files changed, 42 insertions(+), 5 deletions(-) diff --git a/backend/src/routes/productSpreaderSettings.js b/backend/src/routes/productSpreaderSettings.js index e63c07f..2d949d5 100644 --- a/backend/src/routes/productSpreaderSettings.js +++ b/backend/src/routes/productSpreaderSettings.js @@ -39,9 +39,11 @@ router.get('/product/:productId', validateParams(productIdParamSchema), async (r const productId = req.params.productId; const result = await pool.query( - `SELECT * FROM product_spreader_settings - WHERE product_id = $1 - ORDER BY spreader_brand, spreader_model NULLS LAST, setting_value`, + `SELECT pss.*, ue.custom_name as equipment_name, ue.manufacturer, ue.model as equipment_model + FROM product_spreader_settings pss + LEFT JOIN user_equipment ue ON pss.equipment_id = ue.id + WHERE pss.product_id = $1 + ORDER BY ue.custom_name NULLS LAST, pss.spreader_brand, pss.spreader_model NULLS LAST, pss.setting_value`, [productId] ); @@ -50,6 +52,11 @@ router.get('/product/:productId', validateParams(productIdParamSchema), async (r data: { settings: result.rows.map(row => ({ id: row.id, + equipmentId: row.equipment_id, + equipmentName: row.equipment_name, + equipmentManufacturer: row.manufacturer, + equipmentModel: row.equipment_model, + // Legacy fields spreaderBrand: row.spreader_brand, spreaderModel: row.spreader_model, settingValue: row.setting_value, diff --git a/frontend/src/components/Applications/ApplicationPlanModal.js b/frontend/src/components/Applications/ApplicationPlanModal.js index f1a3ca6..e742cf9 100644 --- a/frontend/src/components/Applications/ApplicationPlanModal.js +++ b/frontend/src/components/Applications/ApplicationPlanModal.js @@ -239,6 +239,8 @@ const ApplicationPlanModal = ({ ? `/api/product-spreader-settings/product/${productApiId}` : `/api/product-spreader-settings/user-product/${productApiId}`; + console.log('Checking spreader settings:', { product, equipmentId, endpoint }); + const response = await fetch(endpoint, { headers: { 'Authorization': `Bearer ${localStorage.getItem('authToken')}` @@ -247,20 +249,33 @@ const ApplicationPlanModal = ({ if (response.ok) { const data = await response.json(); + console.log('Spreader settings response:', data); const settings = data.data?.settings || []; - const equipmentSetting = settings.find(s => s.equipmentId === parseInt(equipmentId)); + + // For shared products, check if any setting has the matching equipmentId + // For user products, the query already filters by equipment + const equipmentSetting = settings.find(s => { + console.log('Comparing setting:', s, 'with equipmentId:', equipmentId); + return s.equipmentId === parseInt(equipmentId); + }); + + console.log('Found equipment setting:', equipmentSetting); if (!equipmentSetting) { // No spreader setting found, prompt user to add one + console.log('No setting found, showing form'); setCurrentProductForSettings(product); setShowSpreaderSettingsForm(true); } else { // Store the spreader setting for calculations + console.log('Setting found, storing for calculations'); setSpreaderSettings(prev => ({ ...prev, [`${product.id}_${equipmentId}`]: equipmentSetting })); } + } else { + console.log('Response not ok:', response.status); } } catch (error) { console.error('Failed to check spreader settings:', error); @@ -300,7 +315,20 @@ const ApplicationPlanModal = ({ }); if (response.ok) { + const result = await response.json(); + console.log('Spreader setting saved successfully:', result); + toast.success('Spreader setting added successfully'); + + // Store the new setting for calculations + if (result.data?.setting) { + setSpreaderSettings(prev => ({ + ...prev, + [`${currentProductForSettings.id}_${selectedEquipmentId}`]: result.data.setting + })); + } + + // Close the form setShowSpreaderSettingsForm(false); setCurrentProductForSettings(null); setSpreaderFormData({ @@ -309,7 +337,9 @@ const ApplicationPlanModal = ({ notes: '' }); } else { - throw new Error('Failed to save spreader settings'); + const errorData = await response.json(); + console.error('Failed to save spreader settings:', errorData); + throw new Error(errorData.message || 'Failed to save spreader settings'); } } catch (error) { console.error('Failed to save spreader settings:', error);