# dav0407/gym-crm-ms architecture

> Architecture diagram of the GitHub repository dav0407/gym-crm-ms, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-09-27). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/dav0407/gym-crm-ms
- Repository: https://github.com/dav0407/gym-crm-ms

## Overview

##### Project type and purpose
- This is a microservice‐based backend system for gym CRM, composed of three Spring Boot services:
- gym-crm-service-discovery (Eureka server)
- gym-crm (Main Service A – Gym CRM Core)
- trainer-session-management (Service B – Trainer Workload Management)
- Purpose: manage users, trainees, trainers, training sessions, and calculate trainer workloads using JWT security, service discovery, Feign clients, circuit breakers, and JMS messaging.

##### File structure
- Top level:
- docker-compose.yaml – orchestration of all services and infrastructure
- gym-crm-service-discovery – Eureka server
- gym-crm – primary CRM microservice
- trainer-session-management – workload/service B microservice
- Each service follows standard Spring Boot layout: src/main/java (controllers, services, configs, entities, repositories), src/main/resources (application.yaml profiles, import scripts, Prometheus config), src/test.
- gym-crm has additional folders: actuator, aop, filter, handler, mapper → indicates cross‐cutting concerns, metrics, logging, global exception handling.
- JMS configurations in both gym-crm and trainer-session-management → they communicate via JMS as well as Feign.

##### Key architectural patterns & technologies (from README)
- Microservices with service discovery (Netflix Eureka).
- Inter‐service communication: OpenFeign client + Spring Cloud Circuit Breaker (Resilience4j) + JMS for asynchronous workload messages.
- Security: Spring Security + JWT + ownership validation.
- Data persistence: Spring Data JPA with separate Postgres/MySQL databases per service.
- Observability: Actuator, Prometheus, structured logging, metrics AOP.
- Docker & Docker Compose for containerized deployment.

##### Main components & interactions
A. Service Discovery (Eureka)
- Registers all instances of gym-crm and trainer-session-management
- Enables dynamic lookup and load balancing
B. Gym CRM Core (Service A)
- API Layer: Controllers (UserController, TraineeController, TrainerController, TrainingController)
- Business Layer: Services (UserService, TraineeService, TrainerService, TrainingService), Aspects (Metrics, Logging), Filters (JWT auth, transaction logging)
- Data Layer: Repositories → relational database (Postgres)
- Inter‐service calls:
- Synchronous: OpenFeign client to Service B endpoints for workload updates
- Asynchronous: JMS template/producer sends workload change messages to a queue
- Circuit breaker wraps calls to Service B
C. Trainer Session Management (Service B)
- JMS Listener: processes TrainerWorkloadRequest messages
- API Layer: TrainerHoursController for direct REST queries
- Business Layer: TrainerWorkingHoursService, Aspects, Filters
- Data Layer: Repository → separate relational database for workload data
D. External Infrastructure
- Databases: one per service (Gym CRM DB, Workload DB)
- Message Broker: JMS provider (e.g., ActiveMQ)
- Docker Compose network connecting all services + Eureka + DBs + broker

##### Relationships & data flow
1. At startup, all services register with Eureka
2. Client (or Swagger UI) → REST call → gym-crm
3. gym-crm authenticates via JWT, invokes business logic, reads/writes its DB
4. On training session creation/deletion, gym-crm:
a. Sends JMS message to workload queue
b. Calls Service B via Feign (fallback via circuit breaker if queue fails)
5. trainer-session-management picks up JMS message (or direct call), updates its DB with monthly aggregation
6. Metrics & health endpoints exposed via Actuator, scraped by Prometheus

##### Architectural patterns & design principles
- Microservices with single responsibility (separate domain contexts)
- Service discovery for loose coupling
- Circuit breaker for resilience
- JWT stateless authentication
- AOP for cross‐cutting concerns (logging, metrics)
- Layered architecture within each service (Controller → Service → Repository)
- Asynchronous messaging for decoupling workload updates

##### Elements
- Client at the top (Postman / UI)

## Diagram (Mermaid)

