From 2681631d5d5c74a2a176416775398db9f7ed938b Mon Sep 17 00:00:00 2001 From: laxmanhalaki Date: Wed, 26 Nov 2025 11:55:57 +0530 Subject: [PATCH] avg cycle, and my equest satat mismatch fixed --- src/pages/MyRequests/MyRequests.tsx | 24 ++++++++++++++++-------- src/pages/Requests/Requests.tsx | 8 +++++++- src/services/dashboard.service.ts | 6 +++++- 3 files changed, 28 insertions(+), 10 deletions(-) diff --git a/src/pages/MyRequests/MyRequests.tsx b/src/pages/MyRequests/MyRequests.tsx index e0950cf..cc993b1 100644 --- a/src/pages/MyRequests/MyRequests.tsx +++ b/src/pages/MyRequests/MyRequests.tsx @@ -61,22 +61,29 @@ export function MyRequests({ onViewRequest, dynamicRequests = [] }: MyRequestsPr const [loadingStats, setLoadingStats] = useState(false); // Fetch stats from backend API (calculates from entire dataset using SQL, not by fetching data) - // Backend automatically filters by userId for non-admin users (initiator_id = userId) + // Stats should reflect filters (priority, search) but NOT status + // Status filter should not affect stats - stats should always show all status counts + // Total changes when other filters are applied, but stays stable when only status changes const fetchBackendStats = useCallback(async () => { if (!user?.userId) return; try { setLoadingStats(true); - // Use backend stats API - it automatically filters by userId for non-admin users + // Use backend stats API - explicitly filter by user's initiator_id + // This ensures "My Requests" only shows requests where user is the initiator + // Even for admin users, we want to see only their own requests in "My Requests" // This calculates stats from entire dataset using SQL COUNT queries, not by fetching data + // Note: status is undefined - stats should show all statuses, not filtered by status + // Note: No date range filter - stats should match the list which shows all requests const stats = await dashboardService.getRequestStats( - 'month', // Default date range + undefined, // dateRange - no date filter to match the list undefined, // startDate undefined, // endDate + undefined, // status - My Requests stats show all statuses, not filtered by status (same as All Requests) filters.priorityFilter !== 'all' ? filters.priorityFilter : undefined, undefined, // department - undefined, // initiator (already filtered by userId in backend) + user.userId, // initiator - explicitly filter by user's own userId to ensure only their requests undefined, // approver undefined, // approverType filters.searchTerm || undefined, @@ -97,17 +104,18 @@ export function MyRequests({ onViewRequest, dynamicRequests = [] }: MyRequestsPr } finally { setLoadingStats(false); } - }, [user?.userId, filters.searchTerm, filters.priorityFilter]); // Exclude statusFilter + }, [user?.userId, filters.searchTerm, filters.priorityFilter]); // Exclude statusFilter - stats don't change when only status changes - // Fetch stats when filters change (except status filter) - // Stats are calculated from entire dataset via backend SQL queries (no data fetching needed) + // Fetch stats when filters change (excluding status filter) + // Stats should reflect priority and search filters, but NOT status filter + // Total changes when other filters are applied, but stays stable when only status changes useEffect(() => { const timeoutId = setTimeout(() => { fetchBackendStats(); }, filters.searchTerm ? 500 : 0); return () => clearTimeout(timeoutId); - }, [filters.searchTerm, filters.priorityFilter, fetchBackendStats]); // Exclude statusFilter + }, [filters.searchTerm, filters.priorityFilter, fetchBackendStats]); // Exclude statusFilter - stats don't change when only status changes // Handle dynamic requests (fallback until API loads) const convertedDynamicRequests = transformRequests(dynamicRequests); diff --git a/src/pages/Requests/Requests.tsx b/src/pages/Requests/Requests.tsx index 72434ad..f3891c3 100644 --- a/src/pages/Requests/Requests.tsx +++ b/src/pages/Requests/Requests.tsx @@ -168,10 +168,12 @@ export function Requests({ onViewRequest }: RequestsProps) { }); } else { // For breached/compliant or no SLA filter, use dashboard stats API + // Note: status is undefined here because All Requests stats should show all statuses const stats = await dashboardService.getRequestStats( statsDateRange, statsStartDate ? statsStartDate.toISOString() : undefined, statsEndDate ? statsEndDate.toISOString() : undefined, + undefined, // status - All Requests stats show all statuses, not filtered by status filtersWithoutStatus?.priority, filtersWithoutStatus?.department, filtersWithoutStatus?.initiator, @@ -304,8 +306,12 @@ export function Requests({ onViewRequest }: RequestsProps) { search: filters.searchTerm || undefined, slaCompliance: filters.slaComplianceFilter !== 'all' ? filters.slaComplianceFilter : undefined }; + // All Requests (admin/normal user) should always have a date range + // Default to 'month' if no date range is selected + const statsDateRange = filters.dateRange || 'month'; + fetchBackendStatsRef.current( - filters.dateRange, + statsDateRange, filters.customStartDate, filters.customEndDate, filtersWithoutStatus diff --git a/src/services/dashboard.service.ts b/src/services/dashboard.service.ts index 158cb98..1021bc5 100644 --- a/src/services/dashboard.service.ts +++ b/src/services/dashboard.service.ts @@ -185,6 +185,7 @@ class DashboardService { dateRange?: DateRange, startDate?: string, endDate?: string, + status?: string, priority?: string, department?: string, initiator?: string, @@ -199,7 +200,10 @@ class DashboardService { params.startDate = startDate; params.endDate = endDate; } - // Add filters (excluding status - stats should show all statuses) + // Add filters + if (status && status !== 'all') { + params.status = status; + } if (priority && priority !== 'all') { params.priority = priority; }