# darshan98solanki/ssd architecture

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

- Interactive diagram: https://gitdiagram.com/darshan98solanki/ssd
- Repository: https://github.com/darshan98solanki/ssd

## Overview

##### Project Type and Purpose
- This is a backend API service built with Node.js and Express.js, exposing a set of RESTful endpoints for managing dairy purchase orders, bills, customer data, and PDF report generation. It uses MySQL as its persistence layer, Zod for request validation, JWT for authentication, and is configured for deployment on Vercel.

##### Key Components
a. Client (Consumer of the API)
- Could be a web frontend, mobile app, or Postman/Insomnia.
b. API Gateway / Express Server
- index.js: initializes Express, applies global middleware (CORS, JSON parser, JWT auth).
- connection.js: establishes and exports the MySQL connection pool.
- types.js: contains shared TypeScript/JS types or interfaces.
c. Middleware Layer
- JWT authentication middleware (verifies tokens on protected routes).
- Zod validation middleware (validates request payloads against schemas).
d. Router / Controller Layer
- Individual route handlers for login, signup, purchases, bills, reports, profile, etc.
- Each handler invokes the service/business logic layer.
e. Service / Business Logic Layer
- Implements domain logic: create customer, compute standard price, add/fetch/update/delete purchases, calculate due amounts, mark payments, filter by organization/dates, etc.
f. Data Access Layer
- Uses connection.js to execute SQL queries against the MySQL database.
- Manages schema objects: users, customers, purchases, payments.
g. PDF Report Generator
- Reads basefiles in /reports/basefiles (templates), merges with data, writes PDF to /reports.
h. Storage
- Local file system (reports folder) or a configured cloud storage (if extended).
i. Database
- MySQL instance (hosted or cloud-hosted) with tables for users, organizations, customers, purchases, payments.
j. Deployment Platform
- Vercel (configured via vercel.json).

##### Interactions & Data Flows
- The Client sends HTTP(S) requests to the Express API.
- Requests pass through the JWT middleware (for routes requiring authentication).
- Payloads are validated by Zod middleware.
- Validated requests hit the router/controller layer; controllers call the service layer.
- Service layer issues SQL queries via the Data Access layer to MySQL.
- Query results propagate back up to controllers, which return JSON responses (or trigger PDF generation).
- For report endpoints, service fetches data, passes it to the PDF generator, which writes files to the reports directory; controllers then return a download link or the PDF bytes.

##### Architectural Patterns & Principles
- Layered architecture (Middleware → Controllers → Services → Data Access).
- Separation of concerns: validation, authentication, business logic, data persistence.
- Stateless REST APIs with JWT-based security.
- Configuration-as-code (connection.js, vercel.json).

##### Detailed Breakdown to Capture
- Authentication routes (login, signup, forgot_password) and JWT token lifecycle.
- Protected CRUD routes for customers, purchases, bills, and profile settings.
- Report generation flow: /get_full_report → Service gathers user & purchase data → PDF generator uses basefiles → outputs to reports directory.
- Query-based endpoints for filtering by organization, dates, payment status, today’s bills, totals aggregation.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Presentation Layer
    Client["Client (Web/Mobile/Postman)"]:::external
    subgraph "API Layer" 
        direction TB
        Express["Express Server"]:::api
        Types["Shared Types / Interfaces"]:::api
    end

    %% Middleware Layer
    subgraph "Middleware Layer"
        direction TB
        JWT["JWT Authentication"]:::middleware
        Zod["Zod Validation"]:::middleware
    end

    %% Controller Layer
    Controllers["Controllers (Routes)"]:::service

    %% Service Layer
    Services["Business Logic Services"]:::service

    %% Data Access Layer
    DataAccess["Data Access Layer"]:::db

    %% Database
    MySQL["MySQL Database"]:::db

    %% PDF Generation & Storage
    subgraph "Report Generation"
        direction TB
        PDFGen["PDF Report Generator"]:::service
        BaseTemplates["Report Templates"]:::storage
        ReportsStorage["Generated Reports Storage"]:::storage
    end

    %% Deployment
    Vercel["Vercel Deployment"]:::platform

    %% Connections
    Client -->|"HTTP Request"| Express
    Express -->|"uses types"| Types
    Express -->|"incoming request"| JWT
    JWT -->|"validated token"| Zod
    Zod -->|"validated payload"| Controllers
    Controllers -->|"invoke"| Services
    Services -->|"SQL SELECT/INSERT/UPDATE"| DataAccess
    DataAccess -->|"query results"| Services
    Services -->|"return data"| Controllers
    Controllers -->|"HTTP Response"| Client

    %% Report flow
    Controllers -->|"GET /get_full_report"| Services
    Services -->|"gather data"| DataAccess
    Services -->|"pass to PDF generator"| PDFGen
    PDFGen -->|"reads templates"| BaseTemplates
    PDFGen -->|"writes PDF"| ReportsStorage
    Controllers -->|"return PDF link/bytes"| Client

    %% Database Connection
    DataAccess -->|"Connection Pool"| MySQL

    %% Deployment relationships
    Express -.->|deployed on| Vercel
    ReportsStorage -.->|deployed on| Vercel

    %% Click Events
    click Express "https://github.com/darshan98solanki/ssd/blob/main/index.js"
    click Types "https://github.com/darshan98solanki/ssd/blob/main/types.js"
    click DataAccess "https://github.com/darshan98solanki/ssd/blob/main/connection.js"
    click BaseTemplates "https://github.com/darshan98solanki/ssd/tree/main/reports/basefiles"
    click ReportsStorage "https://github.com/darshan98solanki/ssd/tree/main/reports/"
    click Vercel "https://github.com/darshan98solanki/ssd/blob/main/vercel.json"

    %% Styles
    classDef api fill:#E0F7FA,stroke:#006064,stroke-width:1px
    classDef middleware fill:#FFF3E0,stroke:#E65100,stroke-width:1px
    classDef service fill:#E8F5E9,stroke:#1B5E20,stroke-width:1px
    classDef db fill:#FFFDE7,stroke:#F9A825,stroke-width:1px
    classDef storage fill:#ECEFF1,stroke:#607D8B,stroke-width:1px
    classDef external fill:#F3E5F5,stroke:#4A148C,stroke-width:1px
    classDef platform fill:#FCE4EC,stroke:#AD1457,stroke-width:1px
```
