Skip to content

Robust Node.js REST API built with TypeScript, Express.js, and MongoDB. It powers the Shoishob platform, handling authentication, payments, and data access with production-grade security and code quality tooling.

Notifications You must be signed in to change notification settings

tareksabbir/Shoihob-backend

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

32 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Shoishob Backend

Robust Node.js REST API built with TypeScript, Express.js, and MongoDB.
It powers the Shoishob platform, handling authentication, payments, and data access with production-grade security and code quality tooling.


✨ Key Features

Area What You Get Packages / Techniques
Typed Code End-to-end TypeScript, strict tsconfig typescript, ts-node-dev
Scalable API Layered architecture (Routes β†’ Controllers β†’ Services β†’ Models) express, mongoose
Authentication Stateless JWT auth (access + refresh tokens) jsonwebtoken, HTTP-only cookies
Payments Fully-integrated SSL Commerz flow (initiate β†’ redirect β†’ IPN) sslcommerz-lts
Validation Runtime payload validation & type inference zod
Error Handling Centralized error classes + global handler Custom ApiError, catchAsync
Security Helmet, rate-limiting, CORS, .env isolation helmet, express-rate-limit, cors, dotenv
Dev Ex Lint, format, pre-commit hooks, VS Code launch eslint, prettier, husky, lint-staged
CI-ready Dockerfile & GitHub Actions workflow (optional) see Deployment

πŸ—οΈ Architectural Overview

System Architecture

Shoihob-backend follows a modular architecture with distinct components that handle specific business domains. The system is built using Node.js with Express framework and TypeScript, connecting to a MongoDB database for data persistence and integrating with SSLCommerz for payment processing.

System Architecture

Architectural Layers

The system follows a layered architecture pattern that promotes separation of concerns and maintainability.

Architectural Layers

πŸ“‘ API Layer

This layer is responsible for handling HTTP requests and routing them to appropriate business logic.

  • Routes: Define API endpoints and connect them to corresponding controllers.
  • Controllers: Manage the request/response lifecycle and delegate core operations to services.
  • Middleware: Intercepts and processes requests before they reach the controller (e.g., authentication, input validation, error handling).

API Structure

The API is organized as a RESTful service with versioned endpoints. All routes are prefixed with /api/v1 and are organized by functionality. The Express router maps incoming requests to the appropriate controller functions.

API Structure

🧠 Business Logic Layer

This layer contains Service Classes that encapsulate the core logic of the application. It:

  • Implements domain-specific rules and use cases.
  • Coordinates actions involving multiple models or external services.
  • Keeps controllers lean and focused on the request flow.

πŸ—ƒοΈ Data Access Layer

Responsible for communicating with the database using Mongoose. It includes:

  • Schemas & Models: Define data structures and constraints for MongoDB collections.
  • CRUD Operations: Abstracts direct database operations and enforces a consistent access pattern.

🧩 Core Modules

The Shoihob-backend is structured into several key modules, each responsible for a distinct business function. This modular approach ensures better scalability, maintainability, and separation of concerns.

Module Path Description
User /user Handles user registration, profile management, and role-based access control.
Auth /jwt Manages authentication using JWT tokens.
Turf /turf Manages sports field listings, details, and maintenance.
Booking /bookings Handles turf reservation, scheduling, and payment processing.
Payment History /history Stores and manages transaction records and payment history.
Tournament /tournament-details Enables creation and management of tournaments.
Tournament Registration /tournamentRegistration Facilitates user registration and participation in tournaments.
Admin Stats /stats Provides analytics and system insights for administrators.
Owner Stats /ownerStats Delivers usage analytics specifically for turf owners.

Core Modules

Module Description
User Module Manages user accounts, profiles, and role-based access control (user, admin, superAdmin).
Authentication Module Implements secure JWT-based authentication and authorization.
Turf Details Module Handles sports venue data including availability, pricing, and location.
Turf Booking Module Manages turf reservations, slot selection, payment processing, and confirmation.
Payment History Module Tracks all transaction records, including bookings and tournament registrations.
Tournament Module Manages tournament creation, scheduling, and turf associations.
Tournament Registration Module Handles user registration for tournaments, with integrated payment processing.
Statistics Module Provides analytical dashboards and metrics for admins and turf owners.

