# chumbi-dev/cita-backend architecture

> Architecture diagram of the GitHub repository chumbi-dev/cita-backend, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-06-10). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/chumbi-dev/cita-backend
- Repository: https://github.com/chumbi-dev/cita-backend

## Overview

##### Project type and purpose
- This is a serverless, event-driven backend for managing medical appointments across multiple countries (Peru “PE” and Chile “CL”).
- Its core function is to receive appointment requests via HTTP, route them by country, persist them in country-specific MySQL databases, and update a global status in DynamoDB.

##### File structure
- Top-level config:
- serverless.ts – Serverless Framework descriptor (Lambda definitions, environment variables, IAM, event sources).
- package.json / tsconfig.json – TypeScript setup and dependencies.
- src/handlers – AWS Lambda entry points:
- appointment.ts – “createAppointment” Lambda (API Gateway → SNS publish).
- appointment_pe.ts / appointment_cl.ts – country-specific processors (SQS → MySQL).
- eventbridge.ts – EventBridge listener (Event → DynamoDB update).
- getAppointments.ts – query endpoint (not detailed in README, likely DynamoDB read).
- src/application – business logic layer (AppointmentService).
- src/domain – domain model (Appointment).
- src/infrastructure – adapters:
- dynamo/AppointmentRepository.ts
- sns/SnsPublisher.ts
- src/utils – input validation, common utilities.

##### README for additional insights
- Confirms an event-driven pipeline: API Gateway → Lambda → SNS → SQS → Lambda → RDS → EventBridge → Lambda → DynamoDB.
- Technologies: AWS Lambda, API Gateway, SNS, SQS, RDS MySQL (per country), DynamoDB, EventBridge, Serverless Framework.

- Environment variables and config (serverless.ts) as sidecar notes if needed.
- Filter policy split (SNS → SQS_PE / SQS_CL).
- Event-propagation pattern (EventBridge → DynamoDB).
- Optional “getAppointments” read path for completeness.

## Diagram (Mermaid)

```mermaid
flowchart TD
    subgraph "Client"
        Client["User Client"]:::client
    end

    subgraph "AWS Cloud"
        APIGW["API Gateway"]:::aws
        createApp["Lambda: createAppointment"]:::lambda
        sns["SNS Topic"]:::aws
        sqsPE["SQS Queue PE"]:::aws
        sqsCL["SQS Queue CL"]:::aws
        processPE["Lambda: processAppointmentPE"]:::lambda
        processCL["Lambda: processAppointmentCL"]:::lambda
        rdsPE[(MySQL RDS PE)]:::db
        rdsCL[(MySQL RDS CL)]:::db
        eventBus["EventBridge Bus"]:::aws
        handleEB["Lambda: handleEventBridgeResponse"]:::lambda
        getApps["Lambda: getAppointments"]:::lambda
        dynamo[(DynamoDB AppointmentsTable)]:::db
    end

    subgraph "Code Artifacts"
        appointmentService["AppointmentService"]:::code
        appointmentModel["Appointment Model"]:::code
        snsPublisher["SnsPublisher"]:::code
        dynamoRepo["AppointmentRepository"]:::code
        validator["Validator Utilities"]:::code
        config["serverless.ts"]:::code
        deps["package.json & tsconfig.json"]:::code
    end

    Client -->|"POST /appointments"| APIGW
    APIGW -->|"invoke"| createApp
    createApp -->|"Publish to SNS"| sns
    sns -->|"filter countryISO=PE"| sqsPE
    sns -->|"filter countryISO=CL"| sqsCL
    sqsPE -->|"trigger"| processPE
    sqsCL -->|"trigger"| processCL
    processPE -->|Write Appointment| rdsPE
    processCL -->|Write Appointment| rdsCL
    processPE -->|"Emit result event"| eventBus
    processCL -->|"Emit result event"| eventBus
    eventBus -->|"Rule: appointment.result"| handleEB
    handleEB -->|"Update status"| dynamo

    Client -->|"GET /appointments"| APIGW
    APIGW -->|"invoke"| getApps
    getApps -->|"Read"| dynamo

    click createApp "https://github.com/chumbi-dev/cita-backend/blob/main/src/handlers/appointment.ts"
    click processPE "https://github.com/chumbi-dev/cita-backend/blob/main/src/handlers/appointment_pe.ts"
    click processCL "https://github.com/chumbi-dev/cita-backend/blob/main/src/handlers/appointment_cl.ts"
    click handleEB "https://github.com/chumbi-dev/cita-backend/blob/main/src/handlers/eventbridge.ts"
    click getApps "https://github.com/chumbi-dev/cita-backend/blob/main/src/handlers/getAppointments.ts"
    click appointmentService "https://github.com/chumbi-dev/cita-backend/blob/main/src/application/AppointmentService.ts"
    click appointmentModel "https://github.com/chumbi-dev/cita-backend/blob/main/src/domain/Appointment.ts"
    click snsPublisher "https://github.com/chumbi-dev/cita-backend/blob/main/src/infrastructure/sns/SnsPublisher.ts"
    click dynamoRepo "https://github.com/chumbi-dev/cita-backend/blob/main/src/infrastructure/dynamo/AppointmentRepository.ts"
    click validator "https://github.com/chumbi-dev/cita-backend/blob/main/src/utils/validator.ts"
    click config "https://github.com/chumbi-dev/cita-backend/blob/main/serverless.ts"
    click deps "https://github.com/chumbi-dev/cita-backend/blob/main/package.json"

    classDef lambda fill:#cce5ff,stroke:#004085
    classDef aws fill:#d4edda,stroke:#155724
    classDef db fill:#fff3cd,stroke:#856404
    classDef client fill:#e2e3e5,stroke:#6c757d
    classDef code fill:#f8d7da,stroke:#721c24
```
