Skip to content

Latest commit

 

History

2 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

E-Commerce API

This is a backend API for an e-commerce platform that supports user registration, login, product management, and order management with JWT authentication, cron jobs for notifications, and proper error handling.

API, JWT, Authentication, Product Management, Order Management, Cron Jobs, Nodemailer.

Table of Contents 📃

Starting 🚀

Pre-requirements 📋

Before starting, make sure you have the following installed:

Installation 🔧

Local Installation:

  1. Clone this repository
git clone https://github.com/your-repository/e-commerce-api.git
  1. Navigate to the project directory
cd e-commerce-api

Server Setup:

  1. Install dependencies
npm install
  1. Create a .env file Copy the example environment file and modify it with your credentials.
cp .env.example .env
  1. Update .env file with your credentials:
# Server Configuration
PORT=3000

# MongoDB Configuration
MONGODB_URI=mongodb://localhost:27017/e-commerce

# JWT Configuration
JWT_SECRET=your-secret-key-here

# Email Configuration (for nodemailer)
EMAIL_FROM=your-email@example.com
ADMIN_EMAIL=admin@example.com
NODE_ENV=production
SMTP_HOST=smtp.gmail.com
SMTP_PORT=465
SMTP_USER=your-email@example.com
SMTP_PASS=your-email-password

Client Setup:

If your project includes a client, navigate to the client folder and set it up:

cd client
npm install
cp .env.example .env.local

In the client's .env.local file, set the API URL:

REACT_APP_API_URL="http://localhost:3000/api"

API Endpoints 📡

User Registration and Login:

  1. Register User
    • Method: POST
    • URL: /api/auth/register
    • Body:
{
    "name": "John Doe",
    "email": "john@example.com",
    "password": "password123"
}
  • Response:
{
    "message": "User registered successfully"
}
  1. Login User
    • Method: POST
    • URL: /api/auth/login
    • Body:
{
    "email": "john@example.com",
    "password": "password123"
}
  • Response:
{
    "token": "JWT_AUTH_TOKEN"
}

Product Management:

  1. Create Product
    • Method: POST
    • URL: /api/products
    • Body:
{
    "name": "Product Name",
    "description": "Product description",
    "price": 20.5,
    "stock": 100
}
  • Response:
{
    "message": "Product created successfully"
}
  1. Update Product
    • Method: PUT
    • URL: /api/products/:id
    • Body:
{
    "price": 25.0,
    "stock": 50
}
  • Response:
{
    "message": "Product updated successfully"
}
  1. Get All Products
    • Method: GET
    • URL: /api/products
    • Query Params: ?page=1&limit=10&minPrice=10&maxPrice=100&search=product
    • Response:
{
    "products": [
        {
            "name": "Product Name",
            "description": "Product description",
            "price": 20.5,
            "stock": 100
        }
    ],
    "pagination": {
        "currentPage": 1,
        "totalPages": 5
    }
}
  1. Delete Product
    • Method: DELETE
    • URL: /api/products/:id
    • Response:
{
    "message": "Product soft-deleted successfully"
}

Order Management:

  1. Create Order
    • Method: POST
    • URL: /api/orders
    • Body:
{
    "user": "userId",
    "products": [
        {
            "productId": "productId",
            "quantity": 2
        }
    ]
}
  • Response:
{
    "message": "Order created successfully"
}
  1. Get All Orders for a User
    • Method: GET
    • URL: /api/orders/user/:userId
    • Response:
{
    "orders": [
        {
            "orderId": "12345",
            "status": "Pending",
            "totalPrice": 50.0
        }
    ]
}
  1. Update Order Status
    • Method: PUT
    • URL: /api/orders/:id
    • Body:
{
    "status": "Shipped"
}
  • Response:
{
    "message": "Order status updated"
}
  1. Delete Order
    • Method: DELETE
    • URL: /api/orders/:id
    • Response:
{
    "message": "Order deleted successfully"
}

Cron Jobs ⏰

  1. Product Stock Monitoring Cron:
  • Frequency: Runs daily at midnight
  • Functionality: Notifies the admin if a product's stock falls below 10
  • Example: Email notification sent using Nodemailer
  1. Order Fulfillment Reminder Cron:
  • Frequency: Runs every hour
  • Functionality: Sends email reminders for pending orders older than 24 hours

Built with 🛠️

  • Node.js - JavaScript runtime environment
  • Express.js - Backend framework
  • MongoDB - NoSQL database
  • Mongoose - MongoDB object modeling
  • Nodemailer - For sending emails
  • JWT - For authentication
  • Cron - For scheduling tasks
  • Joi - For validation

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages