# Troubleshooting: 404 on /api/transporter/register/

## Apache (mod_wsgi) setup

If you use **Apache** (not nginx), the loadboard VirtualHost must match the request. Check your config:

```bash
# Is loadboard site enabled?
ls -la /etc/apache2/sites-enabled/ | grep loadboard

# Edit the VirtualHost
sudo nano /etc/apache2/sites-available/loadboard.conf
```

**Add ServerName and ServerAlias** so requests to both your IP and 127.0.0.1 hit loadboard:

```apache
ServerName 173.249.56.172
ServerAlias 127.0.0.1 localhost
```

**Restart Apache** (not gunicorn — Apache uses mod_wsgi):

```bash
sudo systemctl restart apache2
```

**Check which VirtualHost handles requests:** If the default site is first and has no ServerName, it may catch requests before loadboard. Either make loadboard the default or ensure your IP/127.0.0.1 match loadboard's ServerName/ServerAlias.

---

## Fix "Unknown column 'company_name'" (migration out of sync)

**Cause:** `manage.py` did not load `.env`, so `migrate` ran against SQLite while Apache uses MySQL. The fix in `manage.py` loads `.env` so migrations use the same DB as production.

**Steps** (after pulling the updated `manage.py`):

```bash
cd /var/www/html/loadboard
source venv/bin/activate
# Ensure .env uses MySQL (DJANGO_DB_ENGINE=mysql)
python manage.py migrate accounts 0003_add_transporter_document_types --fake
python manage.py migrate
deactivate
touch Loadboard/wsgi.py
sudo systemctl restart apache2
```

`touch Loadboard/wsgi.py` forces mod_wsgi to reload the app.

---

## Create api.log (if missing)

The log file is created on first write. Pre-create it so `tail -f` works:

```bash
touch /var/www/html/loadboard/api.log
sudo chown www-data:www-data /var/www/html/loadboard/api.log
sudo chmod 644 /var/www/html/loadboard/api.log
```

Restart Apache, then make any API request to start logging:

```bash
sudo systemctl restart apache2
tail -f /var/www/html/loadboard/api.log
```

---

## 0. What's serving port 80?

```bash
sudo ss -tlnp | grep :80
```

- **Apache (apache2/httpd):** Use `sudo systemctl restart apache2` after code changes.
- **Nginx:** Use `sudo systemctl restart nginx` and ensure gunicorn is running.

## 1. Verify Django URL resolution

Run this on the server to confirm Django knows the route:

```bash
cd /var/www/html/loadboard && ./venv/bin/python deploy/check_urls.py
```

You should see `OK` for `/api/transporter/register/`. If you see `404`, the URL config is wrong.

## 3. Verify code is deployed

On the server, confirm the transporter routes exist:

```bash
cd /var/www/html/loadboard
grep -A2 "transporter_urls" api/urls.py
```

You should see:
```
transporter_urls = [
    path('register/', views.transporter_register),
    ...
```

If not, the transporter code hasn't been deployed. Push your local changes and pull on the server:

```bash
# On your local machine (ensure transporter APIs are committed)
git add -A && git commit -m "Add transporter registration APIs" && git push origin staging

# On the server
cd /var/www/html/loadboard
git pull origin staging
```

---

## 4. Full deployment update

After pulling new code:

```bash
cd /var/www/html/loadboard
source venv/bin/activate
pip install -r requirements.txt
python manage.py migrate
python manage.py collectstatic --noinput
deactivate
# Apache (mod_wsgi):
sudo systemctl restart apache2
# Or if using nginx + gunicorn:
# sudo systemctl restart gunicorn && sudo systemctl reload nginx
```

---

## 5. Test URLs

| URL | Expected |
|-----|----------|
| `http://173.249.56.172/api/auth/login/` | Login form / 405 (method not allowed for GET) or JSON |
| `http://173.249.56.172/api/shipper/register/` | 405 for GET, or JSON for POST |
| `http://173.249.56.172/api/transporter/register/` | Same as shipper |

If shipper works but transporter returns 404, the `api/urls.py` on the server is missing `transporter_urls`.

---

## 6. ALLOWED_HOSTS

Ensure `173.249.56.172` is in `DJANGO_ALLOWED_HOSTS` (in `/var/www/html/loadboard/.env`):

```
DJANGO_ALLOWED_HOSTS=173.249.56.172,localhost,yourdomain.com
```

---

## 7. Quick registration test (POST)

```bash
curl -X POST http://173.249.56.172/api/transporter/register/ \
  -H "Content-Type: application/json" \
  -d '{"email":"test@example.com","password":"Pass1234!","account_type":"DRIVER"}'
```

Success returns `{"status_code": 201, "message": "Transporter registered successfully.", ...}`.
