Skip to content

sherloCod3/order-management-api

Repository files navigation

Node REST Orders API

A robust, enterprise-grade RESTful API for managing orders. This service handles comprehensive CRUD operations for orders and their nested items, powered by a relational PostgreSQL database and guarded by JSON Web Token (JWT) authentication.

Key Features

  • Order Management: Full CRUD capabilities for bulk orders and individual item arrays.
  • Relational Integrity: Implements proper one-to-many relationship mapping between Orders and Items.
  • Secure: Endpoint access guarded by Bearer Token JWT authentication.
  • Documented: Fully documented interactive OpenAPI/Swagger specification.
  • Type-safe Database: Uses Prisma ORM for robust migrations and database queries.

Tech Stack

  • Language: JavaScript (Node.js)
  • Framework: Express.js
  • Database: PostgreSQL 15
  • ORM: Prisma
  • Authentication: JWT (jsonwebtoken)
  • Documentation: Swagger UI & YAML
  • Infrastructure: Docker & Docker Compose

Prerequisites

Before running the application locally, ensure you have the following installed:

  • Node.js (v18.x or v20.x recommended)
  • npm (Node Package Manager)
  • Docker and Docker Compose (to spin up the Postgres database)

Getting Started

1. Clone the Repository

git clone <your-repository-url>
cd <repository-directory>

2. Install Dependencies

Install all necessary Node runtime packages and development tools:

npm install

3. Environment Setup

Create a .env file at the root of the project to configure the database credentials and JWT secrets.

You can use the following default configuration for basic local development:

# Relational DB connection string (PostgreSQL)
DATABASE_URL="postgresql://postgres:password@localhost:5432/jitterbit_orders?schema=public"

# Application Config
PORT=3000
JWT_SECRET="super-secret-key-jitterbit-challenge-2026"

4. Database Setup

Spin up the local PostgreSQL instance using Docker Compose:

docker-compose up -d

Once the container is running and healthy on port 5432, push the database schema using Prisma:

npx prisma db push

This command instantly provisions the Order and Item tables defined in prisma/schema.prisma.

5. Start the Application

You can start the server in standard mode or development mode (which auto-restarts on save):

# Start standard server
npm start

# Run using Nodemon (Development)
npm run dev

The application will report that it is running at http://localhost:3000.


Interactive Documentation (Swagger)

A full interactive console mapping all endpoints, expected payloads, and query parameters is generated via Swagger.

Start the server, then navigate to: http://localhost:3000/api-docs


Architecture Overview

Directory Structure

├── prisma/
│   └── schema.prisma         # Database scheme definitions for Orders & Items
├── src/
│   ├── controllers/          # Business logic and payload handling
│   │   ├── authController.js # Handles login/token generation
│   │   └── orderController.js# Handles Order CRUD
│   ├── middlewares/          # Security and Interceptors
│   │   ├── authMiddleware.js # Parses/validates Bearer JWT
│   │   └── errorMiddleware.js# Standardized error emission wrapper
│   ├── routes/               # Express routing layer
│   │   ├── authRoutes.js
│   │   └── orderRoutes.js
│   ├── utils/                # Helper utilities
│   │   └── dataMapper.js     # Data transformation and payload adaptation
│   ├── app.js                # Core Express application assembly
│   └── server.js             # HTTP Server bootstrap
├── swagger.yml               # OpenAPI definition document
├── docker-compose.yml        # PostgreSQL container infrastructure
└── package.json

Data Flow

  1. Client hits a /order or /auth endpoint.
  2. Express Router parses the JSON body.
  3. authMiddleware.js intercepts private /order routes, decoding and validating the JWT authorization header.
  4. Payload hits orderController.js.
  5. dataMapper.js is leveraged to adapt external localized payload schemas (e.g. numeroPedido) gracefully into deep internal entity models (orderId).
  6. Prisma communicates symmetrically to Postgres.
  7. Express responds with properly formatted standard data and correct HTTP Status codes (e.g., 201 Created, 400 Bad Request, 404 Not Found).

Available Scripts

Command Description
npm start Boots standard Express server
npm run dev Boots dev-server via Nodemon
npx prisma studio Opens a web-based GUI to view your PostgreSQL tables locally
docker-compose up -d Starts the database container
docker-compose down Shuts down the database container

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors