From 64ed95271fe61abed8cd7ffe70f6e53683ef9136 Mon Sep 17 00:00:00 2001 From: bot-backend Date: Wed, 17 Jun 2026 20:49:16 +0300 Subject: [PATCH] fix(leads): window revenue_total/deals_total to leads_window (#1383) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Both metrics were querying prinzip_deals without any date filter, returning all-time figures while the surrounding stats (leads_window, converted_window, conv_pct_window) were scoped to the last N months. Now both subqueries restrict to deals linked to leads in window_leads (via deal_id IN (...)), making all «за период» figures consistent. --- backend/app/api/v1/admin_leads.py | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/backend/app/api/v1/admin_leads.py b/backend/app/api/v1/admin_leads.py index 71e9b54c..c5a7a89d 100644 --- a/backend/app/api/v1/admin_leads.py +++ b/backend/app/api/v1/admin_leads.py @@ -149,8 +149,20 @@ def leads_stats( 2 ) AS conv_pct_window, (SELECT COUNT(DISTINCT source) FROM prinzip_leads) AS sources_total, - (SELECT SUM(deal_price) FROM prinzip_deals) AS revenue_total, - (SELECT COUNT(*) FROM prinzip_deals) AS deals_total + ( + SELECT SUM(d.deal_price) + FROM prinzip_deals d + WHERE d.deal_id IN ( + SELECT deal_id FROM window_leads WHERE deal_id IS NOT NULL + ) + ) AS revenue_total, + ( + SELECT COUNT(*) + FROM prinzip_deals d + WHERE d.deal_id IN ( + SELECT deal_id FROM window_leads WHERE deal_id IS NOT NULL + ) + ) AS deals_total FROM window_leads """ ), -- 2.45.3