# SLA Tracking with Working Hours - Implementation Guide
## Overview
The SLA tracking system automatically **pauses during non-working hours** and **resumes during working hours**, ensuring accurate TAT (Turnaround Time) calculations.
## π― Features
β
**Automatic Pause/Resume** - Stops counting during:
- Weekends (Saturday & Sunday)
- Non-working hours (before 9 AM, after 6 PM)
- Holidays (from database)
β
**Real-Time Updates** - Progress updates every minute
β
**Visual Indicators** - Shows when paused vs active
β
**Working Hours Display** - Shows elapsed and remaining in working hours (e.g., "2d 3h")
β
**Next Resume Time** - Shows when tracking will resume during paused state
## π Components Created
### 1. **Utility: `slaTracker.ts`**
Core calculation functions:
- `isWorkingTime()` - Check if current time is working hours
- `calculateElapsedWorkingHours()` - Count only working hours
- `calculateRemainingWorkingHours()` - Working hours until deadline
- `getSLAStatus()` - Complete SLA status with pause/resume info
- `formatWorkingHours()` - Format hours as "2d 3h"
### 2. **Hook: `useSLATracking.ts`**
React hook for real-time tracking:
```typescript
const slaStatus = useSLATracking(startDate, deadline);
// Returns: { progress, elapsedHours, remainingHours, isPaused, statusText, ... }
```
### 3. **Component: `SLATracker.tsx`**
Visual component with pause/resume indicators:
```tsx
```
## π Usage Examples
### In MyRequests Page (Already Integrated)
```tsx
{request.createdAt && request.dueDate &&
request.status !== 'approved' && request.status !== 'rejected' && (
)}
```
### In RequestDetail Page
Replace the existing SLA progress bar with:
```tsx
import { SLATracker } from '@/components/sla/SLATracker';
// In the SLA Progress section:
```
### In OpenRequests Page
```tsx
{request.createdAt && request.dueDate && (
)}
```
## π¨ Visual States
### Active (During Working Hours)
```
SLA Progress [βΆοΈ Active] [On track]
ββββββββββββββββ 45.2%
Elapsed: 1d 4h Remaining: 1d 4h
```
### Paused (Outside Working Hours)
```
SLA Progress [βΈοΈ Paused] [On track]
βββββββββΈοΈββββββ 45.2%
Elapsed: 1d 4h Remaining: 1d 4h
β οΈ Resumes in 14h 30m
```
### Critical (>75%)
```
SLA Progress [βΆοΈ Active] [SLA critical]
ββββββββββββββββββββββ 87.5%
Elapsed: 6d 6h Remaining: 1d 2h
```
### Breached (100%)
```
SLA Progress [βΈοΈ Paused] [SLA breached]
βββββββββββββββββββββββ 100.0%
Elapsed: 8d 0h Remaining: 0h
β οΈ Resumes in 2h 15m
```
## βοΈ Configuration
Working hours are defined in `slaTracker.ts`:
```typescript
const WORK_START_HOUR = 9; // 9 AM
const WORK_END_HOUR = 18; // 6 PM
const WORK_START_DAY = 1; // Monday
const WORK_END_DAY = 5; // Friday
```
To change these, update the constants in the utility file.
## π How It Works
### Backend (Already Implemented)
1. β
Calculates deadlines using `addWorkingHours()` (skips weekends, holidays, non-work hours)
2. β
Stores calculated deadline in database
3. β
TAT scheduler triggers notifications at 50%, 75%, 100% (accounting for working hours)
### Frontend (New Implementation)
1. **Receives** pre-calculated deadline from backend
2. **Calculates** real-time elapsed working hours from start to now
3. **Displays** accurate progress that only counts working time
4. **Shows** pause indicator when outside working hours
5. **Updates** every minute automatically
### Example Flow:
**Friday 4:00 PM** (within working hours)
- SLA Progress: [βΆοΈ Active] 25%
- Shows real-time progress
**Friday 6:01 PM** (after hours)
- SLA Progress: [βΈοΈ Paused] 25%
- Shows "Resumes in 15h" (Monday 9 AM)
**Monday 9:00 AM** (work resumes)
- SLA Progress: [βΆοΈ Active] 25%
- Continues from where it left off
**Monday 10:00 AM** (1 working hour later)
- SLA Progress: [βΆοΈ Active] 30%
- Progress updates only during working hours
## π― Benefits
1. **Accurate SLA Tracking** - Only counts actual working time
2. **User Transparency** - Users see when SLA is paused
3. **Realistic Deadlines** - No false urgency during weekends
4. **Aligned with Backend** - Frontend display matches backend calculations
5. **Real-Time Updates** - Live progress without page refresh
## π§ͺ Testing
Test different scenarios:
1. **During working hours** β Should show "Active" badge
2. **After 6 PM** β Should show "Paused" and resume time
3. **Weekends** β Should show "Paused" and Monday 9 AM resume
4. **Progress calculation** β Should only count working hours
## π Notes
- The frontend assumes the backend has already calculated the correct deadline
- Progress bars update every 60 seconds
- Paused state is visual only - actual TAT calculations are on backend
- For holidays, consider integrating with backend holiday API in future enhancement