pmtool / src /hooks /useAssetRequestNotifications.ts
devarshia5's picture
Upload 487 files
d97b8f9 verified
Raw
History Blame Contribute Delete
1.75 kB
import { useQuery } from '@tanstack/react-query';
import { useAuth } from '@/lib/auth-context';
import { assetRequestApi } from '@/services/assetRequestApi';
/**
* Hook to get unread asset request notifications count
* For employees: count of requests with status changes (approved, rejected, assigned)
* For admins: count of pending requests
*/
export const useAssetRequestNotifications = () => {
const { userData } = useAuth();
const isAdmin = userData?.roleId === 1 || userData?.roleName?.toLowerCase() === 'admin';
const employeeId = userData?.employeeId || 0;
// For admins: get count of pending requests
const { data: pendingRequests = [] } = useQuery({
queryKey: ['assetRequests', 'pending'],
queryFn: () => assetRequestApi.getPending(),
enabled: isAdmin,
staleTime: 30000,
refetchInterval: 60000, // Refresh every minute
});
// For employees: get their requests and count status changes
// In a real implementation, this would track which notifications have been "read"
// For now, we'll just count non-pending requests as potential notifications
const { data: employeeRequests = [] } = useQuery({
queryKey: ['assetRequests', 'employee', employeeId],
queryFn: () => assetRequestApi.getByEmployeeId(employeeId),
enabled: !isAdmin && employeeId > 0,
staleTime: 30000,
refetchInterval: 60000,
});
// Calculate notification count
const notificationCount = isAdmin
? pendingRequests.length
: employeeRequests.filter(
(req) => req.status === 'approved' || req.status === 'rejected' || req.status === 'assigned'
).length;
return {
notificationCount,
hasNotifications: notificationCount > 0,
};
};