# Press Zone Backend

## Architecture
- **API**: Express + Prisma + PostgreSQL at `api/`
- **Admin Panel**: React + Vite + Tailwind at `admin-panel/`
- **Worker**: Bull queue processor for async translation jobs

## Key Patterns
- API responses: `successResponse({ data })` / `errorResponse('CODE', 'msg')`
- Admin auth middleware: `authenticateAdmin`
- Service pattern: singleton with in-memory cache (see tierService, settingsService)
- Zod validation for all API inputs
- Routes mounted in `api/src/server.ts`

## Deployment

**Connection script**: `connect_prd.sh` (`ssh root@100.116.176.87`)

**Full deployment procedure**: See `.claude/skills/backend/deployment-dockerization.md`

### Quick Reference
- **SSH**: `ssh root@100.116.176.87`
- **User**: `press-api`
- **Backend path**: `/home/press-api/Press.Zone-Works/press-zone-backend/`
- **API URL**: `https://api.press.zone`
- **Admin URL**: `https://admin.translate.press.zone`
- **Health check**: `curl https://api.press.zone/health`

### Deploy Commands (on server)
```bash
# Pull latest code
su - press-api -c 'cd ~/Press.Zone-Works && git pull origin master'

# Rebuild and restart API + Worker
su - press-api -c 'cd ~/Press.Zone-Works/press-zone-backend && podman-compose up -d --build api worker'

# If cached layers cause stale builds
su - press-api -c 'cd ~/Press.Zone-Works/press-zone-backend && podman-compose build --no-cache api && podman-compose up -d --force-recreate api worker'

# Verify
curl https://api.press.zone/health
su - press-api -c 'podman ps --format "table {{.Names}}\t{{.Status}}"'
```

### Admin Panel Deploy (frontend only)
```bash
# Build locally with correct API URL
cd admin-panel && VITE_API_URL=/api npm run build

# Commit dist/, push, then on server:
su - press-api -c 'cd ~/Press.Zone-Works && git pull origin master'
# nginx serves the new files immediately
```

## Prisma Migrations
- Non-interactive env: `prisma migrate dev` fails; create migration dir + SQL manually, then `prisma migrate deploy`
- Always run `npx prisma generate` after migration before build
- API container command includes `npx prisma migrate deploy` on startup

## Database Access (Production)
```bash
su - press-api -c 'podman exec -it tpz-postgres psql -U translate_user -d translate_db'
```

## Build Commands
- API: `cd api && npm run build`
- Admin panel: `cd admin-panel && VITE_API_URL=/api npm run build`

## Playwright Testing
- Config: `admin-panel/playwright.config.ts`
- Base URL: `http://100.116.176.87` (HTTP, Tailscale IP)
- Auth setup stores state in `tests/e2e/.auth/admin.json`
- Run: `cd admin-panel && npx playwright test`
