asdf
This commit is contained in:
@@ -69,30 +69,49 @@ router.get('/plans', async (req, res, next) => {
|
|||||||
|
|
||||||
const whereClause = whereConditions.join(' AND ');
|
const whereClause = whereConditions.join(' AND ');
|
||||||
|
|
||||||
|
// First get basic plan info with sections
|
||||||
const result = await pool.query(
|
const result = await pool.query(
|
||||||
`SELECT ap.*,
|
`SELECT ap.*,
|
||||||
STRING_AGG(DISTINCT ls.name, ', ') as section_names,
|
STRING_AGG(DISTINCT ls.name, ', ') as section_names,
|
||||||
SUM(ls.area) as total_section_area,
|
SUM(DISTINCT ls.area) as total_section_area,
|
||||||
|
COUNT(DISTINCT aps.lawn_section_id) as section_count,
|
||||||
p.name as property_name, p.address as property_address,
|
p.name as property_name, p.address as property_address,
|
||||||
ue.custom_name as equipment_name, et.name as equipment_type,
|
ue.custom_name as equipment_name, et.name as equipment_type
|
||||||
COUNT(DISTINCT app.id) as product_count,
|
|
||||||
SUM(app.calculated_product_amount) as total_product_amount,
|
|
||||||
MAX(app.calculated_water_amount) as total_water_amount,
|
|
||||||
AVG(app.target_speed_mph) as avg_speed_mph,
|
|
||||||
COUNT(DISTINCT aps.lawn_section_id) as section_count
|
|
||||||
FROM application_plans ap
|
FROM application_plans ap
|
||||||
JOIN application_plan_sections aps ON ap.id = aps.plan_id
|
JOIN application_plan_sections aps ON ap.id = aps.plan_id
|
||||||
JOIN lawn_sections ls ON aps.lawn_section_id = ls.id
|
JOIN lawn_sections ls ON aps.lawn_section_id = ls.id
|
||||||
JOIN properties p ON ls.property_id = p.id
|
JOIN properties p ON ls.property_id = p.id
|
||||||
LEFT JOIN user_equipment ue ON ap.equipment_id = ue.id
|
LEFT JOIN user_equipment ue ON ap.equipment_id = ue.id
|
||||||
LEFT JOIN equipment_types et ON ue.equipment_type_id = et.id
|
LEFT JOIN equipment_types et ON ue.equipment_type_id = et.id
|
||||||
LEFT JOIN application_plan_products app ON ap.id = app.plan_id
|
|
||||||
WHERE ${whereClause}
|
WHERE ${whereClause}
|
||||||
GROUP BY ap.id, p.name, p.address, ue.custom_name, et.name
|
GROUP BY ap.id, p.name, p.address, ue.custom_name, et.name
|
||||||
ORDER BY ap.planned_date DESC, ap.created_at DESC`,
|
ORDER BY ap.planned_date DESC, ap.created_at DESC`,
|
||||||
queryParams
|
queryParams
|
||||||
);
|
);
|
||||||
|
|
||||||
|
// Then get product info separately to avoid duplication
|
||||||
|
const planIds = result.rows.map(row => row.id);
|
||||||
|
let productInfo = {};
|
||||||
|
|
||||||
|
if (planIds.length > 0) {
|
||||||
|
const productResult = await pool.query(
|
||||||
|
`SELECT plan_id,
|
||||||
|
COUNT(id) as product_count,
|
||||||
|
SUM(calculated_product_amount) as total_product_amount,
|
||||||
|
MAX(calculated_water_amount) as total_water_amount,
|
||||||
|
AVG(target_speed_mph) as avg_speed_mph
|
||||||
|
FROM application_plan_products
|
||||||
|
WHERE plan_id = ANY($1)
|
||||||
|
GROUP BY plan_id`,
|
||||||
|
[planIds]
|
||||||
|
);
|
||||||
|
|
||||||
|
productInfo = productResult.rows.reduce((acc, row) => {
|
||||||
|
acc[row.plan_id] = row;
|
||||||
|
return acc;
|
||||||
|
}, {});
|
||||||
|
}
|
||||||
|
|
||||||
// Get spreader settings and product details for each plan
|
// Get spreader settings and product details for each plan
|
||||||
const plansWithSettings = await Promise.all(
|
const plansWithSettings = await Promise.all(
|
||||||
result.rows.map(async (plan) => {
|
result.rows.map(async (plan) => {
|
||||||
@@ -175,6 +194,8 @@ router.get('/plans', async (req, res, next) => {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const planProductInfo = productInfo[plan.id] || {};
|
||||||
|
|
||||||
return {
|
return {
|
||||||
id: plan.id,
|
id: plan.id,
|
||||||
status: plan.status,
|
status: plan.status,
|
||||||
@@ -186,10 +207,10 @@ router.get('/plans', async (req, res, next) => {
|
|||||||
propertyName: plan.property_name,
|
propertyName: plan.property_name,
|
||||||
propertyAddress: plan.property_address,
|
propertyAddress: plan.property_address,
|
||||||
equipmentName: plan.equipment_name || plan.equipment_type,
|
equipmentName: plan.equipment_name || plan.equipment_type,
|
||||||
productCount: parseInt(plan.product_count),
|
productCount: parseInt(planProductInfo.product_count || 0),
|
||||||
totalProductAmount: parseFloat(plan.total_product_amount || 0),
|
totalProductAmount: parseFloat(planProductInfo.total_product_amount || 0),
|
||||||
totalWaterAmount: parseFloat(plan.total_water_amount || 0),
|
totalWaterAmount: parseFloat(planProductInfo.total_water_amount || 0),
|
||||||
avgSpeedMph: parseFloat(plan.avg_speed_mph || 0),
|
avgSpeedMph: parseFloat(planProductInfo.avg_speed_mph || 0),
|
||||||
spreaderSetting: spreaderSetting?.setting_value || null,
|
spreaderSetting: spreaderSetting?.setting_value || null,
|
||||||
productDetails: productDetails,
|
productDetails: productDetails,
|
||||||
createdAt: plan.created_at,
|
createdAt: plan.created_at,
|
||||||
|
|||||||
Reference in New Issue
Block a user