linking stuff
This commit is contained in:
@@ -7,6 +7,37 @@ const { calculateApplication } = require('../utils/applicationCalculations');
|
||||
|
||||
const router = express.Router();
|
||||
|
||||
// Helper function to get spreader settings for equipment and product
|
||||
async function getSpreaderSettingsForEquipment(equipmentId, productId, userProductId, userId) {
|
||||
// Get spreader settings for the specific product and equipment combination
|
||||
let query, params;
|
||||
|
||||
if (userProductId) {
|
||||
query = `
|
||||
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.equipment_id = ue.id
|
||||
WHERE pss.user_product_id = $1 AND pss.equipment_id = $2 AND ue.user_id = $3
|
||||
ORDER BY pss.created_at DESC
|
||||
LIMIT 1
|
||||
`;
|
||||
params = [userProductId, equipmentId, userId];
|
||||
} else {
|
||||
query = `
|
||||
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.equipment_id = ue.id
|
||||
WHERE pss.product_id = $1 AND pss.equipment_id = $2 AND ue.user_id = $3
|
||||
ORDER BY pss.created_at DESC
|
||||
LIMIT 1
|
||||
`;
|
||||
params = [productId, equipmentId, userId];
|
||||
}
|
||||
|
||||
const result = await pool.query(query, params);
|
||||
return result.rows[0] || null;
|
||||
}
|
||||
|
||||
// @route GET /api/applications/plans
|
||||
// @desc Get all application plans for current user
|
||||
// @access Private
|
||||
@@ -902,4 +933,67 @@ router.get('/stats', async (req, res, next) => {
|
||||
}
|
||||
});
|
||||
|
||||
// @route GET /api/applications/spreader-settings/:equipmentId/:productId
|
||||
// @desc Get recommended spreader settings for specific equipment and product combination
|
||||
// @access Private
|
||||
router.get('/spreader-settings/:equipmentId/:productId', async (req, res, next) => {
|
||||
try {
|
||||
const { equipmentId, productId } = req.params;
|
||||
const { isUserProduct } = req.query; // Indicates if productId refers to user_products table
|
||||
|
||||
// Verify equipment belongs to user
|
||||
const equipmentCheck = await pool.query(
|
||||
'SELECT id, custom_name, manufacturer, model 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 equipment = equipmentCheck.rows[0];
|
||||
|
||||
// Get spreader settings
|
||||
const spreaderSetting = await getSpreaderSettingsForEquipment(
|
||||
parseInt(equipmentId),
|
||||
isUserProduct === 'true' ? null : parseInt(productId),
|
||||
isUserProduct === 'true' ? parseInt(productId) : null,
|
||||
req.user.id
|
||||
);
|
||||
|
||||
if (!spreaderSetting) {
|
||||
return res.json({
|
||||
success: true,
|
||||
data: {
|
||||
hasSettings: false,
|
||||
message: 'No spreader settings found for this equipment and product combination',
|
||||
equipment: {
|
||||
id: equipment.id,
|
||||
name: equipment.custom_name || `${equipment.manufacturer} ${equipment.model}`.trim()
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
res.json({
|
||||
success: true,
|
||||
data: {
|
||||
hasSettings: true,
|
||||
setting: {
|
||||
id: spreaderSetting.id,
|
||||
settingValue: spreaderSetting.setting_value,
|
||||
rateDescription: spreaderSetting.rate_description,
|
||||
notes: spreaderSetting.notes
|
||||
},
|
||||
equipment: {
|
||||
id: equipment.id,
|
||||
name: equipment.custom_name || `${equipment.manufacturer} ${equipment.model}`.trim()
|
||||
}
|
||||
}
|
||||
});
|
||||
} catch (error) {
|
||||
next(error);
|
||||
}
|
||||
});
|
||||
|
||||
module.exports = router;
|
||||
Reference in New Issue
Block a user