# cpojha/subscription-api architecture

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

- Interactive diagram: https://gitdiagram.com/cpojha/subscription-api
- Repository: https://github.com/cpojha/subscription-api

## Overview

##### Main Components
- API Server: Represent the primary Express server (app.js or index.js) that handles incoming HTTP requests.
- Configuration Layer: Show the config directory where various settings for arcjet, environment variables, nodemailer, redis, and upstash are defined.
- Routes: Represent the routing layer that maps URL endpoints to their corresponding controllers. Include endpoints for authentication, user management, subscriptions, and workflows.
- Database: Include MongoDB (using Mongoose for schema validation) as a central storage component, showing its interaction with the models.
- Models: Represent the Mongoose models (users, subscriptions, workflows) that define the data schema and persist data.
- Utility Modules: Indicate utility functions such as email templates, send-email functions, OTP handling, etc.
- External Services: Highlight third-party integrations like:
- Email Service (Nodemailer for sending emails)
- PM2 for process management
- Azure for deployment and network configuration (with port 3000 exposed)
- Redis/Upstash as potential caching or task queue layers
- Cron Jobs/Workers: Include a component for scheduled tasks (like the reminderWorker.js from the scripts directory or similar logic) that periodically checks subscriptions nearing expiration.

##### Relationships and Data Flow
- Separation between the API’s endpoints (routes) and middleware that intercepts requests for tasks like validation, error handling, and rate limiting.
- How the controllers interface with the models (data layer), and how results are eventually sent back as a response.
- Detailed interactions for workflow automation: how triggers (e.g., subscription expiring) lead to automated actions (renew subscription, send emails).
- Interactions with external services such as email delivery via Nodemailer and process management via PM2.

##### Architectural Patterns & Design Principles
- Layered Architecture/MVC: Emphasize that the API follows a layered structure (routes → controllers → models) which aligns with the MVC pattern.
- Middleware Pattern: Note the use of Express middleware for cross-cutting concerns (authentication, error handling, rate limiting).
- Configuration and Environment Management: Mention the use of configuration files for managing different aspects (database, nodemailer, redis/upstash) and environment variables for deployment.

##### Final Suggestions for the Principal Software Engineer
- Layers around it for configuration, middleware, routes/controllers, and database interactions.
- External services (Nodemailer, Azure, PM2, etc.) to their respective points of interaction.