πŸ“ Source Root: src/app/routes/index.ts

🧱 Module Structure

Each module adheres to a standardized internal structure, which improves code readability, collaboration, and scalability:

src/
 └── app/
      └── [module-name]/
           β”œβ”€β”€ controllers/     # Handles HTTP request/response logic
           β”œβ”€β”€ services/        # Contains core business logic
           β”œβ”€β”€ models/          # Mongoose schemas and data access definitions
           β”œβ”€β”€ routes/          # Defines REST API endpoints
           └── validators/      # Joi or Zod schemas for request validation (if any)

Request Flow

The following diagram illustrates how a typical request flows through the system:

Request Flow

Example: Booking a Turf

Example: Booking a Turf

Authentication and Authorization

The system implements JWT (JSON Web Token) based authentication and role-based authorization.

Authentication and Authorization

Data Models

The system uses MongoDB as its database and Mongoose for object data modeling. The following diagram illustrates the key data models and their relationships:

Data Models

Error Handling

The system implements a global error handling mechanism to ensure consistent error responses across all API endpoints.

 Error Handling

Integration with External Services

The system integrates with external services, particularly for payment processing:

 Integration with External Services


πŸ“‚ Directory Layout

src/
β”œβ”€β”€ app/
β”‚   β”œβ”€β”€ module/
β”‚   β”‚   β”œβ”€β”€ User/
β”‚   β”‚   β”‚   β”œβ”€β”€ User.controller.ts
β”‚   β”‚   β”‚   β”œβ”€β”€ User.service.ts
β”‚   β”‚   β”‚   β”œβ”€β”€ User.model.ts
β”‚   β”‚   β”‚   β”œβ”€β”€ User.interface.ts
β”‚   β”‚   β”‚   β”œβ”€β”€ User.constant.ts
β”‚   β”‚   β”‚   └── User.validation.ts
β”‚   β”‚   └── Payment/  (etc.)
β”‚   └── common/         # shared middlewares & utils
β”œβ”€β”€ config/             # mongo, sslcommerz, etc.
β”œβ”€β”€ constance/          # enumerations & magic numbers
β”œβ”€β”€ errors/             # ApiError, global handler
β”œβ”€β”€ shared/             # helpers (catchAsync, sendResponse, pick)
β”œβ”€β”€ app.ts              # Express app
└── server.ts           # entry point (env, DB connect, graceful shutdown)

βœ… Prerequisites

  • Node.js 20.4+ (LTS recommended)
  • Yarn 4.9+ or npm 10+
  • MongoDB (Atlas or local)
  • SSL Commerz merchant account

⚑ Quick Start

# 1. Clone
git clone https://github.com/tareksabbir/shoishob-backend.git
cd shoishob-backend

# 2. Install
yarn install        # or: npm ci

# 3. Configure
cp .env.example .env   # fill real secrets

# 4. Run dev server (auto-restart, TS support)
yarn start

