From d9ab650d3966100b3e6fde17224a3c641edbbc2d Mon Sep 17 00:00:00 2001 From: Jake Kasper Date: Mon, 25 Aug 2025 09:52:58 -0400 Subject: [PATCH] asdf --- backend/src/routes/products.js | 47 ++++++++++++++----- .../make_spreader_brand_nullable.sql | 7 +++ 2 files changed, 41 insertions(+), 13 deletions(-) create mode 100644 database/migrations/make_spreader_brand_nullable.sql diff --git a/backend/src/routes/products.js b/backend/src/routes/products.js index 7209ad1..60fb9e1 100644 --- a/backend/src/routes/products.js +++ b/backend/src/routes/products.js @@ -568,19 +568,40 @@ router.put('/user/:id', validateParams(idParamSchema), validateRequest(userProdu // Then add the new settings 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)`, - [ - userProductId, - 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)`, + [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 + ] + ); + } } } diff --git a/database/migrations/make_spreader_brand_nullable.sql b/database/migrations/make_spreader_brand_nullable.sql new file mode 100644 index 0000000..0c94d1a --- /dev/null +++ b/database/migrations/make_spreader_brand_nullable.sql @@ -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; \ No newline at end of file