admin
This commit is contained in:
@@ -859,12 +859,11 @@ router.get('/products/user/:id/spreader-settings', validateParams(idParamSchema)
|
||||
const userProductId = req.params.id;
|
||||
|
||||
const result = await pool.query(`
|
||||
SELECT pss.*, ue.custom_name as equipment_name, ss.brand_name, ss.model, ss.setting_number
|
||||
SELECT pss.*, ue.custom_name as equipment_name, ue.manufacturer, ue.model as equipment_model
|
||||
FROM product_spreader_settings pss
|
||||
JOIN user_equipment ue ON pss.user_equipment_id = ue.id
|
||||
LEFT JOIN spreader_settings ss ON pss.spreader_setting_id = ss.id
|
||||
LEFT JOIN user_equipment ue ON pss.equipment_id = ue.id
|
||||
WHERE pss.user_product_id = $1
|
||||
ORDER BY ue.custom_name, ss.brand_name
|
||||
ORDER BY ue.custom_name NULLS LAST, pss.spreader_brand, pss.spreader_model NULLS LAST, pss.setting_value
|
||||
`, [userProductId]);
|
||||
|
||||
res.json({
|
||||
@@ -873,14 +872,14 @@ router.get('/products/user/:id/spreader-settings', validateParams(idParamSchema)
|
||||
spreaderSettings: result.rows.map(setting => ({
|
||||
id: setting.id,
|
||||
userProductId: setting.user_product_id,
|
||||
userEquipmentId: setting.user_equipment_id,
|
||||
equipmentId: setting.equipment_id,
|
||||
equipmentName: setting.equipment_name,
|
||||
spreaderSettingId: setting.spreader_setting_id,
|
||||
brandName: setting.brand_name,
|
||||
model: setting.model,
|
||||
settingNumber: setting.setting_number,
|
||||
customRate: setting.custom_rate ? parseFloat(setting.custom_rate) : null,
|
||||
customRateUnit: setting.custom_rate_unit,
|
||||
equipmentManufacturer: setting.manufacturer,
|
||||
equipmentModel: setting.equipment_model,
|
||||
spreaderBrand: setting.spreader_brand,
|
||||
spreaderModel: setting.spreader_model,
|
||||
settingValue: setting.setting_value,
|
||||
rateDescription: setting.rate_description,
|
||||
notes: setting.notes,
|
||||
createdAt: setting.created_at
|
||||
}))
|
||||
@@ -891,4 +890,81 @@ router.get('/products/user/:id/spreader-settings', validateParams(idParamSchema)
|
||||
}
|
||||
});
|
||||
|
||||
// @route POST /api/admin/products/user/:id/spreader-settings
|
||||
// @desc Add spreader setting for a user product
|
||||
// @access Private (Admin)
|
||||
router.post('/products/user/:id/spreader-settings', validateParams(idParamSchema), async (req, res, next) => {
|
||||
try {
|
||||
const userProductId = req.params.id;
|
||||
const { equipmentId, spreaderBrand, spreaderModel, settingValue, rateDescription, notes } = req.body;
|
||||
|
||||
// Verify the user product exists
|
||||
const productCheck = await pool.query(
|
||||
'SELECT id FROM user_products WHERE id = $1',
|
||||
[userProductId]
|
||||
);
|
||||
|
||||
if (productCheck.rows.length === 0) {
|
||||
throw new AppError('User product not found', 404);
|
||||
}
|
||||
|
||||
const result = await pool.query(`
|
||||
INSERT INTO product_spreader_settings
|
||||
(user_product_id, equipment_id, spreader_brand, spreader_model, setting_value, rate_description, notes)
|
||||
VALUES ($1, $2, $3, $4, $5, $6, $7)
|
||||
RETURNING *
|
||||
`, [userProductId, equipmentId, spreaderBrand, spreaderModel, settingValue, rateDescription, notes]);
|
||||
|
||||
const setting = result.rows[0];
|
||||
|
||||
res.status(201).json({
|
||||
success: true,
|
||||
message: 'Spreader setting added successfully',
|
||||
data: {
|
||||
spreaderSetting: {
|
||||
id: setting.id,
|
||||
userProductId: setting.user_product_id,
|
||||
equipmentId: setting.equipment_id,
|
||||
spreaderBrand: setting.spreader_brand,
|
||||
spreaderModel: setting.spreader_model,
|
||||
settingValue: setting.setting_value,
|
||||
rateDescription: setting.rate_description,
|
||||
notes: setting.notes,
|
||||
createdAt: setting.created_at
|
||||
}
|
||||
}
|
||||
});
|
||||
} catch (error) {
|
||||
next(error);
|
||||
}
|
||||
});
|
||||
|
||||
// @route DELETE /api/admin/products/user/spreader-settings/:id
|
||||
// @desc Delete spreader setting
|
||||
// @access Private (Admin)
|
||||
router.delete('/products/user/spreader-settings/:id', validateParams(idParamSchema), async (req, res, next) => {
|
||||
try {
|
||||
const settingId = req.params.id;
|
||||
|
||||
// Check if setting exists
|
||||
const settingCheck = await pool.query(
|
||||
'SELECT id FROM product_spreader_settings WHERE id = $1',
|
||||
[settingId]
|
||||
);
|
||||
|
||||
if (settingCheck.rows.length === 0) {
|
||||
throw new AppError('Spreader setting not found', 404);
|
||||
}
|
||||
|
||||
await pool.query('DELETE FROM product_spreader_settings WHERE id = $1', [settingId]);
|
||||
|
||||
res.json({
|
||||
success: true,
|
||||
message: 'Spreader setting deleted successfully'
|
||||
});
|
||||
} catch (error) {
|
||||
next(error);
|
||||
}
|
||||
});
|
||||
|
||||
module.exports = router;
|
||||
Reference in New Issue
Block a user