# manoj-js/message-management-system architecture

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

- Interactive diagram: https://gitdiagram.com/manoj-js/message-management-system
- Repository: https://github.com/manoj-js/message-management-system

## Overview

##### Project Type and Purpose
- Modular Monolith Backend Service
- Provides REST APIs for message CRUD + full-text search
- Implements Domain-Driven Design (DDD) + event-driven communication
- Multi-tenant support and distributed caching

##### File Structure
- src/app.module.ts: the root NestJS module
- src/common: cross-cutting concerns (middleware, DTOs, guards, interceptors, tenant context)
- src/message: message bounded context (controllers, services, repositories, schemas/entities)
- src/search: search context (controllers, services)
- src/elastic-consumer: background consumer that reads Kafka events and indexes into Elasticsearch
- src/shared: shared infra modules (Kafka base consumer/producer, Elasticsearch client, Redis client)
- docker-compose.yml: local infra (MongoDB, Kafka, Zookeeper, Elasticsearch, Redis)

##### README Insights
- REST-based API layer with Swagger/OpenAPI docs
- Event flow: on message create/update, a Kafka event is emitted → consumed by ElasticConsumer → indexed into Elasticsearch
- Search flows directly query Elasticsearch via Search Module
- Redis used for caching (e.g., pagination results or hot items)
- Multi-tenant context injected via middleware and guards

##### Main Components & Interactions
a. API Clients (e.g., Web/Mobile, Postman)
b. NestJS Application
– Common Module (LoggerMiddleware, TenantMiddleware, AuthGuard, GlobalExceptionFilter, TransformInterceptor)
– Message Module
- MessageController → MessageApplicationService →
– MongoDB Repository (CRUD)
– Kafka Producer (emit message events)
– Search Module
- SearchController → SearchApplicationService → Elasticsearch (reads indexed data)
– Elastic-Consumer Module (Kafka Consumer)
- Reads events → ElasticConsumerService → writes to Elasticsearch
– Shared Module
- Kafka clients, Elasticsearch service, Redis service
– Config Module (environment variables, connection URIs)
– Main.ts (application bootstrap, Swagger setup)
c. External Systems
– MongoDB (primary data store)
– Kafka Cluster (message broker)
– Elasticsearch Cluster (search index)
– Redis (distributed cache)

##### Architectural Patterns & Design Principles
- Domain-Driven Design: separate bounded contexts (message, search)
- Modular Monolith: feature modules loaded into a single process
- Event-Driven: decoupled write (Message Module) and read (Search) via Kafka
- CQRS-like segregation: writes go to MongoDB; reads go to Elasticsearch
- Middleware & Guards enforce multi-tenancy and authentication

## Diagram (Mermaid)

