# varomnrg/studytrack architecture

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

- Interactive diagram: https://gitdiagram.com/varomnrg/studytrack
- Repository: https://github.com/varomnrg/studytrack

## Overview

##### Main Components
- API Layer: This is in the src/api directory containing separate subdirectories for agenda, authentications, notes, tasks, and users. Each subdirectory contains files for routes, handlers, and an index, showing clear endpoints segregation.
- Validation Layer: Under src/validator and its subdirectories corresponding to the API resources (agenda, authentications, notes, tasks, users), this layer ensures incoming data is properly formatted and meets the schema requirements.
- Service Layer: Located in src/services/postgres, this layer encapsulates PostgreSQL database interactions for various entities. Here, each service (e.g., AgendaService, UsersService) abstracts the database operations.
- Token Management: This is provided by the src/tokenize/TokenManager.js, indicating the use of JWT for authentication and authorization.
- Error Handling: The src/error directory contains custom error classes (ClientError, InvariantError, NotFoundError) to provide structured error handling.
- Database Migrations: The migrations directory holds migration scripts for creating tables, ensuring that the database schema is defined and maintained across different installations.

##### Relationships and Interactions
- A client (or API consumer) sends a request to the API endpoints.
- The request first enters the API layer via routes in src/api/{component}/routes.js. Each request is handled by a handler (found in handler.js) that orchestrates the flow.
- Prior to processing, the request data is validated by corresponding schemas in the src/validator/{component} directories.
- After validation, the handler forwards the request to the service layer (e.g., UsersService, NotesService) in src/services/postgres, which performs database operations.
- If authentication is required, the TokenManager (from src/tokenize) handles the creation and validation of JWTs.
- Finally, errors (if any) are dealt with centrally using the custom error classes from src/error.
- Responses (success or error) are then sent back to the client.

##### Architectural Patterns & Design Principles
- Layered Architecture: Separation of concerns is evident – API routing, validation, business logic, and database interactions are in isolated directories.
- RESTful Design Principles: Each API component adheres to REST conventions for its respective resource.
- Modular Design: The project is organized by resource, making future additions or modifications easier.
- Use of Middlewares/Handlers: Consistent error handling and token-based authentication are integrated as cross-cutting concerns.

##### Relevant Technologies
- Node.js with Hapi.js for the API server,
- PostgreSQL for database management,
- JWT for authentication (TokenManager),
- npm scripts provided for production, development, migrations, and testing,
- Custom error handling to maintain uniform error responses.

