# MLM Backend API

Node.js + Express backend for MLM Product-Based System

## Installation

```bash
npm install
```

## Configuration

Create a `.env` file:

```
PORT=5000
NODE_ENV=development

# Supabase Configuration
SUPABASE_URL=your_supabase_url
SUPABASE_ANON_KEY=your_supabase_anon_key
SUPABASE_SERVICE_ROLE_KEY=your_supabase_service_role_key

# JWT Secret
JWT_SECRET=your_jwt_secret_key_here_change_this_in_production
```

## Running the Server

Development mode:
```bash
npm run dev
```

Production mode:
```bash
npm start
```

## Project Structure

```
backend/
├── config/
│   ├── database.js      # Supabase configuration
│   └── multer.js        # File upload configuration
├── controllers/
│   ├── authController.js
│   ├── memberController.js
│   ├── customerController.js
│   ├── productController.js
│   ├── orderController.js
│   ├── adminController.js
│   └── walletController.js
├── middleware/
│   └── auth.js          # JWT authentication
├── routes/
│   ├── auth.js
│   ├── member.js
│   ├── customer.js
│   ├── product.js
│   ├── order.js
│   ├── admin.js
│   └── wallet.js
├── uploads/             # File upload directory
├── .env                 # Environment variables
├── .env.example         # Example environment file
├── package.json
└── server.js            # Main entry point
```

## API Documentation

See main README.md for complete API documentation.

## Deployment

### Using PM2 (Process Manager)

1. Install PM2:
```bash
npm install -g pm2
```

2. Start the application:
```bash
pm2 start server.js --name mlm-backend
```

3. Save PM2 configuration:
```bash
pm2 save
pm2 startup
```

### Using Docker

Create `Dockerfile`:
```dockerfile
FROM node:18-alpine
WORKDIR /app
COPY package*.json ./
RUN npm install --production
COPY . .
EXPOSE 5000
CMD ["npm", "start"]
```

Build and run:
```bash
docker build -t mlm-backend .
docker run -p 5000:5000 --env-file .env mlm-backend
```

### Environment Variables for Production

Make sure to set these in production:
- `NODE_ENV=production`
- Strong `JWT_SECRET`
- Proper Supabase credentials
- Configure CORS for your frontend domain

## Security Notes

1. Change default admin password
2. Use strong JWT secret
3. Enable HTTPS in production
4. Configure proper CORS settings
5. Regular database backups
6. Monitor error logs

## MLM Income Calculation

The system automatically calculates and distributes income when orders are approved:

1. **Member Orders**: 30% DSPCI to member, 10% DBR to sponsor, 1% TRI to upline
2. **Customer Orders**: 15% cashback to customer, 15% DCI to sponsor member

All transactions are recorded in the `transactions` table.
