How sebasgao05/duitama-taxi-pricing works
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: fareService.ts Fare service · fareRoutes.ts Fare routes · template.yaml SAM stack · fareController.ts Fare controller
Components 19
Runtime
HTTP app and Lambda entrypoints
- Local server · Starts the app for local Node.js execution
src/server.ts - Lambda handler · Adapts API Gateway events to the Express app
src/lambda.ts
Pricing Engine
Request handling and fare calculation
- Fare routes · Main API boundary for fares, read-only lookups, docs, and health
src/routes/fareRoutes.ts - Fare controller · Handles request/response concerns for fare endpoints
src/controllers/fareController.ts - Fare service · Computes fares and applies lookup priority and surcharges
src/services/fareService.ts - Validation · Exact barrio matching and request normalization helpers
src/utils/validation.ts - Time rules · Classifies time-of-day and holiday surcharge conditions
src/utils/time.ts - Health check · Lightweight uptime endpoint with no pricing dependencies
src/routes/fareRoutes.ts
Data
Versioned pricing and mapping tables
- Barrios · General barrio and sector mapping data
src/data/barrios.json - Terminal barrios · Terminal-specific barrio mapping data
src/data/barrios_terminal.json - Special routes · Explicit route overrides that take priority
src/data/rutas_especiales.json - Tariffs · Base tariff table used by the pricing engine
src/data/tarifas.json
Contract
Shared domain types and API docs
- Domain types · Shared request, response, and fare domain types
src/domain/types.ts - API spec · Swagger definition served by the docs endpoint
src/swagger.yaml
Delivery
Deployment and CI/CD
- API tests · Covers fare API behavior and time logic
src/__tests__/api.test.ts - Time tests · Validates date and time classification rules
src/__tests__/time.test.ts - SAM stack · Defines the Lambda/API Gateway deployment stack
template.yaml - SAM config · Environment-specific deployment configuration
samconfig.toml - CI workflow · Builds, tests, and deploys the stack across environments
.github/workflows/ci.yml
Connections 22
- 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