avg cycle, and my equest satat mismatch fixed
This commit is contained in:
parent
3dbe5e6900
commit
2681631d5d
@ -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);
|
||||
|
||||
@ -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
|
||||
|
||||
@ -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;
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user