linking stuff
This commit is contained in:
@@ -351,13 +351,34 @@ router.post('/user', validateRequest(userProductSchema), async (req, res, next)
|
||||
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]
|
||||
);
|
||||
// Check if equipment exists and belongs to user if equipmentId is provided
|
||||
if (setting.equipmentId) {
|
||||
const equipmentCheck = await pool.query(
|
||||
'SELECT id FROM user_equipment WHERE id = $1 AND user_id = $2',
|
||||
[setting.equipmentId, req.user.id]
|
||||
);
|
||||
|
||||
if (equipmentCheck.rows.length === 0) {
|
||||
throw new AppError(`Equipment with id ${setting.equipmentId} not found`, 404);
|
||||
}
|
||||
|
||||
await pool.query(
|
||||
`INSERT INTO product_spreader_settings
|
||||
(user_product_id, equipment_id, setting_value, rate_description, notes)
|
||||
VALUES ($1, $2, $3, $4, $5)`,
|
||||
[userProduct.id, setting.equipmentId, setting.settingValue,
|
||||
setting.rateDescription, setting.notes]
|
||||
);
|
||||
} else {
|
||||
// Fall back to legacy brand/model approach
|
||||
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]
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -412,17 +433,27 @@ router.get('/user/:id', validateParams(idParamSchema), async (req, res, next) =>
|
||||
|
||||
const userProduct = result.rows[0];
|
||||
|
||||
// Get spreader settings for this user product
|
||||
// Get spreader settings for this user product with equipment details
|
||||
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`,
|
||||
`SELECT pss.*, ue.custom_name as equipment_name, ue.manufacturer, ue.model as equipment_model,
|
||||
ue.spreader_type, ue.capacity_lbs
|
||||
FROM product_spreader_settings pss
|
||||
LEFT JOIN user_equipment ue ON pss.equipment_id = ue.id
|
||||
WHERE pss.user_product_id = $1
|
||||
ORDER BY ue.custom_name NULLS LAST, pss.spreader_brand, pss.spreader_model NULLS LAST, pss.setting_value`,
|
||||
[userProductId]
|
||||
);
|
||||
|
||||
spreaderSettings = settingsResult.rows.map(row => ({
|
||||
id: row.id,
|
||||
equipmentId: row.equipment_id,
|
||||
equipmentName: row.equipment_name,
|
||||
equipmentManufacturer: row.manufacturer,
|
||||
equipmentModel: row.equipment_model,
|
||||
equipmentType: row.spreader_type,
|
||||
equipmentCapacity: row.capacity_lbs ? parseFloat(row.capacity_lbs) : null,
|
||||
// Legacy fields
|
||||
spreaderBrand: row.spreader_brand,
|
||||
spreaderModel: row.spreader_model,
|
||||
settingValue: row.setting_value,
|
||||
|
||||
Reference in New Issue
Block a user