# 🔄 Production Update Quick Guide - LuckyChit

Quick reference for updating backend and frontend in production.

---

## 🎯 What Changed?

- ✅ **Backend only?** → Update Node.js backend
- ✅ **Frontend only?** → Rebuild & redeploy Flutter app
- ✅ **Both?** → Update backend first, then frontend

---

## 🔧 Backend Updates (Node.js API)

### Quick Update (Zero Downtime)

```bash
# On production server
cd /home/luckychit/apps/chitfund/backend

# Pull latest code
git pull origin main

# Install dependencies (if package.json changed)
npm install

# Reload with zero downtime
pm2 reload luckychit-api

# Or use the deployment script
./deploy.sh production

# Check status
pm2 logs luckychit-api --lines 50
```

**Time:** ~30 seconds  
**Downtime:** None (zero-downtime reload)

---

## 📱 Frontend Updates (Flutter UI)

### Option A: Web Deployment (Fastest)

#### Step 1: Build on Your Machine

```bash
# On your Windows dev machine
cd C:\Users\sunde\workspace\chitfund\luckychit

# Build for production
flutter clean
flutter pub get
flutter build web --release
```

#### Step 2: Upload to Server

```bash
# Option 1: Using SCP
scp -r build/web/* user@your-server:/var/www/luckychit/

# Option 2: Using the auto-deploy script (edit config first)
chmod +x deploy-web-auto.sh
./deploy-web-auto.sh

# Option 3: Manual copy (if you have server access)
# Copy build/web/* to /var/www/luckychit/ on server
```

#### Step 3: Clear Cache (Important!)

Users need to hard refresh their browser:
- **Chrome/Edge:** Ctrl + Shift + R
- **Firefox:** Ctrl + Shift + R
- **Mac:** Cmd + Shift + R

**Time:** ~2-3 minutes  
**Downtime:** None

### Option B: Android App Update

```bash
# On your machine
cd luckychit

# Update version in pubspec.yaml first!
# version: 1.0.2+3  # Increment this

# Build
chmod +x build-android.sh
./build-android.sh

# Upload to Play Store or distribute APK
```

**Time:** ~5-10 minutes (build only)  
**Downtime:** None

---

## 🚀 Complete Update Workflow (Both Backend + Frontend)

### Step 1: Update Backend First

```bash
# On production server
cd /home/luckychit/apps/chitfund/backend
git pull origin main
npm install
pm2 reload luckychit-api
```

### Step 2: Update Frontend

```bash
# On your Windows machine
cd C:\Users\sunde\workspace\chitfund\luckychit
flutter clean
flutter pub get
flutter build web --release

# Upload to server
scp -r build/web/* user@server:/var/www/luckychit/
```

### Step 3: Verify Everything Works

```bash
# Test backend API
curl http://your-server:3000/api/health

# Test frontend (visit in browser)
# https://your-domain.com
# Hard refresh: Ctrl+Shift+R
```

---

## 📋 Quick Commands Cheat Sheet

### Backend

```bash
# Status
pm2 list

# Logs
pm2 logs luckychit-api

# Restart
pm2 restart luckychit-api

# Reload (zero downtime)
pm2 reload luckychit-api

# Monitor
pm2 monit
```

### Frontend (Web)

```bash
# Build
cd luckychit
flutter build web --release

# Deploy (local script)
./deploy-web.sh

# Deploy (auto-upload to server)
./deploy-web-auto.sh
```

### Frontend (Android)

```bash
# Build
cd luckychit
./build-android.sh

# Output:
# - build/app/outputs/flutter-apk/app-release.apk
# - build/app/outputs/bundle/release/app-release.aab
```

---

## ⚡ Fast Updates (Development Mode)

### Backend Hot Reload

```bash
# On server, use nodemon for auto-restart on file changes
npm install -g nodemon
pm2 delete luckychit-api
pm2 start nodemon --name "luckychit-api" -- backend/server.js --watch
```

### Frontend Hot Reload

```bash
# On your machine, run in dev mode
cd luckychit
flutter run -d web-server --web-port 8080

# Changes auto-reload in browser
```

---

## 🔍 Troubleshooting Updates

### Backend Won't Update

**Check if code actually changed:**
```bash
cd backend
git status
git log -1
```

**PM2 not picking up changes:**
```bash
pm2 reload luckychit-api --update-env
# or
pm2 restart luckychit-api
```

**Native module issues (bcrypt):**
```bash
cd backend
./fix-bcrypt.sh
```

### Frontend Still Shows Old Version

**Browser cache:**
- Hard refresh: Ctrl+Shift+R
- Or open in Incognito mode

**Server cache:**
```bash
# Clear nginx cache (if using nginx)
sudo rm -rf /var/cache/nginx/*
sudo systemctl restart nginx
```

**Verify files updated:**
```bash
# On server
ls -lh /var/www/luckychit/
# Check modification times
```

**Force rebuild:**
```bash
cd luckychit
flutter clean
rm -rf build/
flutter pub get
flutter build web --release
```

---

## 📊 Version Management

### Check Current Versions

**Backend:**
```bash
# Check package.json
cat backend/package.json | grep version

# Or add to your API
# GET /api/version
```

**Frontend:**
```bash
# Check pubspec.yaml
cat luckychit/pubspec.yaml | grep version
```

### Update Versions

**Backend (`package.json`):**
```json
{
  "name": "luckychit-backend",
  "version": "1.0.2",
  ...
}
```

**Frontend (`pubspec.yaml`):**
```yaml
version: 1.0.2+3
# Format: major.minor.patch+buildNumber
```

---

## 🎯 Pre-Deployment Checklist

### Before Updating Backend

- [ ] Code tested locally
- [ ] Database migrations ready (if any)
- [ ] Environment variables updated (if needed)
- [ ] No breaking API changes (or frontend updated too)

### Before Updating Frontend

- [ ] Backend API compatible
- [ ] Version number incremented
- [ ] Tested locally with production backend
- [ ] Build completes without errors

### After Updating

- [ ] Backend: `pm2 logs` shows no errors
- [ ] Frontend: Hard refresh shows new UI
- [ ] Test critical features
- [ ] Check error logs
- [ ] Monitor for 10-15 minutes

---

## 🚨 Emergency Rollback

### Backend Rollback

```bash
cd backend
git log --oneline -5  # See recent commits
git checkout <previous-commit-hash>
pm2 reload luckychit-api
```

### Frontend Rollback

```bash
# On your machine
cd luckychit
git checkout <previous-commit-hash>
flutter build web --release
scp -r build/web/* user@server:/var/www/luckychit/
```

---

## 📞 Need Help?

**Detailed Guides:**
- Backend: `PM2_PRODUCTION_GUIDE.md`
- Frontend: `FLUTTER_DEPLOYMENT_GUIDE.md`
- Troubleshooting: `DEPLOYMENT_TROUBLESHOOTING.md`

**Quick Start:**
- PM2 Quick Start: `PM2_QUICK_START.md`

---

## 💡 Pro Tips

1. **Always update backend before frontend** (ensures API compatibility)

2. **Test in staging first** if you have a staging server

3. **Update during low traffic times** (early morning, late night)

4. **Keep a deployment log:**
   ```bash
   echo "$(date): Updated to version 1.0.2" >> deployment.log
   ```

5. **Monitor after deployment:**
   ```bash
   pm2 monit  # Backend monitoring
   # Watch error logs for 10-15 minutes
   ```

6. **Communicate with users:**
   - Add a banner: "Maintenance in progress"
   - Or: "New version deployed, please refresh"

---

**Remember: Backend → Zero downtime | Frontend → Hard refresh needed! 🎯**

