Re_Backend/TAT_QUICK_START.md

270 lines
5.4 KiB
Markdown

# ⏰ TAT Notifications - Quick Start Guide
## 🎯 Goal
Get TAT (Turnaround Time) notifications working in **under 5 minutes**!
---
## ✅ Step 1: Setup Redis (Required)
### 🚀 Option A: Upstash (RECOMMENDED - No Installation!)
**Best for Windows/Development - 100% Free**
1. **Sign up**: Go to https://console.upstash.com/
2. **Create Database**: Click "Create Database"
- Name: `redis-tat-dev`
- Type: Regional
- Region: Choose closest to you
- Click "Create"
3. **Copy Connection URL**: You'll get a URL like:
```
rediss://default:AbCd1234...@us1-mighty-shark-12345.upstash.io:6379
```
4. **Update `.env` in Re_Backend/**:
```bash
REDIS_URL=rediss://default:AbCd1234...@us1-mighty-shark-12345.upstash.io:6379
```
**Done!** No installation, no setup, works everywhere!
---
### Alternative: Docker (If you prefer local)
If you have Docker Desktop:
```bash
docker run -d --name redis-tat -p 6379:6379 redis:latest
```
Then in `.env`:
```bash
REDIS_URL=redis://localhost:6379
```
---
## ⚡ Step 2: Enable Test Mode (HIGHLY RECOMMENDED)
For testing, enable **fast mode** where **1 hour = 1 minute**:
### Edit `.env` file in `Re_Backend/`:
```bash
TAT_TEST_MODE=true
```
This means:
- ✅ 6-hour TAT = 6 minutes (instead of 6 hours)
- ✅ 48-hour TAT = 48 minutes (instead of 48 hours)
- ✅ Perfect for quick testing!
---
## 🚀 Step 3: Restart Backend
```bash
cd Re_Backend
npm run dev
```
### You Should See:
```
✅ [TAT Queue] Connected to Redis
✅ [TAT Worker] Initialized and listening
⏰ TAT Configuration:
- Test Mode: ENABLED (1 hour = 1 minute)
- Redis: rediss://***@upstash.io:6379
```
💡 If you see connection errors, double-check your `REDIS_URL` in `.env`
---
## 🧪 Step 4: Test It!
### Create a Request:
1. **Frontend**: Create a new workflow request
2. **Set TAT**: 6 hours (becomes 6 minutes in test mode)
3. **Submit** the request
### Watch the Magic:
```
✨ At 3 minutes: ⏳ 50% notification
✨ At 4.5 minutes: ⚠️ 75% notification
✨ At 6 minutes: ⏰ 100% breach notification
```
### Check Logs:
```bash
# You'll see:
[TAT Scheduler] ✅ TAT jobs scheduled for request...
[TAT Processor] Processing tat50 for request...
[TAT Processor] tat50 notification sent for request...
```
---
## 📊 Verify in Database
```sql
SELECT
approver_name,
tat_hours,
tat50_alert_sent,
tat75_alert_sent,
tat_breached,
status
FROM approval_levels
WHERE status = 'IN_PROGRESS';
```
You should see the flags change as notifications are sent!
---
## ❌ Troubleshooting
### "ECONNREFUSED" or Connection Error?
**Problem**: Can't connect to Redis
**Solution**:
1. **Check `.env` file**:
```bash
# Make sure REDIS_URL is set correctly
REDIS_URL=rediss://default:YOUR_PASSWORD@YOUR_URL.upstash.io:6379
```
2. **Verify Upstash Database**:
- Go to https://console.upstash.com/
- Check database status (should be "Active")
- Copy connection URL again if needed
3. **Test Connection**:
- Use Upstash's Redis CLI in their console
- Or install `redis-cli` and test:
```bash
redis-cli -u "rediss://default:YOUR_PASSWORD@YOUR_URL.upstash.io:6379" ping
# Should return: PONG
```
### No Notifications?
**Checklist**:
- ✅ REDIS_URL set in `.env`?
- ✅ Backend restarted after setting REDIS_URL?
- ✅ TAT_TEST_MODE=true in `.env`?
- ✅ Request submitted (not just created)?
- ✅ Logs show "Connected to Redis"?
### Still Issues?
```bash
# Check detailed logs
Get-Content Re_Backend/logs/app.log -Tail 50 -Wait
# Look for:
# ✅ [TAT Queue] Connected to Redis
# ❌ [TAT Queue] Redis connection error
```
---
## 🎓 Testing Scenarios
### Quick Test (6 minutes):
```
TAT: 6 hours (6 minutes in test mode)
├─ 3 min ⏳ 50% reminder
├─ 4.5 min ⚠️ 75% warning
└─ 6 min ⏰ 100% breach
```
### Medium Test (24 minutes):
```
TAT: 24 hours (24 minutes in test mode)
├─ 12 min ⏳ 50% reminder
├─ 18 min ⚠️ 75% warning
└─ 24 min ⏰ 100% breach
```
---
## 📚 More Information
- **Full Documentation**: `Re_Backend/docs/TAT_NOTIFICATION_SYSTEM.md`
- **Testing Guide**: `Re_Backend/docs/TAT_TESTING_GUIDE.md`
- **Redis Setup**: `Re_Backend/INSTALL_REDIS.txt`
---
## 🎉 Production Mode
When ready for production:
1. **Disable Test Mode**:
```bash
# In .env
TAT_TEST_MODE=false
```
2. **Restart Backend**
3. **TAT will now use real hours**:
- 48-hour TAT = actual 48 hours
- Working hours: Mon-Fri, 9 AM - 6 PM
---
## 🆘 Need Help?
Common fixes:
### 1. Verify Upstash Connection
```bash
# In Upstash Console (https://console.upstash.com/)
# - Click your database
# - Use the "CLI" tab to test: PING
# - Should return: PONG
```
### 2. Check Environment Variables
```bash
# In Re_Backend/.env, verify:
REDIS_URL=rediss://default:YOUR_PASSWORD@YOUR_URL.upstash.io:6379
TAT_TEST_MODE=true
```
### 3. Clear Redis Queue (if needed)
```bash
# In Upstash Console CLI tab:
FLUSHALL
# This clears all jobs - use only if you need a fresh start
```
### 4. Restart Backend
```bash
cd Re_Backend
npm run dev
```
### 5. Check Logs
```bash
Get-Content logs/app.log -Tail 50 -Wait
```
---
**Status Check**:
- [ ] Upstash Redis database created
- [ ] REDIS_URL copied to `.env`
- [ ] TAT_TEST_MODE=true in `.env`
- [ ] Backend restarted
- [ ] Logs show "TAT Queue: Connected to Redis"
- [ ] Test request submitted
✅ All checked? **You're ready!**
---
**Last Updated**: November 4, 2025
**Author**: Royal Enfield Workflow Team