# Vaishnavi Creation - E-commerce Backend API A comprehensive e-commerce backend API built with Node.js, Express, PostgreSQL, and MongoDB, designed for fashion and wardrobe management with AI-powered features. ## ๐Ÿš€ Tech Stack - **Backend**: Node.js 18+, Express.js - **Databases**: - PostgreSQL (transactional data) with Prisma ORM - MongoDB (flexible product/wardrobe documents) with Mongoose - **Caching & Jobs**: Redis with BullMQ - **File Storage**: AWS S3 - **Authentication**: JWT with bcrypt - **Containerization**: Docker - **CI/CD**: GitHub Actions ## ๐Ÿ“ Project Structure ``` vaishnavi-backend/ โ”œโ”€โ”€ src/ โ”‚ โ”œโ”€โ”€ config/ # Database and app configuration โ”‚ โ”œโ”€โ”€ controllers/ # Route controllers โ”‚ โ”œโ”€โ”€ middleware/ # Custom middleware (auth, error handling) โ”‚ โ”œโ”€โ”€ models/ # Database models โ”‚ โ”‚ โ””โ”€โ”€ mongodb/ # MongoDB models (Product, Wardrobe) โ”‚ โ”œโ”€โ”€ routes/ # API routes โ”‚ โ”œโ”€โ”€ services/ # Business logic services โ”‚ โ”œโ”€โ”€ jobs/ # Background job processors โ”‚ โ”œโ”€โ”€ utils/ # Utility functions โ”‚ โ”œโ”€โ”€ types/ # TypeScript type definitions โ”‚ โ””โ”€โ”€ server.js # Application entry point โ”œโ”€โ”€ prisma/ # Prisma schema and migrations โ”œโ”€โ”€ tests/ # Test files โ””โ”€โ”€ package.json ``` ## ๐Ÿ› ๏ธ Setup & Installation ### Prerequisites - Node.js 18+ - PostgreSQL 15+ (running locally) - MongoDB 7+ (running locally) - Redis 7+ (optional, for caching and job queues) ### 1. Clone and Install Dependencies ```bash git clone cd vaishnavi-backend npm install ``` ### 2. Environment Configuration Copy the environment template and configure your variables: ```bash cp .env.example .env ``` Edit `.env` with your local database configuration: ```env # Database URLs - Update with your local credentials DATABASE_URL="postgresql://postgres:yourpassword@localhost:5432/vaishnavi_db" MONGODB_URI="mongodb://localhost:27017/vaishnavi_products" REDIS_URL="redis://localhost:6379" # JWT Secrets - Change these in production! JWT_SECRET="your-super-secret-jwt-key" JWT_REFRESH_SECRET="your-super-secret-refresh-key" # AWS S3 Configuration (Optional) AWS_ACCESS_KEY_ID="your-aws-access-key" AWS_SECRET_ACCESS_KEY="your-aws-secret-key" AWS_S3_BUCKET="vaishnavi-files" ``` ### 3. Database Setup #### PostgreSQL Setup 1. **Create the database**: ```sql -- Connect to PostgreSQL and create the database CREATE DATABASE vaishnavi_db; ``` 2. **Generate Prisma client and run migrations**: ```bash # Generate Prisma client npm run db:generate # Run migrations to create tables npm run db:migrate # Seed database with sample data npm run db:seed ``` #### MongoDB Setup MongoDB will automatically create collections when first accessed. Make sure your MongoDB service is running on the default port (27017). #### Redis Setup (Optional) If you have Redis installed locally, make sure it's running on port 6379. If not, you can comment out the Redis URL in your `.env` file. ### 4. Start Development Server ```bash npm run dev ``` The API will be available at `http://localhost:3000` ## ๐Ÿ”ง Troubleshooting ### Database Connection Issues 1. **PostgreSQL Connection Error**: - Ensure PostgreSQL is running: `sudo systemctl start postgresql` (Linux) or start PostgreSQL service (Windows) - Check your connection string in `.env` - Verify the database exists: `psql -U postgres -c "CREATE DATABASE vaishnavi_db;"` 2. **MongoDB Connection Error**: - Ensure MongoDB is running: `sudo systemctl start mongod` (Linux) or start MongoDB service (Windows) - Check if MongoDB is listening on port 27017: `netstat -an | grep 27017` 3. **Redis Connection Error** (Optional): - If you don't have Redis installed, you can comment out the REDIS_URL in your `.env` file - The application will work without Redis, but caching and background jobs won't be available ## ๐Ÿ“š API Endpoints ### Authentication - `POST /api/auth/register` - User registration - `POST /api/auth/login` - User login - `POST /api/auth/refresh` - Refresh JWT token - `POST /api/auth/logout` - User logout - `GET /api/auth/me` - Get current user profile ### Users - `GET /api/users/profile` - Get user profile - `PUT /api/users/profile` - Update user profile - `GET /api/users/addresses` - Get user addresses - `POST /api/users/addresses` - Add address - `GET /api/users/orders` - Get user orders - `GET /api/users/wishlist` - Get user wishlist ### Products - `GET /api/products` - Get all products (with filters) - `GET /api/products/:slug` - Get single product - `POST /api/products` - Create product (Admin) - `PUT /api/products/:id` - Update product (Admin) - `DELETE /api/products/:id` - Delete product (Admin) ### Orders - `POST /api/orders` - Create new order - `GET /api/orders` - Get user orders - `GET /api/orders/:id` - Get single order - `PUT /api/orders/:id/cancel` - Cancel order ### Wardrobe - `GET /api/wardrobe` - Get user's wardrobe - `POST /api/wardrobe/items` - Add item to wardrobe - `PUT /api/wardrobe/items/:id` - Update wardrobe item - `DELETE /api/wardrobe/items/:id` - Remove wardrobe item - `GET /api/wardrobe/recommendations` - Get outfit recommendations ### Admin - `GET /api/admin/dashboard` - Dashboard statistics - `GET /api/admin/users` - Manage users - `GET /api/admin/orders` - Manage orders - `GET /api/admin/products` - Manage products - `GET /api/admin/categories` - Manage categories - `GET /api/admin/coupons` - Manage coupons ## ๐Ÿ” Authentication The API uses JWT-based authentication. Include the token in the Authorization header: ``` Authorization: Bearer ``` ### User Roles - `CUSTOMER` - Default role for registered users - `ADMIN` - Full access to all endpoints - `MODERATOR` - Limited admin access - `SELLER` - Can manage their own products ## ๐Ÿ—„๏ธ Database Schema ### PostgreSQL (Prisma) - Users & Authentication - Orders & Transactions - Addresses - Reviews & Ratings - Wishlist & Cart - Categories & Coupons - System Configuration ### MongoDB (Mongoose) - Products (flexible schema for variants, images, AI tags) - Wardrobe (user clothing collections with AI analysis) ## ๐Ÿš€ Deployment ### Environment Variables for Production Ensure all production environment variables are set: - Database URLs (production databases) - JWT secrets (strong, unique values) - AWS credentials - Email/SMS service credentials ### Deployment Options You can deploy this application to various platforms: - **Heroku**: Use the Procfile and configure environment variables - **AWS EC2**: Set up Node.js environment and configure databases - **DigitalOcean App Platform**: Connect your repository and configure environment variables - **Railway**: Connect your GitHub repository for automatic deployments - **Vercel**: For serverless deployment (with some modifications) ## ๐Ÿงช Testing ```bash # Run tests npm test # Run tests in watch mode npm run test:watch # Generate coverage report npm run test:coverage ``` ## ๐Ÿ“ Scripts - `npm start` - Start production server - `npm run dev` - Start development server with nodemon - `npm run build` - Generate Prisma client - `npm run lint` - Run ESLint - `npm run format` - Format code with Prettier - `npm run db:studio` - Open Prisma Studio - `npm run db:migrate` - Run database migrations ## ๐Ÿ”„ Background Jobs The application uses BullMQ with Redis for processing: - AI image tagging for wardrobe items - Email notifications - Image processing and optimization - Recommendation engine updates ## ๐Ÿ“Š Monitoring & Logging - Health check endpoint: `GET /health` - Structured logging with Morgan - Error tracking and reporting - Database connection monitoring ## ๐Ÿค Contributing 1. Fork the repository 2. Create a feature branch: `git checkout -b feature/amazing-feature` 3. Commit changes: `git commit -m 'Add amazing feature'` 4. Push to branch: `git push origin feature/amazing-feature` 5. Open a Pull Request ## ๐Ÿ“„ License This project is licensed under the ISC License. ## ๐Ÿ†˜ Support For support and questions: - Create an issue in the repository - Check the API documentation at `/api/docs` - Review the health check at `/health` --- **Built with โค๏ธ for Vaishnavi Creation**