```mermaid
flowchart TB
    %% Clients
    subgraph "Clients"
        style Clients fill:#f0f0f0,stroke:#333,stroke-width:1px
        Web["Web/Mobile Clients"]:::clients
        Postman["Postman (Postman)"]:::clients
    end

    %% NestJS Application
    subgraph "NestJS Application"
        style NestJS Application fill:#e8f4fc,stroke:#333,stroke-width:1px
        AppModule["AppModule"]:::backend
        Main["Main (bootstrap & Swagger)"]:::backend

        subgraph "Common Module"
            Logger["LoggerMiddleware"]:::backend
            TenantMW["TenantMiddleware"]:::backend
            AuthG["AuthGuard"]:::backend
            ExFilter["GlobalExceptionFilter"]:::backend
            TI["TransformInterceptor"]:::backend
            TenantCtx["TenantContext"]:::backend
        end

        subgraph "Config Module"
            Config["Configuration"]:::backend
        end

        subgraph "Message Module"
            MsgCtrl["MessageController"]:::backend
            MsgApp["MessageApplicationService"]:::backend
            MsgRepoInt["MessageRepositoryInterface"]:::backend
            MsgRepo["MongoDBMessageRepository"]:::backend
            MsgProd["MessageProducerService"]:::backend
        end

        subgraph "Search Module"
            SearchCtrl["SearchController"]:::backend
            SearchApp["SearchApplicationService"]:::backend
        end

        subgraph "Elastic-Consumer Module"
            ElasticConsMod["ElasticConsumerModule"]:::backend
            MsgConsumer["MessageConsumer"]:::backend
            MsgEventInt["MessageEventInterface"]:::backend
        end

        subgraph "Shared Module"
            KafkaBaseProd["BaseProducer"]:::backend
            KafkaBaseCons["BaseConsumer"]:::backend
            ESservice["ElasticsearchService"]:::backend
            ESMapping["MessageMapping"]:::backend
        end
    end

    %% External Systems
    subgraph "External Systems"
        Mongo["MongoDB"]:::datastore
        Kafka["Kafka Cluster"]:::broker
        ES["Elasticsearch"]:::datastore
        Redis["Redis"]:::datastore
    end

    %% HTTP Request Flow
    Web -->|HTTP| Logger
    Postman -->|HTTP| Logger
    Logger -->|inject tenant| TenantMW
    TenantMW -->|auth guard| AuthG
    AuthG --> MsgCtrl
    TenantMW --> MsgCtrl
    MsgCtrl -->|calls| MsgApp
    MsgApp -->|CRUD| MsgRepo
    MsgRepo -->|reads/writes| Mongo
    MsgApp -->|produce event| MsgProd
    MsgApp -->|cache lookup/update| Redis

    %% Event Flow
    MsgProd -->|emit event| Kafka
    Kafka -->|consume| MsgConsumer
    MsgConsumer -->|index| ESservice
    ESservice -->|writes index| ES

    %% Search Flow
    SearchCtrl -->|calls| SearchApp
    SearchApp -->|queries| ES

    %% Bootstrapping and Module Links
    Main --> AppModule
    AppModule --> Config
    AppModule --> Logger
    AppModule --> TenantMW
    AppModule --> AuthG
    AppModule --> ExFilter
    AppModule --> TI
    AppModule --> TenantCtx
    AppModule --> MsgCtrl
    AppModule --> SearchCtrl
    AppModule --> ElasticConsMod
    AppModule --> KafkaBaseProd
    AppModule --> KafkaBaseCons
    AppModule --> ESservice

    %% Click Events
    click AppModule "https://github.com/manoj-js/message-management-system/blob/main/src/app.module.ts"
    click Main "https://github.com/manoj-js/message-management-system/blob/main/src/main.ts"
    click Logger "https://github.com/manoj-js/message-management-system/blob/main/src/common/middlewares/logger.middleware.ts"
    click TenantMW "https://github.com/manoj-js/message-management-system/blob/main/src/common/middlewares/tenant.middleware.ts"
    click AuthG "https://github.com/manoj-js/message-management-system/blob/main/src/common/guards/auth.guard.ts"
    click ExFilter "https://github.com/manoj-js/message-management-system/blob/main/src/common/filters/global-exception.filter.ts"
    click TI "https://github.com/manoj-js/message-management-system/blob/main/src/common/interceptor/transform.interceptor.ts"
    click TenantCtx "https://github.com/manoj-js/message-management-system/blob/main/src/common/contexts/tenant.context.ts"
    click Config "https://github.com/manoj-js/message-management-system/blob/main/src/config/configuration.ts"
    click MsgCtrl "https://github.com/manoj-js/message-management-system/blob/main/src/message/controllers/message.controller.ts"
    click MsgApp "https://github.com/manoj-js/message-management-system/blob/main/src/message/services/message-application.service.ts"
    click MsgProd "https://github.com/manoj-js/message-management-system/blob/main/src/message/services/message-producer.service.ts"
    click MsgRepo "https://github.com/manoj-js/message-management-system/blob/main/src/message/repositories/mongodb-message.repository.ts"
    click MsgRepoInt "https://github.com/manoj-js/message-management-system/blob/main/src/message/repositories/message.repository.interface.ts"
    click ESservice "https://github.com/manoj-js/message-management-system/blob/main/src/shared/elasticsearch/elasticsearch.service.ts"
    click ESMapping "https://github.com/manoj-js/message-management-system/blob/main/src/shared/elasticsearch/message.mapping.ts"
    click KafkaBaseProd "https://github.com/manoj-js/message-management-system/blob/main/src/shared/kafka/base.produer.ts"
    click KafkaBaseCons "https://github.com/manoj-js/message-management-system/blob/main/src/shared/kafka/base.consumer.ts"
    click SearchCtrl "https://github.com/manoj-js/message-management-system/blob/main/src/search/controllers/search.controller.ts"
    click SearchApp "https://github.com/manoj-js/message-management-system/blob/main/src/search/services/search-application.service.ts"
    click MsgConsumer "https://github.com/manoj-js/message-management-system/blob/main/src/elastic-consumer/message.consumer.ts"
    click MsgEventInt "https://github.com/manoj-js/message-management-system/blob/main/src/elastic-consumer/interfaces/message.event.inerface.ts"

    %% Styles
    classDef backend fill:#cfe2f3,stroke:#0b5394,stroke-width:1px
    classDef datastore fill:#d9ead3,stroke:#38761d,stroke-width:1px
    classDef broker fill:#f9cb9c,stroke:#e69138,stroke-width:1px
    classDef clients fill:#dddddd,stroke:#666,stroke-width:1px
```
