73 lines
2.6 KiB
TypeScript
73 lines
2.6 KiB
TypeScript
/**
|
|
* Calculation utilities for request stats
|
|
*/
|
|
|
|
import type { RequestStats, BackendStats } from '../types/requests.types';
|
|
|
|
export function calculateStatsFromFilteredData(
|
|
allFilteredRequests: any[],
|
|
isOrgLevel: boolean,
|
|
backendStats: BackendStats | null,
|
|
hasActiveFilters: boolean,
|
|
totalRecords: number,
|
|
convertedRequests: any[]
|
|
): RequestStats {
|
|
// Check if we have active filters (excluding default date range)
|
|
const hasFilters = hasActiveFilters;
|
|
|
|
// Use allFilteredRequests (all filtered data before pagination) for accurate stats
|
|
if (allFilteredRequests.length > 0) {
|
|
const total = allFilteredRequests.length;
|
|
const pending = allFilteredRequests.filter((r: any) => {
|
|
const status = (r.status || '').toString().toUpperCase();
|
|
return status === 'PENDING' || status === 'IN_PROGRESS';
|
|
}).length;
|
|
const approved = allFilteredRequests.filter((r: any) => {
|
|
const status = (r.status || '').toString().toUpperCase();
|
|
return status === 'APPROVED';
|
|
}).length;
|
|
const rejected = allFilteredRequests.filter((r: any) => {
|
|
const status = (r.status || '').toString().toUpperCase();
|
|
return status === 'REJECTED';
|
|
}).length;
|
|
const draft = allFilteredRequests.filter((r: any) => {
|
|
const status = (r.status || '').toString().toUpperCase();
|
|
return status === 'DRAFT';
|
|
}).length;
|
|
const closed = allFilteredRequests.filter((r: any) => {
|
|
const status = (r.status || '').toString().toUpperCase();
|
|
return status === 'CLOSED';
|
|
}).length;
|
|
|
|
return {
|
|
total,
|
|
pending,
|
|
approved,
|
|
rejected,
|
|
draft,
|
|
closed
|
|
};
|
|
} else if (isOrgLevel && !hasFilters && backendStats) {
|
|
// No filters and backend stats available - use backend stats for better performance
|
|
return {
|
|
total: backendStats.total,
|
|
pending: backendStats.pending,
|
|
approved: backendStats.approved,
|
|
rejected: backendStats.rejected,
|
|
draft: backendStats.draft,
|
|
closed: backendStats.closed
|
|
};
|
|
} else {
|
|
// Fallback: calculate from convertedRequests (paginated data - less accurate)
|
|
return {
|
|
total: totalRecords || convertedRequests.length,
|
|
pending: convertedRequests.filter(r => r.status === 'pending' || r.status === 'in-progress').length,
|
|
approved: convertedRequests.filter(r => r.status === 'approved').length,
|
|
rejected: convertedRequests.filter(r => r.status === 'rejected').length,
|
|
draft: convertedRequests.filter(r => r.status === 'draft').length,
|
|
closed: convertedRequests.filter(r => r.status === 'closed').length
|
|
};
|
|
}
|
|
}
|
|
|