# Bina Plus v2 — Local Dev Environment

**Setup date:** 2026-04-27
**Branch cloned:** `main` (latest, commit `2158bd9`)
**Purpose:** Sandbox for new design + developments on top of latest main, isolated from existing `bina_plus/` workspace.

> **Read [`AGENTS.md`](./AGENTS.md) first** for repo conventions, git remotes, and feature-branch workflow. This file is the *infrastructure* reference (ports, DB, keys). Recent V4 features and open items are in [`SESSION_NOTES.md`](./SESSION_NOTES.md). Production / staging deployment details are in [`DEPLOYMENT.md`](./DEPLOYMENT.md).

---

## What's running here

| | Path | Port | Database | Node |
|--|---|---|---|---|
| Backend | `/home/user/bina_plus_workspace/main/backend` | **1813** | local mongo `binaplus-local` | 18.19.1 |
| Frontend (user) | `/home/user/bina_plus_workspace/main/frontend_v3` | **1814** | — | 18.19.1 |
| Admin | `/home/user/bina_plus_workspace/main/admin_frontend` | **1815** | — | **20.19.6** (Vite requires) |
| MongoDB | local systemd `mongod` | 27017 | `binaplus-local` | — |

URLs:
- Frontend: `http://localhost:1814/` (or via VPN/tunnel `http://95.217.88.138:1814`)
- Admin: `http://localhost:1815/` (or `http://95.217.88.138:1815`)
- Backend API: `http://localhost:1813/api`

### Admin login
Use one of the super_admin accounts from the imported `adminusers` collection:
- `israel25@enativ.com`
- `g.hananel20@gmail.com`

(Passwords are the same as in production since we cloned the data.)

The previous workspace at `/home/user/bina_plus/` is **untouched** and still runs on ports 1811/1812 against the old `bina-plus-israel` cloud DB.

---

## Local database — `binaplus-local`

Restored from a filtered `mongodump` of the production `bina-plus-main` DB on 2026-04-27 using a **read-only** Atlas user. We pulled 5.9 MB instead of ~735 MB.

### Collections imported (18)

| Collection | Docs | Notes |
|---|---|---|
| `users` | 28,206 | All real users |
| `subscriptions` | 28,299 | All subscription records |
| `aimodelgroups` | 7 | OpenAI, Gemini, Claude, X-AI, Perplexity, MiniMax, Black Forest Labs |
| `adminusers` | 2 | Includes `israel25@enativ.com` super_admin |
| `configs` | 7 | System configs |
| `prompts` | 3 | |
| `filterprompts`, `filterinstructions`, `filtersettings`, `filterbatches`, `filtertestresults` | 100 total | SafeMode filtering data |
| `featurerequests` | 65 | |
| `generalfeedbacks` | 870 | |
| `companyanalytics` | 22 | |
| `verifications`, `incomingimages`, `storagefiles`, `aimodels` | 0 | empty |

### Collections **not imported** (heavy / not needed)

| Collection | Source size |
|---|---|
| `chathistories` | **617 MB / 74,423 docs** — user chat history |
| `imagehistories` | 71 MB / 80,322 docs — user image history |
| `blockedimages` | 8 MB / 14,385 docs |
| `useranalytics` | 8 MB / 24,226 docs |
| `feedbacks` | 3 MB / 4,606 docs |

Total saved: ~707 MB.

---

## How to start / stop

### Backend
```bash
cd /home/user/bina_plus_workspace/main/backend
npm run start:dev      # NODE_ENV=stg, watches src/, port 1813
```
Stop: `pkill -f "nest start" -P $$` (only this instance) or kill the PID.

### Frontend
```bash
cd /home/user/bina_plus_workspace/main/frontend_v3
npm run serve          # vue-cli-service serve, port 1814
```

### Admin (requires Node 20+)
```bash
cd /home/user/bina_plus_workspace/main/admin_frontend
PATH=/usr/local/n/versions/node/20.19.6/bin:$PATH npm run dev   # Vite, port 1815
```
The system has Node 18 system-wide; Node 20.19.6 is installed under `/usr/local/n/`
and is required because Vite 7 / `crypto.hash` was introduced in Node 20.

All three auto-reload on file changes.

### Verify
```bash
curl -s -o /dev/null -w "%{http_code}\n" http://localhost:1813/api/app-config   # → 200
curl -s -o /dev/null -w "%{http_code}\n" http://localhost:1814                  # → 200
```

### Logs
- `/tmp/v2_backend.log`
- `/tmp/v2_frontend.log`

---

## Configuration changes vs upstream

### `backend/.env.stg`
- `APP_PORT=1813` (was unset / heroku)
- `DATABASE_HOST=mongodb://localhost:27017` (was Atlas SRV)
- `DATABASE_NAME=binaplus-local` (was `bina-plus-main-dup`)

Original env preserved as `.env.stg.original`.

### `frontend_v3/vue.config.js`
Added:
```js
devServer: {
  port: 1814,
  host: '0.0.0.0',
  allowedHosts: 'all'
}
```

### `frontend_v3/.env.local` (new file)
```
VUE_APP_API_URL=http://localhost:1813/api
VUE_APP_URL=http://localhost:1814
NODE_ENV=development
```

---

## How to refresh the local DB from production

When data drifts and you want a fresh copy:

```bash
# Dump (read-only Atlas user)
cd /tmp && mkdir -p mongodump-fresh && cd mongodump-fresh
mongodump \
  --uri="mongodb+srv://israel-readonly:563Usu9b4V9cqvC9@bina-plus-main.3bqici.mongodb.net/" \
  --db=bina-plus-main \
  --excludeCollection=chathistories \
  --excludeCollection=imagehistories \
  --excludeCollection=blockedimages \
  --excludeCollection=useranalytics \
  --excludeCollection=feedbacks \
  --gzip --out=.

# Restore (drops local first)
mongorestore --uri="mongodb://localhost:27017" --db=binaplus-local --gzip --drop bina-plus-main/

rm -rf /tmp/mongodump-fresh
```

The Atlas user is **read-only** — verified by `--writeConcern` not being writable. Even if we accidentally try, the user's role on Atlas would refuse it.

---

## Status of pending ports from `/home/user/bina_plus/`

All major items from the old workspace have been ported into V4 and are now part of this repo (Unified Chat, quick-tiles, admin-quick-tiles, Flux providerKey fix). See [`SESSION_NOTES.md`](./SESSION_NOTES.md) for what was added and what's still open.

---

## API Keys — Status

Tested 2026-04-27. All keys live in `backend/.env.stg`.

| Service | Status | Used by |
|---|---|---|
| OpenAI (`CHAT_GPT_TOKEN`) | ✅ 200 | GPT-3.5/4/4o, DALL-E |
| Anthropic (`CLAUDE_API_KEY`) | ✅ 200 | Claude 3 Haiku/Sonnet/Opus, Claude 3.5/4 |
| Google (`GOOGLE_GEMINI_API_KEY`) | ✅ 200 | Gemini 1.5/2.0 chat + image |
| Replicate (`REPLICA_API_KEY`) | ✅ 200 | Flux, Recraft, Ideogram (via Replicate) |
| OpenRouter (`OPENROUTER_API_KEY`) | ✅ 200 | Grok, DeepSeek, Llama, Qwen, Mistral, etc. |
| Stability AI (`STABILITY_AI_KEY`) | ❌ 401 | SD 3.5 / SD Ultra / SD Core — **need fresh key** |
| AWS (`AWS_*`) | ✅ assumed | S3 image storage, SES email |
| Google Cloud Service Account | ✅ assumed | Translate, Cloud Storage |
| Google OAuth | ✅ assumed | Google login |
| WhatsApp Business | ✅ assumed | WhatsApp integration |
| PayMe | ✅ sandbox | Payments (test) |

**Bottom line:** chat works for all major providers, image generation works for everything **except Stability AI's SD models**. Replicate handles all Flux variants now (after the providerKey fix from the previous session — though that fix lives in the old `bina_plus/` workspace and would need to be applied to this DB if Flux models are added here).

**Not present (intentionally):** `SERPAPI_KEY` (web search for Trip Planner — was a v1-session addition not in main), Stripe/CardCom keys (user said billing is irrelevant).

## Safety notes

- Source DB (`bina-plus-main`) is touched **only via `israel-readonly` Atlas user**. Any write attempts will fail at the server.
- We never `mongorestore` back to Atlas — only ever to `localhost:27017`.
- The previous workspace `bina_plus/` continues to operate on its own ports; nothing here interferes with it.
- API keys (OpenAI, Anthropic, etc.) in `.env.stg` are the staging keys copied from upstream. They are real and billable. Replace with sandbox keys before heavy testing if needed. The user said billing config will be supplied later.
