# sebasgao05/duitama-taxi-pricing architecture

> Architecture diagram of the GitHub repository sebasgao05/duitama-taxi-pricing, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-04-09). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/sebasgao05/duitama-taxi-pricing
- Repository: https://github.com/sebasgao05/duitama-taxi-pricing

## Overview

1. **System shape**  
   This repository is a serverless REST API for taxi fare calculation in Duitama. The core runtime is a TypeScript/Node.js Express app, packaged to run both locally and inside AWS Lambda behind API Gateway. There is no database; the pricing rules and geographic mappings live in versioned JSON files in the repo.

2. **Entry points and execution modes**  
   There are two main boot paths: `src/server.ts` for local Express execution and `src/lambda.ts` for the AWS Lambda handler. API Gateway forwards requests to Lambda, and the app strips the deployment stage (`/qa`, `/staging`, `/production`) before routing, so the same Express routes work in every environment.

3. **HTTP boundary and routing**  
   The route layer is centered in `src/routes/fareRoutes.ts`. It exposes the fare API endpoints plus supporting read-only endpoints such as sectors, barrios, special routes, docs, and health. This is the main HTTP boundary between external clients and the pricing logic.

4. **Controller/service split**  
   `src/controllers/fareController.ts` handles request/response concerns, while `src/services/fareService.ts` contains the fare computation and lookup logic. That separation keeps HTTP plumbing out of the pricing rules and makes the service layer the main business-logic boundary.

5. **Pricing data model**  
   The source data lives under `src/data/` as JSON tables: general barrios, terminal-specific barrios, special routes, and tariffs. The logic chooses among these datasets in a strict priority order, so the data files are effectively the configuration layer for the pricing engine.

6. **Fare calculation flow**  
   The fare engine follows a fixed decision path: special-route override first, then terminal-specific pricing, then general sector pricing using the highest applicable sector between origin and destination. After the base fare is chosen, the service applies time-of-day logic and a defined holiday/end-of-year surcharge rule. The output includes source, sector applied, type of fare, and any surcharges.

7. **Validation and time utilities**  
   `src/utils/validation.ts` and `src/utils/time.ts` support the service layer with input normalization, exact barrio matching, and time/date classification. The repository emphasizes deterministic matching rather than fuzzy lookup, which matters for how the diagram should show data flow into the pricing engine.

8. **Contract and type boundary**  
   `src/domain/types.ts` defines the shared domain types used across controller, service, and tests. This acts as the schema boundary for fare requests, fare responses, and supporting domain objects, reducing coupling between the HTTP and calculation layers.

9. **Docs, health, and security surface**  
   `src/swagger.yaml` drives the `/docs` endpoint, giving the API an explicit contract surface separate from the pricing engine. `helmet`-based HTTP hardening and header sanitization are part of the Express boundary. The `/health` endpoint is a lightweight operational check and does not depend on pricing data.

10. **Testing and delivery pipeline**  
   Unit/integration tests live in `src/__tests__/` and cover both the pricing API and time logic. Deployment is defined with AWS SAM (`template.yaml`, `samconfig.toml`) and GitHub Actions, with environments for QA, staging, and production. The CI/CD flow builds and deploys the packaged Lambda/API Gateway stack, with production gated by manual approval.

