# tranvelocity/crm-api architecture

> Architecture diagram of the GitHub repository tranvelocity/crm-api, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-08-24). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/tranvelocity/crm-api
- Repository: https://github.com/tranvelocity/crm-api

## Overview

#### Purpose and Entry Points
Tran is a Laravel 9 CRM REST API. HTTP enters through `src/public/index.php`, bootstraps at `src/bootstrap/app.php`, and is dispatched by `src/app/Http/Kernel.php`. The CLI entry point is `src/artisan`; it includes the documented HMAC signature-generation command.

#### Runtime and Deployment Boundary
The application runs as PHP/Laravel behind Nginx in Docker. Container and reverse-proxy definitions are at `docker-compose.yml`, `Dockerfile`, and `nginx/conf/app.conf`. The API’s source root is `src/`; Laravel environment, database, queue, cache, mail, Sanctum, and service integration settings are centralized under `src/config/`.

#### Modular CRM Domain
Business capabilities are Nwidart-style Laravel modules, each owning routes, HTTP layer, persistence model, migration, and serialization. Key domain modules are:
- Companies: `src/Modules/Company/Routes/api.php`, `src/Modules/Company/Entities/Company.php`, `src/Modules/Company/Services/CompanyService.php`
- Users and admin identities: `src/Modules/User/Entities/User.php`, `src/Modules/Admin/Entities/Admin.php`
- Applications and memberships: `src/Modules/Application/Entities/Application.php`, `src/Modules/ApplicationUser/Entities/ApplicationUser.php`, `src/Modules/CompanyApplication/Entities/CompanyApplication.php`
- Reference and security history data: `src/Modules/Prefecture/Entities/Prefecture.php`, `src/Modules/PasswordHistory/Entities/PasswordHistory.php`

Module enablement is represented by `src/modules_statuses.json`, with framework module behavior configured in `src/config/modules.php`.

#### Request, Authentication, and API Versioning
Module route providers expose API routes, which map versioned controllers to request handlers; for example `src/Modules/Company/Routes/api.php` reaches `src/Modules/Company/Http/Controllers/v1/CompanyController.php`. Most modules use `Api/v1` controller namespaces; PingPong explicitly demonstrates v1/v2 endpoints in `src/Modules/PingPong/Http/Controllers/Api/v1/PingPongController.php` and `src/Modules/PingPong/Http/Controllers/Api/v2/PingPongController.php`.

API authentication is a custom HMAC boundary, implemented at `src/Modules/Core/Http/Middleware/ApiAuthenticationMiddleware.php`; clients supply the README-described CRM API key, timestamp, and signature headers. Laravel global routing and middleware remain governed by `src/app/Http/Kernel.php`.

#### Application Layer and Persistence Flow
The recurring write/read flow is: route → controller → validated request → service where present → repository → Eloquent entity/database → resource transformer. Representative boundaries are `src/Modules/Company/Http/Requests/CreateCompanyRequest.php`, `src/Modules/Company/Repositories/CompanyRepositoryInterface.php`, and `src/Modules/Company/Transformers/CompanyResource.php`. Other modules follow the same repository/entity/resource convention, such as `src/Modules/Application/Repositories/ApplicationRepository.php`.

Database schema is module-owned through migrations, including `src/Modules/Company/Database/Migrations/2022_08_15_220734_create_company_table.php` and relationship tables under `src/Modules/CompanyApplication/Database/Migrations/`. An ERD source is retained at `docs/Database/ERD.mwb`.

#### Shared Core Contract
Core supplies common model, repository, service, request, error, and response abstractions used across domain modules: `src/Modules/Core/Entities/BaseModel.php`, `src/Modules/Core/Repositories/BaseRepository.php`, and `src/Modules/Core/Traits/WithApiWrapping.php`. Error normalization is centralized in `src/Modules/Core/Exceptions/CustomHandler.php` and domain-specific exception types, establishing a consistent API error boundary.

#### External Services and Operational Interfaces
No concrete third-party business API adapter is visible in the tree, though core defines an external-call failure type at `src/Modules/Core/Exceptions/CallingExternalApiFailureException.php`. AWS CLI is required by local setup/documented operations, while Docker is the primary local runtime. Swagger documentation is served externally from a `/swagger` location referenced by the README, but its source is not present in the supplied tree.

Main entry points: [`nginx/conf/app.conf`](https://github.com/tranvelocity/crm-api/blob/master/nginx/conf/app.conf) (Nginx), [`src/app/Http/Kernel.php`](https://github.com/tranvelocity/crm-api/blob/master/src/app/Http/Kernel.php) (HTTP Kernel), [`src/Modules/Company/Http/Controllers/v1/CompanyController.php`](https://github.com/tranvelocity/crm-api/blob/master/src/Modules/Company/Http/Controllers/v1/CompanyController.php) (Versioned Controllers), [`src/Modules/Company/Routes/api.php`](https://github.com/tranvelocity/crm-api/blob/master/src/Modules/Company/Routes/api.php) (Module API Routes)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_deployment["Deployment"]
  node_docker{{"Docker Compose<br/>container orchestration<br/>[docker-compose.yml]"}}
  node_nginx["Nginx<br/>reverse proxy<br/>[app.conf]"]
  node_php_image["PHP App Image<br/>container image"]
end

subgraph group_runtime["Laravel Runtime"]
  node_http_entry["HTTP Entry<br/>PHP entry point<br/>[index.php]"]
  node_bootstrap["Laravel Bootstrap<br/>application bootstrap<br/>[app.php]"]
  node_kernel["HTTP Kernel<br/>middleware dispatcher<br/>[Kernel.php]"]
  node_artisan["Artisan CLI<br/>CLI entry point"]
  node_configuration["Runtime Configuration<br/>Laravel config"]
end

subgraph group_domain["CRM Modules"]
  node_module_routes["Module API Routes<br/>route providers<br/>[api.php]"]
  node_controllers["Versioned Controllers<br/>HTTP controllers"]
  node_request_validation["Validated Requests<br/>request validation"]
  node_company_service["Company Service<br/>domain service<br/>[CompanyService.php]"]
  node_repositories["Module Repositories<br/>persistence abstraction"]
  node_entities["CRM Entities<br/>Eloquent models<br/>[Company.php]"]
  node_migrations["Module Migrations<br/>database schema"]
  node_resources["API Resources<br/>response transformers"]
  node_module_registry["Module Registry<br/>module enablement"]
end

subgraph group_core["Shared Core"]
  node_hmac_auth["HMAC Authentication<br/>API middleware"]
  node_core_contracts["Core Contracts<br/>shared abstractions<br/>[BaseRepository.php]"]
  node_error_boundary["API Error Boundary<br/>exception handling<br/>[CustomHandler.php]"]
end

node_database[("Database<br/>persistent store")]
node_swagger["Swagger Docs<br/>external documentation"]

node_docker -->|"runs"| node_nginx
node_docker -->|"runs"| node_php_image
node_nginx -->|"forwards requests"| node_http_entry
node_http_entry -->|"bootstraps"| node_bootstrap
node_bootstrap -->|"creates"| node_kernel
node_kernel -->|"applies"| node_hmac_auth
node_hmac_auth -->|"authorizes"| node_module_routes
node_module_registry -->|"enables modules"| node_module_routes
node_module_routes -->|"dispatches"| node_controllers
node_controllers -->|"validates input"| node_request_validation
node_request_validation -->|"passes validated data"| node_company_service
node_company_service -->|"uses"| node_repositories
node_repositories -->|"persists through"| node_entities
node_entities -->|"reads and writes"| node_database
node_migrations -->|"defines schema"| node_database
node_controllers -->|"serializes responses"| node_resources
node_core_contracts -.->|"base contract"| node_repositories
node_core_contracts -.->|"base model"| node_entities
node_kernel -->|"normalizes failures"| node_error_boundary
node_configuration -.->|"configures connection"| node_database
node_nginx -.->|"serves"| node_swagger

click node_docker "https://github.com/tranvelocity/crm-api/blob/master/docker-compose.yml"
click node_nginx "https://github.com/tranvelocity/crm-api/blob/master/nginx/conf/app.conf"
click node_php_image "https://github.com/tranvelocity/crm-api/blob/master/Dockerfile"
click node_http_entry "https://github.com/tranvelocity/crm-api/blob/master/src/public/index.php"
click node_bootstrap "https://github.com/tranvelocity/crm-api/blob/master/src/bootstrap/app.php"
click node_kernel "https://github.com/tranvelocity/crm-api/blob/master/src/app/Http/Kernel.php"
click node_artisan "https://github.com/tranvelocity/crm-api/blob/master/src/artisan"
click node_module_routes "https://github.com/tranvelocity/crm-api/blob/master/src/Modules/Company/Routes/api.php"
click node_controllers "https://github.com/tranvelocity/crm-api/blob/master/src/Modules/Company/Http/Controllers/v1/CompanyController.php"
click node_request_validation "https://github.com/tranvelocity/crm-api/blob/master/src/Modules/Company/Http/Requests/CreateCompanyRequest.php"
click node_company_service "https://github.com/tranvelocity/crm-api/blob/master/src/Modules/Company/Services/CompanyService.php"
click node_repositories "https://github.com/tranvelocity/crm-api/blob/master/src/Modules/Company/Repositories/CompanyRepositoryInterface.php"
click node_entities "https://github.com/tranvelocity/crm-api/blob/master/src/Modules/Company/Entities/Company.php"
click node_migrations "https://github.com/tranvelocity/crm-api/blob/master/src/Modules/Company/Database/Migrations/2022_08_15_220734_create_company_table.php"
click node_resources "https://github.com/tranvelocity/crm-api/blob/master/src/Modules/Company/Transformers/CompanyResource.php"
click node_module_registry "https://github.com/tranvelocity/crm-api/blob/master/src/modules_statuses.json"
click node_hmac_auth "https://github.com/tranvelocity/crm-api/blob/master/src/Modules/Core/Http/Middleware/ApiAuthenticationMiddleware.php"
click node_core_contracts "https://github.com/tranvelocity/crm-api/blob/master/src/Modules/Core/Repositories/BaseRepository.php"
click node_error_boundary "https://github.com/tranvelocity/crm-api/blob/master/src/Modules/Core/Exceptions/CustomHandler.php"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_docker,node_nginx,node_php_image toneBlue
class node_http_entry,node_bootstrap,node_kernel,node_artisan,node_configuration toneAmber
class node_module_routes,node_controllers,node_request_validation,node_company_service,node_repositories,node_entities,node_migrations,node_resources,node_module_registry toneMint
class node_hmac_auth,node_core_contracts,node_error_boundary toneRose
class node_database,node_swagger toneNeutral
```

## Components

### Deployment

- [Docker Compose](https://github.com/tranvelocity/crm-api/blob/master/docker-compose.yml): container orchestration · Runs the PHP application and reverse proxy. · `docker-compose.yml`
- [Nginx](https://github.com/tranvelocity/crm-api/blob/master/nginx/conf/app.conf): reverse proxy · `nginx/conf/app.conf`
- [PHP App Image](https://github.com/tranvelocity/crm-api/blob/master/Dockerfile): container image · `Dockerfile`

### Laravel Runtime

- [HTTP Entry](https://github.com/tranvelocity/crm-api/blob/master/src/public/index.php): PHP entry point · `src/public/index.php`
- [Laravel Bootstrap](https://github.com/tranvelocity/crm-api/blob/master/src/bootstrap/app.php): application bootstrap · `src/bootstrap/app.php`
- [HTTP Kernel](https://github.com/tranvelocity/crm-api/blob/master/src/app/Http/Kernel.php): middleware dispatcher · Applies global routing and middleware. · `src/app/Http/Kernel.php`
- [Artisan CLI](https://github.com/tranvelocity/crm-api/blob/master/src/artisan): CLI entry point · Includes HMAC signature-generation operations. · `src/artisan`
- **Runtime Configuration**: Laravel config · Centralizes database, queue, cache, mail, Sanctum, and integration settings.

### CRM Modules

- [Module API Routes](https://github.com/tranvelocity/crm-api/blob/master/src/Modules/Company/Routes/api.php): route providers · Expose versioned API endpoints. · `src/Modules/Company/Routes/api.php`
- [Versioned Controllers](https://github.com/tranvelocity/crm-api/blob/master/src/Modules/Company/Http/Controllers/v1/CompanyController.php): HTTP controllers · Company and PingPong demonstrate v1; PingPong also exposes v2. · `src/Modules/Company/Http/Controllers/v1/CompanyController.php`
- [Validated Requests](https://github.com/tranvelocity/crm-api/blob/master/src/Modules/Company/Http/Requests/CreateCompanyRequest.php): request validation · `src/Modules/Company/Http/Requests/CreateCompanyRequest.php`
- [Company Service](https://github.com/tranvelocity/crm-api/blob/master/src/Modules/Company/Services/CompanyService.php): domain service · `src/Modules/Company/Services/CompanyService.php`
- [Module Repositories](https://github.com/tranvelocity/crm-api/blob/master/src/Modules/Company/Repositories/CompanyRepositoryInterface.php): persistence abstraction · Repository/entity/resource convention is shared by modules. · `src/Modules/Company/Repositories/CompanyRepositoryInterface.php`
- [CRM Entities](https://github.com/tranvelocity/crm-api/blob/master/src/Modules/Company/Entities/Company.php): Eloquent models · Models companies, identities, applications, memberships, and reference data. · `src/Modules/Company/Entities/Company.php`
- [Module Migrations](https://github.com/tranvelocity/crm-api/blob/master/src/Modules/Company/Database/Migrations/2022_08_15_220734_create_company_table.php): database schema · Modules own schemas and relationship tables. · `src/Modules/Company/Database/Migrations/2022_08_15_220734_create_company_table.php`
- [API Resources](https://github.com/tranvelocity/crm-api/blob/master/src/Modules/Company/Transformers/CompanyResource.php): response transformers · `src/Modules/Company/Transformers/CompanyResource.php`
- [Module Registry](https://github.com/tranvelocity/crm-api/blob/master/src/modules_statuses.json): module enablement · `src/modules_statuses.json`

### Shared Core

- [HMAC Authentication](https://github.com/tranvelocity/crm-api/blob/master/src/Modules/Core/Http/Middleware/ApiAuthenticationMiddleware.php): API middleware · Validates CRM API key, timestamp, and signature headers. · `src/Modules/Core/Http/Middleware/ApiAuthenticationMiddleware.php`
- [Core Contracts](https://github.com/tranvelocity/crm-api/blob/master/src/Modules/Core/Repositories/BaseRepository.php): shared abstractions · Provides base model, repository, service, request, and response behavior. · `src/Modules/Core/Repositories/BaseRepository.php`
- [API Error Boundary](https://github.com/tranvelocity/crm-api/blob/master/src/Modules/Core/Exceptions/CustomHandler.php): exception handling · Normalizes domain and external-call failures. · `src/Modules/Core/Exceptions/CustomHandler.php`

### Other components

- **Database**: persistent store
- **Swagger Docs**: external documentation · Served at /swagger; source is absent from the repository.

## Connections

- Docker Compose → Nginx: runs
- Docker Compose → PHP App Image: runs
- Nginx → HTTP Entry: forwards requests
- HTTP Entry → Laravel Bootstrap: bootstraps
- Laravel Bootstrap → HTTP Kernel: creates
- HTTP Kernel → HMAC Authentication: applies
- HMAC Authentication → Module API Routes: authorizes
- Module Registry → Module API Routes: enables modules
- Module API Routes → Versioned Controllers: dispatches
- Versioned Controllers → Validated Requests: validates input
- Validated Requests → Company Service: passes validated data
- Company Service → Module Repositories: uses
- Module Repositories → CRM Entities: persists through
- CRM Entities → Database: reads and writes
- Module Migrations → Database: defines schema
- Versioned Controllers → API Resources: serializes responses
- Core Contracts → Module Repositories: base contract
- Core Contracts → CRM Entities: base model
- HTTP Kernel → API Error Boundary: normalizes failures
- Runtime Configuration → Database: configures connection
- Nginx → Swagger Docs: serves
