# abujubair7/medicine-tracker architecture

> Architecture diagram of the GitHub repository abujubair7/medicine-tracker, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-05-26). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/abujubair7/medicine-tracker
- Repository: https://github.com/abujubair7/medicine-tracker

## Overview

1. **What this repository is**
   - A Node.js backend for the Dosely medicine-tracking app.
   - It is built with Express, TypeScript, and TypeORM, and persists data in PostgreSQL.
   - The backend is organized as a modular API service rather than a monolith with a single large controller.

2. **Top-level application shape**
   - `src/index.ts` and `src/main.ts` are the startup path.
   - `appController.ts` and `appServices.ts` appear to provide root-level application wiring or shared endpoints, while feature logic lives in modules.
   - Runtime entry is therefore: bootstrap app → register middleware/routes → connect database → serve HTTP requests.

3. **Domain modules are the main architectural boundary**
   - There are two primary feature modules under `src/modules/`:
     - `user/` for authentication and user management
     - `stock/` for medicine stock management
   - Each module follows the same internal pattern:
     - DTOs for input shape/validation
     - entities for database mapping
     - services for business logic
     - controllers for HTTP handling
   - This makes the request flow and ownership boundaries easy to diagram.

4. **Request handling flow**
   - HTTP request enters Express.
   - Global middleware runs first:
     - `authMiddleware.ts` for token-based access control
     - `validateDTO.ts` for request validation
   - Controllers translate the request into service calls.
   - Services contain the actual business rules and talk to TypeORM repositories/entities.
   - Responses are then formatted back by the controller layer.

5. **User subsystem**
   - The user module supports signup, login, and Google login, plus authenticated user lookup endpoints.
   - `userEntity.ts` maps user records to the database.
   - `createUser.dto.ts` and `loginUser.dto.ts` define the allowed request payloads.
   - This module is responsible for identity and authentication-related state, including JWT-based access patterns implied by the middleware and README.

6. **Stock subsystem**
   - The stock module owns medicine containers and medicine records.
   - `stockEntity.ts` and `medicineEntity.ts` represent the database side of that domain.
   - DTOs separate stock container creation/update data from medicine insertion/update data.
   - Endpoints in this module cover:
     - listing stocks for a user
     - creating, renaming, and deleting stocks
     - adding, updating, and deleting medicines within a stock

7. **Important business logic boundary**
   - `src/helpers/deductionHelper.ts` is shared business logic rather than controller logic.
   - It is used for automatic medicine deduction behavior when a stock is fetched.
   - This is a key domain behavior that sits between the service layer and persistence layer, not in the HTTP layer.

8. **Database and migration architecture**
   - `src/database/data-source.ts` is the TypeORM configuration boundary between app code and PostgreSQL.
   - Active schema evolution happens through `src/database/migrations/`.
   - `archived_migrations/` contains older migration history kept for reference, while `migrations/` contains the current deployable migration chain.
   - The repository therefore relies on migration-driven schema changes rather than sync-on-start behavior.

9. **Performance-oriented data access**
   - The README indicates stock listing is optimized to return a medicine count without loading every medicine row.
   - The stock create/insert flows are designed to return small, targeted payloads rather than re-fetching full aggregates.
   - That suggests the services are deliberately shaped to minimize database load and keep API responses lightweight.

10. **Security and API protection**
   - Authentication is enforced at the middleware layer, likely using JWTs and request headers.
   - Validation is also centralized, so DTOs act as a contract before business logic runs.
   - This keeps security and input sanity at the edges of the system, not scattered across services.

11. **Testing architecture**
   - Tests are split into `unit/` and `integration/`.
   - Unit tests cover isolated logic such as services, middleware, helpers, and validation.
   - Integration tests cover HTTP routes with a lightweight app setup.
   - The tests are structured to avoid depending on a real database, which keeps the core architecture service-oriented and mock-friendly.

12. **Delivery and operational shape**
   - The repo includes Docker support via `Dockerfile` and `compose.yml`, indicating containerized local/dev/prod execution.
   - GitHub Actions CI is present in `.github/workflows/ci.yml`, so build/test verification is part of the pipeline.
   - `vercel.json` suggests deployment configuration is also tracked at the repo level, even though the app itself is a traditional backend API service.