Main entry points: [`src/services/fareService.ts`](https://github.com/sebasgao05/duitama-taxi-pricing/blob/main/src/services/fareService.ts) (Fare service), [`src/routes/fareRoutes.ts`](https://github.com/sebasgao05/duitama-taxi-pricing/blob/main/src/routes/fareRoutes.ts) (Fare routes), [`template.yaml`](https://github.com/sebasgao05/duitama-taxi-pricing/blob/main/template.yaml) (SAM stack), [`src/controllers/fareController.ts`](https://github.com/sebasgao05/duitama-taxi-pricing/blob/main/src/controllers/fareController.ts) (Fare controller)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_runtime["Runtime"]
  node_server["Local server<br/>Express bootstrap<br/>[server.ts]"]
  node_lambda["Lambda handler<br/>AWS Lambda entrypoint<br/>[lambda.ts]"]
end

subgraph group_pricing["Pricing Engine"]
  node_routes["Fare routes<br/>HTTP routes<br/>[fareRoutes.ts]"]
  node_controller["Fare controller<br/>HTTP controller<br/>[fareController.ts]"]
  node_service["Fare service<br/>Business logic<br/>[fareService.ts]"]
  node_validation["Validation<br/>Input normalization<br/>[validation.ts]"]
  node_time["Time rules<br/>[time.ts]"]
  node_health(("Health check<br/>Operational endpoint<br/>[fareRoutes.ts]"))
end

subgraph group_data["Data"]
  node_barrios["Barrios<br/>Geo mapping JSON<br/>[barrios.json]"]
  node_terminalbarrios["Terminal barrios<br/>Geo mapping JSON"]
  node_specialroutes["Special routes<br/>Override JSON"]
  node_tariffs["Tariffs<br/>Pricing JSON<br/>[tarifas.json]"]
end

subgraph group_contract["Contract"]
  node_types["Domain types<br/>Shared schema<br/>[types.ts]"]
  node_swagger["API spec<br/>OpenAPI contract<br/>[swagger.yaml]"]
end

subgraph group_delivery["Delivery"]
  node_tests["API tests<br/>Test suite<br/>[api.test.ts]"]
  node_timtests["Time tests<br/>Test suite<br/>[time.test.ts]"]
  node_sam["SAM stack<br/>Deployment template<br/>[template.yaml]"]
  node_samconfig["SAM config<br/>Deploy config<br/>[samconfig.toml]"]
  node_ci["CI workflow<br/>GitHub Actions<br/>[ci.yml]"]
end

node_server -->|"mounts"| node_routes
node_lambda -->|"invokes"| node_routes
node_routes -->|"delegates"| node_controller
node_controller -->|"calls"| node_service
node_service -->|"normalizes"| node_validation
node_service -->|"applies"| node_time
node_service -->|"reads"| node_barrios
node_service -->|"reads"| node_terminalbarrios
node_service -->|"overrides"| node_specialroutes
node_service -->|"prices"| node_tariffs
node_controller -->|"uses"| node_types
node_service -->|"uses"| node_types
node_routes -->|"documents"| node_swagger
node_routes -->|"exposes"| node_health
node_tests -.->|"verifies"| node_service
node_tests -.->|"verifies"| node_routes
node_timtests -.->|"verifies"| node_time
node_ci -->|"runs"| node_tests
node_ci -->|"deploys"| node_sam
node_sam -->|"defines"| node_lambda
node_sam -.->|"supports"| node_server
node_samconfig -->|"configures"| node_sam

click node_server "https://github.com/sebasgao05/duitama-taxi-pricing/blob/main/src/server.ts"
click node_lambda "https://github.com/sebasgao05/duitama-taxi-pricing/blob/main/src/lambda.ts"
click node_routes "https://github.com/sebasgao05/duitama-taxi-pricing/blob/main/src/routes/fareRoutes.ts"
click node_controller "https://github.com/sebasgao05/duitama-taxi-pricing/blob/main/src/controllers/fareController.ts"
click node_service "https://github.com/sebasgao05/duitama-taxi-pricing/blob/main/src/services/fareService.ts"
click node_validation "https://github.com/sebasgao05/duitama-taxi-pricing/blob/main/src/utils/validation.ts"
click node_time "https://github.com/sebasgao05/duitama-taxi-pricing/blob/main/src/utils/time.ts"
click node_barrios "https://github.com/sebasgao05/duitama-taxi-pricing/blob/main/src/data/barrios.json"
click node_terminalbarrios "https://github.com/sebasgao05/duitama-taxi-pricing/blob/main/src/data/barrios_terminal.json"
click node_specialroutes "https://github.com/sebasgao05/duitama-taxi-pricing/blob/main/src/data/rutas_especiales.json"
click node_tariffs "https://github.com/sebasgao05/duitama-taxi-pricing/blob/main/src/data/tarifas.json"
click node_types "https://github.com/sebasgao05/duitama-taxi-pricing/blob/main/src/domain/types.ts"
click node_swagger "https://github.com/sebasgao05/duitama-taxi-pricing/blob/main/src/swagger.yaml"
click node_health "https://github.com/sebasgao05/duitama-taxi-pricing/blob/main/src/routes/fareRoutes.ts"
click node_tests "https://github.com/sebasgao05/duitama-taxi-pricing/blob/main/src/__tests__/api.test.ts"
click node_timtests "https://github.com/sebasgao05/duitama-taxi-pricing/blob/main/src/__tests__/time.test.ts"
click node_sam "https://github.com/sebasgao05/duitama-taxi-pricing/blob/main/template.yaml"
click node_samconfig "https://github.com/sebasgao05/duitama-taxi-pricing/blob/main/samconfig.toml"
click node_ci "https://github.com/sebasgao05/duitama-taxi-pricing/blob/main/.github/workflows/ci.yml"

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_lambda toneBlue
class node_routes,node_controller,node_service,node_validation,node_time,node_health toneAmber
class node_barrios,node_terminalbarrios,node_specialroutes,node_tariffs toneMint
class node_types,node_swagger toneRose
class node_tests,node_timtests,node_sam,node_samconfig,node_ci toneIndigo
```

## Components

### Runtime

HTTP app and Lambda entrypoints

- [Local server](https://github.com/sebasgao05/duitama-taxi-pricing/blob/main/src/server.ts): Express bootstrap · Starts the app for local Node.js execution · `src/server.ts`
- [Lambda handler](https://github.com/sebasgao05/duitama-taxi-pricing/blob/main/src/lambda.ts): AWS Lambda entrypoint · Adapts API Gateway events to the Express app · `src/lambda.ts`

### Pricing Engine

Request handling and fare calculation

- [Fare routes](https://github.com/sebasgao05/duitama-taxi-pricing/blob/main/src/routes/fareRoutes.ts): HTTP routes · Main API boundary for fares, read-only lookups, docs, and health · `src/routes/fareRoutes.ts`
- [Fare controller](https://github.com/sebasgao05/duitama-taxi-pricing/blob/main/src/controllers/fareController.ts): HTTP controller · Handles request/response concerns for fare endpoints · `src/controllers/fareController.ts`
- [Fare service](https://github.com/sebasgao05/duitama-taxi-pricing/blob/main/src/services/fareService.ts): Business logic · Computes fares and applies lookup priority and surcharges · `src/services/fareService.ts`
- [Validation](https://github.com/sebasgao05/duitama-taxi-pricing/blob/main/src/utils/validation.ts): Input normalization · Exact barrio matching and request normalization helpers · `src/utils/validation.ts`
- [Time rules](https://github.com/sebasgao05/duitama-taxi-pricing/blob/main/src/utils/time.ts): Date/time rules · Classifies time-of-day and holiday surcharge conditions · `src/utils/time.ts`
- [Health check](https://github.com/sebasgao05/duitama-taxi-pricing/blob/main/src/routes/fareRoutes.ts): Operational endpoint · Lightweight uptime endpoint with no pricing dependencies · `src/routes/fareRoutes.ts`

### Data

Versioned pricing and mapping tables

- [Barrios](https://github.com/sebasgao05/duitama-taxi-pricing/blob/main/src/data/barrios.json): Geo mapping JSON · General barrio and sector mapping data · `src/data/barrios.json`
- [Terminal barrios](https://github.com/sebasgao05/duitama-taxi-pricing/blob/main/src/data/barrios_terminal.json): Geo mapping JSON · Terminal-specific barrio mapping data · `src/data/barrios_terminal.json`
- [Special routes](https://github.com/sebasgao05/duitama-taxi-pricing/blob/main/src/data/rutas_especiales.json): Override JSON · Explicit route overrides that take priority · `src/data/rutas_especiales.json`
- [Tariffs](https://github.com/sebasgao05/duitama-taxi-pricing/blob/main/src/data/tarifas.json): Pricing JSON · Base tariff table used by the pricing engine · `src/data/tarifas.json`

### Contract

Shared domain types and API docs

- [Domain types](https://github.com/sebasgao05/duitama-taxi-pricing/blob/main/src/domain/types.ts): Shared schema · Shared request, response, and fare domain types · `src/domain/types.ts`
- [API spec](https://github.com/sebasgao05/duitama-taxi-pricing/blob/main/src/swagger.yaml): OpenAPI contract · Swagger definition served by the docs endpoint · `src/swagger.yaml`

### Delivery

Deployment and CI/CD

- [API tests](https://github.com/sebasgao05/duitama-taxi-pricing/blob/main/src/__tests__/api.test.ts): Test suite · Covers fare API behavior and time logic · `src/__tests__/api.test.ts`
- [Time tests](https://github.com/sebasgao05/duitama-taxi-pricing/blob/main/src/__tests__/time.test.ts): Test suite · Validates date and time classification rules · `src/__tests__/time.test.ts`
- [SAM stack](https://github.com/sebasgao05/duitama-taxi-pricing/blob/main/template.yaml): Deployment template · Defines the Lambda/API Gateway deployment stack · `template.yaml`
- [SAM config](https://github.com/sebasgao05/duitama-taxi-pricing/blob/main/samconfig.toml): Deploy config · Environment-specific deployment configuration · `samconfig.toml`
- [CI workflow](https://github.com/sebasgao05/duitama-taxi-pricing/blob/main/.github/workflows/ci.yml): GitHub Actions · Builds, tests, and deploys the stack across environments · `.github/workflows/ci.yml`

## Connections

- Local server → Fare routes: mounts · Local Express bootstrap mounts the HTTP routes
- Lambda handler → Fare routes: invokes · Lambda entrypoint forwards requests into the same route layer
- Fare routes → Fare controller: delegates · Route handlers pass fare requests to the controller
- Fare controller → Fare service: calls · Controller delegates pricing work to the service layer
- Fare service → Validation: normalizes · Service uses validation helpers for deterministic matching
- Fare service → Time rules: applies · Service applies time-based fare classification and surcharges
- Fare service → Barrios: reads · Service uses general barrio and sector mappings
- Fare service → Terminal barrios: reads · Service checks terminal-specific mappings before general pricing
- Fare service → Special routes: overrides · Special routes take precedence in the fare decision path
- Fare service → Tariffs: prices · Tariff table provides the base fare values
- Fare controller → Domain types: uses · Controller shares request and response shapes with the domain layer
- Fare service → Domain types: uses · Service returns structured fare results defined by domain types
- Fare routes → API spec: documents · Docs endpoint exposes the API contract
- Fare routes → Health check: exposes · Routes include a lightweight health check
- API tests → Fare service: verifies · API tests exercise fare behavior through the service boundary
- API tests → Fare routes: verifies · API tests cover the HTTP boundary
- Time tests → Time rules: verifies · Time tests validate time classification rules
- CI workflow → API tests: runs · CI executes the test suites before deployment
- CI workflow → SAM stack: deploys · CI packages and deploys the SAM stack
- SAM stack → Lambda handler: defines · Template defines the Lambda runtime entrypoint
- SAM stack → Local server: supports · Template and build setup support the local and deployed app shape
- SAM config → SAM stack: configures · SAM config supplies environment-specific deployment parameters
