# kaan-market/kaan-backend architecture

> Architecture diagram of the GitHub repository kaan-market/kaan-backend, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-06-03). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/kaan-market/kaan-backend
- Repository: https://github.com/kaan-market/kaan-backend

## Overview

##### Project type and purpose
- This is a backend monolith for a B2B bulk-buying platform (“Kaan Backend”) built with Node.js, Express, PostgreSQL, and Prisma ORM.
- It exposes REST endpoints for phone-based authentication (OTP via Twilio) and user profile management. A simple static “test-frontend” directory provides a minimal client for manual API tests.

##### File structure
- Top level
- package.json, tsconfig.json, jest.config.js → config and dependencies
- prisma/ → database schema and migrations
- src/ → application source code
- test-frontend/ → minimal client for manual testing
- src/app.ts → main Express server setup
- src/middleware/ → cross-cutting concerns (auth guard, request validation)
- src/modules/
- auth/ → controllers, routes, service, tests for OTP-based authentication
- profile/ → routes and service for profile CRUD
- src/utils/ → OTP generation and Twilio integration
- src/generated/prisma/ → auto-generated Prisma client and runtime

##### README for additional insights
- Describes core modules (Auth, Profile) and their flows
- Lists environment variables (DATABASE_URL, TWILIO_*)
- Details database models (User, Otp) and migration history
- Outlines future modules (User Management, Product, Order, Group Buying)

##### Additional recommendations
- Flows (e.g., “OTP request”, “OTP verify → user upsert”)
- Environment variables or configuration externalized from code

## Diagram (Mermaid)

```mermaid
flowchart TD
    Client["Client<br/>(Test Frontend)"]:::client

    subgraph "Kaan Backend"
        direction TB
        App["Express Server<br/>(app.ts)"]:::server

        subgraph "Middleware"
            direction TB
            AuthMW["Auth Middleware"]:::middleware
            ValMW["Validation Middleware"]:::middleware
        end

        subgraph "Auth Module"
            direction TB
            AuthRoutes["Auth Routes"]:::module
            AuthCtrl["Auth Controller"]:::module
            AuthSrv["Auth Service"]:::module
        end

        subgraph "Profile Module"
            direction TB
            ProfileRoutes["Profile Routes"]:::module
            ProfileSrv["Profile Service"]:::module
        end

        subgraph "Utilities"
            direction TB
            TwilioUtil["Twilio Integration"]:::util
            OTPUtil["OTP Generator"]:::util
            ExceptUtil["Exception Util"]:::util
        end

        Prisma["Prisma Client"]:::prisma
    end

    DB["PostgreSQL DB"]:::db
    Twilio["Twilio SMS API"]:::external

    Client -->|HTTP| App
    App --> AuthMW
    App --> ValMW
    App --> AuthRoutes
    App --> ProfileRoutes
    AuthRoutes --> AuthCtrl
    AuthCtrl --> AuthSrv
    AuthSrv --> OTPUtil
    AuthSrv --> TwilioUtil
    TwilioUtil -->|API Call| Twilio
    OTPUtil -->|generate| AuthSrv
    AuthSrv --> Prisma
    ProfileRoutes --> ProfileSrv
    ProfileSrv --> Prisma
    Prisma --> DB

    click Client "https://github.com/kaan-market/kaan-backend/blob/main/test-frontend/index.html"
    click App "https://github.com/kaan-market/kaan-backend/blob/main/src/app.ts"
    click AuthMW "https://github.com/kaan-market/kaan-backend/blob/main/src/middleware/auth.ts"
    click ValMW "https://github.com/kaan-market/kaan-backend/blob/main/src/middleware/validateRequest.ts"
    click AuthRoutes "https://github.com/kaan-market/kaan-backend/blob/main/src/modules/auth/auth.routes.ts"
    click AuthCtrl "https://github.com/kaan-market/kaan-backend/blob/main/src/modules/auth/auth.controller.ts"
    click AuthSrv "https://github.com/kaan-market/kaan-backend/blob/main/src/modules/auth/auth.service.ts"
    click ProfileRoutes "https://github.com/kaan-market/kaan-backend/blob/main/src/modules/profile/profile.routes.ts"
    click ProfileSrv "https://github.com/kaan-market/kaan-backend/blob/main/src/modules/profile/profile.service.ts"
    click Prisma "https://github.com/kaan-market/kaan-backend/blob/main/prisma/schema.prisma"
    click TwilioUtil "https://github.com/kaan-market/kaan-backend/blob/main/src/utils/twilio.ts"
    click OTPUtil "https://github.com/kaan-market/kaan-backend/blob/main/src/utils/otp.ts"
    click ExceptUtil "https://github.com/kaan-market/kaan-backend/blob/main/src/utils/exceptions.ts"

    classDef client fill:#bbbbbb,stroke:#333,stroke-width:1px
    classDef server fill:#bbdefb,stroke:#333,stroke-width:1px
    classDef middleware fill:#c5cae9,stroke:#333,stroke-width:1px
    classDef module fill:#90caf9,stroke:#333,stroke-width:1px
    classDef util fill:#ffecb3,stroke:#333,stroke-width:1px
    classDef prisma fill:#c8e6c9,stroke:#333,stroke-width:1px
    classDef db fill:#a5d6a7,stroke:#333,stroke-width:1px
    classDef external fill:#ffcc80,stroke:#333,stroke-width:1px
```
