# hangxigood/erp-mes architecture

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

- Interactive diagram: https://gitdiagram.com/hangxigood/erp-mes
- Repository: https://github.com/hangxigood/erp-mes

## Overview

##### Main Components

a) Client Layer:
- Next.js Frontend (App Router architecture)
- Authentication Pages (/app/(authPage))
- Batch Record Pages (/app/(batchRecordPage))
- User Management Pages (/app/(userPage))
- React Components (/components)

b) API Layer:
- Next.js API Routes (/app/api)
- Authentication Endpoints
- Batch Record Management
- User Management
- Template Management

c) Service Layer:
- Authentication Service (NextAuth.js)
- Email Service (SendGrid)
- Change Stream Service (/lib/changeStream.js)
- Audit Logging Service

d) Data Layer:
- MongoDB Database
- Mongoose Models:
- BatchRecord
- BatchRecordData
- FieldValueHistory
- Template
- User

**[Client Layer] → [API Layer] → [Service Layer] → [Data Layer]**

##### Important Flows to Highlight

a) Authentication Flow:
**Login Page → NextAuth → MongoDB → Session Management**

b) Batch Record Processing:
**Template Selection → Record Creation → Section Management → Signoff Process → Audit Logging**

c) Change Management:
**User Action → API Route → Change Stream → Audit Log → Database Update**

##### Key Relationships to Emphasize

- Template to BatchRecord relationship
- User to BatchRecord permissions
- Audit log connections to all operations
- Change stream monitoring of database operations

Remember to:
- Notes for important business rules
- Error handling paths
- Scalability points

This system design should effectively communicate both the technical architecture and the business process flow of the ERP-MES system while maintaining clarity and accuracy.

## Diagram (Mermaid)

```mermaid
flowchart TB
    %% Styles
    classDef frontend fill:#3498db,stroke:#2980b9,color:white
    classDef api fill:#2ecc71,stroke:#27ae60,color:white
    classDef service fill:#f1c40f,stroke:#f39c12,color:black
    classDef database fill:#e74c3c,stroke:#c0392b,color:white
    classDef auth fill:#9b59b6,stroke:#8e44ad,color:white
    classDef external fill:#95a5a6,stroke:#7f8c8d,color:white

    %% Client Layer
    subgraph ClientLayer
        Auth["Authentication Pages"]:::frontend
        Batch["Batch Record Pages"]:::frontend
        User["User Management Pages"]:::frontend
        Components["Core Components"]:::frontend
        StateManagement["State Management"]:::frontend
    end

    %% API Layer
    subgraph APILayer
        AuthAPI["Authentication API"]:::api
        UserAPI["User Management API"]:::api
        BatchAPI["Batch Record API"]:::api
        TemplateAPI["Template Management API"]:::api
    end

    %% Service Layer
    subgraph ServiceLayer
        AuthService["Authentication Service"]:::service
        EmailService["Email Service"]:::service
        ChangeStream["Change Stream Service"]:::service
        AuditService["Audit Logging Service"]:::service
        Security["Security Middleware"]:::service
        DBConnection["Database Connection"]:::service
    end

    %% Data Layer
    subgraph DataLayer
        BatchRecordModel["Batch Record Model"]:::database
        BatchRecordDataModel["Batch Record Data Model"]:::database
        FieldValueModel["Field Value History Model"]:::database
        TemplateModel["Template Model"]:::database
        UserModel["User Model"]:::database
    end

    %% Relationships
    Auth --> AuthAPI
    Batch --> BatchAPI
    User --> UserAPI
    Components --> StateManagement
    
    AuthAPI --> AuthService
    UserAPI --> AuthService
    BatchAPI --> ChangeStream
    TemplateAPI --> AuditService
    
    AuthService --> DBConnection
    ChangeStream --> AuditService
    AuditService --> DBConnection
    
    DBConnection --> BatchRecordModel
    DBConnection --> BatchRecordDataModel
    DBConnection --> FieldValueModel
    DBConnection --> TemplateModel
    DBConnection --> UserModel

    %% Click Events
    click Auth "https://github.com/hangxigood/ERP-MES/tree/main/app/(authPage)"
    click Batch "https://github.com/hangxigood/ERP-MES/tree/main/app/(batchRecordPage)"
    click User "https://github.com/hangxigood/ERP-MES/tree/main/app/(userPage)"
    click Components "https://github.com/hangxigood/ERP-MES/tree/main/components"
    click AuthAPI "https://github.com/hangxigood/ERP-MES/blob/main/app/api/auth/[...nextauth]"
    click UserAPI "https://github.com/hangxigood/ERP-MES/tree/main/app/api/(UserManagement)"
    click BatchAPI "https://github.com/hangxigood/ERP-MES/tree/main/app/api/batch-records"
    click TemplateAPI "https://github.com/hangxigood/ERP-MES/tree/main/app/api/templates"
    click AuthService "https://github.com/hangxigood/ERP-MES/blob/main/lib/authOptions.js"
    click EmailService "https://github.com/hangxigood/ERP-MES/blob/main/lib/sendEmail.js"
    click ChangeStream "https://github.com/hangxigood/ERP-MES/blob/main/lib/changeStream.js"
    click AuditService "https://github.com/hangxigood/ERP-MES/blob/main/lib/utils/auditUtils.js"
    click BatchRecordModel "https://github.com/hangxigood/ERP-MES/blob/main/models/BatchRecord.js"
    click BatchRecordDataModel "https://github.com/hangxigood/ERP-MES/blob/main/models/BatchRecordData.js"
    click FieldValueModel "https://github.com/hangxigood/ERP-MES/blob/main/models/FieldValueHistory.js"
    click TemplateModel "https://github.com/hangxigood/ERP-MES/blob/main/models/Template.js"
    click UserModel "https://github.com/hangxigood/ERP-MES/blob/main/models/User.js"
    click StateManagement "https://github.com/hangxigood/ERP-MES/blob/main/contexts/BatchRecordContext.js"
    click Security "https://github.com/hangxigood/ERP-MES/blob/main/middleware.ts"
    click DBConnection "https://github.com/hangxigood/ERP-MES/blob/main/lib/mongoose.js"

    %% Legend
    subgraph Legend
        Frontend["Frontend Component"]:::frontend
        APIComponent["API Component"]:::api
        ServiceComponent["Service Component"]:::service
        DatabaseComponent["Database Component"]:::database
        AuthComponent["Auth Component"]:::auth
        ExternalComponent["External Service"]:::external
    end
```
