How darshan98solanki/ssd works
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.