Main entry points: [`src/main.ts`](https://github.com/abujubair7/medicine-tracker/blob/main/src/main.ts) (main.ts), [`src/modules/stock/stockServices.ts`](https://github.com/abujubair7/medicine-tracker/blob/main/src/modules/stock/stockServices.ts) (stock services), [`src/tests/unit/userServices.test.ts`](https://github.com/abujubair7/medicine-tracker/blob/main/src/tests/unit/userServices.test.ts) (unit tests), [`src/tests/integration/userRoutes.test.ts`](https://github.com/abujubair7/medicine-tracker/blob/main/src/tests/integration/userRoutes.test.ts) (integration tests)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_runtime["Runtime API"]
  node_src_index["index.ts<br/>bootstrap<br/>[index.ts]"]
  node_src_main["main.ts<br/>app bootstrap<br/>[main.ts]"]
  node_src_app_controller["app controller<br/>root routes<br/>[appController.ts]"]
  node_src_app_services["app services<br/>shared app logic<br/>[appServices.ts]"]
  node_auth_middleware["auth middleware<br/>request guard<br/>[authMiddleware.ts]"]
  node_validate_middleware["validate DTO<br/>request validation<br/>[validateDTO.ts]"]
end

subgraph group_domain["Domain Modules"]
  node_user_controller["user controller<br/>http controller<br/>[userController.ts]"]
  node_user_services["user services<br/>business logic<br/>[userServices.ts]"]
  node_user_entity[("user entity<br/>typeorm entity<br/>[userEntity.ts]")]
  node_user_dto["user DTOs<br/>input schema<br/>[createUser.dto.ts]"]
  node_stock_controller["stock controller<br/>http controller<br/>[stockController.ts]"]
  node_stock_services["stock services<br/>business logic<br/>[stockServices.ts]"]
  node_stock_entities[("stock entities<br/>typeorm entities<br/>[stockEntity.ts]")]
  node_stock_dto["stock DTOs<br/>input schema<br/>[stock.dto.ts]"]
  node_deduction_helper{{"deduction helper<br/>domain helper<br/>[deductionHelper.ts]"}}
end

subgraph group_persistence["Persistence"]
  node_data_source[("data source<br/>typeorm config<br/>[data-source.ts]")]
  node_migrations["migrations<br/>schema evolution"]
  node_archived_migrations["archived migrations<br/>migration history"]
end

subgraph group_ops["Delivery"]
  node_dockerfile["Dockerfile<br/>container build"]
  node_compose["compose.yml<br/>local stack<br/>[compose.yml]"]
  node_ci_workflow["ci workflow<br/>pipeline<br/>[ci.yml]"]
end

subgraph group_qa["Testing"]
  node_unit_tests["unit tests<br/>test suite"]
  node_integration_tests["integration tests<br/>test suite<br/>[userRoutes.test.ts]"]
end

node_src_index -->|"starts"| node_src_main
node_src_main -->|"registers"| node_auth_middleware
node_src_main -->|"registers"| node_validate_middleware
node_src_main -->|"mounts"| node_src_app_controller
node_src_main -->|"connects"| node_data_source
node_src_main -->|"routes"| node_user_controller
node_src_main -->|"routes"| node_stock_controller
node_src_app_controller -->|"uses"| node_src_app_services
node_user_controller -->|"validates"| node_validate_middleware
node_user_controller -->|"calls"| node_user_services
node_user_services -->|"persists"| node_user_entity
node_user_services -->|"consumes"| node_user_dto
node_stock_controller -->|"validates"| node_validate_middleware
node_stock_controller -->|"calls"| node_stock_services
node_stock_services -->|"persists"| node_stock_entities
node_stock_services -->|"consumes"| node_stock_dto
node_stock_services -->|"uses"| node_deduction_helper
node_stock_services -->|"queries"| node_data_source
node_data_source -->|"applies"| node_migrations
node_data_source -.->|"references"| node_archived_migrations
node_unit_tests -.->|"covers"| node_user_services
node_unit_tests -.->|"covers"| node_stock_services
node_unit_tests -.->|"covers"| node_auth_middleware
node_unit_tests -.->|"covers"| node_validate_middleware
node_integration_tests -.->|"boots"| node_src_main
node_integration_tests -.->|"hits"| node_user_controller
node_integration_tests -.->|"hits"| node_stock_controller
node_ci_workflow -->|"runs"| node_unit_tests
node_ci_workflow -->|"runs"| node_integration_tests
node_dockerfile -->|"packages"| node_src_main
node_compose -->|"orchestrates"| node_dockerfile

click node_src_index "https://github.com/abujubair7/medicine-tracker/blob/main/src/index.ts"
click node_src_main "https://github.com/abujubair7/medicine-tracker/blob/main/src/main.ts"
click node_src_app_controller "https://github.com/abujubair7/medicine-tracker/blob/main/src/appController.ts"
click node_src_app_services "https://github.com/abujubair7/medicine-tracker/blob/main/src/appServices.ts"
click node_auth_middleware "https://github.com/abujubair7/medicine-tracker/blob/main/src/middleware/authMiddleware.ts"
click node_validate_middleware "https://github.com/abujubair7/medicine-tracker/blob/main/src/middleware/validateDTO.ts"
click node_user_controller "https://github.com/abujubair7/medicine-tracker/blob/main/src/modules/user/userController.ts"
click node_user_services "https://github.com/abujubair7/medicine-tracker/blob/main/src/modules/user/userServices.ts"
click node_user_entity "https://github.com/abujubair7/medicine-tracker/blob/main/src/modules/user/entities/userEntity.ts"
click node_user_dto "https://github.com/abujubair7/medicine-tracker/blob/main/src/modules/user/dto/createUser.dto.ts"
click node_stock_controller "https://github.com/abujubair7/medicine-tracker/blob/main/src/modules/stock/stockController.ts"
click node_stock_services "https://github.com/abujubair7/medicine-tracker/blob/main/src/modules/stock/stockServices.ts"
click node_stock_entities "https://github.com/abujubair7/medicine-tracker/blob/main/src/modules/stock/entities/stockEntity.ts"
click node_stock_dto "https://github.com/abujubair7/medicine-tracker/blob/main/src/modules/stock/dto/stock.dto.ts"
click node_deduction_helper "https://github.com/abujubair7/medicine-tracker/blob/main/src/helpers/deductionHelper.ts"
click node_data_source "https://github.com/abujubair7/medicine-tracker/blob/main/src/database/data-source.ts"
click node_migrations "https://github.com/abujubair7/medicine-tracker/blob/main/src/database/migrations/1769419957484-DeployInit.ts"
click node_archived_migrations "https://github.com/abujubair7/medicine-tracker/blob/main/src/database/archived_migrations/1769068927828-InitialMigration.ts"
click node_dockerfile "https://github.com/abujubair7/medicine-tracker/tree/main/Dockerfile"
click node_compose "https://github.com/abujubair7/medicine-tracker/blob/main/compose.yml"
click node_ci_workflow "https://github.com/abujubair7/medicine-tracker/blob/main/.github/workflows/ci.yml"
click node_unit_tests "https://github.com/abujubair7/medicine-tracker/blob/main/src/tests/unit/userServices.test.ts"
click node_integration_tests "https://github.com/abujubair7/medicine-tracker/blob/main/src/tests/integration/userRoutes.test.ts"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_src_index,node_src_main,node_src_app_controller,node_src_app_services,node_auth_middleware,node_validate_middleware toneBlue
class node_user_controller,node_user_services,node_user_entity,node_user_dto,node_stock_controller,node_stock_services,node_stock_entities,node_stock_dto,node_deduction_helper toneAmber
class node_data_source,node_migrations,node_archived_migrations toneMint
class node_dockerfile,node_compose,node_ci_workflow toneRose
class node_unit_tests,node_integration_tests toneIndigo
```

## Components

### Runtime API

Application bootstrap and request edge

- [index.ts](https://github.com/abujubair7/medicine-tracker/blob/main/src/index.ts): bootstrap · Process entrypoint · `src/index.ts`
- [main.ts](https://github.com/abujubair7/medicine-tracker/blob/main/src/main.ts): app bootstrap · Wires app startup · `src/main.ts`
- [app controller](https://github.com/abujubair7/medicine-tracker/blob/main/src/appController.ts): root routes · Shared application endpoints · `src/appController.ts`
- [app services](https://github.com/abujubair7/medicine-tracker/blob/main/src/appServices.ts): shared app logic · Common application service layer · `src/appServices.ts`
- [auth middleware](https://github.com/abujubair7/medicine-tracker/blob/main/src/middleware/authMiddleware.ts): request guard · Token-based access control · `src/middleware/authMiddleware.ts`
- [validate DTO](https://github.com/abujubair7/medicine-tracker/blob/main/src/middleware/validateDTO.ts): request validation · DTO validation boundary · `src/middleware/validateDTO.ts`

### Domain Modules

Feature boundaries for user and stock logic

- [user controller](https://github.com/abujubair7/medicine-tracker/blob/main/src/modules/user/userController.ts): http controller · Auth and user endpoints · `src/modules/user/userController.ts`
- [user services](https://github.com/abujubair7/medicine-tracker/blob/main/src/modules/user/userServices.ts): business logic · Signup, login, Google login · `src/modules/user/userServices.ts`
- [user entity](https://github.com/abujubair7/medicine-tracker/blob/main/src/modules/user/entities/userEntity.ts): typeorm entity · User persistence model · `src/modules/user/entities/userEntity.ts`
- [user DTOs](https://github.com/abujubair7/medicine-tracker/blob/main/src/modules/user/dto/createUser.dto.ts): input schema · Create and login payloads · `src/modules/user/dto/createUser.dto.ts`
- [stock controller](https://github.com/abujubair7/medicine-tracker/blob/main/src/modules/stock/stockController.ts): http controller · Stock and medicine endpoints · `src/modules/stock/stockController.ts`
- [stock services](https://github.com/abujubair7/medicine-tracker/blob/main/src/modules/stock/stockServices.ts): business logic · Stock and medicine rules · `src/modules/stock/stockServices.ts`
- [stock entities](https://github.com/abujubair7/medicine-tracker/blob/main/src/modules/stock/entities/stockEntity.ts): typeorm entities · Stock and medicine persistence models · `src/modules/stock/entities/stockEntity.ts`
- [stock DTOs](https://github.com/abujubair7/medicine-tracker/blob/main/src/modules/stock/dto/stock.dto.ts): input schema · Stock and medicine payloads · `src/modules/stock/dto/stock.dto.ts`
- [deduction helper](https://github.com/abujubair7/medicine-tracker/blob/main/src/helpers/deductionHelper.ts): domain helper · Automatic medicine deduction logic · `src/helpers/deductionHelper.ts`

### Persistence

Database configuration and schema evolution

- [data source](https://github.com/abujubair7/medicine-tracker/blob/main/src/database/data-source.ts): typeorm config · PostgreSQL connection boundary · `src/database/data-source.ts`
- [migrations](https://github.com/abujubair7/medicine-tracker/blob/main/src/database/migrations/1769419957484-DeployInit.ts): schema evolution · Current deployable migration chain · `src/database/migrations/1769419957484-DeployInit.ts`
- [archived migrations](https://github.com/abujubair7/medicine-tracker/blob/main/src/database/archived_migrations/1769068927828-InitialMigration.ts): migration history · Older migration chain kept for reference · `src/database/archived_migrations/1769068927828-InitialMigration.ts`

### Delivery

Container and CI/CD support

- [Dockerfile](https://github.com/abujubair7/medicine-tracker/tree/main/Dockerfile): container build · Image build definition · `Dockerfile`
- [compose.yml](https://github.com/abujubair7/medicine-tracker/blob/main/compose.yml): local stack · Container orchestration for local runs · `compose.yml`
- [ci workflow](https://github.com/abujubair7/medicine-tracker/blob/main/.github/workflows/ci.yml): pipeline · GitHub Actions test/build automation · `.github/workflows/ci.yml`

### Testing

Unit and integration test structure

- [unit tests](https://github.com/abujubair7/medicine-tracker/blob/main/src/tests/unit/userServices.test.ts): test suite · Isolated logic and middleware tests · `src/tests/unit/userServices.test.ts`
- [integration tests](https://github.com/abujubair7/medicine-tracker/blob/main/src/tests/integration/userRoutes.test.ts): test suite · HTTP route coverage · `src/tests/integration/userRoutes.test.ts`

## Connections

- index.ts → main.ts: starts · Process entry enters app bootstrap
- main.ts → auth middleware: registers · App wires global request guard
- main.ts → validate DTO: registers · App wires request validation
- main.ts → app controller: mounts · Root endpoints are attached to Express
- main.ts → data source: connects · App connects to PostgreSQL via TypeORM
- main.ts → user controller: routes · User module is mounted into the API
- main.ts → stock controller: routes · Stock module is mounted into the API
- app controller → app services: uses · Root endpoints delegate shared logic
- user controller → validate DTO: validates · DTO validation occurs before service calls
- user controller → user services: calls · HTTP handling delegates to business logic
- user services → user entity: persists · User logic reads/writes user records
- user services → user DTOs: consumes · Service layer consumes shaped inputs
- stock controller → validate DTO: validates · Stock requests are DTO-checked
- stock controller → stock services: calls · HTTP handling delegates to stock logic
- stock services → stock entities: persists · Stock logic reads/writes medicine data
- stock services → stock DTOs: consumes · Service layer consumes stock payloads
- stock services → deduction helper: uses · Automatic deduction is shared domain logic
- stock services → data source: queries · Services use TypeORM-backed database access
- data source → migrations: applies · Active schema changes are driven by migrations
- data source → archived migrations: references · Older migration history is kept separate
- unit tests → user services: covers · Unit tests exercise isolated service logic
- unit tests → stock services: covers · Unit tests exercise stock logic
- unit tests → auth middleware: covers · Middleware behavior is tested in isolation
- unit tests → validate DTO: covers · Validation behavior is tested in isolation
- integration tests → main.ts: boots · Integration tests run against the app setup
- integration tests → user controller: hits · User routes are tested over HTTP
- integration tests → stock controller: hits · Stock routes are tested over HTTP
- ci workflow → unit tests: runs · CI verifies isolated logic
- ci workflow → integration tests: runs · CI verifies route behavior
- Dockerfile → main.ts: packages · Container image runs the API entrypoint
- compose.yml → Dockerfile: orchestrates · Compose coordinates containerized execution
