# Node.js + Express + MongoDB Backend Template

A comprehensive, modern backend project structure for an Express application using JavaScript \*\*(Node.js)\*\* with advanced features including multi-language support, push notifications, payment integrations, and more.

## Installation

```bash
npm install
```

## Environment Setup

```bash
cp .env.example .env
# Edit .env with your configuration
```

### Required Environment Variables

```env
# Server Configuration
PORT=3000
NODE_ENV=development

# Database
MONGO_URI=mongodb://localhost:27017/your-database

# JWT
JWT_SECRET=your-jwt-secret-key
JWT_EXPIRE=7d
REFRESH_TOKEN_EXPIRE=30d

# Email Configuration
SMTP_HOST=smtp.gmail.com
SMTP_PORT=587
SMTP_USER=your-email@gmail.com
SMTP_PASS=your-app-password
FROM_EMAIL=noreply@yourdomain.com
FROM_NAME=Your App Name

# Stripe Configuration
STRIPE_SECRET_KEY=sk_test_...
STRIPE_PUBLIC_KEY=pk_test_...
STRIPE_WEBHOOK_SECRET=whsec_...
STRIPE_MODE=test

# App Configuration
APP_NAME=Your App Name
ASSETS_URL=https://yourdomain.com/assets
WEBSITE_URL=https://yourdomain.com
```

## Start Server

```bash
# Development
npm run dev

# Production
npm start

# Seed Database
node importDB.js
```

## API Endpoints

### Authentication

- POST `/api/auth/register` - User registration
- POST `/api/auth/login` - User login
- POST `/api/auth/send-otp` - Send OTP to email
- POST `/api/auth/verify-otp` - Verify OTP
- GET `/api/auth/profile` - Get user profile (protected)
- POST `/api/auth/refresh-token` - Refresh access token
- POST `/api/auth/logout` - User logout (protected)
- POST `/api/auth/social-login` - Social login (Google, Facebook, Apple)

### User Management

- GET `/api/user/search` - Search users
- POST `/api/user/profile` - Update user profile (protected)
- GET `/api/user/profile` - Get user profile (protected)
- GET `/api/user/cart` - Get user cart (protected)
- GET `/api/user/addresses` - Get user addresses (protected)
- GET `/api/user/orders` - Get user orders (protected)

### Notifications

- GET `/api/notification` - Get user notifications (protected)
- POST `/api/notification` - Create notification (protected)
- PUT `/api/notification/:id/read` - Mark notification as read (protected)
- PUT `/api/notification/read-all` - Mark all notifications as read (protected)
- DELETE `/api/notification/:id` - Delete notification (protected)
- GET `/api/notification/unread-count` - Get unread count (protected)
- POST `/api/notification/send` - Send test notification (protected)
- POST `/api/notification/app-store` - App Store server notification
- POST `/api/notification/play-store` - Play Store server notification

### CMS (Content Management)

- GET `/api/cms` - Get all CMS pages with pagination
- GET `/api/cms/:slug` - Get CMS page by slug
- POST `/api/cms` - Create new CMS page (protected)
- GET `/api/cms/:id` - Get CMS page by ID (protected)
- PUT `/api/cms/:id` - Update CMS page (protected)
- DELETE `/api/cms/:id` - Delete CMS page (protected)
- PUT `/api/cms/:id/status` - Toggle CMS page status (protected)

### FAQ Management

- GET `/api/faq` - Get all FAQs with pagination
- GET `/api/faq/categories` - Get FAQ categories
- POST `/api/faq` - Create new FAQ (protected)
- GET `/api/faq/:id` - Get FAQ by ID (protected)
- PUT `/api/faq/:id` - Update FAQ (protected)
- DELETE `/api/faq/:id` - Delete FAQ (protected)
- PUT `/api/faq/:id/status` - Toggle FAQ status (protected)

### Media Management

- GET `/api/media` - Get media files with pagination
- GET `/api/media/:id` - Get media by ID (protected)
- POST `/api/media` - Upload media files (protected)
- PUT `/api/media/:id` - Update media metadata (protected)
- DELETE `/api/media/:id` - Delete media file (protected)
- PUT `/api/media/:id/replace` - Replace media file (protected)

### Tours

- GET `/api/tour` - Get tours with pagination
- GET `/api/tour/:id` - Get tour by ID
- POST `/api/tour` - Create new tour (protected)
- PUT `/api/tour/:id` - Update tour (protected)
- DELETE `/api/tour/:id` - Delete tour (protected)
- PUT `/api/tour/:id/status` - Toggle tour status (protected)

### Purchase & Payments

- POST `/api/purchase/verify` - Verify purchase
- POST `/api/stripe/create-payment-intent` - Create Stripe payment intent
- POST `/api/stripe/confirm-payment` - Confirm Stripe payment

### Admin Management

- POST `/api/admin/login` - Admin login
- POST `/api/admin/forgot-password` - Forgot password
- POST `/api/admin/reset-password` - Reset password with token
- GET `/api/admin/profile` - Get admin profile (protected)
- PUT `/api/admin/profile` - Update admin profile (protected)
- PUT `/api/admin/change-password` - Change admin password (protected)

### Admin APIs (Protected)

#### User Management

- GET `/api/admin/users` - Get all users with pagination
- POST `/api/admin/users/getList` - Get users data with filters
- GET `/api/admin/users/:id` - Get user by ID
- PUT `/api/admin/users/:id` - Update user
- DELETE `/api/admin/users/:id` - Delete user
- PUT `/api/admin/users/:id/status` - Toggle user active/inactive

