# Cartec OT Creation API - Production Deployment

## 📦 Package Contents

This folder contains everything needed to deploy the Cartec OT Creation API to production.

---

## 📁 Folder Structure

```
api_deployment/
├── IMPLEMENTATION.md          # Step-by-step deployment guide
├── README.md                  # This file
├── package.json               # Node.js dependencies
├── env.example                # Environment variables template
├── .gitignore                 # Git ignore rules
├── src/
│   └── index.js              # API source code
├── sql/
│   ├── api_procedure.sql     # Stored procedure (MUST be installed)
│   ├── verify_ot_data.sql    # Verification queries
│   └── quick_queries.sql     # Quick diagnostic queries
├── docs/
│   └── API_DOCUMENTATION.md  # Complete API documentation
└── config/
    └── cartec-api.service    # Systemd service file
```

---

## 🚀 Quick Start

### 1. Read the Implementation Guide

**Start here:** [`IMPLEMENTATION.md`](./IMPLEMENTATION.md)

This guide contains:
- Prerequisites
- Step-by-step installation
- Configuration
- Testing procedures
- Troubleshooting

### 2. Review API Documentation

**Then read:** [`docs/API_DOCUMENTATION.md`](./docs/API_DOCUMENTATION.md)

This document contains:
- Endpoint specifications
- Request/response examples
- Field descriptions
- Error codes
- Integration examples

---

## 📋 Prerequisites Checklist

Before deployment, ensure you have:

- [ ] Ubuntu 22.04 LTS (or similar Linux)
- [ ] Node.js 12+ installed
- [ ] MySQL 8.0+ running
- [ ] `revisa` database exists
- [ ] Root or sudo access
- [ ] At least one technician in `Tecnicos` table

---

## ⚡ Express Deployment (5 Minutes)

```bash
# 1. Deploy files
sudo mkdir -p /opt/cartec_api
cd /var/www/html/W_CartecInstalador/api_deployment
sudo cp -r * /opt/cartec_api/
cd /opt/cartec_api

# 2. Configure environment
cp env.example .env
nano .env  # Edit database credentials

# 3. Install dependencies
npm install

# 4. Install stored procedure
mysql -u root -p revisa < sql/api_procedure.sql

# 5. Install service
sudo cp config/cartec-api.service /etc/systemd/system/
sudo systemctl daemon-reload
sudo systemctl enable cartec-api
sudo systemctl start cartec-api

# 6. Test
curl http://localhost:3000/api/health
```

**For detailed steps, see [`IMPLEMENTATION.md`](./IMPLEMENTATION.md)**

---

## 🎯 API Endpoints

### Health Check
```bash
GET /api/health
```

### Create OT
```bash
POST /api/ot/create
Content-Type: application/json

{
  "cliente": {...},
  "tecnico": {...},
  "servicio": {...},
  "vehiculos": [...]
}
```

**See full documentation:** [`docs/API_DOCUMENTATION.md`](./docs/API_DOCUMENTATION.md)

---

## 🔍 Verification

After deployment, verify everything works:

```bash
# Check service status
sudo systemctl status cartec-api

# Test health endpoint
curl http://localhost:3000/api/health

# Create test OT
curl -X POST http://localhost:3000/api/ot/create \
  -H "Content-Type: application/json" \
  -d @test_request.json

# Verify database records
mysql -u root -p revisa < sql/verify_ot_data.sql
```

---

## 📊 Files Description

| File | Purpose | Required |
|------|---------|----------|
| `IMPLEMENTATION.md` | Deployment guide | ✅ READ FIRST |
| `src/index.js` | API source code | ✅ Yes |
| `package.json` | Dependencies | ✅ Yes |
| `env.example` | Config template | ✅ Yes |
| `sql/api_procedure.sql` | Stored procedure | ✅ MUST INSTALL |
| `sql/verify_ot_data.sql` | Verification | ⚙️ Optional |
| `sql/quick_queries.sql` | Diagnostics | ⚙️ Optional |
| `config/cartec-api.service` | Systemd service | ✅ Yes |
| `docs/API_DOCUMENTATION.md` | API docs | 📖 Reference |

---

## 🛠️ Common Commands

```bash
# Service management
sudo systemctl start cartec-api      # Start service
sudo systemctl stop cartec-api       # Stop service
sudo systemctl restart cartec-api    # Restart service
sudo systemctl status cartec-api     # Check status

# View logs
sudo journalctl -u cartec-api -f     # Follow logs
sudo journalctl -u cartec-api -n 100 # Last 100 lines

# Database
mysql -u root -p revisa < sql/api_procedure.sql  # Install procedure
mysql -u root -p revisa < sql/verify_ot_data.sql # Verify data

# Testing
curl http://localhost:3000/api/health  # Health check
```

---

## 📚 Documentation Links

1. **[IMPLEMENTATION.md](./IMPLEMENTATION.md)** - Deployment guide
2. **[docs/API_DOCUMENTATION.md](./docs/API_DOCUMENTATION.md)** - API reference
3. **[sql/api_procedure.sql](./sql/api_procedure.sql)** - Database procedure
4. **[config/cartec-api.service](./config/cartec-api.service)** - Service config

---

## 🔒 Security Notes

1. **Change default credentials** in `.env`
2. **Set proper file permissions:**
   ```bash
   sudo chown -R www-data:www-data /opt/cartec_api
   sudo chmod 600 /opt/cartec_api/.env
   ```
3. **Configure firewall** if exposing externally
4. **Use HTTPS** in production (Nginx reverse proxy)
5. **Create dedicated database user** (see IMPLEMENTATION.md)

---

## 🐛 Troubleshooting

### API Won't Start
```bash
# Check logs
sudo journalctl -u cartec-api -n 50

# Check if port is available
sudo netstat -tlnp | grep 3000

# Test database connection
mysql -u root -p revisa -e "SELECT 1;"
```

### OT Creation Fails
```bash
# Verify stored procedure exists
mysql -u root -p revisa -e "SHOW PROCEDURE STATUS WHERE Name = 'prc_api_create_OT';"

# Check if technician exists
mysql -u root -p revisa -e "SELECT * FROM Tecnicos WHERE rut_tecnico = '11111111-1';"

# Run diagnostics
mysql -u root -p revisa < sql/quick_queries.sql
```

**For detailed troubleshooting, see [`IMPLEMENTATION.md`](./IMPLEMENTATION.md)**

---

## 📞 Support

1. Check logs: `sudo journalctl -u cartec-api`
2. Review documentation
3. Verify database and stored procedure
4. Test with `curl` examples

---

## ✅ Production Deployment Checklist

- [ ] Read IMPLEMENTATION.md
- [ ] Copy files to `/opt/cartec_api`
- [ ] Configure `.env` with production credentials
- [ ] Run `npm install`
- [ ] Install stored procedure in database
- [ ] Create test technician (if needed)
- [ ] Install systemd service
- [ ] Start and enable service
- [ ] Test health endpoint
- [ ] Create test OT
- [ ] Verify database records
- [ ] Configure firewall (if needed)
- [ ] Set up monitoring/alerts
- [ ] Document API URL for consumers

---

## 🎉 Ready to Deploy!

Follow the steps in **[IMPLEMENTATION.md](./IMPLEMENTATION.md)** to deploy the API to production.

**Estimated deployment time:** 10-15 minutes

---

**Version:** 1.0.0  
**Last Updated:** December 2025  
**Maintained by:** Cartec Development Team

