# yogeshkathayat/bike-stolen-app-nodejs-typescript architecture

> Architecture diagram of the GitHub repository yogeshkathayat/bike-stolen-app-nodejs-typescript, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-05-17). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/yogeshkathayat/bike-stolen-app-nodejs-typescript
- Repository: https://github.com/yogeshkathayat/bike-stolen-app-nodejs-typescript

## Overview

##### Project Type & Purpose
- Type: A Node.js-based RESTful backend microservice (no bundled frontend).
- Purpose: Expose HTTP endpoints for owners to report stolen bikes and for police officers to search, assign, and resolve cases. Uses Express, Sequelize (PostgreSQL), hosted on Heroku.

##### High-Level Component Breakdown
a. Clients & External Users
– Bike owners (reporting cases via HTTP requests or Postman)
– Police officers (search/report/update cases via HTTP)
– API docs consumer (browser hitting Swagger/UI).
b. API Gateway Layer
– Express app (server.ts + app.ts)
– Middlewares (error handling, validation)
c. Application Layer (MVC + Service)
– Routes (src/api/routes)
– Controllers (src/api/controllers)
– Validation middleware (src/api/validations)
– Business Services (src/api/services)
d. Data Access Layer
– Models (src/api/models) with Sequelize ORM
– Database connection/config (src/config/sequelize.ts)
e. Supporting Modules
– Configuration (env vars, constants, logger) in src/config
– Utility helpers (src/util/response.ts)
f. Persistence
– PostgreSQL (Heroku-hosted)
g. Documentation & Tests
– Static API docs (docs/ folder; served at /api/v1/docs/)
– Unit tests (Jest + Supertest under test/)

##### Relationships & Data Flow
1. Client → HTTP request → Express Router
2. Router → (Validation Middleware) → Controller
3. Controller → Service Layer (business rules, automatic assignment logic)
4. Service → Model → PostgreSQL
5. DB → Service → Controller → HTTP response → Client
6. Errors bubble up to centralized error middleware → formatted error response
7. API docs (static files) served alongside Express endpoints

##### Architectural Patterns & Design Principles
- Layered architecture (Router → Controller → Service → Data Access)
- Separation of concerns: validation, logging, error handling extracted to middlewares/utilities
- ORM abstraction (Sequelize) encapsulates SQL queries behind model definitions
- Auto-assignment logic embedded in service layer to ensure one-case-per-officer constraint

##### Final Checklist
[ ] All major components (Clients, Express App, Middlewares, Controllers, Services, Models, DB)
[ ] Annotations for key libraries/frameworks (Express, Sequelize, Jest)
[ ] Deployment target (Heroku) and environment (Node.js, Postgres)

## Diagram (Mermaid)

