# akshatmittal61/balance-it-server architecture

> Architecture diagram of the GitHub repository akshatmittal61/balance-it-server, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-04-02). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/akshatmittal61/balance-it-server
- Repository: https://github.com/akshatmittal61/balance-it-server

## Overview

##### Main Components
a. API/HTTP Server Layer:
- Main server (server.ts) that initializes the HTTP server.
- “routes” directory as the entry points for API requests.
- Nodes for middleware (from the “middlewares” directory) that handle authentication, error handling, etc.

b. Controller Layer:
- That controllers receive requests from the HTTP layer and call appropriate services.

c. Service Layer:
- “services” directory with various business logic components (auth.service, expense.service, group.service, etc.).
- For the email functionality, note the “services/email” subfolder with sender and templating modules.
- Link the controllers to these service components to demonstrate business logic processing.

d. Data / Repository Layer:
- “repo” directory which holds the data access code (auth.repo, expense.repo, group.repo, etc.).
- Also, include the “db” directory that manages the database connection (db/container.ts, db/index.ts) and types.

e. Model and Schema Layer:
- Models (from the “models” directory) which define data models, along with “schema” files that may be used for data validation.

f. Utility and Configuration Modules:
- Components for configuration (“config” folder), which includes base, email, oauth, and variables.
- “utils” directory that contains helper functions and API connectors which support various layers.
- Global constants and types (from the “constants” and “types” directories) that serve as standardized elements across the system.

##### Relationships and Interactions
- Then, link the controller to the corresponding service (e.g., auth.service) where the core business logic is processed.
- That services sometimes call the repository layer (represented by the “repo” directory) to perform CRUD operations on the database (shown via the “db” connection).

##### Architectural Patterns & Design Principles
- Layered architecture: clearly separating controllers, services, repositories, and configurations.
- Dependency injection and configuration management: configurations and modules loaded early in the application initialization.
- Modularity and Separation of Concerns: each directory (e.g., controllers, services, models) focuses on a specific role.
- Use of middleware in Express-like applications to handle authentication, security, and error management.

