# LuckyChit Backend

Node.js + Express + PostgreSQL backend for chit fund management.

---

## 🚀 Quick Setup

```bash
# Install dependencies
npm install

# Configure environment
cp env.example .env
nano .env

# Setup database
sudo -u postgres psql
CREATE DATABASE luckychit;
\q

# Run migration
node run-member-number-migration.js

# Start server
npm start
# or with PM2
pm2 start ecosystem.config.js
```

---

## 📁 Structure

```
backend/
├── src/
│   ├── controllers/      # Request handlers
│   ├── models/          # Database models
│   ├── routes/          # API routes
│   ├── middleware/      # Auth, validation
│   ├── services/        # Background jobs
│   ├── config/          # Configuration
│   └── utils/           # Helper functions
│
├── migrations/          # Database migrations
├── *.js                 # Utility scripts
└── *.md                 # Documentation
```

---

## 🔌 API Endpoints

See [API_DOCUMENTATION.md](./API_DOCUMENTATION.md) for complete reference.

**Base URL**: `http://localhost:3000/api`

### Quick Reference
- **Auth**: `/api/auth/*`
- **Groups**: `/api/chit-groups/*`
- **Members**: `/api/members/*`
- **Draws**: `/api/monthly-draws/*`
- **Payments**: `/api/payments/*`

---

## 💾 Database

### Models
- **User** - User accounts (with email, address, emergency contact)
- **ChitGroup** - Chit fund groups
- **GroupMember** - Relationships (with member_number)
- **MonthlyDraw** - Draw results
- **Payment** - Payment records
- **Notification** - Notification log

### Migrations

**Member Number Migration**:
```bash
node run-member-number-migration.js
```

This adds `member_number` field (1, 2, 3...) to all members.

---

## 🧪 Testing

```bash
# Test database connection
node test-db-connection.js

# Test API
node test-api.js

# Create test user
node create-test-user.js
```

---

## 🔧 Common Commands

```bash
# Development
npm start              # Start dev server
npm run dev           # Watch mode (if configured)

# Production
pm2 start ecosystem.config.js
pm2 restart chitfund-backend
pm2 logs chitfund-backend
pm2 status

# Database
node run-member-number-migration.js
sudo -u postgres psql -d luckychit
```

---

## 📊 Environment Variables

```bash
# Database
DB_HOST=localhost
DB_PORT=5432
DB_NAME=luckychit
DB_USER=luckychit
DB_PASSWORD=your_password

# JWT
JWT_SECRET=your_secret_key
JWT_EXPIRY=7d

# Server
PORT=3000
NODE_ENV=production
```

---

## 🆘 Troubleshooting

See [TROUBLESHOOTING.md](./TROUBLESHOOTING.md) for solutions to common issues:
- Module not found
- PostgreSQL authentication
- Migration errors
- Connection issues

---

## 📚 Documentation

- **[API_DOCUMENTATION.md](./API_DOCUMENTATION.md)** - Complete API reference
- **[TROUBLESHOOTING.md](./TROUBLESHOOTING.md)** - Fix common issues
- **[WHATSAPP_USAGE_EXAMPLES.md](./WHATSAPP_USAGE_EXAMPLES.md)** - WhatsApp integration

---

## 🎯 Latest Updates

**Version 2.0** (November 2025):
- ✅ Member numbers (#1, #2, #3...)
- ✅ Admin edit/delete features
- ✅ Enhanced error messages
- ✅ Bug fixes

See [../CHANGELOG.md](../CHANGELOG.md) for full history.

---

**Status**: Production Ready ✅