```mermaid
flowchart TB
    %% Clients Layer
    subgraph "Clients"
        BO["Bike Owner"]:::http
        PO["Police Officer"]:::http
        DOC["API Docs Consumer"]:::http
    end

    %% API Gateway Layer
    subgraph "API Gateway Layer"
        SE["server.ts"]:::config
        AP["app.ts"]:::config
        ERR["error.ts"]:::config
        VAL_GLOBAL["validation middleware"]:::business
    end

    %% Router Layer
    subgraph "Router Layer"
        RI["index.ts"]:::http
        CR["case.route.ts"]:::http
        OR["officer.route.ts"]:::http
        DR["department.route.ts"]:::http
    end

    %% Validation Layer
    subgraph "Validation Layer"
        CV["case.validation.ts"]:::business
        OV["officer.validation.ts"]:::business
        DV["department.validation.ts"]:::business
    end

    %% Controller Layer
    subgraph "Controller Layer"
        CC["case.controller.ts"]:::business
        OC["officer.controller.ts"]:::business
        DC["department.controller.ts"]:::business
    end

    %% Service Layer
    subgraph "Service Layer"
        CS["case.service.ts"]:::business
        OS["officer.service.ts"]:::business
        DS["department.service.ts"]:::business
    end

    %% Data Access Layer
    subgraph "Data Access Layer"
        CM["case.model.ts"]:::data
        OM["officer.model.ts"]:::data
        DM["department.model.ts"]:::data
    end

    %% Database
    DB["PostgreSQL"]:::db

    %% Configuration & Utilities
    subgraph "Configuration & Utilities"
        VARS["vars.ts"]:::config
        CONST["constants.ts"]:::config
        LOG["logger.ts"]:::config
        SEQ["sequelize.ts"]:::config
        RESP["response.ts"]:::config
    end

    %% API Docs
    DOCS["docs/"]:::http

    %% Connections
    BO -->|"HTTP Request"| SE
    PO -->|"HTTP Request"| SE
    DOC -->|"GET /api/v1/docs"| DOCS

    SE -->|"imports"| AP
    AP -->|"use error middleware"| ERR
    AP -->|"use validation middleware"| VAL_GLOBAL
    AP -->|"use routes"| RI

    RI -->|"mounts"| CR
    RI -->|"mounts"| OR
    RI -->|"mounts"| DR

    CR -->|"POST /cases"| CV
    OR -->|"POST /officers"| OV
    DR -->|"POST /departments"| DV

    CV -->|"validated data"| CC
    OV -->|"validated data"| OC
    DV -->|"validated data"| DC

    CC -->|"calls"| CS
    OC -->|"calls"| OS
    DC -->|"calls"| DS

    CS -->|"uses ORM"| CM
    OS -->|"uses ORM"| OM
    DS -->|"uses ORM"| DM

    CM -->|"SQL Queries"| DB
    OM -->|"SQL Queries"| DB
    DM -->|"SQL Queries"| DB

    %% Config & Utilities connections
    AP -->|"reads env"| VARS
    AP -->|"reads constants"| CONST
    AP -->|"uses logger"| LOG
    SEQ -->|"DB connection"| CM
    SEQ -->|"DB connection"| OM
    SEQ -->|"DB connection"| DM
    AP -->|"uses response helper"| RESP

    AP -->|"serves static"| DOCS

    %% Click Events
    click SE "https://github.com/yogeshkathayat/bike-stolen-app-nodejs-typescript/blob/master/src/server.ts"
    click AP "https://github.com/yogeshkathayat/bike-stolen-app-nodejs-typescript/blob/master/src/config/app.ts"
    click VARS "https://github.com/yogeshkathayat/bike-stolen-app-nodejs-typescript/blob/master/src/config/vars.ts"
    click CONST "https://github.com/yogeshkathayat/bike-stolen-app-nodejs-typescript/blob/master/src/config/constants.ts"
    click LOG "https://github.com/yogeshkathayat/bike-stolen-app-nodejs-typescript/blob/master/src/config/logger.ts"
    click SEQ "https://github.com/yogeshkathayat/bike-stolen-app-nodejs-typescript/blob/master/src/config/sequelize.ts"
    click ERR "https://github.com/yogeshkathayat/bike-stolen-app-nodejs-typescript/blob/master/src/api/middlewares/error.ts"
    click RI "https://github.com/yogeshkathayat/bike-stolen-app-nodejs-typescript/blob/master/src/api/routes/index.ts"
    click CR "https://github.com/yogeshkathayat/bike-stolen-app-nodejs-typescript/blob/master/src/api/routes/case.route.ts"
    click OR "https://github.com/yogeshkathayat/bike-stolen-app-nodejs-typescript/blob/master/src/api/routes/officer.route.ts"
    click DR "https://github.com/yogeshkathayat/bike-stolen-app-nodejs-typescript/blob/master/src/api/routes/department.route.ts"
    click CC "https://github.com/yogeshkathayat/bike-stolen-app-nodejs-typescript/blob/master/src/api/controllers/case.controller.ts"
    click OC "https://github.com/yogeshkathayat/bike-stolen-app-nodejs-typescript/blob/master/src/api/controllers/officer.controller.ts"
    click DC "https://github.com/yogeshkathayat/bike-stolen-app-nodejs-typescript/blob/master/src/api/controllers/department.controller.ts"
    click CV "https://github.com/yogeshkathayat/bike-stolen-app-nodejs-typescript/blob/master/src/api/validations/case.validation.ts"
    click OV "https://github.com/yogeshkathayat/bike-stolen-app-nodejs-typescript/blob/master/src/api/validations/officer.validation.ts"
    click DV "https://github.com/yogeshkathayat/bike-stolen-app-nodejs-typescript/blob/master/src/api/validations/department.validation.ts"
    click CS "https://github.com/yogeshkathayat/bike-stolen-app-nodejs-typescript/blob/master/src/api/services/case.service.ts"
    click OS "https://github.com/yogeshkathayat/bike-stolen-app-nodejs-typescript/blob/master/src/api/services/officer.service.ts"
    click DS "https://github.com/yogeshkathayat/bike-stolen-app-nodejs-typescript/blob/master/src/api/services/department.service.ts"
    click CM "https://github.com/yogeshkathayat/bike-stolen-app-nodejs-typescript/blob/master/src/api/models/case.model.ts"
    click OM "https://github.com/yogeshkathayat/bike-stolen-app-nodejs-typescript/blob/master/src/api/models/officer.model.ts"
    click DM "https://github.com/yogeshkathayat/bike-stolen-app-nodejs-typescript/blob/master/src/api/models/department.model.ts"
    click RESP "https://github.com/yogeshkathayat/bike-stolen-app-nodejs-typescript/blob/master/src/util/response.ts"
    click DOCS "https://github.com/yogeshkathayat/bike-stolen-app-nodejs-typescript/tree/master/docs/"

    %% Styles
    classDef http fill:#D0E8FF,stroke:#0366d6,stroke-width:1px
    classDef business fill:#D2FFD0,stroke:#2c662d,stroke-width:1px
    classDef data fill:#FFF5C2,stroke:#997300,stroke-width:1px
    classDef db fill:#FFC8A2,stroke:#cc5500,stroke-width:1px
    classDef config fill:#E0E0E0,stroke:#555555,stroke-width:1px
```
