From dfe4e8c2f376745591f808a3e365cf93a28713e9 Mon Sep 17 00:00:00 2001 From: Jake Kasper Date: Tue, 26 Aug 2025 07:53:28 -0500 Subject: [PATCH] asdf --- backend/src/routes/applications.js | 45 ++++++++++++++++++++++-------- 1 file changed, 33 insertions(+), 12 deletions(-) diff --git a/backend/src/routes/applications.js b/backend/src/routes/applications.js index 745b56d..cc4e687 100644 --- a/backend/src/routes/applications.js +++ b/backend/src/routes/applications.js @@ -69,30 +69,49 @@ router.get('/plans', async (req, res, next) => { const whereClause = whereConditions.join(' AND '); + // First get basic plan info with sections const result = await pool.query( `SELECT ap.*, 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, - 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 + ue.custom_name as equipment_name, et.name as equipment_type FROM application_plans ap JOIN application_plan_sections aps ON ap.id = aps.plan_id JOIN lawn_sections ls ON aps.lawn_section_id = ls.id JOIN properties p ON ls.property_id = p.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 application_plan_products app ON ap.id = app.plan_id WHERE ${whereClause} GROUP BY ap.id, p.name, p.address, ue.custom_name, et.name ORDER BY ap.planned_date DESC, ap.created_at DESC`, 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 const plansWithSettings = await Promise.all( result.rows.map(async (plan) => { @@ -175,6 +194,8 @@ router.get('/plans', async (req, res, next) => { } } + const planProductInfo = productInfo[plan.id] || {}; + return { id: plan.id, status: plan.status, @@ -186,10 +207,10 @@ router.get('/plans', async (req, res, next) => { propertyName: plan.property_name, propertyAddress: plan.property_address, equipmentName: plan.equipment_name || plan.equipment_type, - productCount: parseInt(plan.product_count), - totalProductAmount: parseFloat(plan.total_product_amount || 0), - totalWaterAmount: parseFloat(plan.total_water_amount || 0), - avgSpeedMph: parseFloat(plan.avg_speed_mph || 0), + productCount: parseInt(planProductInfo.product_count || 0), + totalProductAmount: parseFloat(planProductInfo.total_product_amount || 0), + totalWaterAmount: parseFloat(planProductInfo.total_water_amount || 0), + avgSpeedMph: parseFloat(planProductInfo.avg_speed_mph || 0), spreaderSetting: spreaderSetting?.setting_value || null, productDetails: productDetails, createdAt: plan.created_at,