```mermaid
flowchart TD
    classDef external fill:#f2f2f2,stroke:#333,stroke-width:1px;
    classDef discovery fill:#cccccc,stroke:#333,stroke-width:1px;
    classDef service fill:#cce5ff,stroke:#1a73e8,stroke-width:1px;
    classDef db fill:#ffe5b4,stroke:#e69500,stroke-width:1px;
    classDef message fill:#d4edda,stroke:#2e7d32,stroke-width:1px;
    classDef infra fill:#f0f0f0,stroke:#888,stroke-width:1px;

    Client(("Client / UI")):::external

    subgraph "Service Discovery"
        Eureka["Eureka Server"]:::discovery
    end
    click Eureka "https://github.com/dav0407/gym-crm-ms/blob/master/gym-crm-service-discovery/src/main/java/com/epam/gym_crm_service_discovery/GymCrmServiceDiscoveryApplication.java"

    subgraph "Gym CRM Core (Service A)" 
        direction BT
        GymApp["GymcrmApplication"]:::service
        Controllers["Controllers\n(User, Trainee,\nTrainer, Training)"]:::service
        Services["Services\n(UserService,TrainingService,\nJwtService, etc.)"]:::service
        Repos["Repositories"]:::service
        Configs["Config\n(SecurityConfig, JmsConfig,\nOpenAPIConfig)"]:::service
        AOP["AOP & Logging\n(MetricsAspect,\nOperationLoggingAspect)"]:::service
        Filters["Filters\n(JwtAuthFilter,\nTransactionLoggingFilter)"]:::service
        Handler["GlobalExceptionHandler"]:::service
        GymApp --> Controllers
        Controllers --> Services
        Services --> Repos
        Services --> Configs
        Services --> AOP
        Services --> Filters
        Controllers --> Handler
    end
    click GymApp "https://github.com/dav0407/gym-crm-ms/blob/master/gym-crm/src/main/java/com/epam/gym_crm/GymcrmApplication.java"
    click Controllers "https://github.com/dav0407/gym-crm-ms/tree/master/gym-crm/src/main/java/com/epam/gym_crm/controller"
    click Services "https://github.com/dav0407/gym-crm-ms/tree/master/gym-crm/src/main/java/com/epam/gym_crm/service"
    click Repos "https://github.com/dav0407/gym-crm-ms/tree/master/gym-crm/src/main/java/com/epam/gym_crm/repository"
    click Configs "https://github.com/dav0407/gym-crm-ms/tree/master/gym-crm/src/main/java/com/epam/gym_crm/config"
    click AOP "https://github.com/dav0407/gym-crm-ms/blob/master/gym-crm/src/main/java/com/epam/gym_crm/actuator/MetricsAspect.java"
    click Filters "https://github.com/dav0407/gym-crm-ms/tree/master/gym-crm/src/main/java/com/epam/gym_crm/filter"
    click Handler "https://github.com/dav0407/gym-crm-ms/blob/master/gym-crm/src/main/java/com/epam/gym_crm/handler/GlobalExceptionHandler.java"

    subgraph "Trainer Session Management (Service B)"
        direction BT
        TrainerApp["TrainingSessionManagementApplication"]:::service
        BConfigs["JmsConfig"]:::service
        Listener["TrainerHoursMessageListener"]:::service
        BService["TrainerWorkingHoursService"]:::service
        BRepos["Repository & Model"]:::service
        BLogging["Logging AOP & Filter"]:::service
        TrainerApp --> BConfigs
        TrainerApp --> Listener
        Listener --> BService
        BService --> BRepos
        BService --> BLogging
    end
    click TrainerApp "https://github.com/dav0407/gym-crm-ms/blob/master/trainer-session-management/src/main/java/com/epam/trainer_session_management/TrainingSessionManagementApplication.java"
    click BConfigs "https://github.com/dav0407/gym-crm-ms/blob/master/trainer-session-management/src/main/java/com/epam/trainer_session_management/config/JmsConfig.java"
    click Listener "https://github.com/dav0407/gym-crm-ms/blob/master/trainer-session-management/src/main/java/com/epam/trainer_session_management/listener/TrainerHoursMessageListener.java"
    click BService "https://github.com/dav0407/gym-crm-ms/blob/master/trainer-session-management/src/main/java/com/epam/trainer_session_management/service/TrainerWorkingHoursService.java"
    click BRepos "https://github.com/dav0407/gym-crm-ms/blob/master/trainer-session-management/src/main/java/com/epam/trainer_session_management/model/TrainerWorkingHours.java"

    JMSBroker[(JMS Broker)]:::message
    GymDB[(Gym CRM DB)]:::db
    WorkDB[(Workload DB)]:::db
    DockerComp[(docker-compose.yaml)]:::infra

    Client -->|"HTTP/REST"| GymApp
    GymApp -->|"register"| Eureka
    TrainerApp -->|"register"| Eureka
    GymApp -->|"JMS message"| JMSBroker
    JMSBroker -.->|"consumed by"| Listener
    GymApp -->|"HTTP/REST Feign\nCircuitBreaker"| TrainerApp
    GymApp -->|"JPA"| GymDB
    TrainerApp -->|"JPA"| WorkDB
    DockerComp --> GymApp
    DockerComp --> TrainerApp
    DockerComp --> Eureka
    DockerComp --> JMSBroker
    DockerComp --> GymDB
    DockerComp --> WorkDB

    class Client external
    class JMSBroker message
    class GymDB,WorkDB db
    class DockerComp infra
```