##### Additional Recommendations
- Mention that testing (located in src/tests) verifies these components work correctly together.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Client/Consumer Layer
    Client["Client/Consumer"]:::client

    %% API Layer
    subgraph "API Layer"
        AgendaAPI["Agenda API"]:::api
        AuthAPI["Authentications API"]:::api
        NotesAPI["Notes API"]:::api
        TasksAPI["Tasks API"]:::api
        UsersAPI["Users API"]:::api
    end

    %% Validation Layer
    subgraph "Validation Layer"
        AgendaValidator["Agenda Validator"]:::validator
        AuthValidator["Authentications Validator"]:::validator
        NotesValidator["Notes Validator"]:::validator
        TasksValidator["Tasks Validator"]:::validator
        UsersValidator["Users Validator"]:::validator
    end

    %% Service Layer
    subgraph "Service Layer"
        AgendaService["Agenda Service"]:::service
        AuthService["Authentications Service"]:::service
        NotesService["Notes Service"]:::service
        TasksService["Tasks Service"]:::service
        UsersService["Users Service"]:::service
    end

    %% Token Management
    subgraph "Token Management"
        TokenManager["JWT Token Manager"]:::token
    end

    %% Error Handling
    subgraph "Error Handling"
        ErrorModule["Error Handler"]:::error
    end

    %% Database Layer
    subgraph "Database"
        Database["PostgreSQL Database"]:::database
        Migrations["Database Migrations"]:::migration
    end

    %% Connections: Request Flow
    Client -->|"HTTP_Request"| AgendaAPI
    Client -->|"HTTP_Request"| AuthAPI
    Client -->|"HTTP_Request"| NotesAPI
    Client -->|"HTTP_Request"| TasksAPI
    Client -->|"HTTP_Request"| UsersAPI

    %% API to Token Manager for authentication
    AgendaAPI -- "auth_check" --> TokenManager
    AuthAPI -- "auth_check" --> TokenManager
    NotesAPI -- "auth_check" --> TokenManager
    TasksAPI -- "auth_check" --> TokenManager
    UsersAPI -- "auth_check" --> TokenManager

    %% API to Validation Layer
    AgendaAPI -->|"validate"| AgendaValidator
    AuthAPI -->|"validate"| AuthValidator
    NotesAPI -->|"validate"| NotesValidator
    TasksAPI -->|"validate"| TasksValidator
    UsersAPI -->|"validate"| UsersValidator

    %% Validation to Service Layer
    AgendaValidator -->|"process"| AgendaService
    AuthValidator -->|"process"| AuthService
    NotesValidator -->|"process"| NotesService
    TasksValidator -->|"process"| TasksService
    UsersValidator -->|"process"| UsersService

    %% Service Layer interactions with Database
    AgendaService -->|"db_operation"| Database
    AuthService -->|"db_operation"| Database
    NotesService -->|"db_operation"| Database
    TasksService -->|"db_operation"| Database
    UsersService -->|"db_operation"| Database

    Database -->|"response"| AgendaService
    Database -->|"response"| AuthService
    Database -->|"response"| NotesService
    Database -->|"response"| TasksService
    Database -->|"response"| UsersService

    %% Error Handling connections
    AgendaService -- "error" --> ErrorModule
    AuthService -- "error" --> ErrorModule
    NotesService -- "error" --> ErrorModule
    TasksService -- "error" --> ErrorModule
    UsersService -- "error" --> ErrorModule
    ErrorModule -- "error_response" --> AgendaAPI
    ErrorModule -- "error_response" --> AuthAPI
    ErrorModule -- "error_response" --> NotesAPI
    ErrorModule -- "error_response" --> TasksAPI
    ErrorModule -- "error_response" --> UsersAPI

    %% Database Migrations affecting Database schema
    Migrations ---|"migrate"| Database

    %% Click Events for API Layer
    click AgendaAPI "https://github.com/varomnrg/studytrack/tree/master/src/api/agenda"
    click AuthAPI "https://github.com/varomnrg/studytrack/tree/master/src/api/authentications"
    click NotesAPI "https://github.com/varomnrg/studytrack/tree/master/src/api/notes"
    click TasksAPI "https://github.com/varomnrg/studytrack/tree/master/src/api/tasks"
    click UsersAPI "https://github.com/varomnrg/studytrack/tree/master/src/api/users"

    %% Click Events for Validation Layer
    click AgendaValidator "https://github.com/varomnrg/studytrack/tree/master/src/validator/agenda"
    click AuthValidator "https://github.com/varomnrg/studytrack/tree/master/src/validator/authentications"
    click NotesValidator "https://github.com/varomnrg/studytrack/tree/master/src/validator/notes"
    click TasksValidator "https://github.com/varomnrg/studytrack/tree/master/src/validator/tasks"
    click UsersValidator "https://github.com/varomnrg/studytrack/tree/master/src/validator/users"

    %% Click Events for Service Layer
    click AgendaService "https://github.com/varomnrg/studytrack/blob/master/src/services/postgres/AgendaService.js"
    click AuthService "https://github.com/varomnrg/studytrack/blob/master/src/services/postgres/AuthenticationsService.js"
    click NotesService "https://github.com/varomnrg/studytrack/blob/master/src/services/postgres/NotesService.js"
    click TasksService "https://github.com/varomnrg/studytrack/blob/master/src/services/postgres/TasksService.js"
    click UsersService "https://github.com/varomnrg/studytrack/blob/master/src/services/postgres/UsersService.js"

    %% Click Event for Token Management
    click TokenManager "https://github.com/varomnrg/studytrack/blob/master/src/tokenize/TokenManager.js"

    %% Click Event for Error Handling
    click ErrorModule "https://github.com/varomnrg/studytrack/tree/master/src/error"

    %% Click Event for Database Migrations
    click Migrations "https://github.com/varomnrg/studytrack/tree/master/migrations"

    %% Styling
    classDef client fill:#f9c,stroke:#333,stroke-width:2px;
    classDef api fill:#bbf,stroke:#333,stroke-width:2px;
    classDef validator fill:#afa,stroke:#333,stroke-width:2px;
    classDef service fill:#fed,stroke:#333,stroke-width:2px;
    classDef token fill:#eae,stroke:#333,stroke-width:2px;
    classDef error fill:#faa,stroke:#333,stroke-width:2px;
    classDef database fill:#ddd,stroke:#333,stroke-width:2px;
    classDef migration fill:#cfc,stroke:#333,stroke-width:2px;
```
