Skip to content

dev-kweku/ecom-backend

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

36 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation


✅ Generated: README.md

# 🛒 E-Commerce Backend API (Node.js + Express + TypeScript + Prisma)

A scalable and modular e-commerce backend API built with modern tools and best practices.

## 📦 Tech Stack

- **Runtime**: Node.js + Express
- **Language**: TypeScript
- **ORM**: Prisma
- **Database**: PostgreSQL (or MySQL)
- **Authentication**: JWT (access & refresh tokens)
- **Validation**: Zod
- **Structure**: Modular (MVC-style)
- **Security**: Auth middleware, role-based access

---

## 🧱 Project Structure

src/ ├── config/ # Environment config ├── lib/ # Prisma client, utility functions ├── middleware/ # Auth and error middleware ├── modules/ │ ├── auth/ # Auth logic (signup, login, refresh) │ ├── product/ # Product & category CRUD │ ├── cart/ # Cart operations │ ├── order/ # Checkout & order history ├── routes/ # Route registration └── server.ts # Entry point


---

## 🔐 Auth Features

- JWT Access + Refresh Tokens
- `isAuthenticated` middleware
- User model with roles

### Routes

| Method | Endpoint        | Description         |
|--------|------------------|---------------------|
| POST   | `/auth/register` | Register new user   |
| POST   | `/auth/login`    | Login and get token |
| POST   | `/auth/refresh`  | Refresh JWT token   |
| GET    | `/auth/me`       | Get current user    |

---

## 🛍️ Product Module

- Product + Category models
- Auto-slug generation
- CRUD for admin
- Public browse endpoints

| Method | Endpoint            | Description         |
|--------|----------------------|---------------------|
| GET    | `/products`         | List all products   |
| POST   | `/products`         | Create product      |
| GET    | `/products/:id`     | Get product details |
| PATCH  | `/products/:id`     | Update product      |
| DELETE | `/products/:id`     | Delete product      |

---

## 🛒 Cart Module

- Add/remove/update items
- One cart per user
- Quantity updates
- Automatically linked to products

| Method | Endpoint         | Description        |
|--------|------------------|--------------------|
| GET    | `/cart`          | Get current cart   |
| POST   | `/cart`          | Add item to cart   |
| PATCH  | `/cart/:itemId`  | Update quantity    |
| DELETE | `/cart/:itemId`  | Remove cart item   |

---

## 📦 Order Module

- Place order from cart
- Store price snapshot
- View past orders
- Order status enum

### Order Status

```prisma
enum OrderStatus {
  PENDING
  PAID
  CANCELLED
  SHIPPED
}
Method Endpoint Description
POST /orders/checkout Checkout cart
GET /orders List user orders
GET /orders/:id Get order by ID

🧪 Validation (Zod)

Each module validates its input using zod schemas, e.g.:

export const authSchema = z.object({
  email: z.string().email(),
  password: z.string().min(6),
});

🛡️ Middleware

isAuthenticated

// req.user is injected after verifying JWT
req.user = { id, email, role };

Use in routes:

router.use(isAuthenticated);

🧰 Prisma Models

User

model User {
  id       String   @id @default(uuid())
  email    String   @unique
  password String
  role     Role     @default(CUSTOMER)
  ...
}

Product

model Product {
  id          String @id @default(uuid())
  name        String
  description String
  slug        String @unique
  price       Float
  stock       Int
  categoryId  String
  ...
}

CartItem

model CartItem {
  id        String @id @default(uuid())
  userId    String
  productId String
  quantity  Int    @default(1)
}

Order

model Order {
  id     String     @id @default(uuid())
  userId String
  total  Float
  status OrderStatus @default(PENDING)
  ...
}

⚙️ Dev Scripts

npm install
npx prisma generate
npx prisma migrate dev
npm run dev

🔜 Next Steps

  • ✅ Stripe integration (Payments)
  • ✅ Admin dashboard endpoints
  • ✅ Email confirmation (resend, verify)
  • ✅ Pagination & filters
  • ✅ Reviews & ratings

🤝 Contributing

Pull requests and forks are welcome. Please open an issue for any feature request or bug.


📄 License

MIT – free to use and modify.


About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published