avg cycle, and my equest satat mismatch fixed

This commit is contained in:
laxmanhalaki 2025-11-26 11:55:57 +05:30
parent 3dbe5e6900
commit 2681631d5d
3 changed files with 28 additions and 10 deletions

View File

@ -61,22 +61,29 @@ export function MyRequests({ onViewRequest, dynamicRequests = [] }: MyRequestsPr
const [loadingStats, setLoadingStats] = useState(false); const [loadingStats, setLoadingStats] = useState(false);
// Fetch stats from backend API (calculates from entire dataset using SQL, not by fetching data) // 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 () => { const fetchBackendStats = useCallback(async () => {
if (!user?.userId) return; if (!user?.userId) return;
try { try {
setLoadingStats(true); 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 // 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( const stats = await dashboardService.getRequestStats(
'month', // Default date range undefined, // dateRange - no date filter to match the list
undefined, // startDate undefined, // startDate
undefined, // endDate undefined, // endDate
undefined, // status - My Requests stats show all statuses, not filtered by status (same as All Requests)
filters.priorityFilter !== 'all' ? filters.priorityFilter : undefined, filters.priorityFilter !== 'all' ? filters.priorityFilter : undefined,
undefined, // department 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, // approver
undefined, // approverType undefined, // approverType
filters.searchTerm || undefined, filters.searchTerm || undefined,
@ -97,17 +104,18 @@ export function MyRequests({ onViewRequest, dynamicRequests = [] }: MyRequestsPr
} finally { } finally {
setLoadingStats(false); 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) // Fetch stats when filters change (excluding status filter)
// Stats are calculated from entire dataset via backend SQL queries (no data fetching needed) // 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(() => { useEffect(() => {
const timeoutId = setTimeout(() => { const timeoutId = setTimeout(() => {
fetchBackendStats(); fetchBackendStats();
}, filters.searchTerm ? 500 : 0); }, filters.searchTerm ? 500 : 0);
return () => clearTimeout(timeoutId); 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) // Handle dynamic requests (fallback until API loads)
const convertedDynamicRequests = transformRequests(dynamicRequests); const convertedDynamicRequests = transformRequests(dynamicRequests);

View File

@ -168,10 +168,12 @@ export function Requests({ onViewRequest }: RequestsProps) {
}); });
} else { } else {
// For breached/compliant or no SLA filter, use dashboard stats API // 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( const stats = await dashboardService.getRequestStats(
statsDateRange, statsDateRange,
statsStartDate ? statsStartDate.toISOString() : undefined, statsStartDate ? statsStartDate.toISOString() : undefined,
statsEndDate ? statsEndDate.toISOString() : undefined, statsEndDate ? statsEndDate.toISOString() : undefined,
undefined, // status - All Requests stats show all statuses, not filtered by status
filtersWithoutStatus?.priority, filtersWithoutStatus?.priority,
filtersWithoutStatus?.department, filtersWithoutStatus?.department,
filtersWithoutStatus?.initiator, filtersWithoutStatus?.initiator,
@ -304,8 +306,12 @@ export function Requests({ onViewRequest }: RequestsProps) {
search: filters.searchTerm || undefined, search: filters.searchTerm || undefined,
slaCompliance: filters.slaComplianceFilter !== 'all' ? filters.slaComplianceFilter : 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( fetchBackendStatsRef.current(
filters.dateRange, statsDateRange,
filters.customStartDate, filters.customStartDate,
filters.customEndDate, filters.customEndDate,
filtersWithoutStatus filtersWithoutStatus

View File

@ -185,6 +185,7 @@ class DashboardService {
dateRange?: DateRange, dateRange?: DateRange,
startDate?: string, startDate?: string,
endDate?: string, endDate?: string,
status?: string,
priority?: string, priority?: string,
department?: string, department?: string,
initiator?: string, initiator?: string,
@ -199,7 +200,10 @@ class DashboardService {
params.startDate = startDate; params.startDate = startDate;
params.endDate = endDate; 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') { if (priority && priority !== 'all') {
params.priority = priority; params.priority = priority;
} }