# luciano655dev/daykeeper-api architecture

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

- Interactive diagram: https://gitdiagram.com/luciano655dev/daykeeper-api
- Repository: https://github.com/luciano655dev/daykeeper-api

## Overview

##### Main Components
- API Entry Point & Configuration:
- The main entry is app.js along with a config.js for global settings and constants. This is where the server is initialized.
- Routing Layer:
- The “routes” directory organizes all the endpoints (adminRoutes, authRoutes, dailyQuestionRoutes, dayRoutes, locationRoutes, postRoutes, searchRoutes, storiesRoutes, userRoutes). These routes map incoming HTTP requests to the appropriate controllers.
- Controller Layer:
- The “api/controllers” directory is where you find different controllers (admin, auth, daily question, day, location, post, stories, user). These components handle parsing the request and invoking business logic.
- Business Logic/Service Layer:
- Within “api/services” there are dedicated service files separated by domain (admin, auth, dailyQuestion, day, post, stories, user). They encapsulate the core business operations and often call the repository layer.
- Data Access (Repositories) Layer:
- The “api/repositories” directory’s structure (including “common”, “pipelines”, and a “hideProject” subfolder) suggests the use of pipeline patterns where data is validated and transformed before being sent to or retrieved from the database.
- Data Models:
- In “api/models”, MongoDB models are defined for various entities such as User, Post, CommentLikes, Stories, DailyQuestion, and so on.
- Middleware & Validations:
- The “middlewares” folder organizes various authentication, authorization, validation, and error-handling layers that are applied to many routes.
- Background Jobs:
- The “api/jobs” folder handles scheduled tasks like user cleanup and sending notifications.
- Utilities & External Services:
- Within “api/utils”, components such as email handling, timezone conversion, file deletion, and notification services assist the main business logic.
- External Dependencies:
- The integration with technologies such as AWS (for S3 in api/config/awsS3Config.js), and the use of Express, MongoDB, and NodeJS are crucial backbone services.

##### Relationships & Data Flow
- External Clients (e.g., App, Web) → HTTP Requests hit the defined endpoints in the “routes” directory.
- Routes then forward the requests through Middlewares (authentication, token validation, file validations, etc.) to the Controllers.
- Controllers parse requests, then delegate the processing to Service files.
- Service files execute business logic and interact with the Repository layer to perform CRUD operations on data models.
- Repositories use pipelines to gather and process required data before passing it to the services.
- Model operations interact directly with the MongoDB database.
- Background Jobs run on timed intervals (separate processes) performing tasks like deleting unconfirmed users and sending notifications.
- Utilities provide cross-cutting support like email notifications and file management in AWS.
- The configuration files (like config.js in the root and awsS3Config.js in the api/config folder) tie together environment-specific settings.

##### Architectural Patterns & Design Principles
- MVC (or close to layered architecture) – Routes/Controllers (Presentation), Services (Business Logic), Repositories/Models (Data Access).
- Separation of Concerns – Distinct directories for validations, middleware, models, repositories, services, and jobs.
- Pipeline/Chain-of-Responsibility – Seen in the repository pipelines and validations, ensuring data is processed in stages.
- Modularization – The project structure clearly separates modules by their responsibilities (such as “admin”, “user”, “day”, etc.) for maintainability and scalability.

##### Final Recommendations
- Always cross-check against the file tree to ensure every major module is represented.

## Diagram (Mermaid)

```mermaid
flowchart TD
    subgraph "Client Layer"
        Client["External Clients (Mobile, Web)"]:::client
    end

    subgraph "API Layer"
        Entry["API Entry (app.js)"]:::apientry
        Config["Config (config.js)"]:::config
    end

    subgraph "Routing Layer"
        Routes["Routes (routes/)"]:::routes
    end

    subgraph "Middleware Layer"
        Middleware["Middlewares (middlewares/)"]:::middleware
    end

    subgraph "Controller Layer"
        Controllers["Controllers (api/controllers/)"]:::controllers
    end

    subgraph "Service Layer"
        Services["Services (api/services/)"]:::services
    end

    subgraph "Data Access Layer"
        Repositories["Repositories (api/repositories/)"]:::repositories
        Models["Models (api/models/) - MongoDB"]:::database
    end

    subgraph "Background & Utilities"
        Jobs["Background Jobs (api/jobs/)"]:::jobs
        Utils["Utilities & External Services (api/utils/)"]:::utils
        AWS["AWS S3 Config (api/config/awsS3Config.js)"]:::aws
    end

    %% Connections
    Client -->|"sends HTTP request"| Entry
    Entry -->|"loads settings from"| Config
    Config -->|"defines endpoints for"| Routes
    Routes -->|"applies"| Middleware
    Middleware -->|"delegates to"| Controllers
    Controllers -->|"invokes"| Services
    Services -->|"calls"| Repositories
    Repositories -->|"interacts with"| Models

    %% Additional interactions
    Jobs -->|"triggers"| Services
    Utils -->|"supports"| Services
    Services -->|"utilizes"| Utils
    AWS -->|"configures"| Utils

    %% Click Events
    click Entry "https://github.com/luciano655dev/daykeeper-api/blob/main/app.js"
    click Config "https://github.com/luciano655dev/daykeeper-api/blob/main/config.js"
    click Routes "https://github.com/luciano655dev/daykeeper-api/tree/main/routes/"
    click Middleware "https://github.com/luciano655dev/daykeeper-api/tree/main/middlewares/"
    click Controllers "https://github.com/luciano655dev/daykeeper-api/tree/main/api/controllers/"
    click Services "https://github.com/luciano655dev/daykeeper-api/tree/main/api/services/"
    click Repositories "https://github.com/luciano655dev/daykeeper-api/tree/main/api/repositories/"
    click Models "https://github.com/luciano655dev/daykeeper-api/tree/main/api/models/"
    click Jobs "https://github.com/luciano655dev/daykeeper-api/tree/main/api/jobs/"
    click Utils "https://github.com/luciano655dev/daykeeper-api/tree/main/api/utils/"
    click AWS "https://github.com/luciano655dev/daykeeper-api/blob/main/api/config/awsS3Config.js"

    %% Styles
    classDef client fill:#fceabb,stroke:#e8890d,stroke-width:2px;
    classDef apientry fill:#cde,stroke:#333,stroke-width:2px;
    classDef config fill:#cde,stroke:#333,stroke-width:2px;
    classDef routes fill:#cfc,stroke:#090,stroke-width:2px;
    classDef middleware fill:#ccf,stroke:#009,stroke-width:2px;
    classDef controllers fill:#fcf,stroke:#909,stroke-width:2px;
    classDef services fill:#ffc,stroke:#cc0,stroke-width:2px;
    classDef repositories fill:#fcc,stroke:#900,stroke-width:2px;
    classDef database fill:#cff,stroke:#090,stroke-width:2px;
    classDef jobs fill:#efe,stroke:#060,stroke-width:2px;
    classDef utils fill:#eee,stroke:#555,stroke-width:2px;
    classDef aws fill:#eef,stroke:#556,stroke-width:2px;
```
