prudct spreader
This commit is contained in:
@@ -10,12 +10,14 @@ const router = express.Router();
|
||||
const spreaderSettingSchema = Joi.object({
|
||||
productId: 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(),
|
||||
settingValue: Joi.string().max(20).required(),
|
||||
rateDescription: Joi.string().max(200).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({
|
||||
id: Joi.number().integer().positive().required()
|
||||
@@ -72,9 +74,11 @@ router.get('/user-product/:userProductId', validateParams(idParamSchema), async
|
||||
}
|
||||
|
||||
const result = 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
|
||||
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]
|
||||
);
|
||||
|
||||
@@ -83,6 +87,11 @@ router.get('/user-product/:userProductId', validateParams(idParamSchema), async
|
||||
data: {
|
||||
settings: result.rows.map(row => ({
|
||||
id: row.id,
|
||||
equipmentId: row.equipment_id,
|
||||
equipmentName: row.equipment_name,
|
||||
equipmentManufacturer: row.manufacturer,
|
||||
equipmentModel: row.equipment_model,
|
||||
// Legacy fields
|
||||
spreaderBrand: row.spreader_brand,
|
||||
spreaderModel: row.spreader_model,
|
||||
settingValue: row.setting_value,
|
||||
@@ -102,7 +111,7 @@ router.get('/user-product/:userProductId', validateParams(idParamSchema), async
|
||||
// @access Private
|
||||
router.post('/', validateRequest(spreaderSettingSchema), async (req, res, next) => {
|
||||
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 (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(
|
||||
`INSERT INTO product_spreader_settings
|
||||
(product_id, user_product_id, spreader_brand, spreader_model, setting_value, rate_description, notes)
|
||||
VALUES ($1, $2, $3, $4, $5, $6, $7)
|
||||
(product_id, user_product_id, equipment_id, spreader_brand, spreader_model, setting_value, rate_description, notes)
|
||||
VALUES ($1, $2, $3, $4, $5, $6, $7, $8)
|
||||
RETURNING *`,
|
||||
[productId, userProductId, spreaderBrand, spreaderModel, settingValue, rateDescription, notes]
|
||||
[productId, userProductId, equipmentId, spreaderBrand, spreaderModel, settingValue, rateDescription, notes]
|
||||
);
|
||||
|
||||
const setting = result.rows[0];
|
||||
@@ -132,6 +153,7 @@ router.post('/', validateRequest(spreaderSettingSchema), async (req, res, next)
|
||||
data: {
|
||||
setting: {
|
||||
id: setting.id,
|
||||
equipmentId: setting.equipment_id,
|
||||
spreaderBrand: setting.spreader_brand,
|
||||
spreaderModel: setting.spreader_model,
|
||||
settingValue: setting.setting_value,
|
||||
|
||||
Reference in New Issue
Block a user