# TaxCore Node.js Service - Pre-Deployment Checklist

## ✅ Prerequisites

### 1. System Requirements
- [ ] **Node.js 14+** installed
  ```bash
  # Check version
  node --version
  
  # Install on Ubuntu/Debian
  curl -fsSL https://deb.nodesource.com/setup_18.x | sudo -E bash -
  sudo apt-get install -y nodejs
  ```

- [ ] **npm** installed (comes with Node.js)
  ```bash
  npm --version
  ```

- [ ] **curl** available (for TaxCore API calls)
  ```bash
  curl --version
  ```

### 2. Certificate Setup
- [ ] TaxCore PFX certificate file exists
  ```bash
  # Check common locations
  ls -la FRCS_Certs_Install/*.pfx
  ls -la /var/www/html/TAF/FRCS_Certs_Install/*.pfx
  ```

- [ ] Certificate is readable by the user running Node.js
  ```bash
  chmod 644 /path/to/certificate.pfx
  ```

- [ ] Certificate password is available

### 3. Network Requirements
- [ ] Port 3001 is available (or chosen alternative)
  ```bash
  netstat -tlnp | grep 3001
  ```

- [ ] Firewall allows connections to port 3001
  ```bash
  sudo ufw allow 3001
  ```

- [ ] Can reach TaxCore API endpoints
  ```bash
  curl -v https://api.sandbox.vms.frcs.org.fj/api
  ```

## 🔧 Installation Steps

### 1. Install Node.js Dependencies
```bash
cd /home/mishika/projects/TAF/websocket-server
npm install
```

**Expected output**: Dependencies installed successfully
- [ ] `node_modules/` directory created
- [ ] No error messages

### 2. Configure Environment Variables (Optional)
```bash
cd /home/mishika/projects/TAF
cp .env.example .env
nano .env  # Edit with your values
```

**Variables to set**:
- [ ] `TAXCORE_SERVICE_PORT` (default: 3001)
- [ ] `TAXCORE_PFX_PATH` (path to certificate)
- [ ] `TAXCORE_PFX_PASSWORD` (certificate password)
- [ ] `TAXCORE_TAX_URL` (TaxCore API URL)
- [ ] `TAXCORE_VSDC_URL` (V-SDC API URL)

### 3. Verify Certificate Path
```bash
ls -l $TAXCORE_PFX_PATH
# OR
ls -l /path/to/certificate.pfx
```

- [ ] File exists
- [ ] File is readable
- [ ] Password is correct

## 🚀 Start the Service

### Option 1: Quick Start Script
```bash
cd /home/mishika/projects/TAF
./start-taxcore-service.sh
```

- [ ] Script runs without errors
- [ ] Dependencies checked
- [ ] Certificate found
- [ ] Service starts successfully

### Option 2: Manual Start
```bash
cd /home/mishika/projects/TAF/websocket-server
npm run taxcore
```

- [ ] Service starts on port 3001
- [ ] No error messages in console
- [ ] See "TaxCore Service listening on port 3001"

### Option 3: PM2 (Production)
```bash
cd /home/mishika/projects/TAF/websocket-server
pm2 start taxcore-service.js --name taxcore
pm2 save
pm2 startup  # Follow instructions
```

- [ ] Service shows as "online" in `pm2 status`
- [ ] Auto-start configured
- [ ] Logs accessible via `pm2 logs taxcore`

## ✅ Verification Tests

### 1. Health Check
```bash
curl http://localhost:3001/health
```

**Expected response**:
```json
{"status":"ok","service":"TaxCore Service"}
```

- [ ] Returns 200 OK
- [ ] JSON response received
- [ ] Status is "ok"

### 2. Tax Rates Test
```bash
curl http://localhost:3001/tax-rates
```

**Expected response**:
```json
{"success":true,"data":[...tax rate objects...]}
```

- [ ] Returns 200 OK
- [ ] Tax rates data received
- [ ] No error messages

