prudct spreader

This commit is contained in:
Jake Kasper
2025-08-25 08:53:13 -04:00
parent 88996a8f5b
commit 080b095042

View File

@@ -10,12 +10,14 @@ const router = express.Router();
const spreaderSettingSchema = Joi.object({ const spreaderSettingSchema = Joi.object({
productId: Joi.number().integer().positive().optional(), productId: Joi.number().integer().positive().optional(),
userProductId: Joi.number().integer().positive().optional(), userProductId: Joi.number().integer().positive().optional(),
spreaderBrand: Joi.string().max(100).required(), equipmentId: Joi.number().integer().positive().optional(), // Link to user_equipment
// Legacy fields for backward compatibility
spreaderBrand: Joi.string().max(100).optional(),
spreaderModel: Joi.string().max(100).allow(null, '').optional(), spreaderModel: Joi.string().max(100).allow(null, '').optional(),
settingValue: Joi.string().max(20).required(), settingValue: Joi.string().max(20).required(),
rateDescription: Joi.string().max(200).allow(null, '').optional(), rateDescription: Joi.string().max(200).allow(null, '').optional(),
notes: Joi.string().allow(null, '').optional() notes: Joi.string().allow(null, '').optional()
}).xor('productId', 'userProductId'); // Must have either productId or userProductId, but not both }).xor('productId', 'userProductId').and('equipmentId', 'settingValue'); // Must have either productId or userProductId, but not both
const idParamSchema = Joi.object({ const idParamSchema = Joi.object({
id: Joi.number().integer().positive().required() id: Joi.number().integer().positive().required()
@@ -72,9 +74,11 @@ router.get('/user-product/:userProductId', validateParams(idParamSchema), async
} }
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 user_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.user_product_id = $1
ORDER BY ue.custom_name NULLS LAST, pss.spreader_brand, pss.spreader_model NULLS LAST, pss.setting_value`,
[userProductId] [userProductId]
); );
@@ -83,6 +87,11 @@ router.get('/user-product/:userProductId', validateParams(idParamSchema), async
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,
@@ -102,7 +111,7 @@ router.get('/user-product/:userProductId', validateParams(idParamSchema), async
// @access Private // @access Private
router.post('/', validateRequest(spreaderSettingSchema), async (req, res, next) => { router.post('/', validateRequest(spreaderSettingSchema), async (req, res, next) => {
try { try {
const { productId, userProductId, spreaderBrand, spreaderModel, settingValue, rateDescription, notes } = req.body; const { productId, userProductId, equipmentId, spreaderBrand, spreaderModel, settingValue, rateDescription, notes } = req.body;
// If it's a user product, verify ownership // If it's a user product, verify ownership
if (userProductId) { if (userProductId) {
@@ -116,12 +125,24 @@ router.post('/', validateRequest(spreaderSettingSchema), async (req, res, next)
} }
} }
// If equipment ID is provided, verify it belongs to the user
if (equipmentId) {
const equipmentCheck = await pool.query(
'SELECT id FROM user_equipment WHERE id = $1 AND user_id = $2',
[equipmentId, req.user.id]
);
if (equipmentCheck.rows.length === 0) {
throw new AppError('Equipment not found', 404);
}
}
const result = await pool.query( const result = await pool.query(
`INSERT INTO product_spreader_settings `INSERT INTO product_spreader_settings
(product_id, user_product_id, spreader_brand, spreader_model, setting_value, rate_description, notes) (product_id, user_product_id, equipment_id, spreader_brand, spreader_model, setting_value, rate_description, notes)
VALUES ($1, $2, $3, $4, $5, $6, $7) VALUES ($1, $2, $3, $4, $5, $6, $7, $8)
RETURNING *`, RETURNING *`,
[productId, userProductId, spreaderBrand, spreaderModel, settingValue, rateDescription, notes] [productId, userProductId, equipmentId, spreaderBrand, spreaderModel, settingValue, rateDescription, notes]
); );
const setting = result.rows[0]; const setting = result.rows[0];
@@ -132,6 +153,7 @@ router.post('/', validateRequest(spreaderSettingSchema), async (req, res, next)
data: { data: {
setting: { setting: {
id: setting.id, id: setting.id,
equipmentId: setting.equipment_id,
spreaderBrand: setting.spreader_brand, spreaderBrand: setting.spreader_brand,
spreaderModel: setting.spreader_model, spreaderModel: setting.spreader_model,
settingValue: setting.setting_value, settingValue: setting.setting_value,