# Production Deployment Workflow

## ⚠️ CRITICAL: Code Changes Workflow

### The Problem (FIXED)
- Previously: Frontend ran in DEV mode (`next dev`) with hot reload
- Issue: Hot reload could revert file changes automatically
- Solution: Production mode (`next start`) serves built static files

### How to Apply Code Changes

1. **Edit Source Files**
   ```bash
   # Edit files in frontend/src/
   nano frontend/src/components/your-component.tsx
   ```

2. **Build for Production**
   ```bash
   cd frontend
   npm run build
   ```
   This creates optimized production files in `.next/` directory

3. **Restart Frontend**
   ```bash
   pm2 restart erp-frontend
   ```

4. **Verify Changes**
   ```bash
   # Check logs
   pm2 logs erp-frontend --lines 20
   
   # Test the site
   curl http://localhost:3001
   ```

### Why This Works

- **Production Mode**: Serves pre-built static files from `.next/`
- **No Hot Reload**: Changes don't revert automatically
- **Explicit Updates**: Changes only apply after rebuild + restart
- **Single Source of Truth**: Built files in `.next/` are what's served

### Quick Commands

```bash
# View status
pm2 list

# View logs
pm2 logs erp-frontend
pm2 logs erp-backend

# Restart after changes
cd frontend && npm run build && pm2 restart erp-frontend

# Stop/Start
pm2 stop all
pm2 start ecosystem.config.js

# Save PM2 config
pm2 save
```

## Security Notes

✅ **Login is Secure**
- Credentials sent via POST request body
- No credentials in URL query parameters
- Session storage used for tokens (tab-specific)

## File Structure

```
/var/www/html/erp_codearya/
├── ecosystem.config.js      # PM2 production config
├── frontend/
│   ├── src/                 # SOURCE FILES (edit these)
│   ├── .next/               # BUILT FILES (auto-generated)
│   └── package.json
└── backend/
    └── target/release/       # Production binary
```

## Important Reminders

❌ **DO NOT** run `npm run dev` in production
✅ **DO** use `npm run build` + `pm2 restart` to apply changes
✅ **DO** edit source files - they are the source of truth
✅ **DO** rebuild after making changes