This approach will help create a precise visual representation that reflects the underlying architecture, making it easier to analyze, troubleshoot, and plan future enhancements (such as integrating payment gateways or transitioning to a microservices architecture).

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Global Entities
    Client["Client Requests"]:::client

    %% API Server
    APIServer["Express API Server (app.js)"]:::server

    %% Configuration Layer
    subgraph "Configuration Layer"
        ArcjetConfig["Arcjet Config"]:::config
        EnvConfig["Environment Config"]:::config
        NodemailerConfig["Nodemailer Config"]:::config
        RedisConfig["Redis Config"]:::config
        UpstashConfig["Upstash Config"]:::config
    end

    %% Middleware Layer
    subgraph "Middleware Layer"
        ArcjetMiddleware["Arcjet Middleware (arcjet.middleware.js)"]:::middleware
        AuthMiddleware["Auth Middleware (auth.middleware.js)"]:::middleware
        ErrorMiddleware["Error Middleware (error.middleware.js)"]:::middleware
        RateLimitMiddleware["Rate Limit Middleware (rate-limit.middleware.js)"]:::middleware
    end

    %% Routes Layer
    subgraph "Routes Layer"
        AuthRoutes["Auth Routes (auth.routes.js)"]:::routes
        SubsRoutes["Subscriptions Routes (subs.routes.js)"]:::routes
        UserRouter["User Router (user.router.js)"]:::routes
        WorkflowRoutes["Workflow Routes (workflow.routes.js)"]:::routes
    end

    %% Controllers Layer
    subgraph "Controllers Layer"
        AuthController["Authentication Controller (auth.controllers.js)"]:::controller
        SubscriptionController["Subscription Controller (subscription.controllers.js)"]:::controller
        UsersController["Users Controller (users.controllers.js)"]:::controller
        WorkflowController["Workflow Controller (workflow.controllers.cjs)"]:::controller
    end

    %% Models Layer
    subgraph "Models Layer"
        SubsModel["Subscriptions Model (subs.model.js)"]:::model
        UserModel["User Model (user.model.js)"]:::model
    end

    %% Database (MongoDB)
    Database["MongoDB Database (database/mongodb.js)"]:::database

    %% Utility Modules
    subgraph "Utility Modules"
        EmailTemplate["Email Template Utils (email-template.js)"]:::utility
        SendEmail["Send Email Utils (send-email.js)"]:::utility
        SendOtpTemp["Send OTP Template (send-otp-temp.js)"]:::utility
        SendOtp["Send OTP (send-otp.js)"]:::utility
    end

    %% External Services
    subgraph "External Services"
        EmailService["Email Service (Nodemailer)"]:::external
        ProcessManager["Process Manager (PM2)"]:::external
        CloudDeployment["Cloud Deployment (Azure)"]:::external
        CacheService["Caching Service (Redis/Upstash)"]:::external
    end

    %% Cron Jobs / Workers
    CronWorker["Scheduled Cron Worker (Email Reminders)"]:::cron

    %% Data Flow Connections
    Client -->|"HTTP Request"| APIServer
    APIServer -->|"Loads"| ArcjetConfig
    APIServer -->|"Loads"| EnvConfig
    APIServer -->|"Loads"| NodemailerConfig
    APIServer -->|"Loads"| RedisConfig
    APIServer -->|"Loads"| UpstashConfig

    APIServer -->|"Processes Request Through"| ArcjetMiddleware
    APIServer --> AuthMiddleware
    APIServer --> ErrorMiddleware
    APIServer --> RateLimitMiddleware

    %% Middleware passes flow to Routes
    ArcjetMiddleware -->|"Passes Request"| AuthRoutes
    AuthMiddleware --> SubsRoutes
    ErrorMiddleware --> UserRouter
    RateLimitMiddleware --> WorkflowRoutes

    %% Routes to Controllers
    AuthRoutes -->|"Executes"| AuthController
    SubsRoutes -->|"Executes"| SubscriptionController
    UserRouter -->|"Executes"| UsersController
    WorkflowRoutes -->|"Executes"| WorkflowController

    %% Controllers interaction with Models & Database
    AuthController -->|"Queries/Updates"| UserModel
    SubscriptionController -->|"Queries/Updates"| SubsModel
    UsersController -->|"Queries/Updates"| UserModel
    WorkflowController -->|"Triggers Actions"| SubsModel

    UserModel -->|"Persists to"| Database
    SubsModel -->|"Persists to"| Database

    %% Controllers using Utility Modules and External Services
    AuthController -->|"Sends Emails via"| SendEmail
    SubscriptionController -->|"Uses"| EmailTemplate
    WorkflowController -->|"Triggers"| SendOtp
    WorkflowController -->|"Utilizes"| SendOtpTemp

    %% Utility Modules linked to External Services
    SendEmail -->|"Delivers via"| EmailService

    %% External monitoring and deployment
    APIServer -->|"Managed by"| ProcessManager
    APIServer -->|"Deployed on"| CloudDeployment
    CloudDeployment -->|"Configured with"| CacheService

    %% Cron Worker Flow
    CronWorker -->|"Schedules Email Reminders using"| SendEmail
    CronWorker -->|"Notifies via"| EmailService

    %% Click Events for Component Mapping
    click APIServer "https://github.com/cpojha/subscription-api/blob/main/app.js"
    click ArcjetConfig "https://github.com/cpojha/subscription-api/blob/main/config/arcjet.js"
    click EnvConfig "https://github.com/cpojha/subscription-api/blob/main/config/env.js"
    click NodemailerConfig "https://github.com/cpojha/subscription-api/blob/main/config/nodemailer.js"
    click RedisConfig "https://github.com/cpojha/subscription-api/blob/main/config/redis.js"
    click UpstashConfig "https://github.com/cpojha/subscription-api/blob/main/config/upstash.js"
    click ArcjetMiddleware "https://github.com/cpojha/subscription-api/blob/main/middleware/arcjet.middleware.js"
    click AuthMiddleware "https://github.com/cpojha/subscription-api/blob/main/middleware/auth.middleware.js"
    click ErrorMiddleware "https://github.com/cpojha/subscription-api/blob/main/middleware/error.middleware.js"
    click RateLimitMiddleware "https://github.com/cpojha/subscription-api/blob/main/middleware/rate-limit.middleware.js"
    click AuthController "https://github.com/cpojha/subscription-api/blob/main/controllers/auth.controllers.js"
    click SubscriptionController "https://github.com/cpojha/subscription-api/blob/main/controllers/subscription.controllers.js"
    click UsersController "https://github.com/cpojha/subscription-api/blob/main/controllers/users.controllers.js"
    click WorkflowController "https://github.com/cpojha/subscription-api/blob/main/controllers/workflow.controllers.cjs"
    click AuthRoutes "https://github.com/cpojha/subscription-api/blob/main/routes/auth.routes.js"
    click SubsRoutes "https://github.com/cpojha/subscription-api/blob/main/routes/subs.routes.js"
    click UserRouter "https://github.com/cpojha/subscription-api/blob/main/routes/user.router.js"
    click WorkflowRoutes "https://github.com/cpojha/subscription-api/blob/main/routes/workflow.routes.js"
    click Database "https://github.com/cpojha/subscription-api/blob/main/database/mongodb.js"
    click SubsModel "https://github.com/cpojha/subscription-api/blob/main/models/subs.model.js"
    click UserModel "https://github.com/cpojha/subscription-api/blob/main/models/user.model.js"
    click EmailTemplate "https://github.com/cpojha/subscription-api/blob/main/utils/email-template.js"
    click SendEmail "https://github.com/cpojha/subscription-api/blob/main/utils/send-email.js"
    click SendOtpTemp "https://github.com/cpojha/subscription-api/blob/main/utils/send-otp-temp.js"
    click SendOtp "https://github.com/cpojha/subscription-api/blob/main/utils/send-otp.js"

    %% Styles/Classes
    classDef client fill:#fef3bd,stroke:#333,stroke-width:2px;
    classDef server fill:#cce5ff,stroke:#333,stroke-width:2px;
    classDef config fill:#d1f2eb,stroke:#333,stroke-width:2px;
    classDef middleware fill:#fdebd0,stroke:#333,stroke-width:2px;
    classDef routes fill:#fadbd8,stroke:#333,stroke-width:2px;
    classDef controller fill:#d6eaf8,stroke:#333,stroke-width:2px;
    classDef model fill:#fcf3cf,stroke:#333,stroke-width:2px;
    classDef database fill:#e8daef,stroke:#333,stroke-width:2px,stroke-dasharray: 5, 5;
    classDef utility fill:#e8f8f5,stroke:#333,stroke-width:2px;
    classDef external fill:#f9e79f,stroke:#333,stroke-width:2px,stroke-dasharray: 3, 3;
    classDef cron fill:#f5cba7,stroke:#333,stroke-width:2px,stroke-dasharray: 2, 2;
```
