# Develop and Release Workflow (Sales Dashboard)

This is the safest, repeatable flow to develop locally and publish updates for the sales force.

## 1) Local development (DEV)

### Environment
Use these variables locally:

- `APP_ENV=development`
- `BASE_URL=http://localhost:5000`
- `SECRET_KEY=<your-dev-secret>`
- `SESSION_COOKIE_SECURE=false`
- `SESSION_COOKIE_SAMESITE=Lax`
- `PREFERRED_URL_SCHEME=http`

You can copy from `.env.development.example`.

### Run app
On Windows:

```powershell
.\run_dev.ps1
```

or:

```powershell
$env:APP_ENV="development"
$env:BASE_URL="http://localhost:5000"
python app.py
```

### OAuth in DEV
Google Cloud Console -> Authorized redirect URI must include:

- `http://localhost:5000/oauth2callback`

---

## 2) Pre-release checklist

Before publishing to sales team:

1. Test login end-to-end (Google OAuth)
2. Test dashboard and filters
3. Test role-based views (admin/comercial/viewer/warehouse)
4. Confirm no secrets are hardcoded
5. Confirm app starts with production env values

---

## 3) Production runtime (PROD)

## Required PROD environment variables

- `APP_ENV=production`
- `BASE_URL=https://sales.globalerc.pt`
- `SECRET_KEY=<long-random-production-secret>`
- `SESSION_COOKIE_SECURE=true`
- `SESSION_COOKIE_SAMESITE=Lax`
- `PREFERRED_URL_SCHEME=https`

Important:

- Do NOT set `OAUTHLIB_INSECURE_TRANSPORT` in production.

### OAuth in PROD
Google Cloud Console -> Authorized redirect URI must include:

- `https://sales.globalerc.pt/oauth2callback`

---

## 4) App service stack for sales force

Public access path:

`Browser (sales team) -> Nginx (443) -> gunicorn 127.0.0.1:8000 -> Flask app`

### Start/Restart (Linux server)

```bash
sudo systemctl daemon-reload
sudo systemctl restart gunicorn.sales-dashboard
sudo systemctl reload nginx
sudo systemctl status gunicorn.sales-dashboard --no-pager
```

---

## 5) Publish a new version (recommended sequence)

1. Deploy new code to server folder
2. Ensure env vars are still correct
3. Install deps if changed (`pip install -r requirements.txt`)
4. Restart gunicorn service
5. Reload nginx
6. Validate from external network:
   - `https://sales.globalerc.pt`
   - Login and dashboard load

---

## 6) Fast rollback (if something breaks)

1. Restore previous code snapshot
2. Restart gunicorn
3. Verify `https://sales.globalerc.pt`

---

## 7) Where files are

- App config and env logic: `app.py`
- WSGI entrypoint: `wsgi.py`
- Nginx production example: `nginx.sales.globalerc.pt.conf.example`
- Nginx current config in repo: `nginx.conf`
- Gunicorn systemd example: `gunicorn.sales-dashboard.service.example`
- Dev env example: `.env.development.example`
- Prod env example: `PROD_ENV_VARS.example`
