spreader stuff
This commit is contained in:
@@ -314,7 +314,8 @@ router.post('/user', validateRequest(userProductSchema), async (req, res, next)
|
|||||||
categoryId,
|
categoryId,
|
||||||
productType,
|
productType,
|
||||||
activeIngredients,
|
activeIngredients,
|
||||||
description
|
description,
|
||||||
|
spreaderSettings
|
||||||
} = req.body;
|
} = req.body;
|
||||||
|
|
||||||
// If based on existing product, verify it exists
|
// If based on existing product, verify it exists
|
||||||
@@ -346,6 +347,20 @@ router.post('/user', validateRequest(userProductSchema), async (req, res, next)
|
|||||||
|
|
||||||
const userProduct = result.rows[0];
|
const userProduct = result.rows[0];
|
||||||
|
|
||||||
|
// Handle spreader settings for granular products
|
||||||
|
if (spreaderSettings && Array.isArray(spreaderSettings) && productType === 'granular') {
|
||||||
|
// Add spreader settings for this user product
|
||||||
|
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)`,
|
||||||
|
[userProduct.id, setting.spreaderBrand, setting.spreaderModel, setting.settingValue,
|
||||||
|
setting.rateDescription, setting.notes]
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
res.status(201).json({
|
res.status(201).json({
|
||||||
success: true,
|
success: true,
|
||||||
message: 'Custom product created successfully',
|
message: 'Custom product created successfully',
|
||||||
@@ -380,11 +395,13 @@ router.get('/user/:id', validateParams(idParamSchema), async (req, res, next) =>
|
|||||||
const userProductId = req.params.id;
|
const userProductId = req.params.id;
|
||||||
|
|
||||||
const result = await pool.query(
|
const result = await pool.query(
|
||||||
`SELECT up.*, p.name as base_product_name, p.brand, p.product_type,
|
`SELECT up.*, p.name as base_product_name, p.brand as base_brand, p.product_type as base_product_type,
|
||||||
p.active_ingredients, pc.name as category_name
|
p.active_ingredients as base_active_ingredients, pc.name as base_category_name,
|
||||||
|
upc.name as custom_category_name
|
||||||
FROM user_products up
|
FROM user_products up
|
||||||
LEFT JOIN products p ON up.product_id = p.id
|
LEFT JOIN products p ON up.product_id = p.id
|
||||||
LEFT JOIN product_categories pc ON p.category_id = pc.id
|
LEFT JOIN product_categories pc ON p.category_id = pc.id
|
||||||
|
LEFT JOIN product_categories upc ON up.category_id = upc.id
|
||||||
WHERE up.id = $1 AND up.user_id = $2`,
|
WHERE up.id = $1 AND up.user_id = $2`,
|
||||||
[userProductId, req.user.id]
|
[userProductId, req.user.id]
|
||||||
);
|
);
|
||||||
@@ -395,6 +412,25 @@ router.get('/user/:id', validateParams(idParamSchema), async (req, res, next) =>
|
|||||||
|
|
||||||
const userProduct = result.rows[0];
|
const userProduct = result.rows[0];
|
||||||
|
|
||||||
|
// Get spreader settings for this user product
|
||||||
|
let spreaderSettings = [];
|
||||||
|
const settingsResult = await pool.query(
|
||||||
|
`SELECT * FROM product_spreader_settings
|
||||||
|
WHERE user_product_id = $1
|
||||||
|
ORDER BY spreader_brand, spreader_model NULLS LAST, setting_value`,
|
||||||
|
[userProductId]
|
||||||
|
);
|
||||||
|
|
||||||
|
spreaderSettings = settingsResult.rows.map(row => ({
|
||||||
|
id: row.id,
|
||||||
|
spreaderBrand: row.spreader_brand,
|
||||||
|
spreaderModel: row.spreader_model,
|
||||||
|
settingValue: row.setting_value,
|
||||||
|
rateDescription: row.rate_description,
|
||||||
|
notes: row.notes,
|
||||||
|
createdAt: row.created_at
|
||||||
|
}));
|
||||||
|
|
||||||
res.json({
|
res.json({
|
||||||
success: true,
|
success: true,
|
||||||
data: {
|
data: {
|
||||||
@@ -403,14 +439,18 @@ router.get('/user/:id', validateParams(idParamSchema), async (req, res, next) =>
|
|||||||
baseProductId: userProduct.product_id,
|
baseProductId: userProduct.product_id,
|
||||||
baseProductName: userProduct.base_product_name,
|
baseProductName: userProduct.base_product_name,
|
||||||
customName: userProduct.custom_name,
|
customName: userProduct.custom_name,
|
||||||
brand: userProduct.brand,
|
brand: userProduct.custom_brand || userProduct.base_brand,
|
||||||
categoryName: userProduct.category_name,
|
categoryName: userProduct.custom_category_name || userProduct.base_category_name,
|
||||||
productType: userProduct.product_type,
|
categoryId: userProduct.category_id || (userProduct.product_id ? userProduct.category_id : null),
|
||||||
activeIngredients: userProduct.active_ingredients,
|
productType: userProduct.custom_product_type || userProduct.base_product_type,
|
||||||
customRateAmount: parseFloat(userProduct.custom_rate_amount),
|
activeIngredients: userProduct.custom_active_ingredients || userProduct.base_active_ingredients,
|
||||||
|
description: userProduct.custom_description,
|
||||||
|
customRateAmount: userProduct.custom_rate_amount ? parseFloat(userProduct.custom_rate_amount) : null,
|
||||||
customRateUnit: userProduct.custom_rate_unit,
|
customRateUnit: userProduct.custom_rate_unit,
|
||||||
notes: userProduct.notes,
|
notes: userProduct.notes,
|
||||||
createdAt: userProduct.created_at
|
spreaderSettings: spreaderSettings,
|
||||||
|
createdAt: userProduct.created_at,
|
||||||
|
updatedAt: userProduct.updated_at
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user