# BETSLIP PRO Backend API

Node.js + Express REST API for the BETSLIP PRO sports prediction / betslip-code marketplace.

**Important:** This is not a bookmaker. Users buy tipster betslip codes; betting happens on licensed platforms outside the app. Predictions are not guaranteed wins. Bet responsibly. 18+ only.

## Stack

- Node.js + Express
- Sequelize ORM + Sequelize CLI migrations
- PostgreSQL
- JWT auth + RBAC (`bettor`, `tipster`, `admin`)
- Swagger UI at `/api/docs`

## Deploy (cPanel)

See [DEPLOY_CPANEL.md](DEPLOY_CPANEL.md) for deploying to http://api.liwaletech.co.tz/

## Database

Create the database (once):

```sql
CREATE DATABASE bestslip;
```

Defaults (see `.env`):

| Variable | Value |
|----------|-------|
| DB_USER | postgres |
| DB_NAME | bestslip |
| DB_PASSWORD | qwerty |
| DB_HOST | 127.0.0.1 |
| DB_PORT | 5432 |

## Setup

```bash
cd backend
npm install
cp .env.example .env
npm run db:migrate
npm run db:seed
npm run dev
```

- API: http://127.0.0.1:3000
- Health: http://127.0.0.1:3000/health
- Swagger: http://127.0.0.1:3000/api/docs

## Seed accounts

| Role | Email | Password |
|------|-------|----------|
| Admin | admin@betslip.pro | Admin@12345 |
| Bettor | bettor@betslip.pro | Bettor@123 |
| Approved tipster | tipster@betslip.pro | Tipster@123 |
| Pending tipster | pending.tipster@betslip.pro | Tipster@123 |

## Business rules (MVP)

- New predictions are created as `pending`; only admin can publish/reject/remove.
- Paid `betslipCode` is never returned until `purchase.paymentStatus = paid` (owners/admin excepted).
- Test payment: `POST /api/purchases/:id/confirm-test-payment`
- Commission split on payment: **50% platform / 30% tipster / 20% reserve** (tipster wallet credited immediately).
- Prediction price: TZS 1,000–5,000.

## Main endpoints

- `POST /api/auth/register` / `login` / `me`
- `GET /api/predictions` / `POST /api/predictions`
- `GET /api/tipsters` / `POST /api/tipsters/apply`
- `POST /api/purchases` → `POST /api/purchases/:id/confirm-test-payment`
- `GET /api/wallet` / `POST /api/withdrawals`
- `GET /api/admin/overview` and other `/api/admin/*` routes (admin JWT required)

## Scripts

| Script | Description |
|--------|-------------|
| `npm run dev` | Start with nodemon |
| `npm start` | Production start |
| `npm run db:migrate` | Run migrations |
| `npm run db:seed` | Seed demo data |
| `npm run db:reset` | Undo all, migrate, seed |
