# hmrc/eori-common-component architecture

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

- Interactive diagram: https://gitdiagram.com/hmrc/eori-common-component
- Repository: https://github.com/hmrc/eori-common-component

## Overview

##### Project type and purpose
- This is a Scala/Play Framework backend microservice (“EORI Common Component”) that implements HTTP APIs for subscription management and interacts with multiple downstream HMRC services and a MongoDB datastore.
- It sits behind a frontend UI (EORI Common Component Frontend) and supports journeys like subscriber lookup, subscription handling, VAT info retrieval, email notifications, and tax enrolment.

##### File structure to infer architecture
- app/controllers – HTTP entry points (Play Controllers) that validate headers, parse JSON, invoke services, handle responses.
- app/services – Business logic layer coordinating data flows, building requests for connectors, orchestrating domain workflows (e.g. SubscriptionCompleteBusinessService).
- app/connectors – HTTP client adapters to downstream services (Customs Data Store, Email, GetVatCustomerInformation, MicroserviceAuth, SubscriptionDisplay, TaxEnrolments).
- app/repository – MongoDB repositories for Save4Later and RecipientDetails.
- app/domain & app/models & app/services/dto – Domain and DTO classes for request/response models.
- config & conf – Application configuration (AppConfig), routes, logging.
- audit – Audit trait to capture events for downstream reporting.
- it/test & test – Integration and unit tests.

##### Key architectural insights from README
- Uses Play MVC pattern: HTTP → Controller → Service → Connector/Repository → External Systems/Mongo.
- Requires external services: MongoDB, various HMRC microservices.
- Supports integration via Service Manager or Docker for local development.
- No separate frontend component here; it is purely a backend API service.

##### Main components and their relationships
a. External Clients & Triggers
- HTTP consumers (frontend or API clients) send requests to this service.
b. Play HTTP Layer
- Controllers receive requests, validate headers, parse JSON.
- ErrorResponses handle validation failures.
c. Business Logic Layer
- Services implement domain flows (e.g., retrieve VAT info, store interim data, invoke subscriptions, send email notifications).
- Audit trait mixed in to record key events.
d. Persistence Layer
- MongoDB repositories (Save4Later, RecipientDetails) persist state during journeys.
e. Connector Layer
- Each connector handles HTTP calls to an external HMRC system.
- MicroserviceAuthConnector obtains tokens, HttpStatusCheck/HandleResponses enforce status handling.
f. External Dependencies
- Customs Data Store service
- Email service
- Get VAT Customer Information service
- Subscription Display service
- Tax Enrolments service
- MongoDB
- (Optional) Authorization & auditing pipelines

##### Architectural patterns & design principles
- Layered architecture (Controllers → Services → Connectors/Repositories).
- Dependency injection via Play’s built‑in DI for services and connectors.
- Separation of concerns: domain models vs. transport DTOs.
- Fault handling and retry abstracted in connectors (HandleResponses).
- Audit cross‑cutting concern via trait mixing.

##### Diagramming suggestions
- “HTTP request/response” between Clients and Controllers
- “invoke” between Controllers → Services → Connectors/Repositories
- “persist/read” between Services and MongoDB endpoints
- “audit event” from Services to Audit pipeline
- API Layer (blue)
- Business Logic (green)
- Persistence (orange)
- External Systems (gray)

##### Detail level
- Save4Later flow: Controller → Services → Save4LaterRepository → MongoDB → Retrieval.
- Subscription workflow: HandleSubscriptionController → SubscriptionCompleteBusinessService → TaxEnrolmentsConnector → SubscriptionDisplayConnector → EmailService.

## Diagram (Mermaid)