### 3. Sale Creation Test
```bash
curl -X POST http://localhost:3001/sale \
  -H "Content-Type: application/json" \
  -d '{
    "items": [
      {"name": "Test Item", "qty": 1, "price": 10, "labels": ["G"]}
    ],
    "payments": [
      {"method": "Cash", "amount": 10}
    ],
    "type": "Training",
    "Cashier": "Test User"
  }'
```

**Expected response**:
```json
{"success":true,"receipt":"...journal text...","taxcore":{...}}
```

- [ ] Returns 200 OK
- [ ] Receipt data received
- [ ] No errors in service logs

### 4. POS Integration Test

**Steps**:
1. Open POS interface in browser
2. Add items to cart
3. Complete a sale
4. Check browser console

**Verify**:
- [ ] Sale completes without errors
- [ ] Receipt is generated
- [ ] Console shows TaxCore connection mode
- [ ] Backend logs show successful API call

## 🔍 Troubleshooting Checklist

If service fails to start:

- [ ] Check Node.js is installed: `node --version`
- [ ] Check dependencies installed: `ls node_modules/`
- [ ] Check port not in use: `lsof -i :3001`
- [ ] Check certificate exists: `ls $TAXCORE_PFX_PATH`
- [ ] Check certificate permissions: `ls -l $TAXCORE_PFX_PATH`
- [ ] Check service logs: `tail -f logs/taxcore-service.log`
- [ ] Check environment variables: `env | grep TAXCORE`

If API calls fail:

- [ ] Service is running: `curl http://localhost:3001/health`
- [ ] Certificate is valid (not expired)
- [ ] Certificate password is correct
- [ ] Network can reach TaxCore API
- [ ] Firewall not blocking requests
- [ ] Check service logs for detailed error

## 📊 Production Deployment Checklist

### Security
- [ ] Certificate stored securely with restricted permissions
- [ ] Environment variables not in version control
- [ ] Service runs as non-root user
- [ ] Firewall configured properly
- [ ] HTTPS/TLS for external connections
- [ ] Rate limiting configured (if needed)

### Monitoring
- [ ] PM2 or systemd service configured
- [ ] Auto-restart on failure enabled
- [ ] Log rotation configured
- [ ] Disk space monitoring
- [ ] Service health monitoring
- [ ] Alert system for failures

### Performance
- [ ] Service port accessible from web server
- [ ] Network latency acceptable
- [ ] Certificate I/O performance acceptable
- [ ] Concurrent request handling tested
- [ ] Memory usage monitored

### Backup & Recovery
- [ ] Certificate backed up securely
- [ ] Configuration backed up
- [ ] Rollback plan documented
- [ ] Recovery procedure tested

## 📝 Configuration Summary

After completing setup, document your configuration:

```
Service URL:     http://localhost:3001
Certificate:     /path/to/certificate.pfx
Tax API:         https://api.sandbox.vms.frcs.org.fj/api
VSDC API:        http://devesdc.sandbox...
Connection Mode: [Direct / PHP Proxy]
Process Manager: [PM2 / systemd / manual]
Auto-start:      [Yes / No]
```

## 🎯 Final Validation

Before going to production:

1. **Functional Testing**
   - [ ] All sale types work (Normal, Advance, Proforma, Training, Copy)
   - [ ] Refunds work correctly
   - [ ] Tax rates update correctly
   - [ ] Receipts print correctly

2. **Error Handling**
   - [ ] Service recovers from temporary failures
   - [ ] Proper error messages returned
   - [ ] Logs contain useful debugging info

3. **Performance Testing**
   - [ ] Single sale: < 2 seconds
   - [ ] 10 concurrent sales: acceptable
   - [ ] Service stable under load

4. **Integration Testing**
   - [ ] Works with existing POS workflow
   - [ ] Works with customer selection
   - [ ] Works with multiple payment methods
   - [ ] Database records updated correctly

## ✅ Sign-Off

- [ ] All prerequisites met
- [ ] Service installed and configured
- [ ] All tests passing
- [ ] Documentation reviewed
- [ ] Team trained on new system
- [ ] Rollback plan prepared
- [ ] Ready for production deployment

---

**Date**: _____________
**Deployed by**: _____________
**Verified by**: _____________
