You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
A robust, scalable RESTful API for a full-featured e-commerce platform, built with NestJS, TypeORM, and PostgreSQL. Provides complete backend services including authentication, product management, shopping cart, order processing, Stripe payments, reviews, favourites, coupons, and more.
The API follows NestJS's modular architecture. Each domain feature is a self-contained module with its own controller, service, DTOs, entities, and guards.
# Clone the repository
git clone https://github.com/DevBassel/e-store-API.git
cd backend
# Install dependencies
pnpm install
# Copy and configure environment variables
cp .env.example .env
# Edit .env with your credentials (see Environment Variables section)# Start in development mode (hot-reload)
pnpm run start:dev
The API server starts on http://localhost:4000 (or your configured PORT).
Swagger documentation is available at http://localhost:4000/api.
Seed the Database
Populate the database with sample data using Faker.js:
pnpm run seeding
π Environment Variables
Create a .env file in the project root using .env.example as a template:
β οΈWarning: Set DB_Sync=false in production. Use migrations instead.
π API Reference
All endpoints are prefixed with /api/v1. Interactive documentation is available via Swagger at /api.
Auth (/api/v1/auth)
Method
Endpoint
Auth
Description
POST
/auth/register
β
Register a new user
POST
/auth/login
β
Login and receive JWT tokens
POST
/auth/refresh
β
Refresh access token
POST
/auth/log-out
π
Logout (invalidate refresh token)
Password Management (/api/v1/auth)
Method
Endpoint
Auth
Description
POST
/auth/forgot-password
β
Send password reset email
POST
/auth/reset-forgot-password
β
Reset password via email token
POST
/auth/reset-password
π
Change password (authenticated)
Users (/api/v1/users)
Method
Endpoint
Auth
Description
GET
/users
π Admin
List all users (paginated)
GET
/users/profile
π
Get authenticated user's profile
PATCH
/users/profile
π
Update profile
GET
/users/find/:userId
π
Find a user by ID
Products (/api/v1/products)
Method
Endpoint
Auth
Description
POST
/products
π Admin/Manager
Create product (multipart/form-data with image)
GET
/products
β
List products (paginated, filterable)
GET
/products/:id
β
Get product details
PATCH
/products/:id
π
Update product
DELETE
/products/:id
π Admin/Manager
Delete product
Query Parameters for GET /products:
Param
Type
Default
Description
page
number
1
Page number
limit
number
10
Items per page
category
string
β
Filter by category
min
number
0
Minimum price
max
number
1,000,000
Maximum price
s
string
β
Search term
Categories (/api/v1/categories)
Method
Endpoint
Auth
Description
POST
/categories
β
Create a category
GET
/categories
β
List all categories
GET
/categories/:id
β
Get category by ID
PATCH
/categories/:id
β
Update category
DELETE
/categories/:id
β
Delete category
Cart (/api/v1/cart)
Method
Endpoint
Auth
Description
POST
/cart
π
Add item to cart
GET
/cart
π
Get user's cart
GET
/cart/:id
π
Get specific cart item
PATCH
/cart/:id
π
Update cart item quantity
DELETE
/cart/:id
π
Remove item from cart
Orders (/api/v1/orders)
Method
Endpoint
Auth
Description
POST
/orders
π
Create order from cart
GET
/orders
π
List orders (paginated, status filter)
GET
/orders/:id
π
Get order details
GET
/orders/me
π
Get current user's orders
PATCH
/orders/:id
π
Update order
DELETE
/orders/:id
π
Cancel order
Order Statuses: inProcess β shipped β success | cancel
Payments (/api/v1/payments)
Method
Endpoint
Auth
Description
POST
/payments/create
π
Create Stripe payment intent for an order
POST
/payments/webhook
β
Stripe webhook endpoint (signature verified)
Reviews (/api/v1/reviews)
Method
Endpoint
Auth
Description
POST
/reviews
π
Create a review
GET
/reviews/:productId
β
List reviews for a product (paginated)
GET
/reviews/:id/view
β
Get single review
PATCH
/reviews/:id
π
Update a review
DELETE
/reviews/:id
π
Delete a review
Favourites (/api/v1/favourite)
Method
Endpoint
Auth
Description
POST
/favourite
π
Add product to favourites
GET
/favourite
π
List user's favourites
GET
/favourite/:id
π
Get specific favourite
DELETE
/favourite/:id
π
Remove from favourites
Coupons (/api/v1/coupons)
Method
Endpoint
Auth
Description
POST
/coupons
π Admin
Create coupon
GET
/coupons
π Admin
List coupons (paginated)
GET
/coupons/:id
π Admin
Get coupon details
PATCH
/coupons/:id
π Admin
Update coupon
DELETE
/coupons/:id
π Admin
Delete coupon
π‘οΈ Authorization & Roles
The API uses a role-based access control (RBAC) system with three roles:
Role
Value
Permissions
User
user
Default role. Can manage own cart, orders, reviews, favourites, and profile
Manager
manager
Can create, update, and delete products
Admin
admin
Full access β user management, product management, coupon management
Guards applied:
JwtGuard β Validates the Bearer JWT token from the Authorization header
RoleGuard β Checks the user's role against the @Roles() decorator on the endpoint
ποΈ Database
TypeORM Configuration
The database connection is configured via environment variables and managed by TypeORM:
Entities are auto-discovered from each module's entities/ directory
Synchronization (DB_Sync=true) auto-creates/updates tables from entities (dev only)
Migrations are stored in src/modules/DB/migrations/
Migration Commands
# Generate a migration from entity changes
pnpm run migration:generate -- src/modules/DB/migrations/MigrationName
# Run pending migrations
pnpm run migration:run
# Revert the last migration
pnpm run migration:revert
π³ Docker
Docker Compose (Recommended)
Spins up the API and PostgreSQL together:
docker-compose up -d
Services:
Service
Image
Port
app
Built from Dockerfile
3000
postgres
postgres:latest
5432
Standalone Docker Build
# Build the image
docker build -t e-commerce-api .# Run the container
docker run -p 4000:4000 --env-file .env e-commerce-api
π§ͺ Testing
# Unit tests
pnpm run test# Watch mode
pnpm run test:watch
# Coverage report
pnpm run test:cov
# E2E tests
pnpm run test:e2e
# Debug tests
pnpm run test:debug