```mermaid
flowchart TB
    %% Layers top to bottom
    Client["HTTP Clients / Frontend"]:::external

    subgraph "API Layer"
        Controllers["Play Controllers"]:::api
    end

    subgraph "Configuration"
        AppConfig["AppConfig.scala"]:::config
        RoutesConf["application.conf"]:::config
        ProdRoutes["prod.routes"]:::config
        BuildUrl["BuildUrl.scala"]:::config
    end

    subgraph "Business Logic Layer"
        Services["Services"]:::service
        DTOs["Data Transfer Objects"]:::dto
        Domain["Domain Models"]:::domain
        AuditTrait["Auditable Trait"]:::audit
    end

    subgraph "Persistence Layer"
        Repositories["Mongo Repositories"]:::persistence
    end

    subgraph "Integration Layer"
        Connectors["Connectors"]:::integration
    end

    subgraph "External Systems"
        MongoDB["MongoDB"]:::external
        CustomsDS["Customs Data Store"]:::external
        EmailSvc["Email Service"]:::external
        VATInfo["Get VAT Customer Information"]:::external
        SubDisplay["Subscription Display"]:::external
        TaxEnrol["Tax Enrolments"]:::external
        AuthSvc["Microservice Auth"]:::external
    end

    %% Connections
    Client -->|"HTTP request/response"| Controllers
    Controllers -->|"invoke"| Services
    Controllers -->|"uses config"| AppConfig
    Services -->|"uses DTOs"| DTOs
    Services -->|"uses Domain"| Domain
    Services -->|"audit event"| AuditTrait
    Services -->|"persist/read"| Repositories
    Repositories -->|"Mongo operations"| MongoDB
    Services -->|"invoke"| Connectors
    Connectors -->|"auth token"| AuthSvc
    Connectors -->|"POST /email/send"| EmailSvc
    Connectors -->|"GET /vat/customer"| VATInfo
    Connectors -->|"GET /customs/data"| CustomsDS
    Connectors -->|"GET /subscription/display"| SubDisplay
    Connectors -->|"POST /tax-enrolments"| TaxEnrol
    Connectors -->|"build URLs"| BuildUrl

    %% Click Events
    click Controllers "https://github.com/hmrc/eori-common-component/tree/main/app/uk/gov/hmrc/customs/managesubscription/controllers"
    click Services "https://github.com/hmrc/eori-common-component/tree/main/app/uk/gov/hmrc/customs/managesubscription/services"
    click DTOs "https://github.com/hmrc/eori-common-component/tree/main/app/uk/gov/hmrc/customs/managesubscription/services/dto"
    click Domain "https://github.com/hmrc/eori-common-component/tree/main/app/uk/gov/hmrc/customs/managesubscription/domain"
    click Repositories "https://github.com/hmrc/eori-common-component/tree/main/app/uk/gov/hmrc/customs/managesubscription/repository"
    click Connectors "https://github.com/hmrc/eori-common-component/tree/main/app/uk/gov/hmrc/customs/managesubscription/connectors"
    click AuditTrait "https://github.com/hmrc/eori-common-component/blob/main/app/uk/gov/hmrc/customs/managesubscription/audit/Auditable.scala"
    click AppConfig "https://github.com/hmrc/eori-common-component/blob/main/app/uk/gov/hmrc/customs/managesubscription/config/AppConfig.scala"
    click RoutesConf "https://github.com/hmrc/eori-common-component/blob/main/conf/application.conf"
    click ProdRoutes "https://github.com/hmrc/eori-common-component/blob/main/conf/prod.routes"
    click BuildUrl "https://github.com/hmrc/eori-common-component/blob/main/app/uk/gov/hmrc/customs/managesubscription/BuildUrl.scala"

    %% Styles
    classDef api fill:#ADD8E6,stroke:#000,stroke-width:1px
    classDef config fill:#E6E6FA,stroke:#000,stroke-width:1px
    classDef service fill:#90EE90,stroke:#000,stroke-width:1px
    classDef dto fill:#98FB98,stroke:#000,stroke-width:1px
    classDef domain fill:#98FB98,stroke:#000,stroke-width:1px
    classDef audit fill:#FFB6C1,stroke:#000,stroke-width:1px
    classDef persistence fill:#FFA500,stroke:#000,stroke-width:1px
    classDef integration fill:#C0C0C0,stroke:#000,stroke-width:1px
    classDef external fill:#D3D3D3,stroke:#000,stroke-width:1px
```