- That exceptions (errors and logging) are connected to all layers to emphasize robust error handling.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Top Level: Client Request
    Client["Client"]

    %% API/HTTP Server Layer
    subgraph "API/HTTP Server Layer"
        Svr["HTTP Server"]
        RoutesBase["Route: base.ts"]
        RoutesIndex["Route: index.ts"]
        SvrConn["Server Connection"]
    end

    %% Middleware Layer
    subgraph "Middleware Layer"
        MWAuth["Auth Middleware"]
        MWError["Error Middleware"]
        MWServer["Server Middleware"]
    end

    %% Controller Layer
    subgraph "Controller Layer"
        CtrlAuth["Auth Controller"]
        CtrlUser["User Controller"]
        CtrlWallet["Wallet Controller"]
        CtrlServer["Server Controller"]
    end

    %% Service Layer
    subgraph "Service Layer"
        SvcAuth["Auth Service"]
        SvcExpense["Expense Service"]
        SvcGroup["Group Service"]
        SvcOauth["Oauth Service"]
        SvcOTP["OTP Service"]
        SvcUser["User Service"]
        subgraph "Email Functionality"
            EmailSender["Email Sender"]
            EmailTemplate["Email Template"]
        end
    end

    %% Data/Repository Layer
    subgraph "Data/Repository Layer"
        subgraph "Repository"
            RepoAuth["Auth Repo"]
            RepoExpense["Expense Repo"]
            RepoGroup["Group Repo"]
        end
        subgraph "Database"
            DBContainer["DB Container"]
            DBIndex["DB Index"]
            DBTypes["DB Types"]
            DBConn["DB Connection"]
        end
    end

    %% Model and Schema Layer
    subgraph "Model and Schema Layer"
        Models["Models"]
        SchemaAuth["Auth Schema"]
        SchemaExpense["Expense Schema"]
        SchemaGroup["Group Schema"]
        SchemaMember["Member Schema"]
        SchemaOTP["OTP Schema"]
        SchemaSplit["Split Schema"]
        SchemaUser["User Schema"]
    end

    %% Utility and Configuration Modules
    subgraph "Utility & Configuration Modules"
        Config["Config"]
        Utils["Utils"]
        Constants["Constants"]
        Types["Types"]
    end

    %% Connections between layers
    Client -->|"HTTP request"| Svr
    Svr -->|"Registers"| RoutesBase
    Svr --> RoutesIndex
    RoutesBase --> SvrConn
    RoutesIndex --> SvrConn

    SvrConn -->|"Routes request"| MWAuth
    SvrConn --> MWError
    SvrConn --> MWServer

    MWAuth -->|"Pass request"| CtrlAuth
    MWError --> CtrlUser
    MWServer --> CtrlWallet
    %% Assume Server Controller handles system or generic endpoints
    MWServer --> CtrlServer

    %% Controllers call services (individual arrows for key relationships)
    CtrlAuth -->|"Calls"| SvcAuth
    CtrlUser --> SvcUser
    CtrlWallet --> SvcExpense
    CtrlServer --> SvcGroup
    %% Additional service calls (other services used by controllers)
    CtrlAuth --> SvcOauth
    CtrlAuth --> SvcOTP
    %% Email functionality used by controllers via services
    SvcAuth --> EmailSender
    SvcAuth --> EmailTemplate

    %% Services interact with Repository. Key mappings:
    SvcAuth --> RepoAuth
    SvcExpense --> RepoExpense
    SvcGroup --> RepoGroup

    %% Repository interacts with Database
    RepoAuth --> DBContainer
    RepoExpense --> DBIndex
    RepoGroup --> DBTypes
    DBContainer --> DBConn
    DBIndex --> DBConn
    DBTypes --> DBConn

    %% Models and Schemas are used by services and repositories
    SvcAuth --- Models
    RepoAuth --- SchemaAuth
    RepoExpense --- SchemaExpense
    RepoGroup --- SchemaGroup
    Models --- SchemaUser
    Models --- SchemaMember
    Models --- SchemaOTP
    Models --- SchemaSplit

    %% Utility and Configuration support across various layers (dashed lines)
    Config --- Svr
    Config --- SvcAuth
    Utils --- SvcExpense
    Constants --- CtrlUser
    Types --- Models

    %% Styling via classes
    class Svr,RoutesBase,RoutesIndex,SvrConn api
    class MWAuth,MWError,MWServer middleware
    class CtrlAuth,CtrlUser,CtrlWallet,CtrlServer controller
    class SvcAuth,SvcExpense,SvcGroup,SvcOauth,SvcOTP,SvcUser,EmailSender,EmailTemplate service
    class RepoAuth,RepoExpense,RepoGroup repo
    class DBContainer,DBIndex,DBTypes,DBConn database
    class Models,SchemaAuth,SchemaExpense,SchemaGroup,SchemaMember,SchemaOTP,SchemaSplit,SchemaUser model
    class Config,Utils,Constants,Types util

    %% Click Events for API/HTTP Server Layer
    click Svr "https://github.com/akshatmittal61/balance-it-server/blob/master/server.ts"
    click RoutesBase "https://github.com/akshatmittal61/balance-it-server/blob/master/routes/base.ts"
    click RoutesIndex "https://github.com/akshatmittal61/balance-it-server/blob/master/routes/index.ts"
    click SvrConn "https://github.com/akshatmittal61/balance-it-server/blob/master/connections/server.ts"

    %% Click Events for Middleware Layer
    click MWAuth "https://github.com/akshatmittal61/balance-it-server/blob/master/middlewares/auth.ts"
    click MWError "https://github.com/akshatmittal61/balance-it-server/blob/master/middlewares/error.ts"
    click MWServer "https://github.com/akshatmittal61/balance-it-server/blob/master/middlewares/server.ts"

    %% Click Events for Controller Layer
    click CtrlAuth "https://github.com/akshatmittal61/balance-it-server/blob/master/controllers/auth.controller.ts"
    click CtrlUser "https://github.com/akshatmittal61/balance-it-server/blob/master/controllers/user.controller.ts"
    click CtrlWallet "https://github.com/akshatmittal61/balance-it-server/blob/master/controllers/wallet.controller.ts"
    click CtrlServer "https://github.com/akshatmittal61/balance-it-server/blob/master/controllers/server.controller.ts"

    %% Click Events for Service Layer
    click SvcAuth "https://github.com/akshatmittal61/balance-it-server/blob/master/services/auth.service.ts"
    click SvcExpense "https://github.com/akshatmittal61/balance-it-server/blob/master/services/expense.service.ts"
    click SvcGroup "https://github.com/akshatmittal61/balance-it-server/blob/master/services/group.service.ts"
    click SvcOauth "https://github.com/akshatmittal61/balance-it-server/blob/master/services/oauth.service.ts"
    click SvcOTP "https://github.com/akshatmittal61/balance-it-server/blob/master/services/otp.service.ts"
    click SvcUser "https://github.com/akshatmittal61/balance-it-server/blob/master/services/user.service.ts"
    click EmailSender "https://github.com/akshatmittal61/balance-it-server/blob/master/services/email/sender.service.ts"
    click EmailTemplate "https://github.com/akshatmittal61/balance-it-server/blob/master/services/email/template.ts"

    %% Click Events for Data/Repository Layer
    click RepoAuth "https://github.com/akshatmittal61/balance-it-server/blob/master/repo/auth.repo.ts"
    click RepoExpense "https://github.com/akshatmittal61/balance-it-server/blob/master/repo/expense.repo.ts"
    click RepoGroup "https://github.com/akshatmittal61/balance-it-server/blob/master/repo/group.repo.ts"
    click DBContainer "https://github.com/akshatmittal61/balance-it-server/blob/master/db/container.ts"
    click DBIndex "https://github.com/akshatmittal61/balance-it-server/blob/master/db/index.ts"
    click DBTypes "https://github.com/akshatmittal61/balance-it-server/blob/master/db/types.ts"
    click DBConn "https://github.com/akshatmittal61/balance-it-server/blob/master/connections/db.ts"

    %% Click Events for Model and Schema Layer
    click Models "https://github.com/akshatmittal61/balance-it-server/blob/master/models/index.ts"
    click SchemaAuth "https://github.com/akshatmittal61/balance-it-server/blob/master/schema/auth.ts"
    click SchemaExpense "https://github.com/akshatmittal61/balance-it-server/blob/master/schema/expense.ts"
    click SchemaGroup "https://github.com/akshatmittal61/balance-it-server/blob/master/schema/group.ts"
    click SchemaMember "https://github.com/akshatmittal61/balance-it-server/blob/master/schema/member.ts"
    click SchemaOTP "https://github.com/akshatmittal61/balance-it-server/blob/master/schema/otp.ts"
    click SchemaSplit "https://github.com/akshatmittal61/balance-it-server/blob/master/schema/split.ts"
    click SchemaUser "https://github.com/akshatmittal61/balance-it-server/blob/master/schema/user.ts"

    %% Click Events for Utility and Configuration Modules
    click Config "https://github.com/akshatmittal61/balance-it-server/blob/master/config/index.ts"
    click Utils "https://github.com/akshatmittal61/balance-it-server/blob/master/utils/index.ts"
    click Constants "https://github.com/akshatmittal61/balance-it-server/blob/master/constants/index.ts"
    click Types "https://github.com/akshatmittal61/balance-it-server/blob/master/types/index.ts"
```