#### CMS Management

- GET `/api/admin/cms` - Get all CMS pages
- POST `/api/admin/cms/getList` - Get CMS data with filters
- GET `/api/admin/cms/create` - Get create CMS page data
- POST `/api/admin/cms/create` - Create new CMS page
- GET `/api/admin/cms/:id` - Get edit CMS page data
- PUT `/api/admin/cms/:id` - Update CMS page
- DELETE `/api/admin/cms/:id` - Delete CMS page

#### FAQ Management

- GET `/api/admin/faq` - Get all FAQs
- GET `/api/admin/faq/create` - Get create FAQ page data
- POST `/api/admin/faq/create` - Create new FAQ
- GET `/api/admin/faq/:id` - Get edit FAQ page data
- PUT `/api/admin/faq/:id` - Update FAQ
- DELETE `/api/admin/faq/:id` - Delete FAQ
- PUT `/api/admin/faq/:id/status` - Toggle FAQ active/inactive

#### Stripe Management

- GET `/api/admin/stripe/settings` - Get Stripe settings
- PUT `/api/admin/stripe/settings` - Update Stripe settings
- POST `/api/admin/stripe/test-connection` - Test Stripe connection
- GET `/api/admin/stripe/payments` - Get payment history
- GET `/api/admin/stripe/payments/:id` - Get payment details
- POST `/api/admin/stripe/refund/:id` - Process refund
- GET `/api/admin/stripe/webhooks` - Get webhook logs
- POST `/api/admin/stripe/create-payment-intent` - Create payment intent
- GET `/api/admin/stripe/customers` - Get Stripe customers
- GET `/api/admin/stripe/subscriptions` - Get Stripe subscriptions

#### Dashboard

- GET `/api/admin/dashboard` - Get dashboard statistics
- GET `/api/admin/dashboard/quick-stats` - Get quick statistics
- GET `/api/admin/dashboard/activities` - Get recent activities
- GET `/api/admin/dashboard/analytics` - Get analytics data

## Language Support

The application supports multiple languages with automatic detection and translation:

### Supported Languages

- English (en) - Default
- German (de)
- French (fr)

### Language Detection Priority

1. `Accept-Language` header
2. `X-Language` or `Language` header
3. `lang` or `language` query parameter
4. `language` or `lang` in request body
5. User's saved language preference
6. Default to English

### Usage Examples

#### Setting Language via Headers

```bash
# Accept-Language header
curl -H "Accept-Language: de" http://localhost:3000/api/auth/profile

# Custom header
curl -H "X-Language: fr" http://localhost:3000/api/auth/profile
```

#### Setting Language via Query Parameter

```bash
curl "http://localhost:3000/api/auth/profile?lang=de"
```

#### Setting Language via Request Body

```bash
curl -X POST http://localhost:3000/api/auth/login \
  -H "Content-Type: application/json" \
  -d '{"email": "user@example.com", "password": "password", "language": "fr"}'
```

### Adding New Languages

1. Create a new language file in `src/lang/` (e.g., `es.json`)
2. Add the language code to the supported languages array in `src/utils/language.js`
3. Copy the structure from `en.json` and translate all messages
4. Restart the server

### Message Keys Structure

Messages are organized by category:

```json
{
  "common": {
    "success": "Success",
    "error": "Error",
    "notFound": "Not Found"
  },
  "auth": {
    "login": "Login successful",
    "registerSuccess": "Registration successful"
  },
  "validation": {
    "required": "This field is required",
    "email": "Please enter a valid email address"
  }
}
```

## Usage Examples

### Authentication

```bash
# Register
curl -X POST http://localhost:3000/api/auth/register \
  -H "Content-Type: application/json" \
  -d '{"email": "user@example.com", "password": "password123", "fullName": "John Doe"}'

# Login
curl -X POST http://localhost:3000/api/auth/login \
  -H "Content-Type: application/json" \
  -d '{"email": "user@example.com", "password": "password123"}'

# Social Login
curl -X POST http://localhost:3000/api/auth/social-login \
  -H "Content-Type: application/json" \
  -d '{"email": "user@gmail.com", "socialId": "123456", "socialType": "google", "fullName": "John Doe"}'
```

### File Upload

```bash
# Upload file
curl -X POST http://localhost:3000/api/media \
  -H "Authorization: Bearer YOUR_TOKEN" \
  -F "file=@/path/to/image.jpg" \
  -F "altText=Test image" \
  -F "description=This is a test image"
```

### Stripe Payment

```bash
# Create Payment Intent
curl -X POST http://localhost:3000/api/stripe/create-payment-intent \
  -H "Content-Type: application/json" \
  -d '{"amount": 1000, "currency": "usd"}'
```

## Development

### Adding New Controllers

1. Create controller in `src/controllers/`
2. Extend `BaseController` class
3. Use message keys for all responses:
   ```javascript
   return this.sendResponse(res, "auth.login", userData);
   return this.sendError(res, "validation.required", 400);
   ```

### Adding New API Endpoints

1. Create route file in appropriate `src/routes/` directory
2. Import the controller
3. Define routes with proper middleware
4. Add validation if needed

### Database Seeding

Use the provided seeders to populate initial data:

```bash
node importDB.js
```

## Contributing

1. Follow the existing code structure and patterns
2. Use message keys for all user-facing strings
3. Add proper validation for all inputs
4. Include error handling for all operations
5. Update documentation for new features

## License

This project is licensed under the MIT License.