Visit [http://localhost:5000 /health](http://localhost:5000 /health) β†’ should return {status:"OK"}.


πŸ” Environment Variables

Name Example Required Description
NODE_ENV development βœ” development / production
PORT 5000 βœ” API port
DATABASE_URL mongodb+srv://... βœ” Mongo connection string
JWT_SECRET s3cr3t βœ” HS256 signing secret
JWT_EXPIRES_IN 1d βœ” Token TTL
STORE_ID shoishob_live βœ” SSL Commerz store id
STORE_PASSWORD abcd1234 βœ” SSL Commerz key
CLIENT_URL https://shoishob.app βœ– CORS allow-list
RATE_LIMIT_WINDOW 15 βœ– minutes
RATE_LIMIT_MAX 100 βœ– reqs per window

πŸ› οΈ Scripts

Command What it does
yarn start Start dev server with ts-node-dev
yarn build Compile TypeScript to dist
yarn start:prod Run compiled JS with node
yarn lint:check ESLint analysis
yarn lint:fix ESLint auto-fix
yarn prettier:check Format check
yarn prettier:fix Auto-format
yarn test (add Jest tests soon)
yarn lint-prettier Lint + format in one go

πŸš€ Production Deployment

1. Docker (recommended)

FROM node:20-alpine
WORKDIR /app
COPY package*.json ./
RUN yarn install --prod
COPY . .
RUN yarn build
CMD ["node", "dist/server.js"]
docker build -t shoishob-api .
docker run -p 5000:5000 --env-file .env shoishob-api

2. GitHub Actions CI

# .github/workflows/deploy.yml
name: Deploy
on: push
jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: actions/setup-node@v4
        with: { node-version: '20' }
      - run: yarn install --immutable
      - run: yarn test # add tests!
      - run: yarn build
      # TODO: docker push / ssh deploy / render.com deploy…

πŸ§ͺ Testing Strategy

  • Unit tests: pure functions, services (jest, ts-jest)
  • Integration tests: Express routes with in-memory Mongo (supertest, mongodb-memory-server)
  • E2E: Postman/Newman or Playwright against staging

Add a __tests__/ folder mirroring src/. Aim for 80%+ coverage on business logic.


🌐 API Docs (Swagger/OpenAPI)

  1. Install: yarn add swagger-ui-express swagger-jsdoc
  2. Define specs in docs/openapi.yaml (see sample below).
  3. Mount swagger UI in app.ts:
import swaggerUi from 'swagger-ui-express'
import specs from './docs'

app.use('/docs', swaggerUi.serve, swaggerUi.setup(specs))

Now GET /docs shows live API reference.


πŸ”’ Security Hardening Checklist

  • helmet() HTTP headers
  • express-rate-limit 100 req/15 min per IP
  • .env never committed (git ignored)
  • Mongo connection retries, graceful shutdown
  • Mongoose ObjectId sanitization (mongoose-unique-validator)
  • Unit tests for auth & permissions
  • TODO: Enable CSRF protection for cookie-based auth
  • TODO: Turn on log redaction for sensitive fields

πŸ› Graceful Error Handling

import { ApiError } from './errors'
import httpStatus from 'http-status'

throw new ApiError(httpStatus.BAD_REQUEST, 'Email already exists')

All thrown ApiErrors propagate to the global handler β†’ client gets a consistent JSON error envelope:

{
  "success": false,
  "message": "Email already exists",
  "statusCode": 400,
  "stack": "..." // only in dev
}

πŸ’Έ Payment Flow (SSL Commerz)

  1. Client hits POST /api/v1/payments/init with cart & customer info.
  2. Server creates transaction, gets GatewayPageURL, returns to client.
  3. Client redirects user β†’ SSL Commerz hosted checkout.
  4. After payment, SSL Commerz calls IPN β†’ POST /api/v1/payments/ipn.
  5. We verify signature, update order status, trigger webhooks or emails.
  6. User redirected back to our success or cancel page.

✍️ Commit & Branch Convention

  • main – always deployable
  • dev – integration branch
  • feature/xyz, bugfix/123 – short-lived topic branches

Commit messages follow Conventional Commits:

feat(user): add password reset
fix(payment): correct amount rounding

🀝 Contributing

  1. Fork & create a topic branch: git checkout -b feat/cool-stuff
  2. Run yarn lint-prettier and add tests.
  3. Commit & push, then open a Pull Request.

All PRs trigger CI: lint, tests, type-check.


πŸ—ΊοΈ Roadmap

  • ☐ Refresh-token rotation & logout-all
  • ☐ 2-FA (TOTP)
  • ☐ Soft-delete + audit logs
  • ☐ GraphQL gateway (stretch goal)

πŸ™‹ FAQ

Mongo fails to connect on first start

Check that the DATABASE_URL in .env points to an SRV Atlas URL or your local instance, and your IP is whitelisted in Atlas.

How do I seed initial data?

Run yarn ts-node scripts/seed.ts (script coming soon).


πŸ“ License

MIT


Author

Md. Tarek Rahman Sabbir – https://github.com/tareksabbir


About

Robust Node.js REST API built with TypeScript, Express.js, and MongoDB. It powers the Shoishob platform, handling authentication, payments, and data access with production-grade security and code quality tooling.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published