# mquirosp/backend-bancas architecture

> Architecture diagram of the GitHub repository mquirosp/backend-bancas, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-11-25). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/mquirosp/backend-bancas
- Repository: https://github.com/mquirosp/backend-bancas

## Overview

This is a multi-tenant lottery and sales management backend. Its principal workflow accepts an HTTP ticket-sale request, routes and authenticates it, applies ticket business rules, persists the transaction, and returns a result. Separate domain areas support draws, commissions, credit, accounts and analytics; shared infrastructure provides tenant isolation, caching, realtime events and resilient database access. The README documents the layered flow and major capabilities; some domain implementations were not sampled, so their internal call edges are omitted.

Main entry points: [`src/repositories/ticket.repository.ts`](https://github.com/mquirosp/backend-bancas/blob/master/src/repositories/ticket.repository.ts) (Ticket Repository), [`src/core/prismaClient.ts`](https://github.com/mquirosp/backend-bancas/blob/master/src/core/prismaClient.ts) (PostgreSQL Pools), [`src/api/v1/controllers/ticket.controller.ts`](https://github.com/mquirosp/backend-bancas/blob/master/src/api/v1/controllers/ticket.controller.ts) (Ticket Controller), [`src/server/app.ts`](https://github.com/mquirosp/backend-bancas/blob/master/src/server/app.ts) (HTTP Server)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_api["HTTP API"]
  node_server["HTTP Server<br/>[app.ts]"]
  node_routes["API Routes"]
  node_ticket_controller["Ticket Controller"]
  node_auth["JWT Auth<br/>[auth.middleware.ts]"]
end

subgraph group_sales["Sales Domains"]
  node_ticket_service["Ticket Service<br/>[ticket.service.ts]"]
  node_ticket_repo["Ticket Repository"]
  node_credit["Vendor Credit"]
  node_commission["Commission Rules"]
end

subgraph group_business["Business Operations"]
  node_draws["Draw Evaluation"]
  node_accounts["Accounts Statements"]
  node_reports["Reports &amp; Dashboard"]
  node_exports["Export Services"]
end

subgraph group_infra["Shared Infrastructure"]
  node_tenant["Tenant Context<br/>[tenantContext.ts]"]
  node_db[("PostgreSQL Pools<br/>[prismaClient.ts]")]
  node_cache["Tiered Cache<br/>[cache.service.ts]"]
  node_socket["Realtime Events<br/>[socket.service.ts]"]
  node_resilience["Connection Retry"]
end

node_client(("API Client"))
node_redis[("Redis")]
node_websocket_clients(("WebSocket Clients"))

node_client -->|"sends request"| node_server
node_server -->|"mounts routes"| node_routes
node_routes -->|"routes requests"| node_ticket_controller
node_ticket_controller -.->|"checks identity"| node_auth
node_ticket_controller -->|"calls"| node_ticket_service
node_ticket_service -->|"persists ticket"| node_ticket_repo
node_ticket_repo -->|"queries"| node_db
node_ticket_repo -->|"resolves commission"| node_commission
node_ticket_repo -.->|"checks credit"| node_credit
node_auth -->|"loads session"| node_db
node_tenant -->|"isolates queries"| node_db
node_ticket_repo -->|"retries connection"| node_resilience
node_cache -->|"reads and writes"| node_redis
node_draws -.->|"publishes updates"| node_socket
node_socket -->|"pushes events"| node_websocket_clients
node_accounts -->|"reads statements"| node_db
node_reports -.->|"queries analytics"| node_db
node_exports -.->|"formats exports"| node_reports

click node_server "https://github.com/mquirosp/backend-bancas/blob/master/src/server/app.ts"
click node_routes "https://github.com/mquirosp/backend-bancas/tree/master/src/api/v1/routes"
click node_ticket_controller "https://github.com/mquirosp/backend-bancas/blob/master/src/api/v1/controllers/ticket.controller.ts"
click node_auth "https://github.com/mquirosp/backend-bancas/blob/master/src/middlewares/auth.middleware.ts"
click node_ticket_service "https://github.com/mquirosp/backend-bancas/blob/master/src/domain/ticket/ticket.service.ts"
click node_ticket_repo "https://github.com/mquirosp/backend-bancas/blob/master/src/repositories/ticket.repository.ts"
click node_credit "https://github.com/mquirosp/backend-bancas/blob/master/src/domain/credit/vendorCredit.service.ts"
click node_commission "https://github.com/mquirosp/backend-bancas/tree/master/src/domain/commission"
click node_draws "https://github.com/mquirosp/backend-bancas/tree/master/src/domain/sorteo"
click node_accounts "https://github.com/mquirosp/backend-bancas/tree/master/src/domain/accounts"
click node_reports "https://github.com/mquirosp/backend-bancas/tree/master/src/domain/reports"
click node_exports "https://github.com/mquirosp/backend-bancas/tree/master/src/api/v1/services"
click node_tenant "https://github.com/mquirosp/backend-bancas/blob/master/src/core/tenantContext.ts"
click node_db "https://github.com/mquirosp/backend-bancas/blob/master/src/core/prismaClient.ts"
click node_cache "https://github.com/mquirosp/backend-bancas/blob/master/src/core/cache.service.ts"
click node_socket "https://github.com/mquirosp/backend-bancas/blob/master/src/core/socket.service.ts"
click node_resilience "https://github.com/mquirosp/backend-bancas/blob/master/src/core/withConnectionRetry.ts"

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_server,node_routes,node_ticket_controller,node_auth,node_client,node_websocket_clients toneBlue
class node_ticket_service,node_ticket_repo,node_credit,node_commission,node_redis toneAmber
class node_draws,node_accounts,node_reports,node_exports toneMint
class node_tenant,node_db,node_cache,node_socket,node_resilience toneRose
```

## Components

### HTTP API

- [HTTP Server](https://github.com/mquirosp/backend-bancas/blob/master/src/server/app.ts): `src/server/app.ts`
- [API Routes](https://github.com/mquirosp/backend-bancas/tree/master/src/api/v1/routes): `src/api/v1/routes`
- [Ticket Controller](https://github.com/mquirosp/backend-bancas/blob/master/src/api/v1/controllers/ticket.controller.ts): `src/api/v1/controllers/ticket.controller.ts`
- [JWT Auth](https://github.com/mquirosp/backend-bancas/blob/master/src/middlewares/auth.middleware.ts): `src/middlewares/auth.middleware.ts`

### Sales Domains

- [Ticket Service](https://github.com/mquirosp/backend-bancas/blob/master/src/domain/ticket/ticket.service.ts): `src/domain/ticket/ticket.service.ts`
- [Ticket Repository](https://github.com/mquirosp/backend-bancas/blob/master/src/repositories/ticket.repository.ts): `src/repositories/ticket.repository.ts`
- [Vendor Credit](https://github.com/mquirosp/backend-bancas/blob/master/src/domain/credit/vendorCredit.service.ts): `src/domain/credit/vendorCredit.service.ts`
- [Commission Rules](https://github.com/mquirosp/backend-bancas/tree/master/src/domain/commission): `src/domain/commission`

### Business Operations

- [Draw Evaluation](https://github.com/mquirosp/backend-bancas/tree/master/src/domain/sorteo): `src/domain/sorteo`
- [Accounts Statements](https://github.com/mquirosp/backend-bancas/tree/master/src/domain/accounts): `src/domain/accounts`
- [Reports & Dashboard](https://github.com/mquirosp/backend-bancas/tree/master/src/domain/reports): `src/domain/reports`
- [Export Services](https://github.com/mquirosp/backend-bancas/tree/master/src/api/v1/services): `src/api/v1/services`

### Shared Infrastructure

- [Tenant Context](https://github.com/mquirosp/backend-bancas/blob/master/src/core/tenantContext.ts): `src/core/tenantContext.ts`
- [PostgreSQL Pools](https://github.com/mquirosp/backend-bancas/blob/master/src/core/prismaClient.ts): `src/core/prismaClient.ts`
- [Tiered Cache](https://github.com/mquirosp/backend-bancas/blob/master/src/core/cache.service.ts): `src/core/cache.service.ts`
- [Realtime Events](https://github.com/mquirosp/backend-bancas/blob/master/src/core/socket.service.ts): `src/core/socket.service.ts`
- [Connection Retry](https://github.com/mquirosp/backend-bancas/blob/master/src/core/withConnectionRetry.ts): `src/core/withConnectionRetry.ts`

### Other components

- **API Client**
- **Redis**
- **WebSocket Clients**

## Connections

- API Client → HTTP Server: sends request (evidence: `src/server/app.ts`)
- HTTP Server → API Routes: mounts routes (evidence: `src/server/app.ts`)
- API Routes → Ticket Controller: routes requests (evidence: `README.md`)
- Ticket Controller → JWT Auth: checks identity
- Ticket Controller → Ticket Service: calls (evidence: `src/api/v1/controllers/ticket.controller.ts`)
- Ticket Service → Ticket Repository: persists ticket (evidence: `README.md`)
- Ticket Repository → PostgreSQL Pools: queries (evidence: `src/repositories/ticket.repository.ts`)
- Ticket Repository → Commission Rules: resolves commission (evidence: `src/repositories/ticket.repository.ts`)
- Ticket Repository → Vendor Credit: checks credit (evidence: `src/repositories/ticket.repository.ts`)
- JWT Auth → PostgreSQL Pools: loads session (evidence: `src/middlewares/auth.middleware.ts`)
- Tenant Context → PostgreSQL Pools: isolates queries (evidence: `README.md`)
- Ticket Repository → Connection Retry: retries connection (evidence: `src/repositories/ticket.repository.ts`)
- Tiered Cache → Redis: reads and writes (evidence: `src/core/cache.service.ts`)
- Draw Evaluation → Realtime Events: publishes updates
- Realtime Events → WebSocket Clients: pushes events (evidence: `README.md`)
- Accounts Statements → PostgreSQL Pools: reads statements (evidence: `src/domain/accounts/engine/statement.engine.ts`)
- Reports & Dashboard → PostgreSQL Pools: queries analytics
- Export Services → Reports & Dashboard: formats exports
