This commit is contained in:
Jake Kasper
2025-08-25 09:52:58 -04:00
parent a374fff7e6
commit d9ab650d39
2 changed files with 41 additions and 13 deletions

View File

@@ -568,19 +568,40 @@ router.put('/user/:id', validateParams(idParamSchema), validateRequest(userProdu
// Then add the new settings // Then add the new settings
for (const setting of spreaderSettings) { for (const setting of spreaderSettings) {
await pool.query( // Check if equipment exists and belongs to user if equipmentId is provided
`INSERT INTO product_spreader_settings if (setting.equipmentId) {
(user_product_id, spreader_brand, spreader_model, setting_value, rate_description, notes) const equipmentCheck = await pool.query(
VALUES ($1, $2, $3, $4, $5, $6)`, 'SELECT id FROM user_equipment WHERE id = $1 AND user_id = $2',
[ [setting.equipmentId, req.user.id]
userProductId, );
setting.spreaderBrand,
setting.spreaderModel, if (equipmentCheck.rows.length === 0) {
setting.settingValue, throw new AppError(`Equipment with id ${setting.equipmentId} not found`, 404);
setting.rateDescription, }
setting.notes
] await pool.query(
); `INSERT INTO product_spreader_settings
(user_product_id, equipment_id, setting_value, rate_description, notes)
VALUES ($1, $2, $3, $4, $5)`,
[userProductId, 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)`,
[
userProductId,
setting.spreaderBrand,
setting.spreaderModel,
setting.settingValue,
setting.rateDescription,
setting.notes
]
);
}
} }
} }

View File

@@ -0,0 +1,7 @@
-- Make spreader_brand nullable to support equipment-based spreader settings
-- When equipment_id is used, spreader_brand can be null
ALTER TABLE product_spreader_settings
ALTER COLUMN spreader_brand DROP NOT NULL;
SELECT 'Made spreader_brand nullable for equipment-based settings!' as migration_status;