# INDI-VOUCH Backend

A basic Node.js backend API server for the INDI-VOUCH application.

## Features

- Express.js web framework
- CORS enabled for cross-origin requests
- Environment variable support with dotenv
- Basic error handling and 404 routes
- Health check endpoint
- Development server with nodemon

## Getting Started

### Prerequisites

- Node.js (version 14 or higher)
- npm or yarn

### Installation

1. Install dependencies:
```bash
npm install
```

2. Create a `.env` file in the root directory:
```env
PORT=3000

# Database Configuration
DB_HOST=localhost
DB_PORT=3306
DB_NAME=indi-vouch-new
DB_USER=root
DB_PASSWORD=
```

3. Set up the database:
   - Make sure XAMPP MySQL/MariaDB is running
   - Import the updated database schema: `indi-vouch-new.sql`
   - The database includes all required tables: user, user_status, generation, binary_tree, payment, settings
   - Or create the database manually using phpMyAdmin

4. Verify database setup:
   - Check all tables: `GET http://localhost:3000/api/debug/all-tables`
   - Check settings table: `GET http://localhost:3000/api/debug/settings`
   - Check specific table structure: `GET http://localhost:3000/api/debug/table/settings`

### Running the Application

#### Development Mode
```bash
npm run dev
```

#### Production Mode
```bash
npm start
```

The server will start on `http://localhost:3000` (or the port specified in your `.env` file).

## API Endpoints

### Base Routes

- `GET /` - Welcome message and server status
- `GET /health` - Health check endpoint
- `GET /db-health` - Database connection health check

### API Routes

- `GET /api/tables` - Get all database tables
- `GET /api/users` - Get all users
- `GET /api/users/:id` - Get user by ID

### Response Format

All responses are in JSON format:

```json
{
  "message": "Welcome to INDI-VOUCH Backend API",
  "status": "Server is running successfully",
  "timestamp": "2023-11-01T12:00:00.000Z"
}
```

## Project Structure

```
BACKEND/
├── config/
│   └── database.js   # Database connection configuration
├── index.js          # Main application file
├── package.json      # Project dependencies and scripts
├── .gitignore       # Git ignore rules
├── .env             # Environment variables (create this file)
├── .env.example     # Environment variables template
├── indi-vouch-new.sql # Database schema
└── README.md        # Project documentation
```

## Scripts

- `npm start` - Start the production server
- `npm run dev` - Start the development server with nodemon
- `npm test` - Run tests (placeholder)

## Dependencies

- **express**: Web application framework
- **cors**: Enable CORS with various options
- **dotenv**: Load environment variables from .env file
- **mysql2**: MySQL client for Node.js with focus on performance
- **jsonwebtoken**: JWT implementation for Node.js
- **bcryptjs**: Password hashing library

## Dev Dependencies

- **nodemon**: Monitor for changes and automatically restart server

## Database Tables

The application uses the following main tables:
- **user**: User information and credentials
- **user_status**: User status tracking
- **generation**: MLM sponsor relationships
- **binary_tree**: MLM binary tree structure
- **settings**: Application configuration key-value pairs

## License

ISC
