asdfas
This commit is contained in:
@@ -39,9 +39,11 @@ router.get('/product/:productId', validateParams(productIdParamSchema), async (r
|
|||||||
const productId = req.params.productId;
|
const productId = req.params.productId;
|
||||||
|
|
||||||
const result = await pool.query(
|
const result = await pool.query(
|
||||||
`SELECT * FROM product_spreader_settings
|
`SELECT pss.*, ue.custom_name as equipment_name, ue.manufacturer, ue.model as equipment_model
|
||||||
WHERE product_id = $1
|
FROM product_spreader_settings pss
|
||||||
ORDER BY spreader_brand, spreader_model NULLS LAST, setting_value`,
|
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]
|
[productId]
|
||||||
);
|
);
|
||||||
|
|
||||||
@@ -50,6 +52,11 @@ router.get('/product/:productId', validateParams(productIdParamSchema), async (r
|
|||||||
data: {
|
data: {
|
||||||
settings: result.rows.map(row => ({
|
settings: result.rows.map(row => ({
|
||||||
id: row.id,
|
id: row.id,
|
||||||
|
equipmentId: row.equipment_id,
|
||||||
|
equipmentName: row.equipment_name,
|
||||||
|
equipmentManufacturer: row.manufacturer,
|
||||||
|
equipmentModel: row.equipment_model,
|
||||||
|
// Legacy fields
|
||||||
spreaderBrand: row.spreader_brand,
|
spreaderBrand: row.spreader_brand,
|
||||||
spreaderModel: row.spreader_model,
|
spreaderModel: row.spreader_model,
|
||||||
settingValue: row.setting_value,
|
settingValue: row.setting_value,
|
||||||
|
|||||||
@@ -239,6 +239,8 @@ const ApplicationPlanModal = ({
|
|||||||
? `/api/product-spreader-settings/product/${productApiId}`
|
? `/api/product-spreader-settings/product/${productApiId}`
|
||||||
: `/api/product-spreader-settings/user-product/${productApiId}`;
|
: `/api/product-spreader-settings/user-product/${productApiId}`;
|
||||||
|
|
||||||
|
console.log('Checking spreader settings:', { product, equipmentId, endpoint });
|
||||||
|
|
||||||
const response = await fetch(endpoint, {
|
const response = await fetch(endpoint, {
|
||||||
headers: {
|
headers: {
|
||||||
'Authorization': `Bearer ${localStorage.getItem('authToken')}`
|
'Authorization': `Bearer ${localStorage.getItem('authToken')}`
|
||||||
@@ -247,20 +249,33 @@ const ApplicationPlanModal = ({
|
|||||||
|
|
||||||
if (response.ok) {
|
if (response.ok) {
|
||||||
const data = await response.json();
|
const data = await response.json();
|
||||||
|
console.log('Spreader settings response:', data);
|
||||||
const settings = data.data?.settings || [];
|
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) {
|
if (!equipmentSetting) {
|
||||||
// No spreader setting found, prompt user to add one
|
// No spreader setting found, prompt user to add one
|
||||||
|
console.log('No setting found, showing form');
|
||||||
setCurrentProductForSettings(product);
|
setCurrentProductForSettings(product);
|
||||||
setShowSpreaderSettingsForm(true);
|
setShowSpreaderSettingsForm(true);
|
||||||
} else {
|
} else {
|
||||||
// Store the spreader setting for calculations
|
// Store the spreader setting for calculations
|
||||||
|
console.log('Setting found, storing for calculations');
|
||||||
setSpreaderSettings(prev => ({
|
setSpreaderSettings(prev => ({
|
||||||
...prev,
|
...prev,
|
||||||
[`${product.id}_${equipmentId}`]: equipmentSetting
|
[`${product.id}_${equipmentId}`]: equipmentSetting
|
||||||
}));
|
}));
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
console.log('Response not ok:', response.status);
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Failed to check spreader settings:', error);
|
console.error('Failed to check spreader settings:', error);
|
||||||
@@ -300,7 +315,20 @@ const ApplicationPlanModal = ({
|
|||||||
});
|
});
|
||||||
|
|
||||||
if (response.ok) {
|
if (response.ok) {
|
||||||
|
const result = await response.json();
|
||||||
|
console.log('Spreader setting saved successfully:', result);
|
||||||
|
|
||||||
toast.success('Spreader setting added successfully');
|
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);
|
setShowSpreaderSettingsForm(false);
|
||||||
setCurrentProductForSettings(null);
|
setCurrentProductForSettings(null);
|
||||||
setSpreaderFormData({
|
setSpreaderFormData({
|
||||||
@@ -309,7 +337,9 @@ const ApplicationPlanModal = ({
|
|||||||
notes: ''
|
notes: ''
|
||||||
});
|
});
|
||||||
} else {
|
} 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) {
|
} catch (error) {
|
||||||
console.error('Failed to save spreader settings:', error);
|
console.error('Failed to save spreader settings:', error);
|
||||||
|
|||||||
Reference in New Issue
Block a user