# CMMS Vermicelli API - Production Deployment Guide

## 🚀 **Overview**
This guide covers deploying the CMMS Vermicelli API as a Windows service through Apache proxy on your existing WAMP setup.

## 📋 **Prerequisites**
- WAMP Server running with Apache on port 8080
- SQL Server with `vermi_production` database
- Node.js 16+ installed
- Existing Evantage API working on port 4000

## 🔧 **1. Installation Steps**

### **Step 1: Copy Files to WAMP Directory**
```bash
# Copy the entire project to:
C:\wamp64\www\CMMS_Vermicelli_api\
```

### **Step 2: Install Dependencies**
```bash
cd C:\wamp64\www\CMMS_Vermicelli_api
npm install
```

### **Step 3: Configure Environment**
Create `.env` file in the project root:
```env
NODE_ENV=production
PORT=3005
DB_HOST=localhost
DB_PORT=1433
DB_NAME=vermi_production
DB_USER=sa
DB_PASSWORD=your_sa_password
JWT_SECRET=your_32_character_secret_key
CORS_ORIGIN=http://evantage.ddns.net:8080
```

### **Step 4: Update Apache Configuration**
Add to your existing VirtualHost in Apache config:
```apache
# Add these lines to existing VirtualHost *:8080
ProxyPass /CMMS_Vermicelli_api/ http://127.0.0.1:3005/CMMS_Vermicelli_api/
ProxyPassReverse /CMMS_Vermicelli_api/ http://127.0.0.1:3005/CMMS_Vermicelli_api/
```

### **Step 5: Install as Windows Service**
```bash
# Run as Administrator
npm run install-service
```

### **Step 6: Restart Apache**
Restart Apache through WAMP control panel.

## 🌐 **2. URL Structure**
- Health Check: `http://evantage.ddns.net:8080/CMMS_Vermicelli_api/health`
- API Root: `http://evantage.ddns.net:8080/CMMS_Vermicelli_api/api`
- Authentication: `http://evantage.ddns.net:8080/CMMS_Vermicelli_api/api/auth`
- Production Line: `http://evantage.ddns.net:8080/CMMS_Vermicelli_api/api/productionLineMixAndDeh`

## 🔍 **3. Testing Deployment**
```bash
# Test the API
npm run test-api
```

## 🛠️ **4. Service Management**

### **Start Service**
```bash
# Through Windows Services
services.msc -> CMMS_Vermicelli_API -> Start
```

### **Stop Service**
```bash
# Through Windows Services
services.msc -> CMMS_Vermicelli_API -> Stop
```

### **Uninstall Service**
```bash
npm run uninstall-service
```

### **View Service Status**
```bash
# PowerShell
Get-Service -Name "CMMS_Vermicelli_API"
```

## 📊 **5. Database Setup**
The API requires these tables in `vermi_production` database:
- `mix_deh_mst` (Production line master data)
- `mix_deh_dtl` (Production line detail data)
- `users` (User accounts and roles)

## 🔐 **6. Security Features**
- JWT-based authentication
- Role-based access control (7 roles)
- Rate limiting
- CORS configuration
- Input validation
- SQL injection protection

## 📝 **7. Production Features**
- **Cycle Management**: Start/end production cycles
- **Batch Tracking**: Auto-increment batch numbers
- **Status Persistence**: Cycle status survives system restarts
- **Multi-cycle Prevention**: Only one active cycle per date
- **Batch History**: Complete history preservation
- **Active Cycle Detection**: Real-time cycle monitoring

## 🐛 **8. Troubleshooting**

### **Service Won't Start**
1. Check SQL Server is running
2. Verify database credentials in `.env`
3. Ensure port 3005 is available
4. Check Windows Event Logs

### **API Not Accessible**
1. Verify Apache proxy configuration
2. Check firewall settings
3. Confirm service is running
4. Test local URL first: `http://127.0.0.1:3005/CMMS_Vermicelli_api/health`

### **Database Connection Issues**
1. Test SQL Server connectivity
2. Verify `vermi_production` database exists
3. Check user permissions
4. Test with SQL Server Management Studio

### **Authentication Problems**
1. Verify JWT_SECRET is set
2. Check user registration/login endpoints
3. Ensure proper role assignments
4. Test token expiration settings

## 📋 **9. Monitoring**

### **Health Check**
Monitor: `http://evantage.ddns.net:8080/CMMS_Vermicelli_api/health`

### **Service Logs**
Check Windows Event Viewer under Applications and Services Logs.

### **API Status**
Use the comprehensive test suite: `npm run test-api`

## 🔄 **10. Updates and Maintenance**

### **Update API**
1. Stop the service
2. Update code files
3. Run `npm install` if dependencies changed
4. Start the service

### **Database Migrations**
Update Sequelize models and run sync if schema changes.

### **Backup Strategy**
- Regular database backups
- Configuration file backups
- Service configuration backups

## 🎯 **11. Integration with Existing System**
This API integrates with your existing setup:
- **Port 4000**: Evantage_node_satya API (existing)
- **Port 3005**: CMMS_Vermicelli_api (new)
- **Port 8080**: Apache proxy for both APIs
- **Domain**: evantage.ddns.net serves both APIs

## ✅ **12. Deployment Checklist**
- [ ] Files copied to `C:\wamp64\www\CMMS_Vermicelli_api\`
- [ ] Dependencies installed (`npm install`)
- [ ] Environment configured (`.env` file)
- [ ] Database `vermi_production` exists and accessible
- [ ] Apache proxy configuration updated
- [ ] Windows service installed and running
- [ ] Apache restarted
- [ ] Health check passes
- [ ] API tests pass
- [ ] External URL accessible

## 🆘 **13. Support**
For issues:
1. Check this deployment guide
2. Run the test suite: `npm run test-api`
3. Check Windows Event Logs
4. Verify service status: `services.msc`
5. Test database connectivity

---
**🏭 CMMS Vermicelli API v1.0.0**  
**Production Line Management System**  
**Deployed via WAMP + Apache Proxy** 